简体   繁体   中英

Is pyomo equality expression commutative?

Here's a constraint defined by a function:

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)

I assumed the order of the terms of the equality wouldn't matter but if I switch them, I get different results.

I don't know how to get to the bottom of this.

The generated .nl files differ slightly but I'm in a dead end as I don't know how to interpret them.

Investigating .nl files

Two thee-line sets have a sign difference.

File 1:

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

File 2:

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

When feeding both files to ipopt, I get "infeasible" with file 1 and a solution with file 2. If I edit file 1 to change the signs in either the first or the second three-line set, I get convergence with the same results as file 2.

So the order in the equality of the expression should not matter, but when changing it, I get, in the .nl file, a sign difference that does matter.

Simple example demonstrating how the order of the terms affects the .nl file

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)

Depending of the order of the terms in constraint c1, I don't get the same .nl file.

More specifically, both files are identical except for two lines:

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

When solving, I get the same results. Probably because the example is rubbish.

A theoretical explanation is that you're seeing alternative optimal solutions. It's entirely possible, depending on the problem formulation, that you've got more than one solution that has the optimal objective value. What order you get these in is going to be sensitive to the order of the constraints. If you're using an LP solver you ought to be able to ask it to give you all of the optimal solutions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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