-
Notifications
You must be signed in to change notification settings - Fork 526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Complementarity support for NL Writer v2 #3458
base: main
Are you sure you want to change the base?
Conversation
Hello, I was looking into the unit test failures, specifically the one named t11 in CCTests_nl_nlxfrm. Looking into mpec4.py, I would have expected that no Complementarity component remain after the transformation due to the reclassify_component_type call, should that not the case?
|
@@ -1127,10 +1189,6 @@ def write(self, model): | |||
if hasattr(con, '_complementarity'): | |||
# _type = 5 | |||
r_lines[idx] = f"5 {con._complementarity} {1+column_order[con._vid]}" | |||
if expr_info.nonlinear: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is now done in the complementarity handling bit above
con = comp.c | ||
con._vid = _id | ||
if hasattr(con, '_complementarity_type'): | ||
con._complementarity = con._complementarity_type |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that the _complementarity attribute is currently used below for the 'r' lines writing bit.
complementarity is mainly used in mpec4.py and ampl.py while _complementarity is used in complementarity.py and mpec1.py. Are they not the same ? If they are the same, I am happy to raise a separate PR to rename one into the other
Fixes # .
Summary/Motivation:
At work, I tried to use pyomo to solve a complementarity problem but it raised a ValueError as pyomo\repn\plugins\nl_writer.py does not know how to process Complementarity constraints.
Changes proposed in this PR:
-Add a block to handle complementarity constraints and the auxiliary variable/additional constraints after conversion to its standard form. When comparing my .nl file to AMPL's nl file, it seems that the additional equality constraint we add in to_standard_form() from complementarity.py is
c: (v - expression == 0)
while AMPL defines it as c:( v + expression == 0)
Essentially, Pyomo's auxiliary variable v is minus the auxiliary variable from AMPL, which makes it tricky to reconcile a .nl file written by Pyomo and one written by AMPL. I have not changed complementarity.py as Pyomo's choice is valid, it's simply a matter of convention.
Minimal example reproducing the issue being fixed:
from pyomo.environ import *
from pyomo.mpec import *
from pyomo.opt import WriterFactory
m = ConcreteModel()
m.x = Var(bounds=(0, 1))
m.y = Var(bounds=(0, 1))
m.c = Complementarity(expr=complements(m.x >= 0, m.y >= 0))
m.obj = Objective(expr=m.x+m.y, sense=maximize)
writer = WriterFactory('nl_v2')
filename_v2 = "model_v2.nl"
writer(m, filename_v2, solver_capability=(lambda x: False), io_options={})
Any ideas on how to test the validity of this implementation? I can see the .nl written by the code above has the correct number of variables (x, y and v the auxiliary), 2 constraints, one complementarity linear (5 1 3) and one equality (4 0)
g3 1 1 0 # problem unknown
3 2 1 0 1 # vars, constraints, objectives, ranges, eqns
0 0 1 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)
3 2 # 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
C1
n0
O0 1
n0
x0
r
5 1 3
4 0
b
0 0 1
0 0 1
2 0
k2
1
2
J0 1
1 1
J1 2
0 -1
2 1
G0 2
0 1
1 1
Legal Acknowledgement
By contributing to this software project, I have read the contribution guide and agree to the following terms and conditions for my contribution: