-1

I want to calculate each one of the three terms in the objective function alone gurobi python. how can I do that?

The first term : c*(te[i]+to[i]+t[i,s])*X[i,b,s] for i in Trucks for b in Blocks for s in Slots The second term: d[b,s]*X[i,b,s] for i in Trucks for b in Blocks for s in Slots The third: g*a[s]*X[i,b,s] for i in Trucks for b in Blocks for s in Slots

The objective function equation

m.setObjective(quicksum(c*(te[i]+to[i]+t[i,s])*X[i,b,s] for i in Trucks for b in Blocks for s in Slots) + quicksum(d[b,s]*X[i,b,s] for i in Trucks for b in Blocks for s in Slots) +quicksum( g*a[s]*X[i,b,s] for i in Trucks for b in Blocks for s in Slots), GRB.MINIMIZE)
ForceBru
  • 43,482
  • 10
  • 63
  • 98

1 Answers1

1

The best way to do this is to split up your objective into three separate LinExpr objects. After optimizing, you can use the getValue() method of these objects to extract the expression's value at the optimal solution.

In your case, one example of this is as follows:

# Construct three linear expressions
expr = []
expr.append(quicksum(c*(te[i]+to[i]+t[i,s])*X[i,b,s] for i in Trucks for b in Blocks for s in Slots))
expr.append(quicksum(d[b,s]*X[i,b,s] for i in Trucks for b in Blocks for s in Slots))
expr.append(quicksum(g*a[s]*X[i,b,s] for i in Trucks for b in Blocks for s in Slots))

# Set objective and optimize
m.setObjective(quicksum(expr[i] for i in range(3)), GRB.MINIMIZE)
m.optimize()

# Return linear expression values
if m.status == GRB.Status.OPTIMAL:
    for i in range(3):
        print(expr[i].getValue())
else:
    print('Model not solved to optimality')
Eli Towle
  • 76
  • 4