1

I used https://developers.google.com/optimization/cp/cp_solver to try to resovle a constraint programming problem, and I would like to maximize a function, then maximize another.

I tried to call Maximize twice, but it does not seems to work.

Here is a minimal example code :

var solver = new CpSolver();
var model = new CpModel();

IntVar var1 = model.NewIntVar(100, 300, "var1");
IntVar var2 = model.NewIntVar(200, 500, "var2");
IntVar var3 = model.NewIntVar(100, 400, "var3");

model.Add(var1 + var2 + var3 == 1000);

int prio1 = 1;
int prio2 = 2;
int prio3 = 1;
int secondPrio1 = 4;
int secondPrio2 = 1;
int secondPrio3 = 5;
LinearExpression priority = prio1 * var1 + prio2 * var2 + prio3 * var3;
LinearExpression secondPriority = secondPrio1 * var1 + secondPrio2 * var2 + secondPrio3 * var3;

model.Maximize(priority); // Is overwritten by second Maximize, does not work
model.Maximize(secondPriority);

CpSolverStatus result = solver.Solve(model);

I would like that my solver put the max value to var2 (500) because it has the highest prio#, then it puts the max value to var3 (400) because it has the highest secondPrio#. Then, the remaining (100) would be assigned to var1.

Thanks in advance.

etov
  • 2,972
  • 2
  • 22
  • 36
Damien IFT
  • 73
  • 1
  • 9
  • 1
    I would use two solves. Solve for the first obj. Then fix obj 1 and solve for obj2. – Erwin Kalvelagen May 28 '19 at 10:29
  • 2
    So the overwrite of the objective is the desired behavior. You try `model.Maximize(10000 * priority + secondPriority)` – Laurent Perron May 28 '19 at 20:28
  • Thanks for your answers. These are some clever ideas, but I fear than, with like 10 levels of priorities, it will become really complex to manage. If I choose 10000 * priority + second priority, I need to ensure that secondPriority is always < 10000. However, if OR-Tools does not offer a such feature, I think I will have to do it. – Damien IFT May 29 '19 at 11:56

0 Answers0