diff --git a/pyomo/contrib/mindtpy/algorithm_base_class.py b/pyomo/contrib/mindtpy/algorithm_base_class.py index c0bfc96ed92..ca3d61f67d5 100644 --- a/pyomo/contrib/mindtpy/algorithm_base_class.py +++ b/pyomo/contrib/mindtpy/algorithm_base_class.py @@ -782,7 +782,8 @@ def MindtPy_initialization(self): elif config.init_strategy == 'initial_binary': try: self.curr_int_sol = get_integer_solution(self.working_model) - except TypeError: + except TypeError as e: + config.logger.error(e) raise ValueError( 'The initial integer combination is not provided or not complete. ' 'Please provide the complete integer combination or use other initialization strategy.' @@ -1030,7 +1031,8 @@ def solve_subproblem(self): self.fixed_nlp.tmp_duals[c] = c_geq * max( 0, c_geq * (rhs - value(c.body)) ) - except (ValueError, OverflowError) as error: + except (ValueError, OverflowError) as e: + config.logger.error(e) self.fixed_nlp.tmp_duals[c] = None evaluation_error = True if evaluation_error: @@ -1046,9 +1048,9 @@ def solve_subproblem(self): ignore_infeasible=False, tolerance=config.constraint_tolerance, ) - except InfeasibleConstraintException: - config.logger.warning( - 'infeasibility detected in deactivate_trivial_constraints' + except InfeasibleConstraintException as e: + config.logger.error(str(e) + + '\nInfeasibility detected in deactivate_trivial_constraints.' ) results = SolverResults() results.solver.termination_condition = tc.infeasible @@ -1326,7 +1328,8 @@ def solve_feasibility_subproblem(self): ) if len(feas_soln.solution) > 0: feas_subproblem.solutions.load_from(feas_soln) - except (ValueError, OverflowError) as error: + except (ValueError, OverflowError) as e: + config.logger.error(e) for nlp_var, orig_val in zip( MindtPy.variable_list, self.initial_var_values ): @@ -1451,8 +1454,9 @@ def fix_dual_bound(self, last_iter_cuts): ) try: self.dual_bound = self.stored_bound[self.primal_bound] - except KeyError: - config.logger.info('No stored bound found. Bound fix failed.') + except KeyError as e: + config.logger.error(str(e) + + '\nNo stored bound found. Bound fix failed.') else: config.logger.info( 'Solve the main problem without the last no_good cut to fix the bound.' @@ -1578,7 +1582,8 @@ def solve_main(self): # update_attributes should be before load_from(main_mip_results), since load_from(main_mip_results) may fail. if len(main_mip_results.solution) > 0: self.mip.solutions.load_from(main_mip_results) - except (ValueError, AttributeError, RuntimeError): + except (ValueError, AttributeError, RuntimeError) as e: + config.logger.error(e) if config.single_tree: config.logger.warning('Single tree terminate.') if get_main_elapsed_time(self.timing) >= config.time_limit - 2: @@ -2283,9 +2288,9 @@ def solve_fp_subproblem(self): ignore_infeasible=False, tolerance=config.constraint_tolerance, ) - except InfeasibleConstraintException: - config.logger.warning( - 'infeasibility detected in deactivate_trivial_constraints' + except InfeasibleConstraintException as e: + config.logger.error(str(e) + + '\nInfeasibility detected in deactivate_trivial_constraints.' ) results = SolverResults() results.solver.termination_condition = tc.infeasible diff --git a/pyomo/contrib/mindtpy/cut_generation.py b/pyomo/contrib/mindtpy/cut_generation.py index fe5e1f330a0..212a66f5471 100644 --- a/pyomo/contrib/mindtpy/cut_generation.py +++ b/pyomo/contrib/mindtpy/cut_generation.py @@ -221,9 +221,9 @@ def add_ecp_cuts( if constr.has_ub(): try: upper_slack = constr.uslack() - except (ValueError, OverflowError): - config.logger.warning( - 'constraint {} has caused either a ' + except (ValueError, OverflowError) as e: + config.logger.error(str(e) + + '\nConstraint {} has caused either a ' 'ValueError or OverflowError.' '\n'.format(constr) ) @@ -250,9 +250,9 @@ def add_ecp_cuts( if constr.has_lb(): try: lower_slack = constr.lslack() - except (ValueError, OverflowError): - config.logger.warning( - 'constraint {} has caused either a ' + except (ValueError, OverflowError) as e: + config.logger.error(str(e) + + '\nConstraint {} has caused either a ' 'ValueError or OverflowError.' '\n'.format(constr) ) @@ -375,8 +375,8 @@ def add_affine_cuts(target_model, config, timing): try: mc_eqn = mc(constr.body) except MCPP_Error as e: - config.logger.debug( - 'Skipping constraint %s due to MCPP error %s' + config.logger.error( + '\nSkipping constraint %s due to MCPP error %s' % (constr.name, str(e)) ) continue # skip to the next constraint diff --git a/pyomo/contrib/mindtpy/extended_cutting_plane.py b/pyomo/contrib/mindtpy/extended_cutting_plane.py index 939cad1104b..0c419620019 100644 --- a/pyomo/contrib/mindtpy/extended_cutting_plane.py +++ b/pyomo/contrib/mindtpy/extended_cutting_plane.py @@ -148,8 +148,9 @@ def all_nonlinear_constraint_satisfied(self): if nlc.has_lb(): try: lower_slack = nlc.lslack() - except (ValueError, OverflowError): + except (ValueError, OverflowError) as e: # Set lower_slack (upper_slack below) less than -config.ecp_tolerance in this case. + config.logger.error(e) lower_slack = -10 * config.ecp_tolerance if lower_slack < -config.ecp_tolerance: config.logger.debug( @@ -161,7 +162,8 @@ def all_nonlinear_constraint_satisfied(self): if nlc.has_ub(): try: upper_slack = nlc.uslack() - except (ValueError, OverflowError): + except (ValueError, OverflowError) as e: + config.logger.error(e) upper_slack = -10 * config.ecp_tolerance if upper_slack < -config.ecp_tolerance: config.logger.debug( diff --git a/pyomo/contrib/mindtpy/global_outer_approximation.py b/pyomo/contrib/mindtpy/global_outer_approximation.py index 57d718bdf94..ee3ffb62f55 100644 --- a/pyomo/contrib/mindtpy/global_outer_approximation.py +++ b/pyomo/contrib/mindtpy/global_outer_approximation.py @@ -106,5 +106,5 @@ def deactivate_no_good_cuts_when_fixing_bound(self, no_good_cuts): no_good_cuts[i].deactivate() if self.config.use_tabu_list: self.integer_list = self.integer_list[:valid_no_good_cuts_num] - except KeyError: - self.config.logger.info('No-good cut deactivate failed.') + except KeyError as e: + self.config.logger.error(str(e) + '\nDeactivating no-good cuts failed.') diff --git a/pyomo/contrib/mindtpy/single_tree.py b/pyomo/contrib/mindtpy/single_tree.py index 828ac12f8e2..d24da9feaa4 100644 --- a/pyomo/contrib/mindtpy/single_tree.py +++ b/pyomo/contrib/mindtpy/single_tree.py @@ -84,8 +84,9 @@ def copy_lazy_var_list_values( # will always succeed and the ValueError should never be # raised. v_to.set_value(v_val, skip_validation=True) - except ValueError: + except ValueError as e: # Snap the value to the bounds + config.logger.error(e) if ( v_to.has_lb() and v_val < v_to.lb @@ -733,9 +734,9 @@ def __call__(self): self.add_lazy_oa_cuts( mindtpy_object.mip, None, mindtpy_object, config, opt ) - except ValueError: - config.logger.debug( - "Usually this error is caused by the MIP start solution causing a math domain error. " + except ValueError as e: + config.logger.error(str(e) + + "\nUsually this error is caused by the MIP start solution causing a math domain error. " "We will skip it." ) return diff --git a/pyomo/contrib/mindtpy/util.py b/pyomo/contrib/mindtpy/util.py index 3e6dca6dc95..f2f1d3a6227 100644 --- a/pyomo/contrib/mindtpy/util.py +++ b/pyomo/contrib/mindtpy/util.py @@ -701,8 +701,8 @@ def copy_var_list_values_from_solution_pool( # instead log warnings). This means that the following will # always succeed and the ValueError should never be raised. v_to.set_value(var_val, skip_validation=True) - except ValueError as err: - err_msg = getattr(err, 'message', str(err)) + except ValueError as e: + config.logger.error(e) rounded_val = int(round(var_val)) # Check to see if this is just a tolerance issue if ignore_integrality and v_to.is_integer():