NB:
With docplex and cpoptimizer you can directly write
from docplex.cp.model import CpoModel
mdl = CpoModel(name='buses')
nbbus40 = mdl.integer_var(0,1000,name='nbBus40')
nbbus30 = mdl.integer_var(0,1000,name='nbBus30')
# and suppose we need to order quantities
# that should be 3 multipliers
mdl.add(nbbus40 % 3==0)
mdl.add(nbbus30 % 3==0)
mdl.add(nbbus40*40 + nbbus30*30 >= 300)
mdl.minimize(nbbus40*500 + nbbus30*400)
msol=mdl.solve()
print(msol[nbbus40]," buses 40 seats")
print(msol[nbbus30]," buses 30 seats")
https://github.com/AlexFleischerParis/zoodocplex/blob/master/zoocpomodulo.py------------------------------
ALEX FLEISCHER
------------------------------
Original Message:
Sent: Thu October 08, 2020 03:38 AM
From: Philippe Couronne
Subject: Can a modulo operation be expressed as a constraint in CPLEX?
You're not saying whethe ryou use Cplex (docplex.mp) or docplex.cp). I'll answer for docplex.mp, the idea is the same, syntax may differ.
If variable 'x' is 0 modulo 12, then it is necessarily an integer variable, which is a multiple of 12, so there must exist an integer variable `q` so that
x == 12* q,
x = mymodel.integer_var();
q = mymodel.integer_var() ; # the quotient var
r = mymodel.add_constraint(x == 12 * q)
asserting a reminder `r` would be as simple, stating that x == 12*q +r
Philippe.
------------------------------
Philippe Couronne
Original Message:
Sent: Thu October 08, 2020 01:53 AM
From: Suresh Abeyweera
Subject: Can a modulo operation be expressed as a constraint in CPLEX?
I need to add a constraint that decision variable with modulo expression.I noted that in Stackoverflow there is no direct method we cannot do it but we need to introduce a new decission variable.Is there any simple way that I can do this from docplex.
Stackoverflow link : https://stackoverflow.com/questions/55671058/understanding-mod-operator-in-math-vs-programming
I am looking some simple thing like below.
x = mymodel.integer_var();
r = mymodel.add_constraint(x % 12 == 0,'con2');
Is it possible to have like this. (IDE and languages like minizinc have inbuilt it)
------------------------------
Suresh Abeyweera
------------------------------
#DecisionOptimization