Constraints causing a Syntax error
回答済みHi, I'm trying to perform a minimization problem and for some reason whenever I add my constraints (3) and (4), the next line of code always returns as a Syntax error. Any thoughts on this?

Here's my line's of code for those constraints and error it gives me:
# Constraint 3: for all other months, productions meets or beats demand
con3 = {}
for p in P:
for m in range (1, len(M)):
con3[p,m] = model.addConstr( ((z[p,m] * prodamount[p] + s[p,M[m-1]]) >= (proddemand[p][m] for m in range (NrOfMonths)), 'con3[' + str(p) + ',' + str(m) + ']-')
# Constraint 4: if production beats demand in month, then the product gets stored
con4 = {}
for p in P:
for m in range (1, len(M)):
con4[p,m] = model.addConstr( s[p,m] == (((z[p,m] * prodamount[p]) + s[p,M[m-1]]) - (proddemand[p][m] for m in range (NrOfMonths)), 'con4[' + str(p) + ',' + str(m) + ']-')
# ---- Solve ----
model.setParam('OutputFlag', True)
SyntaxError: invalid syntax
-
正式なコメント
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. -
Note that the code snippet you posted is not reproducible, because \(\texttt{z,prodamount,s,M,proddemand,NrOfMonths}\) are not defined. This makes providing help very hard.
There is a parentheses mismatch. Moreover, you are trying to loop over \(\texttt{range(NrOfMonths)}\) for no visible reason (there is no sum or such in the (3) constraint). The following should work
con3 = {}
for p in P:
for m in range (1, len(M)):
con3[p,m] = model.addConstr( (z[p,m] * prodamount[p] + s[p,M[m-1]]) >=
proddemand[p][m],
'con3[' + str(p) + ',' + str(m) + ']-')The same applies to (4).
0 -
Sorry, here's my full line of code and now it is giving me "Tuple index out of range" error:
# Product characteristics
productname = ('one', 'two', 'three')
holdcost = (6, 8, 10) # euros / (product-month)
NrOfMonths = 12 # planning horizon
proddemand = ( ( 750, 550, 550), # product demand for each month
( 650, 500, 500),
( 600, 450, 500),
( 500, 275, 320.5),
( 130.3, 350, 300),
( 650, 300, 150.2),
( 600, 500, 225),
( 750, 600, 500),
( 650, 500, 450),
( 600, 400.6, 350),
( 500, 300, 300),
( 550, 250, 350) )
# Personnel characteristics
perscost = (2000, 2000, 2500, 2500, 2500, 3000,
3000, 3000, 2500, 2500, 2000, 2000 ) # euro/ (employee-month)
prodamount = (15, 20, 10) # products/ (month-employee)
# ---- Sets ----
P = range (len (productname) ) # set of products
M = range (NrOfMonths) #set of months in year
# ---- Variables ----
# Decision Variables:
z = {} #z(p,m) (employees producing product p in month m)
for p in P:
for m in M:
z[p,m] = model.addVar (lb = 0, vtype = GRB.CONTINUOUS, name = 'Z[' + str(p) + ',' + str(m) + ']')
# Integrate new variables
model.update ()
s = {} #s(p,m) (number of products stored per month)
for p in P:
for m in M:
s[p,m] = model.addVar (lb = 0, vtype = GRB.CONTINUOUS, name = 'S[' + str(p) + ',' + str(m) + ']')
# Integrate new variables
model.update ()
# ---- Objective Function ----
model.setObjective ( quicksum (holdcost[p] * s[p,m] for p in P for m in M) + quicksum (perscost[m] * z[p,m] for p in P for m in M))
model.modelSense = GRB.MINIMIZE
model.update ()
# ---- Constraints ----
# Constraints 1: production meets or beats demand in 1st month
con1 = {}
for p in P:
for m in range (0):
con1[p] = model.addConstr( (z[p,m] * prodamount[p]) >= proddemand[p][0], 'con1[' + str(p) + ']-')
# Constraint 2: if production beats demand in 1st month, then the product gets stored
con2 = {}
for p in P:
for m in range (0):
con2[p] = model.addConstr( s[p,m] == ((z[p,m] * prodamount[p]) - proddemand[p,m]), 'con2[' + str(p) + ']-')
# Constraint 3: for all other months, productions meets or beats demand
con3 = {}
for p in P:
for m in range (1, len(M)):
con3[p,m] = model.addConstr( (z[p,m] * prodamount[p] + s[p,M[m-1]]) >= proddemand[p][m], 'con3[' + str(p) + ',' + str(m) + ']-')
# Constraint 4: if production beats demand in month, then the product gets stored
con4 = {}
for p in P:
for m in range (1, len(M)):
con4[p,m] = model.addConstr( s[p,m] == (z[p,m] * prodamount[p] + s[p,M[m-1]]) - proddemand[p][m], 'con4[' + str(p) + ',' + str(m) + ']-')
# ---- Solve ----
model.setParam('OutputFlag', True) # silencing gurobi output or not
model.setParam ('MIPGap', 0); # find the optimal solution
model.write("output.lp") # print the model in .lp format file
model.optimize ()0 -
You are currently using
proddemand[p][m]
but what you actually mean is
proddemand[m][p]
The small script makes it easier to see
for p in P:
for m in M:
# print(proddemand[p][m]) # results in an error
print(proddemand[m][p]) # worksPlease also note that
proddemand[p,m]
will not work and should be
proddemand[m][p]
Best regards,
Jaromił0 -
Awesome, thank you!
0
投稿コメントは受け付けていません。
コメント
5件のコメント