Reputation: 149
i am a bit confused on how i can use multiple objective functions in Gurobi. I found this but dont realy understand how i can implement multiple objective functions with this.
Usually i set something like a GRBLinExpr as Objective. But there i cant set ObjN as in the example. Another thing is that i want to minimize and maximize some objectives.
Here is a simple example (dose not work):
GRBEnv env = new GRBEnv();
GRBModel model = new GRBModel(env);
var x = model.AddVar(0, 10, 0, GRB.INTEGER, "");
var y = model.AddVar(0, 10, 0, GRB.INTEGER, "");
var z = model.AddVar(0, 10, 0, GRB.INTEGER, "");
var expr1 = new GRBLinExpr();
expr1.AddTerm(1, x);
expr1.AddTerm(1, y);
expr1.AddTerm(1, z);
model.AddConstr(expr1 >= 5, "");
var expr2 = new GRBLinExpr();
expr2.AddTerm(1,z);
model.NumObj = 3; // there are 3 Objectives
model.Parameters.ObjNumber = 1;
model.ObjNWeight = 1;
model.ObjNPriority = 2;
model.ObjNName = "MinSum";
// i need to set this somehow ...
model.SetObjective(expr2, GRB.MAXIMIZE);
model.Parameters.ObjNumber = 2;
model.ObjNWeight = 1;
model.ObjNPriority = 1;
model.ObjNName = "MaxZ";
// i need to set this somehow ...
model.SetObjective(expr1, GRB.MINIMIZE); // overwrites first objective
model.Optimize();
Console.WriteLine($"x={x.X} y={y.X} z={z.X}");
so the solution for this code should be x=0, y=0, Z=10 for example
how can i achieve something like this ?
Upvotes: 0
Views: 734
Reputation: 5653
In version 7.5, the method GRBModel.SetObjectiveN() supports multiple objectives. If you use GRBModel.SetObjectiveN(), your code should work with version 7.5, though you should fix the following errors:
model.NumObj
should be 2model.Parameters.ObjNumber
should be 0 and 1, respectivelyUpvotes: 1