|
27 | 27 | from pyomo.core.expr import value |
28 | 28 | from pyomo.core.base.set_types import NonNegativeIntegers, NonNegativeReals |
29 | 29 | from pyomo.contrib.pyros.util import ( |
| 30 | + call_solver, |
30 | 31 | selective_clone, |
31 | 32 | ObjectiveType, |
32 | 33 | pyrosTerminationCondition, |
@@ -239,31 +240,18 @@ def solve_master_feasibility_problem(model_data, config): |
239 | 240 | else: |
240 | 241 | solver = config.local_solver |
241 | 242 |
|
242 | | - timer = TicTocTimer() |
243 | | - orig_setting, custom_setting_present = adjust_solver_time_settings( |
244 | | - model_data.timing, solver, config |
245 | | - ) |
246 | | - model_data.timing.start_timer("main.master_feasibility") |
247 | | - timer.tic(msg=None) |
248 | | - try: |
249 | | - results = solver.solve(model, tee=config.tee, load_solutions=False) |
250 | | - except ApplicationError: |
251 | | - # account for possible external subsolver errors |
252 | | - # (such as segmentation faults, function evaluation |
253 | | - # errors, etc.) |
254 | | - config.progress_logger.error( |
| 243 | + results = call_solver( |
| 244 | + model=model, |
| 245 | + solver=solver, |
| 246 | + config=config, |
| 247 | + timing_obj=model_data.timing, |
| 248 | + timer_name="main.master_feasibility", |
| 249 | + err_msg=( |
255 | 250 | f"Optimizer {repr(solver)} encountered exception " |
256 | 251 | "attempting to solve master feasibility problem in iteration " |
257 | 252 | f"{model_data.iteration}." |
258 | | - ) |
259 | | - raise |
260 | | - else: |
261 | | - setattr(results.solver, TIC_TOC_SOLVE_TIME_ATTR, timer.toc(msg=None)) |
262 | | - model_data.timing.stop_timer("main.master_feasibility") |
263 | | - finally: |
264 | | - revert_solver_max_time_adjustment( |
265 | | - solver, orig_setting, custom_setting_present, config |
266 | | - ) |
| 253 | + ), |
| 254 | + ) |
267 | 255 |
|
268 | 256 | feasible_terminations = { |
269 | 257 | tc.optimal, |
@@ -482,28 +470,18 @@ def minimize_dr_vars(model_data, config): |
482 | 470 | config.progress_logger.debug(f" Initial DR norm: {value(polishing_obj)}") |
483 | 471 |
|
484 | 472 | # === Solve the polishing model |
485 | | - timer = TicTocTimer() |
486 | | - orig_setting, custom_setting_present = adjust_solver_time_settings( |
487 | | - model_data.timing, solver, config |
488 | | - ) |
489 | | - model_data.timing.start_timer("main.dr_polishing") |
490 | | - timer.tic(msg=None) |
491 | | - try: |
492 | | - results = solver.solve(polishing_model, tee=config.tee, load_solutions=False) |
493 | | - except ApplicationError: |
494 | | - config.progress_logger.error( |
| 473 | + results = call_solver( |
| 474 | + model=polishing_model, |
| 475 | + solver=solver, |
| 476 | + config=config, |
| 477 | + timing_obj=model_data.timing, |
| 478 | + timer_name="main.dr_polishing", |
| 479 | + err_msg=( |
495 | 480 | f"Optimizer {repr(solver)} encountered an exception " |
496 | 481 | "attempting to solve decision rule polishing problem " |
497 | 482 | f"in iteration {model_data.iteration}" |
498 | | - ) |
499 | | - raise |
500 | | - else: |
501 | | - setattr(results.solver, TIC_TOC_SOLVE_TIME_ATTR, timer.toc(msg=None)) |
502 | | - model_data.timing.stop_timer("main.dr_polishing") |
503 | | - finally: |
504 | | - revert_solver_max_time_adjustment( |
505 | | - solver, orig_setting, custom_setting_present, config |
506 | | - ) |
| 483 | + ), |
| 484 | + ) |
507 | 485 |
|
508 | 486 | # interested in the time and termination status for debugging |
509 | 487 | # purposes |
@@ -726,43 +704,25 @@ def solver_call_master(model_data, config, solver, solve_data): |
726 | 704 | solve_mode = "global" if config.solve_master_globally else "local" |
727 | 705 | config.progress_logger.debug("Solving master problem") |
728 | 706 |
|
729 | | - timer = TicTocTimer() |
730 | 707 | for idx, opt in enumerate(solvers): |
731 | 708 | if idx > 0: |
732 | 709 | config.progress_logger.warning( |
733 | 710 | f"Invoking backup solver {opt!r} " |
734 | 711 | f"(solver {idx + 1} of {len(solvers)}) for " |
735 | 712 | f"master problem of iteration {model_data.iteration}." |
736 | 713 | ) |
737 | | - orig_setting, custom_setting_present = adjust_solver_time_settings( |
738 | | - model_data.timing, opt, config |
739 | | - ) |
740 | | - model_data.timing.start_timer("main.master") |
741 | | - timer.tic(msg=None) |
742 | | - try: |
743 | | - results = opt.solve( |
744 | | - nlp_model, |
745 | | - tee=config.tee, |
746 | | - load_solutions=False, |
747 | | - symbolic_solver_labels=True, |
748 | | - ) |
749 | | - except ApplicationError: |
750 | | - # account for possible external subsolver errors |
751 | | - # (such as segmentation faults, function evaluation |
752 | | - # errors, etc.) |
753 | | - config.progress_logger.error( |
| 714 | + results = call_solver( |
| 715 | + model=nlp_model, |
| 716 | + solver=opt, |
| 717 | + config=config, |
| 718 | + timing_obj=model_data.timing, |
| 719 | + timer_name="main.master", |
| 720 | + err_msg=( |
754 | 721 | f"Optimizer {repr(opt)} ({idx + 1} of {len(solvers)}) " |
755 | 722 | "encountered exception attempting to " |
756 | 723 | f"solve master problem in iteration {model_data.iteration}" |
757 | | - ) |
758 | | - raise |
759 | | - else: |
760 | | - setattr(results.solver, TIC_TOC_SOLVE_TIME_ATTR, timer.toc(msg=None)) |
761 | | - model_data.timing.stop_timer("main.master") |
762 | | - finally: |
763 | | - revert_solver_max_time_adjustment( |
764 | | - solver, orig_setting, custom_setting_present, config |
765 | | - ) |
| 724 | + ), |
| 725 | + ) |
766 | 726 |
|
767 | 727 | optimal_termination = check_optimal_termination(results) |
768 | 728 | infeasible = results.solver.termination_condition == tc.infeasible |
|
0 commit comments