Decision Optimization

 View Only
Expand all | Collapse all

Logical constraints for CPLEX instead of linear

  • 1.  Logical constraints for CPLEX instead of linear

    Posted Tue October 10, 2023 06:21 AM

    Hi!

    I've been looking into this documentation page: Logical constraints for CPLEX

    I am trying to move away from using linear rules/constraints for my model. Based on the information from this page, it is possible to add the constraints in the logical format rather than linear. For example, I want to do something like the following:

    variables = ["A", "B", "C", "D"]
    mdl1 = Model(name='test_model')
    for var in variables:
          mdl1.binary_var(name=var)
    mdl1.add(eval("(mdl1.get_var_by_name('A') | mdl1.get_var_by_name('B')) & (mdl1.get_var_by_name('C') | mdl1.get_var_by_name('D'))"))

    However, seems like this is not a valid option due to syntax and formatting errors and the symbols from the previously mentioned documentation do not seem to work.

    What am I doing incorrectly?

    How can I add logic rules? Could you give the example of how to add a rule: A => -(C & B) | D ?

    Which format does the CPLEX expect and is using non-linear rules possible?

    Thank you and best regards,

    Mariia



    ------------------------------
    Mariia Bogdanova
    ------------------------------


  • 2.  RE: Logical constraints for CPLEX instead of linear

    Posted Tue October 10, 2023 08:24 AM

    Hi,

    One possible formulation of your constraint could be:

    mdl.add_if_then(var_A == 1, mdl.logical_or(mdl.logical_and(var_C, var_B), var_D) == 1)

    The documentation for these statements can be found here: https://ibmdecisionoptimization.github.io/docplex-doc/mp/docplex.mp.model.html

    `mdl.add_if_then(...)` takes constraints as arguments.

    `var_A` is a shortcut for `mdl.get_var_by_name('A')`.

    Best regards,



    ------------------------------
    Hugues Juille
    ------------------------------