0

I'm trying to solve a scheduling problem in Python using docplex.cp.

In my problem I have a constraint that states that if a given optional interval variable is present, then some other optional interval variable must be absent.

I have tried this:

mdl.add( 
        mdl.if_then( 
                    mdl.presence_of(optional_interval_var1), 
                    mdl.equal(mdl.presence_of(optional_interval_var2), 0) 
                   ) 
       )

But it doesn't seem to work. I find that this constraint isn't enforced in the solution provided by the solver.

Momafa
  • 3
  • 2

1 Answers1

0

You can use <= between presenceOf

For instance

from docplex.cp.model import *


model = CpoModel()

itvs1=interval_var(optional = True,
                             start = 1,
                             end   = 10)

itvs2=interval_var(optional = True,
                             start = 10,
                             end   = 15)


model.add(minimize(presence_of(itvs1)+presence_of(itvs2)))

#model.add(presence_of(itvs1)==1)

model.add(presence_of(itvs1)<=presence_of(itvs2))

# Solve the model
sol = model.solve(LogPeriod=1000000,trace_log=True)

res1=sol.get_var_solution(itvs1).is_present()
res2=sol.get_var_solution(itvs2).is_present()

print("itvs1 is present : ",res1)
print("itvs2 is present : ",res2)

gives

itvs1 is present :  False
itvs2 is present :  False

But if you uncomment

#model.add(presence_of(itvs1)==1)

then you get

itvs1 is present :  True
itvs2 is present :  True
Alex Fleischer
  • 9,276
  • 2
  • 12
  • 15
  • The kind of output I need is: itvs1 is present : True & itvs2 is present : False. Though the constraint presence_of(itvs1)<=presence_of(itvs2) doesn't do that, it is very close to what I want. I just need to replace it with presence_of(itvs1) == 1- presence_of(itvs2) in the model. I din't know that presence_of() constraints could be linked via inequalities and equalities. Thanks. – Momafa May 18 '21 at 10:59