提问者:小点点

从AMPL转换为Pyomo


我正在尝试将AMPL模型转换为Pyomo(我没有使用它的经验)。我发现语法很难适应,尤其是约束和目标部分。我已经将我的计算机与python、anaconda、Pyomo和GLPK链接在一起,只需要下载实际代码。我是一个初学者,所以如果我的代码写得很糟糕,请原谅我。还在努力掌握这个窍门!

以下是来自AMPL代码的数据:

set PROD := 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30;

set PROD1:= 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30;

ProdCost    414 3   46  519 876 146 827 996 922 308 568 176 58  13  20  974 121 751 130 844 280 123 275 843 717 694 72  413 65  631

HoldingCost 606 308 757 851 148 867 336 44  364 960 69  428 778 485 285 938 980 932 199 175 625 513 536 965 366 950 632 88  698 744

Demand  105 70  135 67  102 25  147 69  23  84  32  41  81  133 180 22  174 80  24  156 28  125 23  137 180 151 39  138 196 69

以下是模型:

set PROD;  # set of production amounts
set PROD1; # set of holding amounts

param ProdCost {PROD} >= 0;     # parameter set of production costs

param Demand {PROD} >= 0;     # parameter set of demand at each time

param HoldingCost {PROD} >= 0;     # parameter set of holding costs

var Inventory {PROD1} >= 0;     # variable that sets inventory amount at each time

var Make {p in PROD} >= 0;  # variable of amount produced at each time

minimize Total_Cost: sum {p in PROD} ((ProdCost[p] * Make[p]) + (Inventory[p] * HoldingCost[p]));

               # Objective: minimize total cost from all production and holding cost

subject to InventoryConstraint {p in PROD}: Inventory[p] = Inventory[p-1] + Make[p] - Demand[p];

                # excess production transfers to inventory

subject to MeetDemandConstraint {p in PROD}: Make[p] >= Demand[p] - Inventory[p-1];

               # Constraint: holding and production must exceed demand

subject to InitialInventoryConstraint: Inventory[0] = 0;

                # Constraint: Inventory must start at 0

这是我到目前为止所拥有的。不确定是否正确:

from pyomo.environ import *

demand=[105,70,135,67,102,25,147,69,23,84,32,41,81,133,180,22,174,80,24,156,28,125,23,137,180,151,39,138,196,69]

holdingcost=[606,308,757,851,148,867,336,44,364,960,69,428,778,485,285,938,980,932,199,175,625,513,536,965,366,950,632,88,698,744]

productioncost=[414,3,46,519,876,146,827,996,922,308,568,176,58,13,20,974,121,751,130,844,280,123,275,843,717,694,72,413,65,631]

model=ConcreteModel()

model.I=RangeSet(1,30)
model.J=RangeSet(0,30)
model.x=Var(model.I, within=NonNegativeIntegers)
model.y=Var(model.J, within=NonNegativeIntegers)

model.obj = Objective(expr = sum(model.x[i]*productioncost[i]+model.y[i]*holdingcost[i] for i in model.I))

def InventoryConstraint(model, i):
    return model.y[i-1] + model.x[i] - demand[i] <= model.y[i]
InvCont = Constraint(model, rule=InventoryConstraint)

def MeetDemandConstraint(model, i):
    return model.x[i] >= demand[i] - model.y[i-1]
DemCont = Constraint(model, rule=MeetDemandConstraint)

def Initial(model):
    return model.y[0] == 0
model.Init = Constraint(rule=Initial)

opt = SolverFactory('glpk')
results = opt.solve(model,load_solutions=True)
model.solutions.store_to(results)
results.write()

谢谢


共1个答案

匿名用户

我看到的唯一问题是您的一些约束声明。您需要将约束附加到模型,传入的第一个参数应该是索引集(我假设它应该是model.I)。

def InventoryConstraint(model, i):
    return model.y[i-1] + model.x[i] - demand[i] <= model.y[i]
model.InvCont = Constraint(model.I, rule=InventoryConstraint)

def MeetDemandConstraint(model, i):
    return model.x[i] >= demand[i] - model.y[i-1]
model.DemCont = Constraint(model.I, rule=MeetDemandConstraint)

用于求解模型的语法有点过时,但应该可以使用。另一种选择是:

opt = SolverFactory('glpk')
opt.solve(model,tee=True) # The 'tee' option prints the solver output to the screen
model.display() # This will print a summary of the model solution

另一个对调试有用的命令是model。pprint()。这将显示整个模型,包括约束和目标的表达式。