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 call_before_subproblem_solve callback in MindtPy #3251

Merged
merged 5 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
13 changes: 13 additions & 0 deletions pyomo/contrib/mindtpy/algorithm_base_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -2949,6 +2949,10 @@ def MindtPy_iteration_loop(self):
skip_fixed=False,
)
if self.curr_int_sol not in set(self.integer_list):
# Call the NLP pre-solve callback
with time_code(self.timing, 'Call before subproblem solve'):
config.call_before_subproblem_solve(self.fixed_nlp)

fixed_nlp, fixed_nlp_result = self.solve_subproblem()
self.handle_nlp_subproblem_tc(fixed_nlp, fixed_nlp_result)

Expand All @@ -2960,6 +2964,10 @@ def MindtPy_iteration_loop(self):
# Solve NLP subproblem
# The constraint linearization happens in the handlers
if not config.solution_pool:
# Call the NLP pre-solve callback
with time_code(self.timing, 'Call before subproblem solve'):
config.call_before_subproblem_solve(self.fixed_nlp)

fixed_nlp, fixed_nlp_result = self.solve_subproblem()
self.handle_nlp_subproblem_tc(fixed_nlp, fixed_nlp_result)

Expand Down Expand Up @@ -2992,6 +3000,11 @@ def MindtPy_iteration_loop(self):
continue
else:
self.integer_list.append(self.curr_int_sol)

# Call the NLP pre-solve callback
with time_code(self.timing, 'Call before subproblem solve'):
config.call_before_subproblem_solve(self.fixed_nlp)

fixed_nlp, fixed_nlp_result = self.solve_subproblem()
self.handle_nlp_subproblem_tc(fixed_nlp, fixed_nlp_result)

Expand Down
9 changes: 9 additions & 0 deletions pyomo/contrib/mindtpy/config_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,15 @@ def _add_common_configs(CONFIG):
doc='Callback hook after a solution of the main problem.',
),
)
CONFIG.declare(
'call_before_subproblem_solve',
ConfigValue(
default=_DoNothing(),
domain=None,
description='Function to be executed before every subproblem',
doc='Callback hook before a solution of the nonlinear subproblem.',
),
)
CONFIG.declare(
'call_after_subproblem_solve',
ConfigValue(
Expand Down
6 changes: 6 additions & 0 deletions pyomo/contrib/mindtpy/single_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,9 @@ def __call__(self):
mindtpy_solver.integer_list.append(mindtpy_solver.curr_int_sol)

# solve subproblem
# Call the NLP pre-solve callback
with time_code(mindtpy_solver.timing, 'Call before subproblem solve'):
config.call_before_subproblem_solve(mindtpy_solver.fixed_nlp)
# The constraint linearization happens in the handlers
fixed_nlp, fixed_nlp_result = mindtpy_solver.solve_subproblem()
# add oa cuts
Expand Down Expand Up @@ -919,6 +922,9 @@ def LazyOACallback_gurobi(cb_m, cb_opt, cb_where, mindtpy_solver, config):
cut_ind = len(mindtpy_solver.mip.MindtPy_utils.cuts.oa_cuts)

# solve subproblem
# Call the NLP pre-solve callback
with time_code(mindtpy_solver.timing, 'Call before subproblem solve'):
config.call_before_subproblem_solve(mindtpy_solver.fixed_nlp)
# The constraint linearization happens in the handlers
fixed_nlp, fixed_nlp_result = mindtpy_solver.solve_subproblem()

Expand Down