To solve this problem, we will use linear programming. The goal is to minimize the total cost of the servings of beans and cereal while meeting the requirements for carbohydrates and protein.

Let's denote:
- \(x\) as the number of servings of beans,
- \(y\) as the number of servings of cereal.

The objective function (to be minimized) represents the total cost, which is $2 per serving of beans and $1 per serving of cereal. So, the objective function can be written as:
\[ \text{Minimize} \quad 2x + y \]

We have two main constraints based on the requirements for carbohydrates and protein:
1. For carbohydrates: \(50x + 30y \geq 300\), because each serving of beans contains 50g of carbohydrates and each serving of cereal contains 30g, and the man wants at least 300g.
2. For protein: \(20x + 5y \geq 150\), because each serving of beans contains 20g of protein and each serving of cereal contains 5g, and he wants at least 150g.

Additionally, \(x\) and \(y\) must be non-negative since they represent the number of servings, which cannot be negative. However, in this context, we are looking for integer solutions because you can't eat a fraction of a serving in practical terms.

Now, let's write the Gurobi code to solve this problem:

```python
from gurobipy import *

# Create a new model
m = Model("diet_problem")

# Create variables
x = m.addVar(vtype=GRB.INTEGER, name="beans")
y = m.addVar(vtype=GRB.INTEGER, name="cereal")

# Set the objective function
m.setObjective(2*x + y, GRB.MINIMIZE)

# Add constraints
m.addConstr(50*x + 30*y >= 300, "carbohydrates")
m.addConstr(20*x + 5*y >= 150, "protein")

# Optimize model
m.optimize()

# Print solution
if m.status == GRB.OPTIMAL:
    print("Optimal solution found:")
    print(f"Beans: {x.x}")
    print(f"Cereal: {y.x}")
    print(f"Total Cost: ${2*x.x + y.x}")
else:
    print("No optimal solution found")
```

This code sets up the model, defines the variables and objective function, adds the constraints, optimizes the model, and then prints out the optimal number of servings of beans and cereal along with the total cost. If no optimal solution is found (which could happen if the constraints are infeasible), it will print a message saying so.