0

Using OPL I could do the write below constraint.Mainly I used or(||) within a constraint and successfully get the results.na_supply_component is 2d decision variable

// MOQ (Minimum Order Quantity) 
con2: forall(i in 1..12,j in 1..5)
na_supply_component[j,i]>=na_moq_supplier[j] || na_supply_component[j,i]==0;

Now I am trying to convert my OPL code into python using docplex package. Sample code as follows.Here I have used 1d Decision variable just to check(purch_val)

Supplier=['q','w','e','r','t'];
n_Supplier=len(Supplier);
r_Supplier=range(n_Supplier);
na_moq_supplier=[10,20,30,40,0]
from docplex.mp.model import Model

x=Model("mymod");
purch_val=x.integer_var_list(n_Supplier,lb=0,ub=10000);
for i in r_Supplier:x.add_constraint(purch_val[i]>=na_moq_supplier[i] or purch_val[i]==0);

When I run I get an error TypeError: Cannot convert a linear constraint to boolean: x1 >= 10 It does not allow me to use or.Could you exaplain what can I do to overcome this error.

suresh_chinthy
  • 377
  • 2
  • 12

1 Answers1

0
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")

mdl.add((nbbus40>=7) + (nbbus30>=7)>=1)



mdl.solve()

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

works fine

and gives

nbBus40  =  6.0
nbBus30  =  2.0

with the logical constraint
nbBus40  =  7.0
nbBus30  =  1.0

from logical constraints

Alex Fleischer
  • 9,276
  • 2
  • 12
  • 15