Please correct my code
回答済みI have this code below. However when I'm running it says my mod.update() & my mod.optimal() are invalid syntax
Could you help me to debug this?
def paper_production(demand, costs, inventory_level, production_level):
##Declare model
mod = gp.Model('paper_production')
mod.setParam('OutputFlag', 0)
mod.setParam('LogToConsole', 0)
obj = None
production = None
emergency = None
## Declare variables
x = [mod.addVar(lb=0, vtype=GRB.CONTINUOUS, name='x_{i}')for i in range (5)]
y = [mod.addVar(lb=0, vtype=GRB.CONTINUOUS, name='y_{i}')for i in range (5)]
s = [mod.addVar(lb=0, vtype=GRB.CONTINUOUS, name='s_{i}')for i in range (5)]
x[0] = 4000
## Constraints
mod.addConstr(x[0] == production_level_ex['start'])
mod.addConstr(500 + x[1] + y[1] - s[1] == demand[1])
mod.addConstr(500 + x[1] + y[1] - demand[1] == s[1])
mod.addConstr(s[1] + x[2] + y[2] - s[2] == demand[2])
mod.addConstr(s[2] + x[3] + y[3] - s[3] == demand[3])
mod.addConstr(s[3] + x[3] + y[4] - s[4] == demand[4])
mod.addConstr(x[4] == production_level_ex['end'])
mod.addConstr(s[4] >= inventory_level_ex['end'])
## Objective
mod.setObjective(gp.quicksum(x[i]+(costs_ex['emergency']*y[i])+(costs_ex['increase']* max(0,x[i]-x[i-1]))+(costs_ex['decrease']*max(0,x[i-1]-x[i])) + (costs_ex['storage']*s[i]) + (costs_ex['penalty']*max(0,s[4]-inventory_level_ex['start'])), GRB.MINIMIZE
## Update model and optimize
mod.update()
mod.optimize()
return mod.objVal
print(paper_production(demand_ex, costs_ex, inventory_level_ex, production_level_ex))
0
-
Your code is not reproducible but there are some issues with your objective definition
- some brackets ')' are missing
- 'i' is not defined
- you need to model max via general constraints, see Model.addGenConstrMax()
please also have a look into the following article which gives a general idea of How do I model logical and general expressions? – Gurobi Help Center
0
サインインしてコメントを残してください。
コメント
1件のコメント