From 4471b7caab4b120c4a00c627bc015fb9995962b3 Mon Sep 17 00:00:00 2001 From: Michael Bynum Date: Sat, 10 Feb 2024 21:33:18 -0700 Subject: [PATCH] run black --- pyomo/contrib/solver/gurobi.py | 25 ++- pyomo/contrib/solver/ipopt.py | 16 +- pyomo/contrib/solver/sol_reader.py | 4 +- .../solver/tests/solvers/test_solvers.py | 144 ++++++------------ 4 files changed, 76 insertions(+), 113 deletions(-) diff --git a/pyomo/contrib/solver/gurobi.py b/pyomo/contrib/solver/gurobi.py index 2dcdacd320d..50d241e1e88 100644 --- a/pyomo/contrib/solver/gurobi.py +++ b/pyomo/contrib/solver/gurobi.py @@ -69,12 +69,12 @@ def __init__( visibility=visibility, ) self.use_mipstart: bool = self.declare( - 'use_mipstart', + 'use_mipstart', ConfigValue( - default=False, - domain=bool, + default=False, + domain=bool, description="If True, the values of the integer variables will be passed to Gurobi.", - ) + ), ) @@ -339,9 +339,12 @@ def _solve(self): self._solver_model.setParam('MIPGap', config.rel_gap) if config.abs_gap is not None: self._solver_model.setParam('MIPGapAbs', config.abs_gap) - + if config.use_mipstart: - for pyomo_var_id, gurobi_var in self._pyomo_var_to_solver_var_map.items(): + for ( + pyomo_var_id, + gurobi_var, + ) in self._pyomo_var_to_solver_var_map.items(): pyomo_var = self._vars[pyomo_var_id][0] if pyomo_var.is_integer() and pyomo_var.value is not None: self.set_var_attr(pyomo_var, 'Start', pyomo_var.value) @@ -866,7 +869,9 @@ def _postsolve(self, timer: HierarchicalTimer): if status == grb.LOADED: # problem is loaded, but no solution results.termination_condition = TerminationCondition.unknown elif status == grb.OPTIMAL: # optimal - results.termination_condition = TerminationCondition.convergenceCriteriaSatisfied + results.termination_condition = ( + TerminationCondition.convergenceCriteriaSatisfied + ) elif status == grb.INFEASIBLE: results.termination_condition = TerminationCondition.provenInfeasible elif status == grb.INF_OR_UNBD: @@ -894,7 +899,11 @@ def _postsolve(self, timer: HierarchicalTimer): else: results.termination_condition = TerminationCondition.unknown - if results.termination_condition != TerminationCondition.convergenceCriteriaSatisfied and config.raise_exception_on_nonoptimal_result: + if ( + results.termination_condition + != TerminationCondition.convergenceCriteriaSatisfied + and config.raise_exception_on_nonoptimal_result + ): raise RuntimeError( 'Solver did not find the optimal solution. Set opt.config.raise_exception_on_nonoptimal_result = False to bypass this error.' ) diff --git a/pyomo/contrib/solver/ipopt.py b/pyomo/contrib/solver/ipopt.py index 4c4b932381d..5eb877f0867 100644 --- a/pyomo/contrib/solver/ipopt.py +++ b/pyomo/contrib/solver/ipopt.py @@ -89,15 +89,15 @@ def __init__( implicit_domain=implicit_domain, visibility=visibility, ) - self.timing_info.no_function_solve_time: Optional[float] = ( - self.timing_info.declare( - 'no_function_solve_time', ConfigValue(domain=NonNegativeFloat) - ) + self.timing_info.no_function_solve_time: Optional[ + float + ] = self.timing_info.declare( + 'no_function_solve_time', ConfigValue(domain=NonNegativeFloat) ) - self.timing_info.function_solve_time: Optional[float] = ( - self.timing_info.declare( - 'function_solve_time', ConfigValue(domain=NonNegativeFloat) - ) + self.timing_info.function_solve_time: Optional[ + float + ] = self.timing_info.declare( + 'function_solve_time', ConfigValue(domain=NonNegativeFloat) ) diff --git a/pyomo/contrib/solver/sol_reader.py b/pyomo/contrib/solver/sol_reader.py index a2e4d90b898..04f12feb25e 100644 --- a/pyomo/contrib/solver/sol_reader.py +++ b/pyomo/contrib/solver/sol_reader.py @@ -122,7 +122,9 @@ def parse_sol_file( # TODO: this is solver dependent # But this was the way in the previous version - and has been fine thus far? result.solution_status = SolutionStatus.infeasible - result.termination_condition = TerminationCondition.iterationLimit # this is not always correct + result.termination_condition = ( + TerminationCondition.iterationLimit + ) # this is not always correct elif (exit_code[1] >= 500) and (exit_code[1] <= 599): exit_code_message = ( "FAILURE: the solver stopped by an error condition " diff --git a/pyomo/contrib/solver/tests/solvers/test_solvers.py b/pyomo/contrib/solver/tests/solvers/test_solvers.py index 0499f1abb5d..658aaf41b13 100644 --- a/pyomo/contrib/solver/tests/solvers/test_solvers.py +++ b/pyomo/contrib/solver/tests/solvers/test_solvers.py @@ -21,10 +21,7 @@ if not param_available: raise unittest.SkipTest('Parameterized is not available.') -all_solvers = [ - ('gurobi', Gurobi), - ('ipopt', ipopt), -] +all_solvers = [('gurobi', Gurobi), ('ipopt', ipopt)] mip_solvers = [('gurobi', Gurobi)] nlp_solvers = [('ipopt', ipopt)] qcp_solvers = [('gurobi', Gurobi), ('ipopt', ipopt)] @@ -43,7 +40,7 @@ def _load_tests(solver_list): class TestSolvers(unittest.TestCase): @parameterized.expand(input=_load_tests(all_solvers)) def test_remove_variable_and_objective( - self, name: str, opt_class: Type[SolverBase], + self, name: str, opt_class: Type[SolverBase] ): # this test is for issue #2888 opt: SolverBase = opt_class() @@ -65,9 +62,7 @@ def test_remove_variable_and_objective( self.assertAlmostEqual(m.x.value, 2) @parameterized.expand(input=_load_tests(all_solvers)) - def test_stale_vars( - self, name: str, opt_class: Type[SolverBase], - ): + def test_stale_vars(self, name: str, opt_class: Type[SolverBase]): opt: SolverBase = opt_class() if not opt.available(): raise unittest.SkipTest @@ -108,9 +103,7 @@ def test_stale_vars( self.assertFalse(m.y.stale) @parameterized.expand(input=_load_tests(all_solvers)) - def test_range_constraint( - self, name: str, opt_class: Type[SolverBase], - ): + def test_range_constraint(self, name: str, opt_class: Type[SolverBase]): opt: SolverBase = opt_class() if not opt.available(): raise unittest.SkipTest @@ -131,9 +124,7 @@ def test_range_constraint( self.assertAlmostEqual(duals[m.c], 1) @parameterized.expand(input=_load_tests(all_solvers)) - def test_reduced_costs( - self, name: str, opt_class: Type[SolverBase], - ): + def test_reduced_costs(self, name: str, opt_class: Type[SolverBase]): opt: SolverBase = opt_class() if not opt.available(): raise unittest.SkipTest @@ -150,9 +141,7 @@ def test_reduced_costs( self.assertAlmostEqual(rc[m.y], 4) @parameterized.expand(input=_load_tests(all_solvers)) - def test_reduced_costs2( - self, name: str, opt_class: Type[SolverBase], - ): + def test_reduced_costs2(self, name: str, opt_class: Type[SolverBase]): opt: SolverBase = opt_class() if not opt.available(): raise unittest.SkipTest @@ -172,9 +161,7 @@ def test_reduced_costs2( self.assertAlmostEqual(rc[m.x], 1) @parameterized.expand(input=_load_tests(all_solvers)) - def test_param_changes( - self, name: str, opt_class: Type[SolverBase], - ): + def test_param_changes(self, name: str, opt_class: Type[SolverBase]): opt: SolverBase = opt_class() if not opt.available(): raise unittest.SkipTest @@ -210,9 +197,7 @@ def test_param_changes( self.assertAlmostEqual(duals[m.c2], a1 / (a2 - a1)) @parameterized.expand(input=_load_tests(all_solvers)) - def test_immutable_param( - self, name: str, opt_class: Type[SolverBase], - ): + def test_immutable_param(self, name: str, opt_class: Type[SolverBase]): """ This test is important because component_data_objects returns immutable params as floats. We want to make sure we process these correctly. @@ -252,9 +237,7 @@ def test_immutable_param( self.assertAlmostEqual(duals[m.c2], a1 / (a2 - a1)) @parameterized.expand(input=_load_tests(all_solvers)) - def test_equality( - self, name: str, opt_class: Type[SolverBase], - ): + def test_equality(self, name: str, opt_class: Type[SolverBase]): opt: SolverBase = opt_class() if not opt.available(): raise unittest.SkipTest @@ -292,9 +275,7 @@ def test_equality( self.assertAlmostEqual(duals[m.c2], -a1 / (a2 - a1)) @parameterized.expand(input=_load_tests(all_solvers)) - def test_linear_expression( - self, name: str, opt_class: Type[SolverBase], - ): + def test_linear_expression(self, name: str, opt_class: Type[SolverBase]): opt: SolverBase = opt_class() if not opt.available(): raise unittest.SkipTest @@ -332,9 +313,7 @@ def test_linear_expression( self.assertTrue(bound <= m.y.value) @parameterized.expand(input=_load_tests(all_solvers)) - def test_no_objective( - self, name: str, opt_class: Type[SolverBase], - ): + def test_no_objective(self, name: str, opt_class: Type[SolverBase]): opt: SolverBase = opt_class() if not opt.available(): raise unittest.SkipTest @@ -365,9 +344,7 @@ def test_no_objective( self.assertAlmostEqual(duals[m.c2], 0) @parameterized.expand(input=_load_tests(all_solvers)) - def test_add_remove_cons( - self, name: str, opt_class: Type[SolverBase], - ): + def test_add_remove_cons(self, name: str, opt_class: Type[SolverBase]): opt: SolverBase = opt_class() if not opt.available(): raise unittest.SkipTest @@ -421,9 +398,7 @@ def test_add_remove_cons( self.assertAlmostEqual(duals[m.c2], a1 / (a2 - a1)) @parameterized.expand(input=_load_tests(all_solvers)) - def test_results_infeasible( - self, name: str, opt_class: Type[SolverBase], - ): + def test_results_infeasible(self, name: str, opt_class: Type[SolverBase]): opt: SolverBase = opt_class() if not opt.available(): raise unittest.SkipTest @@ -472,7 +447,7 @@ def test_results_infeasible( res.solution_loader.get_reduced_costs() @parameterized.expand(input=_load_tests(all_solvers)) - def test_duals(self, name: str, opt_class: Type[SolverBase],): + def test_duals(self, name: str, opt_class: Type[SolverBase]): opt: SolverBase = opt_class() if not opt.available(): raise unittest.SkipTest @@ -496,7 +471,7 @@ def test_duals(self, name: str, opt_class: Type[SolverBase],): @parameterized.expand(input=_load_tests(qcp_solvers)) def test_mutable_quadratic_coefficient( - self, name: str, opt_class: Type[SolverBase], + self, name: str, opt_class: Type[SolverBase] ): opt: SolverBase = opt_class() if not opt.available(): @@ -519,9 +494,7 @@ def test_mutable_quadratic_coefficient( self.assertAlmostEqual(m.y.value, 0.0869525991355825, 4) @parameterized.expand(input=_load_tests(qcp_solvers)) - def test_mutable_quadratic_objective( - self, name: str, opt_class: Type[SolverBase], - ): + def test_mutable_quadratic_objective(self, name: str, opt_class: Type[SolverBase]): opt: SolverBase = opt_class() if not opt.available(): raise unittest.SkipTest @@ -546,13 +519,13 @@ def test_mutable_quadratic_objective( self.assertAlmostEqual(m.y.value, 0.09227926676152151, 4) @parameterized.expand(input=_load_tests(all_solvers)) - def test_fixed_vars( - self, name: str, opt_class: Type[SolverBase], - ): + def test_fixed_vars(self, name: str, opt_class: Type[SolverBase]): for treat_fixed_vars_as_params in [True, False]: opt: SolverBase = opt_class() if opt.is_persistent(): - opt.config.auto_updates.treat_fixed_vars_as_params = treat_fixed_vars_as_params + opt.config.auto_updates.treat_fixed_vars_as_params = ( + treat_fixed_vars_as_params + ) if not opt.available(): raise unittest.SkipTest m = pe.ConcreteModel() @@ -587,9 +560,7 @@ def test_fixed_vars( self.assertAlmostEqual(m.y.value, 2) @parameterized.expand(input=_load_tests(all_solvers)) - def test_fixed_vars_2( - self, name: str, opt_class: Type[SolverBase], - ): + def test_fixed_vars_2(self, name: str, opt_class: Type[SolverBase]): opt: SolverBase = opt_class() if opt.is_persistent(): opt.config.auto_updates.treat_fixed_vars_as_params = True @@ -627,9 +598,7 @@ def test_fixed_vars_2( self.assertAlmostEqual(m.y.value, 2) @parameterized.expand(input=_load_tests(all_solvers)) - def test_fixed_vars_3( - self, name: str, opt_class: Type[SolverBase], - ): + def test_fixed_vars_3(self, name: str, opt_class: Type[SolverBase]): opt: SolverBase = opt_class() if opt.is_persistent(): opt.config.auto_updates.treat_fixed_vars_as_params = True @@ -645,9 +614,7 @@ def test_fixed_vars_3( self.assertAlmostEqual(m.x.value, 2) @parameterized.expand(input=_load_tests(nlp_solvers)) - def test_fixed_vars_4( - self, name: str, opt_class: Type[SolverBase], - ): + def test_fixed_vars_4(self, name: str, opt_class: Type[SolverBase]): opt: SolverBase = opt_class() if opt.is_persistent(): opt.config.auto_updates.treat_fixed_vars_as_params = True @@ -667,9 +634,7 @@ def test_fixed_vars_4( self.assertAlmostEqual(m.y.value, 2**0.5) @parameterized.expand(input=_load_tests(all_solvers)) - def test_mutable_param_with_range( - self, name: str, opt_class: Type[SolverBase], - ): + def test_mutable_param_with_range(self, name: str, opt_class: Type[SolverBase]): opt: SolverBase = opt_class() if not opt.available(): raise unittest.SkipTest @@ -743,7 +708,10 @@ def test_mutable_param_with_range( self.assertAlmostEqual(m.x.value, (b2 - b1) / (a1 - a2), 6) self.assertAlmostEqual(m.y.value, a1 * (b2 - b1) / (a1 - a2) + b1, 6) self.assertAlmostEqual(res.incumbent_objective, m.y.value, 6) - self.assertTrue(res.objective_bound is None or res.objective_bound <= m.y.value + 1e-12) + self.assertTrue( + res.objective_bound is None + or res.objective_bound <= m.y.value + 1e-12 + ) duals = res.solution_loader.get_duals() self.assertAlmostEqual(duals[m.con1], (1 + a1 / (a2 - a1)), 6) self.assertAlmostEqual(duals[m.con2], -a1 / (a2 - a1), 6) @@ -751,15 +719,16 @@ def test_mutable_param_with_range( self.assertAlmostEqual(m.x.value, (c2 - c1) / (a1 - a2), 6) self.assertAlmostEqual(m.y.value, a1 * (c2 - c1) / (a1 - a2) + c1, 6) self.assertAlmostEqual(res.incumbent_objective, m.y.value, 6) - self.assertTrue(res.objective_bound is None or res.objective_bound >= m.y.value - 1e-12) + self.assertTrue( + res.objective_bound is None + or res.objective_bound >= m.y.value - 1e-12 + ) duals = res.solution_loader.get_duals() self.assertAlmostEqual(duals[m.con1], (1 + a1 / (a2 - a1)), 6) self.assertAlmostEqual(duals[m.con2], -a1 / (a2 - a1), 6) @parameterized.expand(input=_load_tests(all_solvers)) - def test_add_and_remove_vars( - self, name: str, opt_class: Type[SolverBase], - ): + def test_add_and_remove_vars(self, name: str, opt_class: Type[SolverBase]): opt = opt_class() if not opt.available(): raise unittest.SkipTest @@ -805,7 +774,7 @@ def test_add_and_remove_vars( self.assertAlmostEqual(m.y.value, -1) @parameterized.expand(input=_load_tests(nlp_solvers)) - def test_exp(self, name: str, opt_class: Type[SolverBase],): + def test_exp(self, name: str, opt_class: Type[SolverBase]): opt = opt_class() if not opt.available(): raise unittest.SkipTest @@ -819,7 +788,7 @@ def test_exp(self, name: str, opt_class: Type[SolverBase],): self.assertAlmostEqual(m.y.value, 0.6529186341994245) @parameterized.expand(input=_load_tests(nlp_solvers)) - def test_log(self, name: str, opt_class: Type[SolverBase],): + def test_log(self, name: str, opt_class: Type[SolverBase]): opt = opt_class() if not opt.available(): raise unittest.SkipTest @@ -833,9 +802,7 @@ def test_log(self, name: str, opt_class: Type[SolverBase],): self.assertAlmostEqual(m.y.value, -0.42630274815985264) @parameterized.expand(input=_load_tests(all_solvers)) - def test_with_numpy( - self, name: str, opt_class: Type[SolverBase], - ): + def test_with_numpy(self, name: str, opt_class: Type[SolverBase]): opt: SolverBase = opt_class() if not opt.available(): raise unittest.SkipTest @@ -863,9 +830,7 @@ def test_with_numpy( self.assertAlmostEqual(m.y.value, a1 * (b2 - b1) / (a1 - a2) + b1) @parameterized.expand(input=_load_tests(all_solvers)) - def test_bounds_with_params( - self, name: str, opt_class: Type[SolverBase], - ): + def test_bounds_with_params(self, name: str, opt_class: Type[SolverBase]): opt: SolverBase = opt_class() if not opt.available(): raise unittest.SkipTest @@ -897,9 +862,7 @@ def test_bounds_with_params( self.assertAlmostEqual(m.y.value, 3) @parameterized.expand(input=_load_tests(all_solvers)) - def test_solution_loader( - self, name: str, opt_class: Type[SolverBase], - ): + def test_solution_loader(self, name: str, opt_class: Type[SolverBase]): opt: SolverBase = opt_class() if not opt.available(): raise unittest.SkipTest @@ -949,9 +912,7 @@ def test_solution_loader( self.assertAlmostEqual(duals[m.c1], 1) @parameterized.expand(input=_load_tests(all_solvers)) - def test_time_limit( - self, name: str, opt_class: Type[SolverBase], - ): + def test_time_limit(self, name: str, opt_class: Type[SolverBase]): opt: SolverBase = opt_class() if not opt.available(): raise unittest.SkipTest @@ -1002,13 +963,12 @@ def test_time_limit( opt.config.raise_exception_on_nonoptimal_result = False res = opt.solve(m) self.assertIn( - res.termination_condition, {TerminationCondition.maxTimeLimit, TerminationCondition.iterationLimit} + res.termination_condition, + {TerminationCondition.maxTimeLimit, TerminationCondition.iterationLimit}, ) @parameterized.expand(input=_load_tests(all_solvers)) - def test_objective_changes( - self, name: str, opt_class: Type[SolverBase], - ): + def test_objective_changes(self, name: str, opt_class: Type[SolverBase]): opt: SolverBase = opt_class() if not opt.available(): raise unittest.SkipTest @@ -1072,9 +1032,7 @@ def test_objective_changes( self.assertAlmostEqual(res.incumbent_objective, 4) @parameterized.expand(input=_load_tests(all_solvers)) - def test_domain( - self, name: str, opt_class: Type[SolverBase], - ): + def test_domain(self, name: str, opt_class: Type[SolverBase]): opt: SolverBase = opt_class() if not opt.available(): raise unittest.SkipTest @@ -1098,9 +1056,7 @@ def test_domain( self.assertAlmostEqual(res.incumbent_objective, 0) @parameterized.expand(input=_load_tests(mip_solvers)) - def test_domain_with_integers( - self, name: str, opt_class: Type[SolverBase], - ): + def test_domain_with_integers(self, name: str, opt_class: Type[SolverBase]): opt: SolverBase = opt_class() if not opt.available(): raise unittest.SkipTest @@ -1124,9 +1080,7 @@ def test_domain_with_integers( self.assertAlmostEqual(res.incumbent_objective, 1) @parameterized.expand(input=_load_tests(all_solvers)) - def test_fixed_binaries( - self, name: str, opt_class: Type[SolverBase], - ): + def test_fixed_binaries(self, name: str, opt_class: Type[SolverBase]): opt: SolverBase = opt_class() if not opt.available(): raise unittest.SkipTest @@ -1153,9 +1107,7 @@ def test_fixed_binaries( self.assertAlmostEqual(res.incumbent_objective, 1) @parameterized.expand(input=_load_tests(mip_solvers)) - def test_with_gdp( - self, name: str, opt_class: Type[SolverBase], - ): + def test_with_gdp(self, name: str, opt_class: Type[SolverBase]): opt: SolverBase = opt_class() if not opt.available(): raise unittest.SkipTest @@ -1248,7 +1200,7 @@ def test_variables_elsewhere2(self, name: str, opt_class: Type[SolverBase]): self.assertNotIn(m.z, sol) @parameterized.expand(input=_load_tests(all_solvers)) - def test_bug_1(self, name: str, opt_class: Type[SolverBase],): + def test_bug_1(self, name: str, opt_class: Type[SolverBase]): opt: SolverBase = opt_class() if not opt.available(): raise unittest.SkipTest @@ -1271,7 +1223,7 @@ def test_bug_1(self, name: str, opt_class: Type[SolverBase],): self.assertAlmostEqual(res.incumbent_objective, 3) @parameterized.expand(input=_load_tests(all_solvers)) - def test_bug_2(self, name: str, opt_class: Type[SolverBase],): + def test_bug_2(self, name: str, opt_class: Type[SolverBase]): """ This test is for a bug where an objective containing a fixed variable does not get updated properly when the variable is unfixed.