6262from pyomo .opt import SolverFactory
6363from pyomo .environ import Block , ComponentUID
6464from pyomo .opt import TerminationCondition
65+ from pyomo .opt .results .solver import assert_optimal_termination
6566
6667from pyomo .contrib .sensitivity_toolbox .sens import get_dsdp
6768
@@ -347,24 +348,6 @@ def _get_labeled_model_helper(experiment):
347348 raise RuntimeError (f"Failed to clone labeled model: { exc } " )
348349
349350
350- def _check_solver_termination_result (model , solver_result ):
351- """
352- Checks if the Pyomo model solves to optimality
353- """
354- if solver_result .solver .termination_condition == TerminationCondition .optimal :
355- model .solutions .load_from (solver_result )
356- else :
357- logger .error (
358- "Solution is not optimal. Termination condition: %s, Status: %s" ,
359- solver_result .solver .termination_condition ,
360- solver_result .solver .status ,
361- )
362- raise RuntimeError (
363- f"Model from experiment did not solve appropriately. "
364- f"Make sure the model is well-posed."
365- )
366-
367-
368351def _count_total_experiments (experiment_list ):
369352 """
370353 Counts the number of data points in the list of experiments
@@ -439,8 +422,8 @@ def _compute_jacobian(experiment, theta_vals, step, solver, tee):
439422
440423 # re-solve the model with the estimated parameters
441424 solver = pyo .SolverFactory (solver )
442- results = solver .solve (model , tee = tee , load_solutions = False )
443- _check_solver_termination_result ( model , results )
425+ results = solver .solve (model , tee = tee )
426+ assert_optimal_termination ( results )
444427
445428 # get the measured variables
446429 y_hat_list = [y_hat for y_hat , y in model .experiment_outputs .items ()]
@@ -466,8 +449,8 @@ def _compute_jacobian(experiment, theta_vals, step, solver, tee):
466449 param .fix (orig_value + relative_perturbation )
467450
468451 # solve the model
469- results = solver .solve (model , tee = tee , load_solutions = False )
470- _check_solver_termination_result ( model , results )
452+ results = solver .solve (model , tee = tee )
453+ assert_optimal_termination ( results )
471454
472455 # forward perturbation measured variables
473456 y_hat_plus = [pyo .value (y_hat ) for y_hat , y in model .experiment_outputs .items ()]
@@ -476,8 +459,8 @@ def _compute_jacobian(experiment, theta_vals, step, solver, tee):
476459 param .fix (orig_value - relative_perturbation )
477460
478461 # re-solve the model
479- results = solver .solve (model , tee = tee , load_solutions = False )
480- _check_solver_termination_result ( model , results )
462+ results = solver .solve (model , tee = tee )
463+ assert_optimal_termination ( results )
481464
482465 # backward perturbation measured variables
483466 y_hat_minus = [
@@ -722,8 +705,8 @@ def _kaug_FIM(experiment, obj_function, theta_vals, solver, tee, estimated_var=N
722705 param .fix (theta_vals [param .name ])
723706
724707 solver = pyo .SolverFactory (solver )
725- results = solver .solve (model , tee = tee , load_solutions = False )
726- _check_solver_termination_result ( model , results )
708+ results = solver .solve (model , tee = tee )
709+ assert_optimal_termination ( results )
727710
728711 # Probe the solved model for dsdp results (sensitivities s.t. parameters)
729712 params_dict = {k .name : v for k , v in model .unknown_parameters .items ()}
@@ -1094,7 +1077,7 @@ def _Q_opt(
10941077 solver .options [key ] = self .solver_options [key ]
10951078
10961079 solve_result = solver .solve (self .ef_instance , tee = self .tee )
1097- _check_solver_termination_result ( self . ef_instance , solve_result )
1080+ assert_optimal_termination ( solve_result )
10981081 elif kwargs and all (arg .value in kwargs for arg in UnsupportedArgs ):
10991082 deprecation_warning (
11001083 "You're using a deprecated call to the `theta_est()` function "
@@ -1300,10 +1283,8 @@ def _cov_at_theta(self, method, solver, step):
13001283 param .fix (self .estimated_theta [param .name ])
13011284
13021285 # re-solve the model with the estimated parameters
1303- results = pyo .SolverFactory (solver ).solve (
1304- model , tee = self .tee , load_solutions = False
1305- )
1306- _check_solver_termination_result (model , results )
1286+ results = pyo .SolverFactory (solver ).solve (model , tee = self .tee )
1287+ assert_optimal_termination (results )
13071288
13081289 # choose and evaluate the sum of squared errors expression
13091290 if self .obj_function == ObjectiveType .SSE :
0 commit comments