def _generate_model(self): self.model = ConcreteModel() model = self.model model._name = self.description model.f = Var() model.x = Var(bounds=(1,3)) model.fi = Param([1,2,3],mutable=True) model.fi[1] = 1.0 model.fi[2] = 2.0 model.fi[3] = 0.0 model.xi = Param([1,2,3],mutable=True) model.xi[1] = 1.0 model.xi[2] = 2.0 model.xi[3] = 3.0 model.p = Var(within=NonNegativeReals) model.n = Var(within=NonNegativeReals) model.lmbda = Var([1,2,3]) model.obj = Objective(expr=model.p+model.n) model.c1 = ConstraintList() model.c1.add(0.0 <= model.lmbda[1] <= 1.0) model.c1.add(0.0 <= model.lmbda[2] <= 1.0) model.c1.add(0.0 <= model.lmbda[3]) model.c2 = SOSConstraint(var=model.lmbda, index=[1,2,3], sos=2) model.c3 = Constraint(expr=summation(model.lmbda) == 1) model.c4 = Constraint(expr=model.f==summation(model.fi,model.lmbda)) model.c5 = Constraint(expr=model.x==summation(model.xi,model.lmbda)) model.x = 2.75 model.x.fixed = True # Make an empty SOSConstraint model.c6 = SOSConstraint(var=model.lmbda, index=[1,2,3], sos=2) model.c6.set_items([],[]) assert len(list(model.c6.get_items())) == 0
def _process_bilinear_constraints(block, v1, v2, var_values, bilinear_constrs): # TODO check that the appropriate variable bounds exist. if not (v2.has_lb() and v2.has_ub()): logger.warning( textwrap.dedent("""\ Attempting to transform bilinear term {v1} * {v2} using effectively discrete variable {v1}, but {v2} is missing a lower or upper bound: ({v2lb}, {v2ub}). """.format(v1=v1, v2=v2, v2lb=v2.lb, v2ub=v2.ub)).strip()) return False blk = Block() unique_name = unique_component_name( block, ("%s_%s_bilinear" % (v1.local_name, v2.local_name)).replace( '[', '').replace(']', '')) block._induced_linearity_info.add_component(unique_name, blk) # TODO think about not using floats as indices in a set blk.valid_values = Set(initialize=sorted(var_values)) blk.x_active = Var(blk.valid_values, domain=Binary, initialize=1) blk.v_increment = Var(blk.valid_values, domain=v2.domain, bounds=(v2.lb, v2.ub), initialize=v2.value) blk.v_defn = Constraint(expr=v2 == summation(blk.v_increment)) @blk.Constraint(blk.valid_values) def v_lb(blk, val): return v2.lb * blk.x_active[val] <= blk.v_increment[val] @blk.Constraint(blk.valid_values) def v_ub(blk, val): return blk.v_increment[val] <= v2.ub * blk.x_active[val] blk.select_one_value = Constraint(expr=summation(blk.x_active) == 1) # Categorize as case 1 or case 2 for bilinear_constr in bilinear_constrs: # repn = generate_standard_repn(bilinear_constr.body) # Case 1: no other variables besides bilinear term in constraint. v1 # (effectively discrete variable) is positive. # if (len(repn.quadratic_vars) == 1 and len(repn.linear_vars) == 0 # and repn.nonlinear_expr is None): # _reformulate_case_1(v1, v2, discrete_constr, bilinear_constr) # NOTE: Case 1 is left unimplemented for now, because it involves some # messier logic with respect to how the transformation needs to happen. # Case 2: this is everything else, but do we want to have a special # case if there are nonlinear expressions involved with the constraint? pass _reformulate_case_2(blk, v1, v2, bilinear_constr) pass
def _process_bilinear_constraints(block, v1, v2, var_values, bilinear_constrs): # TODO check that the appropriate variable bounds exist. if not (v2.has_lb() and v2.has_ub()): logger.warning(textwrap.dedent("""\ Attempting to transform bilinear term {v1} * {v2} using effectively discrete variable {v1}, but {v2} is missing a lower or upper bound: ({v2lb}, {v2ub}). """.format(v1=v1, v2=v2, v2lb=v2.lb, v2ub=v2.ub)).strip()) return False blk = Block() unique_name = unique_component_name( block, ("%s_%s_bilinear" % (v1.local_name, v2.local_name)) .replace('[', '').replace(']', '')) block._induced_linearity_info.add_component(unique_name, blk) # TODO think about not using floats as indices in a set blk.valid_values = Set(initialize=var_values) blk.x_active = Var(blk.valid_values, domain=Binary, initialize=1) blk.v_increment = Var( blk.valid_values, domain=v2.domain, bounds=(v2.lb, v2.ub), initialize=v2.value) blk.v_defn = Constraint(expr=v2 == summation(blk.v_increment)) @blk.Constraint(blk.valid_values) def v_lb(blk, val): return v2.lb * blk.x_active[val] <= blk.v_increment[val] @blk.Constraint(blk.valid_values) def v_ub(blk, val): return blk.v_increment[val] <= v2.ub * blk.x_active[val] blk.select_one_value = Constraint(expr=summation(blk.x_active) == 1) # Categorize as case 1 or case 2 for bilinear_constr in bilinear_constrs: # repn = generate_standard_repn(bilinear_constr.body) # Case 1: no other variables besides bilinear term in constraint. v1 # (effectively discrete variable) is positive. # if (len(repn.quadratic_vars) == 1 and len(repn.linear_vars) == 0 # and repn.nonlinear_expr is None): # _reformulate_case_1(v1, v2, discrete_constr, bilinear_constr) # NOTE: Case 1 is left unimplemented for now, because it involves some # messier logic with respect to how the transformation needs to happen. # Case 2: this is everything else, but do we want to have a special # case if there are nonlinear expressions involved with the constraint? pass _reformulate_case_2(blk, v1, v2, bilinear_constr) pass
def _generate_model(self): self.model = None self.model = ConcreteModel() model = self.model model._name = self.description n = 7 m = 7 model.N = RangeSet(1,n) model.M = RangeSet(1,m) model.c = Param(model.N, rule=c_rule) model.b = Param(model.M, rule=b_rule) model.A = Param(model.M, model.N, rule=A_rule) model.x = Var(model.N, within=NonNegativeReals) model.y = Var(model.M, within=NonNegativeReals) model.cost = Objective(expr=summation(model.c, model.x)) model.primalcon = Constraint(model.M, rule=primalcon_rule) #model.dual = Suffix(direction=Suffix.IMPORT) #model.rc = Suffix(direction=Suffix.IMPORT) model.slack = Suffix(direction=Suffix.IMPORT) model.urc = Suffix(direction=Suffix.IMPORT) model.lrc = Suffix(direction=Suffix.IMPORT)
def res_global_cost_limit_rule(m): if math.isinf(m.global_prop_dict["value"][min(m.stf), "Cost limit"]): return pyomo.Constraint.Skip elif m.global_prop_dict["value"][min(m.stf), "Cost limit"] >= 0: return (pyomo.summation(m.costs) <= m.global_prop_dict["value"][min(m.stf), "Cost limit"]) else: return pyomo.Constraint.Skip
def res_global_cost_limit_rule(m): if math.isinf(m.global_prop_dict["value"]["Cost limit"]): return pyomo.Constraint.Skip elif m.global_prop_dict["value"]["Cost limit"] >= 0: return(pyomo.summation(m.costs) <= m.global_prop_dict["value"] ["Cost limit"]) else: return pyomo.Constraint.Skip
def _generate_model(self): self.model = ConcreteModel() model = self.model model._name = self.description model.a = Param(initialize=0.1) model.x = Var(within=NonNegativeReals) model.y = Var([1, 2], within=NonNegativeReals) model.obj = Objective(expr=model.x + model.y[1] + 2 * model.y[2]) model.c1 = Constraint(expr=model.a <= model.y[2]) model.c2 = Constraint(expr=2.0 <= model.x <= 10.0) model.c3 = SOSConstraint(var=model.y, index=[1, 2], sos=1) model.c4 = Constraint(expr=summation(model.y) == 1) # Make an empty SOSConstraint model.c5 = SOSConstraint(var=model.y, index=[1, 2], sos=1) model.c5.set_items([], []) assert len(list(model.c5.get_items())) == 0
def _generate_model(self): self.model = ConcreteModel() model = self.model model._name = self.description model.a = Param(initialize=0.1) model.x = Var(within=NonNegativeReals) model.y = Var([1,2],within=NonNegativeReals) model.obj = Objective(expr=model.x + model.y[1]+2*model.y[2]) model.c1 = Constraint(expr=model.a <= model.y[2]) model.c2 = Constraint(expr=2.0 <= model.x <= 10.0) model.c3 = SOSConstraint(var=model.y, index=[1,2], sos=1) model.c4 = Constraint(expr=summation(model.y) == 1) # Make an empty SOSConstraint model.c5 = SOSConstraint(var=model.y, index=[1,2], sos=1) model.c5.set_items([],[]) assert len(list(model.c5.get_items())) == 0
def _constraint7_rule(model): expr = model.gamma * model.A_pick - \ summation(model.Lambda_pick, model.zeta_pick) return (None, expr, 0)
def _constraint4_rule(model): expr = model.gamma * model.A_put - \ summation(model.Lambda_put, model.zeta_put) return (None, expr, 0)
def _constraint2_rule(model): return (summation(model.theta_pick), 1)
def _constraint1_rule(model): return (summation(model.theta_put), 1)
def _objectiveB_rule(model): expr = model.Ca * (model.alpha_put + model.alpha_pick) + \ summation(model.Cth_put, model.theta_put) + \ summation(model.Cth_pick, model.theta_pick) return (expr, model.FirstStageCost)
def _generate_model(self): self.model = None self.model = ConcreteModel() model = self.model model._name = self.description model.s = Set(initialize=[1,2]) model.x_unused = Var() model.x_unused.stale = False model.x_unused_initialy_stale = Var() model.x_unused_initialy_stale.stale = True model.X_unused = Var(model.s) model.X_unused_initialy_stale = Var(model.s) for i in model.s: model.X_unused[i].stale = False model.X_unused_initialy_stale[i].stale = True model.x = Var() model.x.stale = False model.x_initialy_stale = Var() model.x_initialy_stale.stale = True model.X = Var(model.s) model.X_initialy_stale = Var(model.s) for i in model.s: model.X[i].stale = False model.X_initialy_stale[i].stale = True model.obj = Objective(expr= model.x + \ model.x_initialy_stale + \ summation(model.X) + \ summation(model.X_initialy_stale)) model.c = ConstraintList() model.c.add( model.x >= 1 ) model.c.add( model.x_initialy_stale >= 1 ) model.c.add( model.X[1] >= 0 ) model.c.add( model.X[2] >= 1 ) model.c.add( model.X_initialy_stale[1] >= 0 ) model.c.add( model.X_initialy_stale[2] >= 1 ) # Test that stale flags do not get updated # on inactive blocks (where "inactive blocks" mean blocks # that do NOT follow a path of all active parent blocks # up to the top-level model) flat_model = model.clone() model.b = Block() model.B = Block(model.s) model.b.b = flat_model.clone() model.B[1].b = flat_model.clone() model.B[2].b = flat_model.clone() model.b.deactivate() model.B.deactivate() model.b.b.activate() model.B[1].b.activate() model.B[2].b.deactivate() assert model.b.active is False assert model.B[1].active is False assert model.B[1].active is False assert model.b.b.active is True assert model.B[1].b.active is True assert model.B[2].b.active is False
def obj_rule(m): return pyomo.summation(m.costs)
def obj_expression_cost(m): return summation(m.P1) * m.C[1] + summation(m.P2) * m.C[2]
def obj_rule(m): # Return sum of total costs over all cost types. # Simply calculates the sum of m.costs over all m.cost_types. return pyen.summation(m.costs)
def _generate_model(self): self.model = ConcreteModel() model = self.model model._name = self.description model.s = Set(initialize=[1,2]) model.x_unused = Var(within=Integers) model.x_unused.stale = False model.x_unused_initialy_stale = Var(within=Integers) model.x_unused_initialy_stale.stale = True model.X_unused = Var(model.s, within=Integers) model.X_unused_initialy_stale = Var(model.s, within=Integers) for i in model.s: model.X_unused[i].stale = False model.X_unused_initialy_stale[i].stale = True model.x = Var(within=IntegerInterval(bounds=(None,None))) model.x.stale = False model.x_initialy_stale = Var(within=Integers) model.x_initialy_stale.stale = True model.X = Var(model.s, within=Integers) model.X_initialy_stale = Var(model.s, within=Integers) for i in model.s: model.X[i].stale = False model.X_initialy_stale[i].stale = True model.obj = Objective(expr= model.x + \ model.x_initialy_stale + \ summation(model.X) + \ summation(model.X_initialy_stale)) model.c = ConstraintList() model.c.add( model.x >= 1 ) model.c.add( model.x_initialy_stale >= 1 ) model.c.add( model.X[1] >= 0 ) model.c.add( model.X[2] >= 1 ) model.c.add( model.X_initialy_stale[1] >= 0 ) model.c.add( model.X_initialy_stale[2] >= 1 ) # Test that stale flags do not get updated # on inactive blocks (where "inactive blocks" mean blocks # that do NOT follow a path of all active parent blocks # up to the top-level model) flat_model = model.clone() model.b = Block() model.B = Block(model.s) model.b.b = flat_model.clone() model.B[1].b = flat_model.clone() model.B[2].b = flat_model.clone() model.b.deactivate() model.B.deactivate() model.b.b.activate() model.B[1].b.activate() model.B[2].b.deactivate() assert model.b.active is False assert model.B[1].active is False assert model.B[1].active is False assert model.b.b.active is True assert model.B[1].b.active is True assert model.B[2].b.active is False