Generalizing exponential constraints
回答済みDear Community,
I have successfully modeled a problem in Gurobi with exponential constraints and objective functions. As a next step, I would like to generalize my constraints to make the model leaner. Unfortunately, this is not working at the moment. Is there any way to achieve this or is this not possible with exponential functions in Gurobi?
My attempt:
m.addConstr((-b)*t[0] == l[0])
m.addGenConstrExp(l[0],y[0])
m.addConstr((-b)*t[1] == l[1])
m.addGenConstrExp(l[1],y[1])
m.addConstr((-b)*t[2] == l[2])
m.addGenConstrExp(l[2],y[2])
into
m.addConstr(((-b)*t[i] for i in range(num_work)) == (l[i] for i in range(num_work)))
m.addGenConstrExp((l[i] for i in range(num_work)),(y[i] for i in range(num_work)))
My Model:
from gurobipy import Model, GRB, quicksum
c = 1
k = 1
a = 0.05
b = 1/600
T = 8*60
e = 5
num_work = k+1
num_bre = k
m = Model("Optimization")
m.params.NonConvex = 2
t = m.addVars(num_work, lb=0, ub=T, name="work")
u = m.addVars(num_bre, lb=0, ub=T, name="break")
r = m.addVars(num_work, lb=0, ub=c, name="work rate")
z = m.addVars(num_bre, lb=0, ub=c, name="work rate break")
#exponential functions
l = m.addVars(num_work, lb=-GRB.INFINITY, name="exponent")
y = m.addVars(num_work, name="e-Funktion")
w = m.addVars(num_bre, lb=-GRB.INFINITY, name="exponent2")
p = m.addVars(num_bre, name="e-Funktion2")
#objective function
q = m.addVars(num_work, name="Objective Function")
#constraints for fatigue
m.addConstr((-b)*t[0] == l[0])
m.addGenConstrExp(l[0],y[0])
m.addConstr((-b)*t[1] == l[1])
m.addGenConstrExp(l[1],y[1])
#constraints for recovery
m.addConstr((-a)*u[0] == w[0])
m.addGenConstrExp(w[0],p[0])
#m.addConstr((-a)*u[1] == w[1])
#m.addGenConstrExp(w[1],p[1])
m.addConstr(1 == r[0])
m.addConstr(r[0]*y[0] == z[0])
m.addConstr((z[0]+(c-z[0]) * (1-p[0])) == r[1])
#m.addConstr(r[1]*y[1] == z[1])
#m.addConstr(c+(z[1]-c) * p[1] == r[2])
m.addConstr((quicksum(t[i] for i in range(num_work)) + quicksum(u[i] for i in range(num_bre))) <= T)
#objective function
m.addConstr((quicksum(((r[i] - r[i]*y[i])/b) for i in range(num_work)) == quicksum(q[i] for i in range(num_work))))
m.setObjective(quicksum(q[i] for i in range(num_work)), GRB.MAXIMIZE)
m.optimize()
Thank you very much!
Best,
Elina Joksch
0
-
正式なコメント
This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum, or try Gurobot, our chatbot interface offering instant, expert-level support. -
Hi Elina,
You can make your implementation cleaner and more compact like below:
m.addConstrs((-b * t[i] == l[i] for i in range(num_work)), name="fatigue")
for i in range(num_work):
m.addGenConstrExp(l[i], y[i], name=f"fatigue_exponential_{i}")You still need to add the exponential constraints in the form \(y=e^l\) one by one.Best regards,Maliheh0
投稿コメントは受け付けていません。
コメント
2件のコメント