Objective code for summation
回答済みI am beginner for gurobi and trying to use via python. I couldn't write a code for this summation. Would you please help me?
∑∑ t * Xkt.
t and k are indices. Xkt is a binary decision variable.t is time and k consists of strings like book names. When I try to use sum or quicksum python gives error like 'unhashable list' or something like that.
t = [1, 2, 3, 4, 5]
k = ['k1', 'k2', 'k3', 'k4', 'k5']
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 why not try our AI Gurobot?. -
The code is as follow;
import gurobipy as gp
from gurobipy import GRB
T = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
K = ["k1","k2","k3","k4","k5","k6","k7","k8","k9"]
z = gp.Model("MP")
x = z.addVars (T, K, vtype =GRB.BINARY, name = "jf")
Job_assigns = z.addConstrs ((x.sum("*", k) == 1 for k in K), name = "job_assign")
# Objective
Result = gp.quicksum (x[T,K]*T)
z.setObjective(Result, GRB.MINIMIZE)0 -
Hi Musa,
The error \(\texttt{TypeError: unhashable type: 'list'}\) means that you are trying to use a list object as an index. You are doing this at \(\texttt{x[T,K]}\) where both \(\texttt{T}\) and \(\texttt{K}\) are lists.
You can achieve the correct summation via
Result = gp.quicksum (x[t,k] * t for t in T for k in K)
Best regards,
Jaromił0 -
Thank you. It worked well.
0
投稿コメントは受け付けていません。
コメント
4件のコメント