Simpler approach to Product IntVar Variables (CP-SAT | C# | ORTools 7.8)

360 views
Skip to first unread message

Miguel Ganilho Santos

unread,
Aug 25, 2020, 3:21:49 PM8/25/20
to or-tools-discuss
Hi,

It's possible to have a simpler approach to this constraint implementation?
//CONSTRAINT: (x + y) * z > 15
model.Add(x + y == aux1);
model.AddProdEquality(aux2, new List<IntVar>() { aux1, z });
model.Add(aux2 > 15);

Complete C# implementation of CP-SAT model:
CpModel model = new CpModel();
CpSolver solver;

IntVar x = model.NewIntVar(0, 10, "x");
IntVar y = model.NewIntVar(0, 10, "y");
IntVar z = model.NewIntVar(0, 10, "z");

IntVar aux1 = model.NewIntVar(0, 20, "aux1");
IntVar aux2 = model.NewIntVar(0, 200, "aux2");

List<IntVar> vars = new List<IntVar>() { x, y,z, aux1,aux2 };

//CONSTRAINT: (x + y) * z > 15
model.Add(x + y == aux1);
model.AddProdEquality(aux2, new List<IntVar>() { aux1, z });
model.Add(aux2 > 15);

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

if (status == CpSolverStatus.Feasible ^ status == CpSolverStatus.Optimal)
{
foreach (var v in vars)
{
Console.WriteLine(String.Format("  {0} = {1}", v.ShortString(), solver.Value(v)));
}
}
else
{
Console.WriteLine("no solution");
}
 
Thanks,
Miguel

Laurent Perron

unread,
Aug 25, 2020, 4:00:07 PM8/25/20
to or-tools-discuss
IntVar x = model.NewIntVar(0, 10, "x");
IntVar y = model.NewIntVar(0, 10, "y");
IntVar z = model.NewIntVar(0, 10, "z");

IntVar aux1 = model.NewIntVar(1, 20, "aux1");
IntVar aux2 = model.NewIntVar(15, 200, "aux2");

//CONSTRAINT: (x + y) * z > 15
model.Add(x + y == aux1);
model.AddProdEquality(aux2, new List<IntVar>() { aux1, z });

:-)
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/c6ec7524-3c6d-490f-bc3d-3dafc94b8b71n%40googlegroups.com.

Miguel Ganilho Santos

unread,
Aug 25, 2020, 5:16:32 PM8/25/20
to or-tools-discuss
tks Laurent.
I was looking for a "more magical way" to avoid the creation of 2 additional model variables to make a simple operation with other 3 (in my real implementation it represent's thousands of aux variables),
but your tip it's a very cleaver way of simplifing my example ;) 

Laurent Perron

unread,
Aug 25, 2020, 5:45:25 PM8/25/20
to or-tools-discuss
Actually, traditional CP solver May hide the complexity, but they will create the intermediate variables internally. 

It is then very easy to create duplicates, and it creates a lot of problems if you create a product expression and do it use it. 

For instance: var x = y * z, will create and tmp car, and add the product constraint to the solver. Even if you never use x in your model. 

This is why I prefer to make everything explicit, even if it is more verbose. 

Reply all
Reply to author
Forward
0 new messages