Multi Objective
OngoingHello,
I am trying to solve a multi objective function problem. This is my code:
Z1 = quicksum([yps[p,s] * auftraege['Profit'][p] for p in range(P) for s in range(S)])
Z2 = -quicksum([yps[p,s] * auftraege[f'M{s+1}Energie'][p] for p in range(P) for s in range(S)])
Z3 = -quicksum([yps[p,s] * auftraege[f'M{s+1}Cost'][p] for p in range(P) for s in range(S)])
# Setze die Zielfunktionen
model.setObjectiveN(Z1, index=0, priority=1, name="Z1")
model.setObjectiveN(Z2, index=1, priority=2, name="Z2")
model.setObjectiveN(Z3, index=2, priority=3, name="Z3")
model.optimize()
if model.status == GRB.INFEASIBLE or model.status == GRB.UNBOUNDED:
print("Problem infeasible oder unbounded.")
else:
Z_values=[model.objVal for t in range(3)]
print("Optimal values:", Z_values)
total_objective_value = sum(Z_values)
print(f"Total value of objective function: {total_objective_value}")
In my code, Z1 should be maximized, and Z2 and Z3 minimized (given by signs). Lexicographic optimization should maximize the entire model. Currently, my solution value for all Z is identical. Where is my mistake? Also, why does Gurobi optimize Z3 first, then Z2, then Z1, even though I have set priorities?
-
Official comment
This post is more than two years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Alexandra,
- We optimize highest priority first, so that's why Z3 is solved first in your case.
- When you iterate over range(3) to retrieve the objective values, you'll always get the same objective value since you don't request the objective value for a specific objective. While you iterate, you should set the ObjNumber model attribute to t and then request the ObjNVal attribute. See this example for some more information.
Hope that helps!
Kind regards,
Ronald0 -
Hi Ronald,
thank you very much for your help!
Is this the right way to implement?
model.setObjectiveN(Z1, index=0, priority=2, name="Z1")
model.setObjectiveN(Z2, index=1, priority=1, name="Z2")
model.setObjectiveN(Z3, index=2, priority=0, name="Z3")
model.optimize()Z_values = []
for t in range(3):
model.setParam(GRB.Param.ObjNumber, t)
Z_values.append(model.ObjNVal)if model.status == GRB.INFEASIBLE or model.status == GRB.UNBOUNDED:
print("Das Problem ist unlösbar oder unbeschränkt.")
else:
print("Optimale Zielfunktionswerte:", Z_values)total_objective_value = sum(Z_values)
print(f"Gesamtwert der Zielfunktion: {total_objective_value}")Unfortunately, I now get the value 0 as the optimal solution. Am I missing something?
King regards,
Alexandra
0
Post is closed for comments.
Comments
3 comments