0

Given three interval variables say

a = mdl.interval_var(name='a',start=(0,10),end=(0,10),size=5) #have revenue of 10
b = mdl.interval_var(name='b',start=(0,10),end=(0,10),size=5) #have revenue of 5
c = mdl.interval_var(name='c',start=(0,10),end=(0,10),size=5) #have revenue of 4

each represents a process to produce products a,b and c. Say products a,b,c each generate revenue of 10,5,4 respectively. And we have a deadline of 6 - this means that whatever product with end>=6 will not calculate towards the total revenue. Say if we have

 a (start=1, end=6, size=5, length=5)
 b (start=0, end=5, size=5, length=5)
 c (start=0, end=5, size=5, length=5)

Then the total revenue is 5+4=10 (since product a have end=6 (which satisfies end>=6)

Here's an initial code:

from docplex.cp.model import CpoModel
mdl = CpoModel()
a = mdl.interval_var(name='a',start=(0,10),end=(0,10),size=5) #have revenue of 10
b = mdl.interval_var(name='b',start=(0,10),end=(0,10),size=5) #have revenue of 5
c = mdl.interval_var(name='c',start=(0,10),end=(0,10),size=5) #have revenue of 4
mdl.add(mdl.maximize(...))#how to write this line
msol = mdl.solve(FailLimit=100000, TimeLimit=10)
msol.print_solution()

How can we accomplish what I describe here in the line of mdl.add(mdl.maximize(...)) (or anything that work)?

william007
  • 17,375
  • 25
  • 118
  • 194

1 Answers1

1
from docplex.cp.model import CpoModel
mdl = CpoModel()
deadline=5
a = mdl.interval_var(name='a',start=(0,10),end=(0,10),size=5) #have revenue of 10
b = mdl.interval_var(name='b',start=(0,10),end=(0,10),size=5) #have revenue of 5
c = mdl.interval_var(name='c',start=(0,10),end=(0,10),size=5) #have revenue of 4

mdl.add(mdl.start_of(a)==1)

mdl.add(mdl.maximize(mdl.presence_of(a)*(mdl.end_of(a)<=deadline)*10+
                     mdl.presence_of(b)*(mdl.end_of(b)<=deadline)*5+
                     mdl.presence_of(c)*(mdl.end_of(c)<=deadline)*4))
        
msol = mdl.solve(FailLimit=100000, TimeLimit=10)
msol.print_solution()

gives objective 9 since 9=5+4

Alex Fleischer
  • 9,276
  • 2
  • 12
  • 15
  • 1
    If interval variables are optional (which is not the case here), it is better to factorize the interval variables in the formulation of the objective (and exploit the absence value for expressions like end_of(itv,Value_If_Absent): mdl.add(mdl.maximize(mdl.end_of(a,deadline+1)<=deadline)*10 + mdl.end_of(b,deadline+1)<=deadline)*5 + mdl.end_of(c,deadline+1)<=deadline)*4)) – Philippe Laborie Jun 21 '21 at 07:32