william007
william007

Reputation: 18537

Maximize revenue with deadline

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)?

Upvotes: 0

Views: 96

Answers (1)

Alex Fleischer
Alex Fleischer

Reputation: 10037

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

Upvotes: 1

Related Questions