Maximize positives and minimize negatives?

493 views
Skip to first unread message

ABu NeNe

unread,
Jun 19, 2022, 11:59:22 AM6/19/22
to or-tools-discuss
Hi, I'm new to linear programming using or-tools. I have x numbers of variables which can be both negative and positive but need to adds up to 0 in both vertically and horizontally.

I'm looking at using CP-SAT Solver in Java to do this with the codes as below. How do I get the maximize positives and minimize negatives? Do I need to model.maximize or model.minimize few or all of the variables or call the solver.bestObjectiveBound()?

a1 = 100, b1 = -100
a2 = -125, b2 = 150
a3 = 200, b3 = -100, c3 = 50
a4 = 50, b4 = -100, c4 = -25

//create variables
IntVar a1 = model.newIntVar(0, 100, "a1");
IntVar a2 = model.newIntVar(-125, 0, "a2");
...
//create constraints
model.addEquality(LinearExpr.newBuilder().add(a1).add(a2).add(a3).add(a4), 0);
model.addEquality(LinearExpr.newBuilder().add(b1).add(b2).add(b3).add(b4), 0);
model.addEquality(LinearExpr.newBuilder().add(c3).add(c4), 0);

model.addEquality(LinearExpr.newBuilder().add(a1).add(b1), 0);
model.addEquality(LinearExpr.newBuilder().add(a2).add(b2), 0);
model.addEquality(LinearExpr.newBuilder().add(a3).add(b3).add(c3), 0);
model.addEquality(LinearExpr.newBuilder().add(a4).add(b4).add(c4), 0);

//model.minimize(b2);

CpSolver solver = new CpSolver();
CpSolverStatus status = solver.solve(model);
...

Laurent Perron

unread,
Jun 19, 2022, 12:06:07 PM6/19/22
to or-tools-discuss
use addMinEquality and addMaxEquality. See https://google.github.io/or-tools/java/classcom_1_1google_1_1ortools_1_1sat_1_1CpModel.html#aff5b6c6b7cfe8f853b79e6b2628b6fc5

bestObjectiveBounds() make no sense in this question. See https://stackoverflow.com/questions/72676340/bestobjectivebound-vs-objectivevalue
Laurent Perron | Operations Research | lpe...@google.com | (33) 1 42 68 53 00



--
You received this message because you are subscribed to the Google Groups "or-tools-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to or-tools-discu...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/or-tools-discuss/f122f66b-4e09-49e1-a14f-f53319200adcn%40googlegroups.com.

ABu NeNe

unread,
Jun 19, 2022, 9:03:03 PM6/19/22
to or-tools-discuss
Hi Laurent,

My constraints consist of variables make up of positive and negative integers which add up to 0.

To use addMinEquality and addMaxEquality, does that mean I need to create a variable with both lower and upper bound value 0 for target?

IntVar obj= model.newIntVar(0, 0, "target1");

model.addMinEquality(obj, LinearExpr[])
Reply all
Reply to author
Forward
0 new messages