Search code examples
pythonmathematical-optimizationlinear-programmingcplexdocplex

How to write the constraint (A + B <= 1) in DOcplex?


I am new to DOcplex. I have two binary decision variables A and B and I wish to write the constraint (A+B <=1). I tried to write the constraint using "+" operator but it does not work.

Here is the code that causes the error at run-time:

A= {mdl.binary_var(name="A")}

B= {mdl.binary_var(name="B")}

mdl.add_constraint(A+B<=1)

And this is the error:

Exception has occurred: TypeError unsupported operand type(s) for +: 'set' and 'set'


Solution

  • from docplex.mp.model import Model
    
    mdl = Model(name='my model')
    
    A= mdl.binary_var(name="A")
    
    B= mdl.binary_var(name="B")
    
    mdl.add_constraint(A+B<=1)
    
    
    
    mdl.solve()
    
    
    print("A = ",A.solution_value)
    print("B = ",B.solution_value)
    

    works fine