6

我有一个要解决的线性整数程序。我安装了求解器 glpk (感谢这个答案)和 pyomo。我写了这样的代码:

from pyomo.environ import *
from pyomo.opt import SolverFactory

a = 370
b = 420
c = 2

model             = ConcreteModel()
model.x           = Var([1,2], domain=NonNegativeIntegers)
model.Objective   = Objective(expr = a * model.x[1] + b * model.x[2], sense=minimize)
model.Constraint1 = Constraint(expr = model.x[1] + model.x[2] == c)
# ... more constraints

opt = SolverFactory('glpk')

results = opt.solve(model)

这产生了 file 的解决方案results.yaml

我有很多问题要使用相同的模型但具有不同ab、 和c值来解决。我想为、 和分配不同的值a,求解模型,获得 和 的解,并列出、、和。我阅读了文档,但示例仅将解决方案写入文件,例如.bcmodel.x[1]model.x[2]abcmodel.x[1]model.x[2]results.yaml

有什么方法可以从代码中访问解决方案值?

谢谢,

4

3 回答 3

13

这是脚本的修改版本,它说明了打印变量值的两种不同方式:(1) 通过显式引用每个变量和 (2) 通过迭代模型中的所有变量。

# Pyomo v4.4.1
# Python 2.7
from pyomo.environ import *
from pyomo.opt import SolverFactory

a = 370
b = 420
c = 4

model             = ConcreteModel()
model.x           = Var([1,2], domain=Binary)
model.y           = Var([1,2], domain=Binary)
model.Objective   = Objective(expr = a * model.x[1] + b * model.x[2] + (a-b)*model.y[1] + (a+b)*model.y[2], sense=maximize)
model.Constraint1 = Constraint(expr = model.x[1] + model.x[2] + model.y[1] + model.y[2] <= c)

opt = SolverFactory('glpk')

results = opt.solve(model)

#
# Print values for each variable explicitly
#
print("Print values for each variable explicitly")
for i in model.x:
  print str(model.x[i]), model.x[i].value
for i in model.y:
  print str(model.y[i]), model.y[i].value
print("")

#
# Print values for all variables
#
print("Print values for all variables")
for v in model.component_data_objects(Var):
  print str(v), v.value

这是生成的输出:

Print values for each variable explicitly
x[1] 1.0
x[2] 1.0
y[1] 0.0
y[2] 1.0

Print values for all variables
x[1] 1.0
x[2] 1.0
y[1] 0.0
y[2] 1.0
于 2016-08-25T13:18:48.210 回答
11

我不确定这是否是您正在寻找的,但这是一种在我的脚本中打印一些变量的方式。

from pyomo.environ import *
from pyomo.opt import SolverFactory
from pyomo.core import Var

M = AbstractModel()
opt = SolverFactory('glpk')

# Vars, Params, Objective, Constraints....

instance = M.create_instance('input.dat') # reading in a datafile
results = opt.solve(instance, tee=True)
results.write()
instance.solutions.load_from(results)

for v in instance.component_objects(Var, active=True):
    print ("Variable",v)
    varobject = getattr(instance, str(v))
    for index in varobject:
        print ("   ",index, varobject[index].value)
于 2016-08-01T19:56:33.113 回答
0

我在urbs 项目pyomoio中找到了该模块。提取集合、参数、变量等,并以对象的形式返回,非常方便在jupyter notebooks中可视化。pandas

我建立一个简单的模型

model = ConcreteModel()
model.act = Set(initialize=list('IJK'))
model.goods = Set(initialize=list('ijk'))
u0 = {}
u0['i', 'J'] = 2.
u0['k', 'I'] = .3
model.U0 = Param(model.goods, model.act, initialize=u0, default=0)

然后我可以在 pandas DataFrame 中读取它,并适当设置所有标签。

import pyomoio as po
u_df = po.get_entity(model, 'U0').unstack()
print(u_df)

# act      I    J    K
# goods               
# i      0.0  2.0  0.0
# j      0.0  0.0  0.0
# k      0.3  0.0  0.0
于 2019-04-12T23:42:43.440 回答