Mod operator for scalar product

27 views
Skip to first unread message

Saurabh Potdar

unread,
Feb 9, 2022, 1:50:19 AM2/9/22
to choco-solver
Available operators for scalar are "=", ">=", "<="
model.scalar(variable, coeff, "=", target).post();

Now I'm trying to add the following constraint
scalar product of  variable * coeff %10 == 0.
So how to do this?

Saurabh Potdar

unread,
Feb 9, 2022, 3:49:46 AM2/9/22
to choco-solver
Model model = new Model("");
int[] duration = new int[]{1, 2, 1, 2, 3, 4, 1, 2};
int[] marks = new int[]{10, 5, 1, 5, 3, 4, 1, 2};
int targetDuration = 5;

IntVar[] questionSelectedOrNot = model.intVarArray("a", 8, 0, 1);

model.scalar(questionSelectedOrNot, duration, "=", targetDuration).post();
//Add a new constraint to model
// scalar product of questionSelectedOrNot and marks is a multiple of 10

if (model.getSolver().solve()) {
        System.out.println(questionSelectedOrNot);
}

Arthur Godet

unread,
Feb 28, 2022, 3:26:58 AM2/28/22
to choco-solver
Hi,
Sorry for the late answer, the mail notifications from the Google group were in my spam filter.
In Choco Solver, the modulo constraint is called mod, and you can call it from the model. To add the constraint you want, you will need an intermediate variable, let's call it scalarProduct :
IntVar scalarProduct = model.intVar(0, 31); // 31 being the maximum value of the scalar product you want
model.scalar(questionSelectedOrNot, marks, "=", scalarProduct).post();
model.mod(scalarProduct, 10, 0).post();

Have a nice day,
Arthur Godet
Reply all
Reply to author
Forward
0 new messages