Skip to content

Commit

Permalink
redesign calc_jacobians function
Browse files Browse the repository at this point in the history
  • Loading branch information
ZedongPeng committed Dec 7, 2023
1 parent d229461 commit 3d1db13
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 8 deletions.
4 changes: 3 additions & 1 deletion pyomo/contrib/mindtpy/extended_cutting_plane.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,9 @@ def check_config(self):
def initialize_mip_problem(self):
'''Deactivate the nonlinear constraints to create the MIP problem.'''
super().initialize_mip_problem()
self.jacobians = calc_jacobians(self.mip, self.config) # preload jacobians
self.jacobians = calc_jacobians(
self.mip, self.config.differentiate_mode
) # preload jacobians
self.mip.MindtPy_utils.cuts.ecp_cuts = ConstraintList(
doc='Extended Cutting Planes'
)
Expand Down
4 changes: 3 additions & 1 deletion pyomo/contrib/mindtpy/feasibility_pump.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ def check_config(self):
def initialize_mip_problem(self):
'''Deactivate the nonlinear constraints to create the MIP problem.'''
super().initialize_mip_problem()
self.jacobians = calc_jacobians(self.mip, self.config) # preload jacobians
self.jacobians = calc_jacobians(
self.mip, self.config.differentiate_mode
) # preload jacobians
self.mip.MindtPy_utils.cuts.oa_cuts = ConstraintList(
doc='Outer approximation cuts'
)
Expand Down
4 changes: 3 additions & 1 deletion pyomo/contrib/mindtpy/outer_approximation.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ def check_config(self):
def initialize_mip_problem(self):
'''Deactivate the nonlinear constraints to create the MIP problem.'''
super().initialize_mip_problem()
self.jacobians = calc_jacobians(self.mip, self.config) # preload jacobians
self.jacobians = calc_jacobians(
self.mip, self.config.differentiate_mode
) # preload jacobians
self.mip.MindtPy_utils.cuts.oa_cuts = ConstraintList(
doc='Outer approximation cuts'
)
Expand Down
10 changes: 5 additions & 5 deletions pyomo/contrib/mindtpy/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
numpy = attempt_import('numpy')[0]


def calc_jacobians(model, config):
def calc_jacobians(model, differentiate_mode):
"""Generates a map of jacobians for the variables in the model.
This function generates a map of jacobians corresponding to the variables in the
Expand All @@ -51,15 +51,15 @@ def calc_jacobians(model, config):
----------
model : Pyomo model
Target model to calculate jacobian.
config : ConfigBlock
The specific configurations for MindtPy.
differentiate_mode : String
The differentiate mode to calculate Jacobians.
"""
# Map nonlinear_constraint --> Map(
# variable --> jacobian of constraint w.r.t. variable)
jacobians = ComponentMap()
if config.differentiate_mode == 'reverse_symbolic':
if differentiate_mode == 'reverse_symbolic':
mode = EXPR.differentiate.Modes.reverse_symbolic
elif config.differentiate_mode == 'sympy':
elif differentiate_mode == 'sympy':
mode = EXPR.differentiate.Modes.sympy
for c in model.MindtPy_utils.nonlinear_constraint_list:
vars_in_constr = list(EXPR.identify_variables(c.body))
Expand Down

0 comments on commit 3d1db13

Please sign in to comment.