Unsupported Type for LinExpr Argument
進行中Hello,
I am having trouble assigning constraints and my objective function. For my objective function, I am looking to maximize the summation of an element-wise product of a utility matrix (predefined and randomly generated) and a matrix of the same size (of which each entry is a binary variable of either 0 or 1).
# Number of agents
T = 10
# Number of resources
I = 5
# Utility Matrix
u = np.random.normal(loc=0, scale=1.0, size=(T, I))
# Budget for Resources
b = np.random.randint(low = 1, high = 10, size = (I, 1))
# Create a new model
offline_m = gp.Model('test')
# Suppress Gurobi output when set to 0
offline_m.setParam('OutputFlag', 1)
# Create varibles
x = offline_m.addMVar(shape=(T, I), vtype=GRB.BINARY, name='x')
# Set objective
obj = gp.quicksum(gp.quicksum(u[t,i]*x[t,i] for t in range(T)) for i in range(I))
offline_m.setObjective(obj, GRB.MAXIMIZE)
# Throws error:
# Error code 10003: Unsupported type (<class 'gurobipy.gurobipy.MLinExpr'>) for LinExpr addition argument
# Add budget constraint for resources
offline_m.addConstrs((gp.quicksum(x[:,i]) <= b[i] for i in range(I)), name='resourceconstraint')
# Throws error:
# Error code 10003: Unsupported type (<class 'gurobipy.gurobipy.MVar'>) for LinExpr addition argument
# Add constraint where only one agent can be assigned to a resource
offline_m.addConstrs((gp.quicksum(x[t,:]) <= 1 for t in range(T)), name='agenttoresource')
# Throws error:
# Error code 10003: Unsupported type (<class 'gurobipy.gurobipy.MVar'>) for LinExpr addition argument
# Add nonnegativity constraint
offline_m.addConstrs((x[t,i] >= 0 for t in range(T) for i in range(I)), name='nonnegativity')
# Does not throw error
# Optimize model
offline_m.optimize()
I have included the errors above, along with the lines that are causing them. Do you have any idea what is causing my errors? Thank you!
-
正式なコメント
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 quicksum() function is used to add together Var, LinExpr, and/or QuadExpr objects. It's not compatible with matrix-friendly MVar, MLinExpr, and MQuadExpr objects, which is what your code constructs when the \( \texttt{x} \) variables are defined as MVar objects.
To fix these errors, try one of the following:
1. Use Python's built-in \( \texttt{sum()} \) function instead of \( \texttt{quicksum()} \).
2. Define \( \texttt{x} \) with Model.addVars() to create Var objects instead of MVar objects. You will have to rewrite the parts of your code that use matrix slicing.
0 -
Thank you so much for your fast response, the timing was perfect!
0 -
Hello;
I am using Gurobi to solve a MILP problem. And got the following error.
obj= (quicksum((F[j] *Y[j]) forjinJ) +quicksum(C[i,j] *R[i,j] for i in I for j in J) +quicksum(c[i,n,j] *f[i,n,j] for i in I for j in J) +quicksum(g[i,j,k]*L[i,j,k] for i in I for j in J for k in K))--------------------------------------------------------------------------- GurobiError Traceback (most recent call last) Input obj = (quicksum((F[j] * Y[j]) for j in J) + quicksum(C[i,j] * R[i,j] for i in I for j in J) + 3 quicksum(c[i,n,j] * f[i,n,j] for i in I for j in J) + quicksum(g[i,j,k]*L[i,j,k] for i in I for j in J for k in K)) File src\gurobipy\gurobi.pxi:3632, in gurobipy.quicksum() File src\gurobipy\linexpr.pxi:473, in gurobipy.LinExpr.__iadd__() File src\gurobipy\linexpr.pxi:183, in gurobipy.LinExpr.add() GurobiError: Unsupported type (<class 'pandas.core.series.Series'>) for LinExpr addition argumentGurobiError: Unsupported type (<class 'pandas.core.series.Series'>) for LinExpr addition argument
Error is indicated on the bolded part of the model as shown in the screen shoot below. How can I solve this problem please?
Note: F(j) is a list of continuous value and Y(j) is binary variable.
0
投稿コメントは受け付けていません。
コメント
4件のコメント