def _tighten_bounds(self): self.range_reduction_model_setup() assert self._best_objective != self.unbounded_objective() # setup objective assert self.problem.pyomo_model_objective.active self.problem.pyomo_model_objective.deactivate() tmp_objective = pmo.objective() tmp_objective_name = add_tmp_component(self.problem.pyomo_model, "rr_objective", tmp_objective) # setup optimality bound if necessary tmp_optbound_name = None tmp_optbound = None if self._best_objective != self.infeasible_objective(): tmp_optbound = create_optimality_bound( self, self.problem.pyomo_model_objective, self._best_objective) tmp_optbound_name = add_tmp_component(self.problem.pyomo_model, "optimality_bound", tmp_optbound) self.range_reduction_constraint_added(tmp_optbound) try: return self._tighten_bounds_impl(tmp_objective) finally: # reset objective delattr(self.problem.pyomo_model, tmp_objective_name) self.problem.pyomo_model_objective.activate() self.range_reduction_objective_changed( self.problem.pyomo_model_objective) # remove optimality bound if it was added if tmp_optbound is not None: self.range_reduction_constraint_removed(tmp_optbound) delattr(self.problem.pyomo_model, tmp_optbound_name) self.range_reduction_model_cleanup()
def test_create_optimality_bound(self): # max problem = MaxProblem() model = pmo.block() model.objective = pmo.objective(sense=pmo.maximize) con = create_optimality_bound(problem, model.objective, 100) assert con.ctype is pmo.constraint._ctype assert con.body is model.objective assert con.lb == 100 # min problem = MinProblem() model = pmo.block() model.objective = pmo.objective(sense=pmo.minimize) con = create_optimality_bound(problem, model.objective, 100) assert con.ctype is pmo.constraint._ctype assert con.body is model.objective assert con.ub == 100