lengleng
lengleng

Reputation: 11

How to describe the objective is bigger than zero with OR-Tools for python

I'm trying to discribe a MIP with objective is f(r)=sum of (max(0,U(m,r)-SC(m,r))) for m in set M. the constraint is for m in set M, for r in set R,U(m,r)<=C(m,r). the decision variable is Map[m,p] is a binary variable.

CMR = [[6,40,5,20],[2,10,1,5 ]] 
PR  = [[2, 10],[3, 2]]
model=cp_model.CpModel()
Map = {}
for m in range(num_machine):
   for p in range(num_process):
       Map[m,p] = model.NewBoolVar('Map')
for m in range(num_machine):
   for r in range(num_resource):
       for p in range(num_process):
           model.Add( sum([Map[m,p] * PR[p][r] for p in range(num_process)]) <= CMR[m][r])
           model.Add( sum([Map[m,p] for p in range(num_process) ]) == 1)
f,f1=[],[]
for r in range(num_resource):
    t=[]
    for m in range(num_machine):
        t.append(sum([Map[m,p] * PR[p][r] for p in range(num_process)])  - CMR[m][r+num_resource])
    f.append(t)
f1=[sum(u) for u in f]
objective_terms = []
for r in range(num_resource):
    objective_terms.append(f1[r]*w1[r])
model.Minimize(sum(objective_terms))
solver = cp_model.CpSolver()

the obj I can get now is U(m,r)-SC(m,r) , but what I actually want is (max(0,U(m,r)-SC(m,r))), if I change t.append as" t1.append(max(0, (sum([Map[m,p] * PR[p][r] for p in range(num_process)]) - CMR[m][r + num_resource])))" I will get the Error:NotImplementedError: Evaluating a BoundedLinearExpr as a Boolean value is not supported. Could anyone familiar with or-tools could help to fix the (max(0,a value)) in the objective setting.

Thank you very much

Upvotes: 1

Views: 447

Answers (1)

Stradivari
Stradivari

Reputation: 2766

You have to create a new variable delta for U(m,r)-SC(m,r) (just use model.Add) and another for your term in the objective, let's call it excess, then you just have to use a AddMaxEquality(excess, [delta, 0]) to set it's value.

The code would be similar to this example:

model.Add(delta == soft_min - sum_var)
excess = model.NewIntVar(0, 7, prefix + ': under_sum')
model.AddMaxEquality(excess, [delta, 0])

Upvotes: 2

Related Questions