-1

I'm trying to transfer my constraints from Cplex OPT studio to the DOcplex Python.

The logic between the constraints is - if 1 of them does not met all the decision variables needs to be 0.

In Cplex Studio its look like that:

forall(r in risk_list) (0.2 * sum (i in funds) x[i][r]* risk[i] >=  r - 0.005) && (sum(i in funds)  x[i][r] == 5)|| sum (i in funds)x[i][r]==0;

How can I define "OR" "AND" logic between the constraints?

0

1 Answer 1

1

It's not always a good idea to move from OPL to python. And from python you can also call an OPL model.

But in docplex you can use logical constraints.

See How to Implement Logical OR constraint in CPLEX Python

zoological.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*460 + nbbus30*360)

mdl.solve()

for v in mdl.iter_integer_vars():
    print(v," = ",v.solution_value)

print()
print("with the logical constraint")

nbKindOfBuses = mdl.integer_var(name='nbKindOfBuses')
mdl.add(nbKindOfBuses==(nbbus40>=1)+(nbbus30>=1))

mdl.minimize(nbbus40*460 + nbbus30*360+(nbKindOfBuses-1)*(500))

mdl.solve()

for v in mdl.iter_integer_vars():
    print(v," = ",v.solution_value)

zooifthen.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)

mdl.solve()

for v in mdl.iter_integer_vars():
   print(v," = ",v.solution_value)

print()
print("with if nb buses 40 more than 3  then nbBuses30 more than 7")

#if then constraint
mdl.add(mdl.if_then(nbbus40>=3,nbbus30>=7))
mdl.minimize(nbbus40*500 + nbbus30*400)

mdl.solve()

 

for v in mdl.iter_integer_vars():
    print(v," = ",v.solution_value) 
Sign up to request clarification or add additional context in comments.

6 Comments

Hi Alex, Thanks for your comment. I tried to use the first example and i go an error. the logical constraints is:Mymodel.add((sum(0.2 * x [i,r] * risk_dict[i] for i in id) <= r + 0.005) + (sum(0.2 * x [i,r] * risk_dict[i] for i in id) >= r - 0.005) >=1 for r in risk_list) and i get the error:" raise DOcplexException(resolved_message) docplex.mp.utils.DOcplexException: Conversion from constraint to expression is available only for discrete constraints,"
One more question - Do you know how to call the Cplex OPL model from python?
i tried to install doopl and the error is : Could not find a version that satisfies the requirement doopl (from versions: none)
With float constraints you could use indicators , see github.com/AlexFleischerParis/zoodocplex/blob/master/…. Many ways to call OPL from python : doopl, ticdat pypi.org/project/ticdat or call OPL with oplrun github.com/AlexFleischerParis/howtowithopl/blob/master/…
Do you know if its possible also for "Logical_and" with constraints variables? and do you have an example of how to change the variables format result for the ModelReader?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.