-
Notifications
You must be signed in to change notification settings - Fork 525
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
Greybox Objectives #3364
Open
michaelbynum
wants to merge
7
commits into
Pyomo:main
Choose a base branch
from
michaelbynum:greybox
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+297
−94
Open
Greybox Objectives #3364
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3398780
adding some comments
michaelbynum 6b46cfc
Merge branch 'main' into greybox
michaelbynum 6878ca0
Merge branch 'main' into greybox
michaelbynum dd6248d
add support for objectives in external grey box
michaelbynum c08a188
fix typo
michaelbynum 2f8bbd6
fix typo
michaelbynum c7fee0c
Merge branch 'main' into greybox
mrmundt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
151 changes: 151 additions & 0 deletions
151
pyomo/contrib/pynumero/examples/external_grey_box/external_with_objective.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
import math | ||
import numpy as np | ||
from scipy.sparse import coo_matrix | ||
import pyomo.environ as pe | ||
from pyomo.contrib.pynumero.interfaces.external_grey_box import ( | ||
ExternalGreyBoxModel, | ||
ExternalGreyBoxBlock, | ||
) | ||
|
||
|
||
class Unconstrained(ExternalGreyBoxModel): | ||
""" | ||
min (x+2)**2 + (y-2)**2 | ||
""" | ||
|
||
def input_names(self): | ||
return ['x', 'y'] | ||
|
||
def set_input_values(self, input_values): | ||
self._input_values = list(input_values) | ||
|
||
def has_objective(self): | ||
return True | ||
|
||
def evaluate_objective(self): | ||
x = self._input_values[0] | ||
y = self._input_values[1] | ||
return (x + 2) ** 2 + (y - 2) ** 2 | ||
|
||
def evaluate_grad_objective(self): | ||
x = self._input_values[0] | ||
y = self._input_values[1] | ||
return np.asarray([2 * (x + 2), 2 * (y - 2)], dtype=float) | ||
|
||
|
||
class Constrained(ExternalGreyBoxModel): | ||
""" | ||
min x**2 + y**2 | ||
s.t. 0 == y - exp(x) | ||
""" | ||
|
||
def input_names(self): | ||
return ['x', 'y'] | ||
|
||
def set_input_values(self, input_values): | ||
self._input_values = list(input_values) | ||
|
||
def has_objective(self): | ||
return True | ||
|
||
def evaluate_objective(self): | ||
x = self._input_values[0] | ||
y = self._input_values[1] | ||
return x**2 + y**2 | ||
|
||
def evaluate_grad_objective(self): | ||
x = self._input_values[0] | ||
y = self._input_values[1] | ||
return np.asarray([2 * x, 2 * y], dtype=float) | ||
|
||
def equality_constraint_names(self): | ||
return ['c1'] | ||
|
||
def evaluate_equality_constraints(self): | ||
x = self._input_values[0] | ||
y = self._input_values[1] | ||
return np.asarray([y - math.exp(x)], dtype=float) | ||
|
||
def evaluate_jacobian_equality_constraints(self): | ||
x = self._input_values[0] | ||
row = [0, 0] | ||
col = [0, 1] | ||
data = [-math.exp(x), 1] | ||
jac = coo_matrix((data, (row, col)), shape=(1, 2)) | ||
return jac | ||
|
||
|
||
class ConstrainedWithHessian(Constrained): | ||
def evaluate_hessian_objective(self): | ||
row = [0, 1] | ||
col = [0, 1] | ||
data = [2, 2] | ||
hess = coo_matrix((data, (row, col)), shape=(2, 2)) | ||
return hess | ||
|
||
def set_equality_constraint_multipliers(self, eq_con_multiplier_values): | ||
self._dual = eq_con_multiplier_values[0] | ||
|
||
def evaluate_hessian_equality_constraints(self): | ||
x = self._input_values[0] | ||
row = [0] | ||
col = [0] | ||
data = [-math.exp(x) * self._dual] | ||
hess = coo_matrix((data, (row, col)), shape=(2, 2)) | ||
return hess | ||
|
||
|
||
def solve_unconstrained(): | ||
m = pe.ConcreteModel() | ||
m.z = pe.Var() | ||
m.grey_box = ExternalGreyBoxBlock(external_model=Unconstrained()) | ||
m.c = pe.Constraint(expr=m.z == m.grey_box.inputs['x'] + 1) | ||
|
||
opt = pe.SolverFactory('cyipopt') | ||
opt.config.options['hessian_approximation'] = 'limited-memory' | ||
res = opt.solve(m, tee=True) | ||
pe.assert_optimal_termination(res) | ||
x = m.grey_box.inputs['x'].value | ||
y = m.grey_box.inputs['y'].value | ||
assert math.isclose(x, -2) | ||
assert math.isclose(y, 2) | ||
return m | ||
|
||
|
||
def solve_constrained(): | ||
m = pe.ConcreteModel() | ||
m.z = pe.Var() | ||
m.grey_box = ExternalGreyBoxBlock(external_model=Constrained()) | ||
m.c2 = pe.Constraint(expr=m.z == m.grey_box.inputs['x'] + 1) | ||
|
||
opt = pe.SolverFactory('cyipopt') | ||
opt.config.options['hessian_approximation'] = 'limited-memory' | ||
res = opt.solve(m, tee=True) | ||
pe.assert_optimal_termination(res) | ||
x = m.grey_box.inputs['x'].value | ||
y = m.grey_box.inputs['y'].value | ||
assert math.isclose(x, -0.4263027509962655) | ||
assert math.isclose(y, 0.6529186403960969) | ||
return m | ||
|
||
|
||
def solve_constrained_with_hessian(): | ||
m = pe.ConcreteModel() | ||
m.z = pe.Var() | ||
m.grey_box = ExternalGreyBoxBlock(external_model=ConstrainedWithHessian()) | ||
m.c2 = pe.Constraint(expr=m.z == m.grey_box.inputs['x'] + 1) | ||
|
||
opt = pe.SolverFactory('cyipopt') | ||
res = opt.solve(m, tee=True) | ||
pe.assert_optimal_termination(res) | ||
x = m.grey_box.inputs['x'].value | ||
y = m.grey_box.inputs['y'].value | ||
assert math.isclose(x, -0.4263027509962655) | ||
assert math.isclose(y, 0.6529186403960969) | ||
return m | ||
|
||
|
||
if __name__ == '__main__': | ||
m = solve_constrained_with_hessian() | ||
print(f"x: {m.grey_box.inputs['x'].value}") | ||
print(f"y: {m.grey_box.inputs['y'].value}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can you fix black's weird auto-formatting here?