gurobipy.GurobiError: Unable to convert argument to an expression
ユーザーの入力を待っています。I am trying to make an optimize model, after running all restrictions and variables and realizing they are well defined, I get a problem trying to use
model.setObjective(objetivo, GRB.MINIMIZE)
it apears:
gurobipy.GurobiError: Unable to convert argument to an expression
here is my code:
import random
from gurobipy import GRB, Model, quicksum
random.seed(10)
# SUBINDICES:
N = 20
K = 3
T = 30
L = 22
# RANGOS:
Ingredientes = range(1, N+1)
Proovedores = range(1, K+1)
Tiempo = range(1, T+1)
Tiempo2 = range(2, T+1)
Tipo_de_sushi = range(1, L+1)
P = random.sample(Ingredientes, k=int(6))
I_sin_P = []
for i in Ingredientes:
if i not in P:
I_sin_P.append(i)
# PARAMETROS:
V = 4000000
W = 3300000
v = {(i): random.randint(100, 2500) for i in Ingredientes}
d = {(l, t): random.randint(0, 135) for l in Tipo_de_sushi for t in Tiempo}
q = 450
C = {(i): random.randint(2, 25) for i in Ingredientes}
p = {(i, k): random.randint(1000, 2000)
for i in Ingredientes for k in Proovedores}
a = {(i, l): random.randint(30, 50)
for i in Ingredientes for l in Tipo_de_sushi}
P = random.sample(Ingredientes, k=int(6))
I_sin_P = []
for i in Ingredientes:
if i not in P:
I_sin_P.append(i)
exp = {(i): random.randint(1, 20) for i in Ingredientes}
model = Model()
# VARIABLES:
x = model.addVars(Ingredientes, Tiempo, vtype=GRB.INTEGER, name="x_it")
r = model.addVars(Tipo_de_sushi, Tiempo, vtype=GRB.INTEGER, name="r_lt")
I = model.addVars(Ingredientes, exp, Tiempo,
vtype=GRB.INTEGER, name="I_igt")
s = model.addVars(Ingredientes, Tipo_de_sushi, exp, Tiempo,
vtype=GRB.INTEGER, name="s_ilgt")
o = model.addVars(P, Tiempo, vtype=GRB.INTEGER, name="o_pt")
F = model.addVars(Ingredientes, Tiempo, vtype=GRB.INTEGER, name="F_it")
h = model.addVars(Ingredientes, Tiempo, vtype=GRB.INTEGER, name="h_it")
u = model.addVars(Ingredientes, Tiempo, vtype=GRB.INTEGER, name="u_it")
model.update()
# RESTRICCIONES:
# R1
model.addConstrs(((r[l, t]) <= quicksum(s[i, l, e, t]/a[i, l] for e in range(2, exp[i]+1))
for t in Tiempo for l in Tipo_de_sushi for i in I_sin_P), name="R1")
# R2
model.addConstrs((r[l, t] <= d[l, t]
for t in Tiempo for l in Tipo_de_sushi), name="R2")
# R3
model.addConstrs(
(q >= quicksum(r[l, t] for l in Tipo_de_sushi) for t in Tiempo), name="R3")
# R4
model.addConstrs((I[i, exp[i], t] == x[i, t] - quicksum(s[i, l, exp[i], t] for l in Tipo_de_sushi)
for i in I_sin_P for t in Tiempo2), name="R4")
model.addConstrs((I[i, e, t] == I[i, e+1, t-1] - quicksum(s[i, l, e, t] for l in Tipo_de_sushi)
for i in I_sin_P for e in range(2, exp[i]-1) for t in Tiempo2), name="R5")
model.addConstrs((I[i, 1, t] >= I[i, 2, t-1] - quicksum(s[i, l, 1, t]
for l in Tipo_de_sushi) for i in I_sin_P for t in Tiempo2), name="R6")
model.addConstrs((I[i, e, 1] == C[i]
for i in I_sin_P for e in range(1, exp[i]+1)), name="R7")
# R5
model.addConstrs((F[p, t] == F[p, t-1] + x[p, t] - o[p, t-1]
for p in P for t in Tiempo2), name="R8")
model.addConstrs((F[p, 1] == C[p] + x[p, 1] for p in P), name="R11")
# R6
model.addConstrs((quicksum(p[i, k] for k in Proovedores) >= x[i, t]
for t in Tiempo for i in Ingredientes), name="R12")
# R7
model.addConstrs((quicksum(v[i]*quicksum(I[i, e, t] for e in range(2, exp[i])) for i in I_sin_P) <=
V for t in Tiempo), name="R12")
# R8
model.addConstrs((quicksum(v[p]*F[p, t]
for p in P) <= W for t in Tiempo), name="R13")
# R9
model.addConstrs((h[i, t] == I[i, 1, t]
for i in I_sin_P for t in Tiempo), name="R14")
# R10
model.addConstrs((u[p, t] == o[p, t-1] - quicksum(r[l, t]*a[p, l]
for l in Tipo_de_sushi) for p in P for t in Tiempo2), name="R15")
# R11
model.addConstrs((quicksum(d[l, t]*a[p, l] for l in Tipo_de_sushi)
<= o[p, t-1] for t in Tiempo2 for p in P), name="R16")
# R12
model.addConstrs((quicksum(a[i, l]*r[l, t] for l in Tipo_de_sushi) <= quicksum(I[i, e, t-1]
for e in range(2, exp[i])) for t in Tiempo2 for i in I_sin_P), name="R17")
# R13
model.addConstrs((o[p, t] <= F[p, t] for t in Tiempo for p in P), name="R18")
# R14
model.addConstrs((quicksum(r[l, t]
for l in Tipo_de_sushi) >= 250 for t in Tiempo))
# Naturaleza de las variables
model.addConstrs(
(x[i, t] >= 0 for t in Tiempo for i in Ingredientes), name="R19")
model.addConstrs(
(r[l, t] >= 0 for t in Tiempo for l in Tipo_de_sushi), name="R20")
model.addConstrs((I[i, e, t] >= 0 for t in Tiempo for i in Ingredientes for e in range(
1, exp[i])), name="R21")
model.addConstrs((s[i, l, e, t] >= 0 for t in Tiempo for i in Ingredientes for e in range(
1, exp[i]) for l in Tipo_de_sushi), name="R22")
model.addConstrs((o[p, t] >= 0 for t in Tiempo for p in P), name="R23")
model.addConstrs(
(F[i, t] >= 0 for i in Ingredientes for t in Tiempo), name="R24")
model.addConstrs(
(h[i, t] >= 0 for i in Ingredientes for t in Tiempo), name="R25")
model.addConstrs(
(u[i, t] >= 0 for i in Ingredientes for t in Tiempo), name="R26")
# Funcion Objetivo
objetivo = (quicksum(u[p, t] for p in P) + quicksum(h[i, t] for i in I_sin_P) + 0.01 * quicksum(
quicksum(r[l, t]*a[i, l] for i in Ingredientes) for l in Tipo_de_sushi) for t in Tiempo)
model.setObjective(objetivo, GRB.MINIMIZE)
model.optimize()
# Manejo Soluciones
print("\n"+"-"*10+" Manejo Soluciones "+"-"*10)
print(f"El valor objetivo es de: {model.ObjVal}")
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. -
Dear Eduardo,
there is something wrong with your objective function. I intuitively tried to correct its implementation to:
objetivo = quicksum(quicksum(u[p, t] for p in P) + quicksum(h[i, t] for i in I_sin_P) + 0.01 * quicksum(
quicksum(r[l, t]*a[i, l] for i in Ingredientes) for l in Tipo_de_sushi) for t in Tiempo)but then your model is infeasible.
If that is not what you expect, you can share the mathematical formulation of your objective function. We may then be able to help you further.
Best regards
Jonasz0
投稿コメントは受け付けていません。
コメント
2件のコメント