【问题标题】:Converting from AMPL to Pyomo从 AMPL 转换为 Pyomo
【发布时间】:2019-03-04 23:15:29
【问题描述】:

我正在尝试将 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()

谢谢!

【问题讨论】:

    标签: python pyomo ampl glpk


    【解决方案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()。这将显示整个模型,包括约束和目标的表达式。

    【讨论】:

    • 谢谢!现在,当我运行代码时,出现此错误: SolverFactory 无法创建求解器“=glpk”并返回了一个未知求解器对象。有关如何解决此问题的任何提示?谢谢!
    • 听起来 glpk 可执行文件可能不在您的路径上。您应该能够打开 Anaconda 提示符并运行命令glpsol -h 并查看 glpk 选项列表。
    • 我仍然遇到同样的问题。我无法像您建议的那样从 Anaconda 提示中解决问题。有没有比 glpk 更容易实现的求解器?对于我的项目,我只需要在 pyomo 中使用我的 lp 文件运行任何求解器。谢谢!
    • 我之前建议的命令运行成功了吗? glpk 是最容易使用的求解器,因为它可以通过 Anaconda 安装。原来是用conda install -c conda-forge glpk命令安装的吗?
    • 我能够使用您的 glpk 安装程序命令让它工作!我之前一定是错误地安装了它。在该命令之后,我只需要修复列表中的一些越界错误,但现在它可以工作了。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    • 2020-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-17
    相关资源
    最近更新 更多