Skip to content
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

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

quantresearch1
Copy link
Contributor

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:

  1. I agree my contributions are submitted under the BSD license.
  2. I represent I am authorized to make the contributions and grant the license. If my employer has rights to intellectual property that includes these contributions, I represent that I have received permission to make contributions and grant the required license on behalf of that employer.

@blnicho blnicho requested a review from jsiirola January 21, 2025 19:57
@quantresearch1
Copy link
Contributor Author

Hello, I was looking into the unit test failures, specifically the one named t11 in CCTests_nl_nlxfrm.
Looking at t11_nlxfrm.nl, I see the baseline has a linear complementarity constraint in its header while my output does not.
g3 1 1 0 # problem unknown
3 1 0 0 1 # vars, constraints, objectives, ranges, eqns; KNOWN BUG WITH NLv1 WRTIER!!!
0 0 1 0 0 0 # nonlinear constrs, objs; ccons: lin, nonlin, nd, nzlb

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?

    for cobj in instance.component_objects(
        Complementarity,
        active=True,
        descend_into=(Block, Disjunct),
        sort=SortComponents.deterministic,
    ):
        cobjs.append(cobj)
        for index in sorted(cobj.keys()):
            _cdata = cobj[index]
            if not _cdata.active:
                continue
            #
            # Apply a variant of the standard form logic
            #
            self.to_common_form(_cdata, free_vars)
            #
    tdata = instance._transformation_data['mpec.nl']
    tdata.compl_cuids = []
    for cobj in cobjs:
        tdata.compl_cuids.append(ComponentUID(cobj))
        cobj.parent_block().reclassify_component_type(cobj, Block)

@@ -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:
Copy link
Contributor Author

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
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant