Decision Optimization

 View Only
  • 1.  Can a modulo operation be expressed as a constraint in CPLEX?

    Posted Thu October 08, 2020 01:53 AM
    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


  • 2.  RE: Can a modulo operation be expressed as a constraint in CPLEX?

    Posted Thu October 08, 2020 03:38 AM
    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
    ------------------------------



  • 3.  RE: Can a modulo operation be expressed as a constraint in CPLEX?

    Posted Thu October 08, 2020 04:55 AM
    Edited by System Test Fri January 20, 2023 04:25 PM
    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
    ------------------------------



  • 4.  RE: Can a modulo operation be expressed as a constraint in CPLEX?

    Posted Thu October 08, 2020 11:18 PM
    Thanks Alex just noticed this approach is for the constraint programming. As far as it is constrained to the best approach to solve scheduling problems. Is there any method like this for mathematical programming.

    ------------------------------
    Suresh Abeyweera
    ------------------------------



  • 5.  RE: Can a modulo operation be expressed as a constraint in CPLEX?

    Posted Fri October 09, 2020 01:20 AM
    Hi,

    then you should follow Philippe 's advice and introduce a new decision variable

    See https://github.com/AlexFleischerParis/zoodocplex/blob/master/zoomodulo.py

    from docplex.mp.model import Model
    mdl = Model(name='buses')
    nbbus40 = mdl.integer_var(name='nbBus40')
    nbbus30 = mdl.integer_var(name='nbBus30')
    mdl.add_constraint(nbbus40*40 + nbbus30*30 >= 300, 'kids')
    mdl.minimize(nbbus40*500 + nbbus30*400)
    # and suppose we need to order quantities
    # that should be 3 multipliers
    nbbus40div3 = mdl.integer_var(name='nbBus40div3')
    nbbus30div3 = mdl.integer_var(name='nbBus30div3')
    mdl.add(nbbus40==3*nbbus40div3)
    mdl.add(nbbus30==3*nbbus30div3)
    mdl.solve()
    mdl.export("c:\\temp\\buses.lp")
    for v in mdl.iter_integer_vars():
    print(v," = ",v.solution_value)

    ------------------------------
    ALEX FLEISCHER
    ------------------------------