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

IPOPT now returns Lagrange multpliers using the same sign convention as SNOPT. #416

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyoptsparse/pyIPOPT/pyIPOPT.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def eval_intermediate_callback(*args, **kwargs):
sol_inform["text"] = self.informs[status]

# Create the optimization solution
sol = self._createSolution(optTime, sol_inform, obj, x)
sol = self._createSolution(optTime, sol_inform, obj, x, multipliers=-constraint_multipliers)

# Indicate solution finished
self.optProb.comm.bcast(-1, root=0)
Expand Down
8 changes: 5 additions & 3 deletions pyoptsparse/pyOpt_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -1577,7 +1577,7 @@
con_opt = self._mapContoOpt(con)
return self.processContoDict(con_opt, scaled=False, natural=True)

def summary_str(self, minimal_print=False):
def summary_str(self, minimal_print=False, print_multipliers=False):

Check warning on line 1580 in pyoptsparse/pyOpt_optimization.py

View check run for this annotation

Codecov / codecov/patch

pyoptsparse/pyOpt_optimization.py#L1580

Added line #L1580 was not covered by tests
"""
Print Structured Optimization Problem

Expand All @@ -1588,6 +1588,8 @@
variables and constraints with a non-empty status
(for example a violated bound).
This defaults to False, which will print all results.
print_multipliers : bool
If True, print the Lagrange multipliers associated with the constraints.
"""
TOL = 1.0e-6

Expand Down Expand Up @@ -1656,7 +1658,7 @@

if len(self.constraints) > 0:
# must be an instance of the Solution class
if not isinstance(self, Optimization) and self.lambdaStar is not None:
if print_multipliers and self.lambdaStar is not None:
lambdaStar = self.lambdaStar
lambdaStar_label = "Lagrange Multiplier"
else:
Expand Down Expand Up @@ -1716,7 +1718,7 @@
return text

def __str__(self):
return self.summary_str(minimal_print=False)
return self.summary_str(minimal_print=False, print_multipliers=False)

def __getstate__(self) -> dict:
"""
Expand Down
2 changes: 1 addition & 1 deletion pyoptsparse/pyOpt_solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def __str__(self) -> str:
"""
Print Structured Solution
"""
text0 = super().__str__()
text0 = self.summary_str(minimal_print=False, print_multipliers=True)
text1 = ""
lines = text0.split("\n")
lines[1] = lines[1][len("Optimization Problem -- ") :]
Expand Down
11 changes: 11 additions & 0 deletions tests/test_hs071.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,17 @@ def test_optimization(self, optName):
self.assert_solution_allclose(sol, self.tol[optName])
# Check informs
self.assert_inform_equal(sol)
# Check the lagrange multipliers in the solution text
lines = str(sol).split("\n")
constraint_header_line_num = [i for i, line in enumerate(lines) if "Constraints" in line][0]
con1_line_num = constraint_header_line_num + 2
con2_line_num = constraint_header_line_num + 3
lambda_con1 = float(lines[con1_line_num].split()[-1])
lambda_con2 = float(lines[con2_line_num].split()[-1])
if optName in ("IPOPT", "SNOPT", "ParOpt"):
assert_allclose([lambda_con1, lambda_con2], self.lambdaStar[0]["con"], rtol=1.0e-5, atol=1.0e-5)
else:
assert_allclose([lambda_con1, lambda_con2], [9.0e100, 9.0e100], rtol=1.0e-5, atol=1.0e-5)


if __name__ == "__main__":
Expand Down
Loading