def _preprocess(model, objective=True, constraints=True): objective_found = False if objective: for block in model.block_data_objects(active=True): for obj in block.component_data_objects(Objective, active=True, descend_into=False): objective_found = True preprocess_block_objectives(block) break if objective_found: break if constraints: for block in model.block_data_objects(active=True): preprocess_block_constraints(block)
def compute_standard_repn(data, model=None): """ This plugin computes the standard representation for all objectives and constraints. All results are stored in a ComponentMap named "_repn" at the block level. We break out preprocessing of the objectives and constraints in order to avoid redundant and unnecessary work, specifically in contexts where a model is iteratively solved and modified. we don't have finer-grained resolution, but we could easily pass in a Constraint and an Objective if warranted. Required: model: A concrete model instance. """ idMap = {} for block in model.block_data_objects(active=True): preprocess_block_constraints(block, idMap=idMap) preprocess_block_objectives(block, idMap=idMap)
def solve_separation_problem(solver, model, fallback): xfrm = TransformationFactory('core.relax_discrete') if PYOMO_4_0: xfrm.apply(model, inplace=True) else: xfrm.apply_to(model) _block = model._interscenario_plugin # Switch objectives _block.original_obj().deactivate() _block.separation_obj.activate() #_block.separation_variables.unfix() _par = _block.fixed_variable_values _sep = _block.separation_variables allow_slack = _block.allow_slack if allow_slack: epsilon = _block.epsilon for idx in _sep: _sep[idx].setlb(None) _sep[idx].setub(None) else: _sep.unfix() # Note: preprocessing is only necessary if we are changing a # fixed/freed variable. if FALLBACK_ON_BRUTE_FORCE_PREPROCESS: model.preprocess() else: _map = {} preprocess_block_objectives(_block, idMap=_map) preprocess_block_constraints(_block, idMap=_map) #SOLVE output_buffer = StringIO() pyutilib.misc.setup_redirect(output_buffer) try: results = solver.solve(model, tee=True) except: logger.warning("Exception raised solving the interscenario " "evaluation subproblem") logger.warning("Solver log:\n%s" % output_buffer.getvalue()) raise finally: pyutilib.misc.reset_redirect() ss = results.solver.status tc = results.solver.termination_condition #self.timeInSolver += results['Solver'][0]['Time'] if ss == SolverStatus.ok and tc in _acceptable_termination_conditions: state = '' if PYOMO_4_0: model.load(results) else: model.solutions.load_from(results) elif tc in _infeasible_termination_conditions: state = 'INFEASIBLE' ans = "!!!!" else: state = 'NONOPTIMAL' ans = "????" if state: if fallback: #logger.warning("Initial attempt to solve the interscenario cut " # "separation subproblem failed with the default " # "solver (%s)." % (state,) ) pass else: logger.warning("Solving the interscenario cut separation " "subproblem failed (%s)." % (state,) ) logger.warning("Solver log:\n%s" % output_buffer.getvalue()) else: cut = dict((vid, (value(_sep[vid]), value(_par[vid]))) for vid in _block.STAGE1VAR) obj = value(_block.separation_obj) ans = (math.sqrt(obj), cut) output_buffer.close() # Restore the objective _block.original_obj().activate() _block.separation_obj.deactivate() # Turn off the separation variables if allow_slack: for idx in _sep: _sep[idx].setlb(-epsilon) _sep[idx].setub(epsilon) else: _sep.fix(0) if PYOMO_4_0: xfrm.apply(model, inplace=True, undo=True) else: xfrm.apply_to(model, undo=True) if FALLBACK_ON_BRUTE_FORCE_PREPROCESS: pass else: _map = {} preprocess_block_objectives(_block, idMap=_map) return ans
def preprocess_bundles(self, bundles=None, force_preprocess_bundle_objective=False, force_preprocess_bundle_constraints=False): # TODO: Does this import need to be delayed because # it is in a plugins subdirectory from pyomo.solvers.plugins.solvers.persistent_solver import \ PersistentSolver start_time = time.time() if len(self._bundle_instances) == 0: raise RuntimeError( "Unable to preprocess scenario bundles. Bundling " "does not seem to be activated.") if bundles is None: bundles = self._bundle_instances.keys() if self.get_option("verbose"): print("Preprocessing %s bundles" % len(bundles)) preprocess_bundle_objective = 0b01 preprocess_bundle_constraints = 0b10 for bundle_name in bundles: preprocess_bundle = 0 solver = self._bundle_solvers[bundle_name] persistent_solver_in_use = isinstance(solver, PersistentSolver) bundle_ef_instance = self._bundle_instances[bundle_name] if persistent_solver_in_use and \ (not solver.has_instance()): assert self._bundle_first_preprocess[bundle_name] solver.set_instance(bundle_ef_instance) self._bundle_first_preprocess[bundle_name] = False for scenario_name in self._bundle_scenarios[bundle_name]: self._scenario_solvers[scenario_name].set_instance( self._scenario_instances[scenario_name]) # We've preprocessed the instance, reset the relevant flags self._scenario_first_preprocess[scenario_name] = False self.clear_update_flags(scenario_name) self.clear_fixed_variables(scenario_name) self.clear_freed_variables(scenario_name) else: if persistent_solver_in_use: assert not self._bundle_first_preprocess[bundle_name] for scenario_name in self._bundle_scenarios[bundle_name]: if self.objective_updated[scenario_name]: preprocess_bundle |= preprocess_bundle_objective if ((len(self.fixed_variables[scenario_name]) > 0) or \ (len(self.freed_variables[scenario_name]) > 0)) and \ self.get_option("preprocess_fixed_variables"): preprocess_bundle |= \ preprocess_bundle_objective | \ preprocess_bundle_constraints if self._bundle_first_preprocess[bundle_name]: preprocess_bundle |= \ preprocess_bundle_objective | \ preprocess_bundle_constraints self._bundle_first_preprocess[bundle_name] = False if persistent_solver_in_use: # also preprocess on the scenario solver scenario_solver = self._scenario_solvers[scenario_name] isinstance(scenario_solver, PersistentSolver) self._preprocess_scenario(scenario_name, scenario_solver) self._preprocess_scenario(scenario_name, solver) # We've preprocessed the instance, reset the relevant flags self._scenario_first_preprocess[scenario_name] = False self.clear_update_flags(scenario_name) self.clear_fixed_variables(scenario_name) self.clear_freed_variables(scenario_name) if force_preprocess_bundle_objective: preprocess_bundle |= preprocess_bundle_objective if force_preprocess_bundle_constraints: preprocess_bundle |= preprocess_bundle_constraints if preprocess_bundle: if persistent_solver_in_use: assert solver.has_instance() if preprocess_bundle & preprocess_bundle_objective: obj_count = 0 for obj in bundle_ef_instance.component_data_objects( ctype=Objective, descend_into=False, active=True): obj_count += 1 if obj_count > 1: raise RuntimeError( "Persistent solver interface only " "supports a single active objective.") solver.set_objective(obj) if preprocess_bundle & preprocess_bundle_constraints: # we assume the bundle constraints are just simple # linking constraints (e.g., no SOSConstraints) for con in bundle_ef_instance.component_data_objects( ctype=Constraint, descend_into=False, active=True): solver.remove_constraint(con) solver.add_constraint(con) else: idMap = {} if preprocess_bundle & preprocess_bundle_objective: preprocess_block_objectives(bundle_ef_instance, idMap=idMap) if preprocess_bundle & preprocess_bundle_constraints: preprocess_block_constraints(bundle_ef_instance, idMap=idMap) end_time = time.time() if self.get_option("output_times"): print("Bundle preprocessing time=%.2f seconds" % (end_time - start_time))
def solve_separation_problem(solver, model, fallback): xfrm = TransformationFactory('core.relax_discrete') if PYOMO_4_0: xfrm.apply(model, inplace=True) else: xfrm.apply_to(model) _block = model._interscenario_plugin # Switch objectives _block.original_obj().deactivate() _block.separation_obj.activate() #_block.separation_variables.unfix() _par = _block.fixed_variable_values _sep = _block.separation_variables allow_slack = _block.allow_slack if allow_slack: epsilon = _block.epsilon for idx in _sep: _sep[idx].setlb(None) _sep[idx].setub(None) else: _sep.unfix() # Note: preprocessing is only necessary if we are changing a # fixed/freed variable. if FALLBACK_ON_BRUTE_FORCE_PREPROCESS: model.preprocess() else: _map = {} preprocess_block_objectives(_block, idMap=_map) preprocess_block_constraints(_block, idMap=_map) #SOLVE output_buffer = StringIO() setup_redirect(output_buffer) try: results = solver.solve(model, tee=True) except: logger.warning("Exception raised solving the interscenario " "evaluation subproblem") logger.warning("Solver log:\n%s" % output_buffer.getvalue()) raise finally: reset_redirect() ss = results.solver.status tc = results.solver.termination_condition #self.timeInSolver += results['Solver'][0]['Time'] if ss == SolverStatus.ok and tc in _acceptable_termination_conditions: state = '' if PYOMO_4_0: model.load(results) else: model.solutions.load_from(results) elif tc in _infeasible_termination_conditions: state = 'INFEASIBLE' ans = "!!!!" else: state = 'NONOPTIMAL' ans = "????" if state: if fallback: #logger.warning("Initial attempt to solve the interscenario cut " # "separation subproblem failed with the default " # "solver (%s)." % (state,) ) pass else: logger.warning("Solving the interscenario cut separation " "subproblem failed (%s)." % (state, )) logger.warning("Solver log:\n%s" % output_buffer.getvalue()) else: cut = dict((vid, (value(_sep[vid]), value(_par[vid]))) for vid in _block.STAGE1VAR) obj = value(_block.separation_obj) ans = (math.sqrt(obj), cut) output_buffer.close() # Restore the objective _block.original_obj().activate() _block.separation_obj.deactivate() # Turn off the separation variables if allow_slack: for idx in _sep: _sep[idx].setlb(-epsilon) _sep[idx].setub(epsilon) else: _sep.fix(0) if PYOMO_4_0: xfrm.apply(model, inplace=True, undo=True) else: xfrm.apply_to(model, undo=True) if FALLBACK_ON_BRUTE_FORCE_PREPROCESS: pass else: _map = {} preprocess_block_objectives(_block, idMap=_map) return ans