Skip to content

Commit

Permalink
add the log of error message
Browse files Browse the repository at this point in the history
  • Loading branch information
ZedongPeng committed Aug 11, 2023
1 parent cb3669a commit 2147f11
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 30 deletions.
29 changes: 17 additions & 12 deletions pyomo/contrib/mindtpy/algorithm_base_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
Expand Down Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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
):
Expand Down Expand Up @@ -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.'
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
16 changes: 8 additions & 8 deletions pyomo/contrib/mindtpy/cut_generation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
Expand All @@ -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)
)
Expand Down Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions pyomo/contrib/mindtpy/extended_cutting_plane.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions pyomo/contrib/mindtpy/global_outer_approximation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
9 changes: 5 additions & 4 deletions pyomo/contrib/mindtpy/single_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pyomo/contrib/mindtpy/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down

0 comments on commit 2147f11

Please sign in to comment.