関数によって定義された制約は次のとおりです。
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 ファイルの調査
2 つの 3 行セットには符号の違いがあります。
ファイル 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 番目の 3 行セットの符号を変更すると、同じ結果で収束します。ファイル 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 ファイルが得られません。
具体的には、次の 2 行を除いて、両方のファイルは同一です。
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
解くと、同じ結果が得られます。おそらく、例がゴミだからです。