def __init__(self, *args, **kwds): IndexedVar.__init__(self, Any, **kwds) # Constructor for ComponentDict needs to # go last in order to handle any initialization # iterable as an argument ComponentDict.__init__(self, _VarData, *args, **kwds)
def is_bounded(self, config): """ Return True if the uncertainty set is bounded, else False. """ # === Determine bounds on all uncertain params bounding_model = ConcreteModel() bounding_model.util = Block() # So that boundedness checks work for Cardinality and FactorModel sets bounding_model.uncertain_param_vars = IndexedVar(range(len(config.uncertain_params)), initialize=1) for idx, param in enumerate(config.uncertain_params): bounding_model.uncertain_param_vars[idx].value = param.value bounding_model.add_component("uncertainty_set_constraint", config.uncertainty_set.set_as_constraint( uncertain_params=bounding_model.uncertain_param_vars, model=bounding_model, config=config )) for idx, param in enumerate(list(bounding_model.uncertain_param_vars.values())): bounding_model.add_component("lb_obj_" + str(idx), Objective(expr=param, sense=minimize)) bounding_model.add_component("ub_obj_" + str(idx), Objective(expr=param, sense=maximize)) for o in bounding_model.component_data_objects(Objective): o.deactivate() for i in range(len(bounding_model.uncertain_param_vars)): for limit in ("lb", "ub"): getattr(bounding_model, limit + "_obj_" + str(i)).activate() res = config.global_solver.solve(bounding_model, tee=False) getattr(bounding_model, limit + "_obj_" + str(i)).deactivate() if not check_optimal_termination(res): return False return True
def add_bounds_for_uncertain_parameters(model, config): ''' This function solves a set of optimization problems to determine bounds on the uncertain parameters given the uncertainty set description. These bounds will be added as additional constraints to the uncertainty_set_constr constraint. Should only be called once set_as_constraint() has been called on the separation_model object. :param separation_model: the model on which to add the bounds :param config: solver config :return: ''' # === Determine bounds on all uncertain params uncertain_param_bounds = [] bounding_model = ConcreteModel() bounding_model.util = Block() bounding_model.util.uncertain_param_vars = IndexedVar( model.util.uncertain_param_vars.index_set()) for tup in model.util.uncertain_param_vars.items(): bounding_model.util.uncertain_param_vars[tup[0]].value = tup[1].value bounding_model.add_component( "uncertainty_set_constraint", config.uncertainty_set.set_as_constraint( uncertain_params=bounding_model.util.uncertain_param_vars, model=bounding_model, config=config)) for idx, param in enumerate( list(bounding_model.util.uncertain_param_vars.values())): bounding_model.add_component("lb_obj_" + str(idx), Objective(expr=param, sense=minimize)) bounding_model.add_component("ub_obj_" + str(idx), Objective(expr=param, sense=maximize)) for o in bounding_model.component_data_objects(Objective): o.deactivate() for i in range(len(bounding_model.util.uncertain_param_vars)): bounds = [] for limit in ("lb", "ub"): getattr(bounding_model, limit + "_obj_" + str(i)).activate() res = config.global_solver.solve(bounding_model, tee=False) bounds.append(bounding_model.util.uncertain_param_vars[i].value) getattr(bounding_model, limit + "_obj_" + str(i)).deactivate() uncertain_param_bounds.append(bounds) # === Add bounds as constraints to uncertainty_set_constraint ConstraintList for idx, bound in enumerate(uncertain_param_bounds): model.util.uncertain_param_vars[idx].setlb(bound[0]) model.util.uncertain_param_vars[idx].setub(bound[1]) return