Maximum recursion depth exceeded while calling a Python object
AnsweredI am trying to solve the following program:
I wrote the following code:
#########
# Create a new model
m = gp.Model("z_disjoint")
# Create variables
w_t_var = m.addMVar((Tilde_M), vtype=GRB.BINARY, name="w_t_var")
z_var = m.addMVar( (G-1), vtype=GRB.BINARY, name="z_var")
# Set objective
m.setObjective(gp.quicksum(z_var[g]*(w_t_var.T@A_g[g,:,:]@w_t_var) for g in range(G-1)), GRB.MINIMIZE)
# Add constraints:
m.addConstr(gp.quicksum(w_t_var[i] for i in range(Tilde_M)) == M, "w_t_constraint")
m.addConstr(gp.quicksum(z_var[i] for i in range(G-1)) == 1, "z_constraint")
#########
Every time I run this code I get an error at the line that sets the objective that says:
---------------------------------------------------------------------------
RecursionError Traceback (most recent call last)
Cell In[72], line 9
6 z_var = m.addMVar( (G-1), vtype=GRB.BINARY, name="z_var")
8 # Set objective
----> 9 m.setObjective(gp.quicksum(z_var[g]*(w_t_var.T@A_g[g,:,:]@w_t_var) for g in range(G-1)), GRB.MINIMIZE)
11 # Add constraints:
13 m.addConstr(gp.quicksum(w_t_var[i] for i in range(Tilde_M)) == M, "w_t_constraint")
File src/gurobipy/gurobi.pxi:3706, in gurobipy.quicksum()
Cell In[72], line 9, in <genexpr>(.0)
6 z_var = m.addMVar( (G-1), vtype=GRB.BINARY, name="z_var")
8 # Set objective
----> 9 m.setObjective(gp.quicksum(z_var[g]*(w_t_var.T@A_g[g,:,:]@w_t_var) for g in range(G-1)), GRB.MINIMIZE)
11 # Add constraints:
13 m.addConstr(gp.quicksum(w_t_var[i] for i in range(Tilde_M)) == M, "w_t_constraint")
File src/gurobipy/mvar.pxi:322, in gurobipy.MVar.__mul__()
File src/gurobipy/mquadexpr.pxi:1198, in gurobipy.MQuadExpr.__rmul__()
File src/gurobipy/mlinexpr.pxi:1749, in gurobipy.MLinExpr.__rmul__()
File src/gurobipy/mquadexpr.pxi:1198, in gurobipy.MQuadExpr.__rmul__()
File src/gurobipy/mlinexpr.pxi:1749, in gurobipy.MLinExpr.__rmul__()
[... skipping similar frames: gurobipy.MQuadExpr.__rmul__ at line 1198 (1481 times), gurobipy.MLinExpr.__rmul__ at line 1749 (1480 times)]
File src/gurobipy/mlinexpr.pxi:1749, in gurobipy.MLinExpr.__rmul__()
File src/gurobipy/mquadexpr.pxi:1198, in gurobipy.MQuadExpr.__rmul__()
File src/gurobipy/mquadexpr.pxi:1164, in gurobipy.MQuadExpr.__mul__()
File <frozen importlib._bootstrap>:1073, in _handle_fromlist(module, fromlist, import_, recursive)
File <frozen importlib._bootstrap>:1064, in _handle_fromlist(module, fromlist, import_, recursive)
RecursionError: maximum recursion depth exceeded while calling a Python object
I am not sure how to fix it. I tried doing it over three quick sums and got the same exact error. Any help?
0
-
Official comment
This post is more than two 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 Adnan,
While I'm not sure exactly how the recursion arises, the root of the problem here is that your objective contains terms which are the product of three variables.
I'd recommend you read our article How do I model multilinear terms in Gurobi? for a thorough understanding.
Using the concepts in that article you can resolve your problem by replacing the line where you set the objective with the following:
aux = m.addMVar( (G-1), lb=-float("inf"), name="aux")
m.addConstrs(aux[g] == w_t_var.T@A_g[g,:,:]@w_t_var for g in range(G-1))
m.setObjective(gp.quicksum(z_var[g]*aux[g] for g in range(G-1)), GRB.MINIMIZE)- Riley
1
Post is closed for comments.
Comments
2 comments