繁体   English   中英

pyomo等式表达是可交换的吗?

[英]Is pyomo equality expression commutative?

这是一个函数定义的约束:

def my_constraint(model, j):
    a = sum(model.variable_1[i, j] for i in model.i) + sum(model.variable_2[o, j] for o in model.o if o != j)
    b = model.variable_3[j]
    # Apparently, the order matters !?
    return a == b
    # return b == a

model.my_constraint = pe.Constraint(model.j, rule=my_constraint)

我以为平等条件的顺序无关紧要,但是如果我切换它们,将会得到不同的结果。

我不知道该怎么做。

生成的.nl文件略有不同,但是由于我不知道如何解释它们,所以我处于死胡同。

.nl文件调查

两个thee-line集具有符号差异。

文件1:

[...]
24 1
32 -1
35 1
J78 3
25 1
33 -1
34 1
[...]

档案2:

[...]
24 -1
32 1
35 -1
J78 3
25 -1
33 1
34 -1
[...]

将两个文件都送入ipopt时,文件1和文件2的解决方案都是“不可行的”。如果我编辑文件1来更改第一行或第二行三行集中的符号,则会得到相同结果的收敛作为文件2。

因此,表达式相等的顺序无关紧要,但是在更改它时,我在.nl文件中得到了一个很重要的符号差异。

一个简单的示例,演示术语顺序如何影响.nl文件

from pyomo.environ import ConcreteModel, Set, Var, Constraint, Objective
from pyomo.opt import SolverFactory

model = ConcreteModel()

model.i = Set(initialize=['I1'])
model.j = Set(initialize=['J1'])

model.v1 = Var(model.i, model.j)
model.v2 = Var(model.i, model.j)
model.v3 = Var(initialize=0, bounds=(0, None))

def c1(model, i, j):
    #return model.v2[i, j] == model.v1[i, j]
    return model.v1[i, j] == model.v2[i, j]
model.c1 = Constraint(model.i, model.j, rule=c1)

def objective_rule(model):
    return model.v3
model.objective = Objective(rule=objective_rule)

opt = SolverFactory('ipopt')
opt.solve(model, keepfiles=True)

根据约束c1中术语的顺序,我不会得到相同的.nl文件。

更具体地说,两个文件是相同的,除了两行:

g3 1 1 0    # problem unknown
 3 1 1 0 1  # vars, constraints, objectives, ranges, eqns
 0 0 0 0 0 0    # nonlinear constrs, objs; ccons: lin, nonlin, nd, nzlb
 0 0    # network constraints: nonlinear, linear
 0 0 0  # nonlinear vars in constraints, objectives, both
 0 0 0 1    # linear network variables; functions; arith, flags
 0 0 0 0 0  # discrete variables: binary, integer, nonlinear (b,c,o)
 2 1    # nonzeros in Jacobian, obj. gradient
 0 0    # max name lengths: constraints, variables
 0 0 0 0 0  # common exprs: b,c,o,c1,o1
C0
n0
O0 0
n0
x1
2 0
r
4 0.0
b
3
3
2 0
k2
1
2
J0 2
0 -1    # The other file reads   0 1
1 1     #                        1 -1
G0 1
2 1

解决时,我得到相同的结果。 可能是因为该示例是垃圾。

从理论上讲,您正在看到替代的最佳解决方案。 根据问题的表述,完全有可能获得不止一种具有最佳目标价值的解决方案。 您获得这些订单的顺序将对约束的顺序敏感。 如果您使用的是LP解算器,则应该能够要求它为您提供所有最佳解决方案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM