如何在 Pyomo 中声明 objective 函数和约束(抽象模型)
How to declare objective function and constraints in Pyomo (Abstract model)
我正在学习如何在 Pyomo 中创建抽象模型并进行了建模,但是,当我可视化 pprint 时,没有 objective 函数和约束的声明。我怎样才能解决这个问题?我附上代码
提前致谢。
import pyomo as pyo
from pyomo.environ import *
model = AbstractModel()
model.I = Set()
model.J = Set()
model.a = Param(model.I, model.J)
model.b = Param(model.I)
model.c = Param(model.J)
model.x = Var(model.J, domain = NonNegativeReals)
def obj (model):
return sum(model.c[j]*model.x[j] for j in model.J)
model.obj = Objective(rule = obj)
def ax_constraint_rule (model, i):
return sum(model.a[i,j]*model.x[j] for j in model.J) >= model.b[i]
model.AxbConstraint = Constraint(model.I, rule = ax_constraint_rule)
model.pprint()
3 Set Declarations
I : Size=0, Index=None, Ordered=Insertion
Not constructed
J : Size=0, Index=None, Ordered=Insertion
Not constructed
a_index : Size=0, Index=None, Ordered=True
Not constructed
3 Param Declarations
a : Size=0, Index=a_index, Domain=Any, Default=None, Mutable=False
Not constructed
b : Size=0, Index=I, Domain=Any, Default=None, Mutable=False
Not constructed
c : Size=0, Index=J, Domain=Any, Default=None, Mutable=False
Not constructed
1 Var Declarations
x : Size=0, Index=J
Not constructed
7 Declarations: I J a_index a b c x
您似乎错误地缩进了下面的语句,这会使它们成为函数的一部分。他们不是。他们调用函数...通过使它们成为函数的一部分并在 return
语句之后它们是死代码。
那至少应该打印一些东西。然后,您需要通过加载数据(请参阅文档)和 pprint
instance
而不是模型来创建模型的 instance
。祝你好运!
我正在学习如何在 Pyomo 中创建抽象模型并进行了建模,但是,当我可视化 pprint 时,没有 objective 函数和约束的声明。我怎样才能解决这个问题?我附上代码
提前致谢。
import pyomo as pyo
from pyomo.environ import *
model = AbstractModel()
model.I = Set()
model.J = Set()
model.a = Param(model.I, model.J)
model.b = Param(model.I)
model.c = Param(model.J)
model.x = Var(model.J, domain = NonNegativeReals)
def obj (model):
return sum(model.c[j]*model.x[j] for j in model.J)
model.obj = Objective(rule = obj)
def ax_constraint_rule (model, i):
return sum(model.a[i,j]*model.x[j] for j in model.J) >= model.b[i]
model.AxbConstraint = Constraint(model.I, rule = ax_constraint_rule)
model.pprint()
3 Set Declarations
I : Size=0, Index=None, Ordered=Insertion
Not constructed
J : Size=0, Index=None, Ordered=Insertion
Not constructed
a_index : Size=0, Index=None, Ordered=True
Not constructed
3 Param Declarations
a : Size=0, Index=a_index, Domain=Any, Default=None, Mutable=False
Not constructed
b : Size=0, Index=I, Domain=Any, Default=None, Mutable=False
Not constructed
c : Size=0, Index=J, Domain=Any, Default=None, Mutable=False
Not constructed
1 Var Declarations
x : Size=0, Index=J
Not constructed
7 Declarations: I J a_index a b c x
您似乎错误地缩进了下面的语句,这会使它们成为函数的一部分。他们不是。他们调用函数...通过使它们成为函数的一部分并在 return
语句之后它们是死代码。
那至少应该打印一些东西。然后,您需要通过加载数据(请参阅文档)和 pprint
instance
而不是模型来创建模型的 instance
。祝你好运!