Hei all,
我正在尝试为以下形式的非常简单的 QP 建立一个抽象模型
分钟 (x-x0)^2
s.t.
A x = b
C x
我想使用抽象模型,因为我需要通过更改参数(主要是 x0,但也可能是 A、b、C、d)来解决。我现在正在努力简单地在模型实例中设置参数。我不想使用外部数据文件,而是使用内部 python 变量。我在网上找到的所有示例都使用 AMPL 格式的数据文件。
这是我现在的代码
import pyomo.environ as pe
model = pe.AbstractModel()
# the sets
model.n = pe.Param(within=pe.NonNegativeIntegers)
model.m = pe.Param(initialize = 1)
model.ss = pe.RangeSet(1, model.n)
model.os = pe.RangeSet(1, model.m)
# the starting point and the constraint parameters
model.x_hat = pe.Param(model.ss)
model.A = pe.Param(model.os, model.ss)
model.b = pe.Param(model.os)
model.C = pe.Param(model.os, model.os)
model.d = pe.Param(model.ss, model.os)
# the decision variables
model.x_projected = pe.Var(model.ss)
# the cosntraints
# A x = b
def sum_of_elements_rule(model):
value = model.A * model.x_projected
return value == model.d
model.sumelem = pe.Constraint(model.os, rule=sum_of_elements_rule)
# C x <= d
def positivity_constraint(model):
return model.C*model.x_projected <= model.d
model.bounds = pe.Constraint(model.ss, rule=positivity_constraint)
# the cost
def cost_rule(model):
return sum((model.x_projected[i] - model.x[i])**2 for i in model.ss)
model.cost = pe.Objective(rule=cost_rule)
instance = model.create_instance()
不知怎的,我被困在这里了。现在如何设置参数?
谢谢,最好的,西奥
我知道这是一篇旧帖子,但解决方案可能对我有帮助,所以这里是这个问题的解决方案:
## TEST
data_init= {None: dict(
n = {None : 3},
d = {0:0, 1:1, 2:2},
x_hat = {0:10, 1:-1, 2:-100},
b = {None: 10}
)}
# create instance
instance = model.create_instance(data_init)
这会以与您所做的相同但更正式的方式创建实例。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)