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 7e69413 commit c9f7888
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 8 deletions.
3 changes: 2 additions & 1 deletion pyomo/contrib/mindtpy/extended_cutting_plane.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ 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.differentiate_mode
self.mip.MindtPy_utils.nonlinear_constraint_list,
self.config.differentiate_mode,
) # preload jacobians
self.mip.MindtPy_utils.cuts.ecp_cuts = ConstraintList(
doc='Extended Cutting Planes'
Expand Down
3 changes: 2 additions & 1 deletion pyomo/contrib/mindtpy/feasibility_pump.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ 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.differentiate_mode
self.mip.MindtPy_utils.nonlinear_constraint_list,
self.config.differentiate_mode,
) # preload jacobians
self.mip.MindtPy_utils.cuts.oa_cuts = ConstraintList(
doc='Outer approximation cuts'
Expand Down
3 changes: 2 additions & 1 deletion pyomo/contrib/mindtpy/outer_approximation.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ 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.differentiate_mode
self.mip.MindtPy_utils.nonlinear_constraint_list,
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,16 +41,16 @@
numpy = attempt_import('numpy')[0]


def calc_jacobians(model, differentiate_mode):
def calc_jacobians(constraint_list, 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
model.
constraint list.
Parameters
----------
model : Pyomo model
Target model to calculate jacobian.
constraint_list : List
The list of constraints to calculate Jacobians.
differentiate_mode : String
The differentiate mode to calculate Jacobians.
"""
Expand All @@ -61,7 +61,7 @@ def calc_jacobians(model, differentiate_mode):
mode = EXPR.differentiate.Modes.reverse_symbolic
elif differentiate_mode == 'sympy':
mode = EXPR.differentiate.Modes.sympy
for c in model.MindtPy_utils.nonlinear_constraint_list:
for c in constraint_list:
vars_in_constr = list(EXPR.identify_variables(c.body))
jac_list = EXPR.differentiate(c.body, wrt_list=vars_in_constr, mode=mode)
jacobians[c] = ComponentMap(
Expand Down

0 comments on commit c9f7888

Please sign in to comment.