How to compare a expression with 0, Invalid data in vars array
AnsweredThis is my obj : to minimize $\sum^{1}_{t=T_H}(a_t*q_t*p*K + h*y_t+b*z_t)$
A constraint is $y_t = max(0, y_{t1} + \sum_{i∈{j:j+l_j=t}}q_i * p  d_t)$
And all I want to do is to get max(expression, 0). What should I do?
np.random.seed(4)
T_H = [i for i in range(10)]
T = len(T_H)
# transport_cost
K = 50
# holding cost
h = 1
# stockout cost
b = 100
# The number of boxes contained in a tray
p = 40
# VLT
l = [2 for i in range(10)]
# demand
d = [np.random.randint(100) for i in range(10)]
# initial inventory level
I = 109
# var
q = model.addVars(10, vtype=gp.GRB.INTEGER, name='q')
y = model.addVars(10, vtype=gp.GRB.INTEGER, name='y')
z = model.addVars(10, vtype=gp.GRB.INTEGER, name='z')
a = model.addVars(10, vtype=gp.GRB.BINARY, name='a')
# obj
obj = gp.quicksum((a[t] * q[t] * p * K + h * y[t] + b * z[t]) for t in range(T))
model.setObjective(obj, gp.GRB.MINIMIZE)
# cons
for t in range(T):
if t == 0:
model.addConstr(y[0] == I)
model.addConstr(z[0] == 0)
else:
# model.addConstr(temp == y[t  1]  d[t] + gp.quicksum(q[i] * p for i in range(t) if i + l[i] == t))
# model.addConstr(temp1 == 0)
model.addConstr(y[t] == gp.max_(y[t  1]  d[t] + gp.quicksum(q[i] * p for i in range(t) if i + l[i] == t), 0))
# model.addGenConstrMin(y[t], [temp, temp1])
# model.addConstr(z[t] == (z[t1]  d[t] + gp.quicksum(q[i] * p for i in range(t) if i + l[i] == t)))
model.addConstr(y[t] >= 0)
model.addConstr(z[t] >= 0)
model.addConstr(q[t] >= 0)

In another word, Max of value and if negative, return 0.
These are my two main constraints, y_t means the inventory level while z_t means the stockout inventory level in time t.
0 
Oh,sorry, z_t shoule be like this,
0 
Hi,
The issue here is that gp.max_() requires a list of variables (and an optional constant).
You need to introduce additional helper variables that are set equal to the term in your maxfunction. Then you can use these variables in the max_() function.Alternatively, you do not necessarily need the max_() function here. You could just set a lower bound of 0 for the y and zvariables, and add
y_t >= y_{t1} + sum ...
to push y_t up if the righthand side is positive (similar for z_t).
Due to the minimization objective, the y_t and z_t variables should then always be equal to the max of 0 and the term in an optimal solution.Best regards,
Mario0
Please sign in to leave a comment.
Comments
3 comments