def test_bad_bound_type(self): model = ConcreteModel() model.range = Var() model.x = Var(bounds=(-1,1)) args = (model.range,model.x) keywords = {'pw_pts':[-1,0,1],\ 'pw_constr_type':'EQ',\ 'f_rule':lambda model,x: x**2} model.con = Piecewise(*args,**keywords) try: keywords['pw_constr_type'] = 1.0 model.con1 = Piecewise(*args,**keywords) except Exception: pass else: self.fail("Piecewise should fail when initialized "\ "with invalid bound type.") try: del keywords['pw_constr_type'] model.con1 = Piecewise(*args,**keywords) except Exception: pass else: self.fail("Piecewise should fail when initialized "\ "with invalid bound type.")
def test_bad_var_args(self): model = ConcreteModel() model.range = Var() model.x = Var(bounds=(-1,1)) args = (model.range,model.x) keywords = {'pw_pts':[-1,0,1],\ 'pw_constr_type':'EQ',\ 'f_rule':lambda model,x: x**2} model.con = Piecewise(*args,**keywords) try: args = (None,model.x) model.con1 = Piecewise(*args,**keywords) except Exception: pass else: self.fail("Piecewise should fail when initialized "\ "without Pyomo vars as variable args.") try: args = (model.range,None) model.con2 = Piecewise(*args,**keywords) except Exception: pass else: self.fail("Piecewise should fail when initialized "\ "without Pyomo vars as variable args.")
def test_update_contset_indexed_component_piecewise_multiple(self): x = [0.0, 1.5, 3.0, 5.0] y = [1.1, -1.1, 2.0, 1.1] model = ConcreteModel() model.t = ContinuousSet(bounds=(0, 10)) model.s = Set(initialize=['A', 'B', 'C']) model.x = Var(model.s, model.t, bounds=(min(x), max(x))) model.y = Var(model.s, model.t) model.fx = Piecewise(model.s, model.t, model.y, model.x, pw_pts=x, pw_constr_type='EQ', f_rule=y) self.assertEqual(len(model.fx), 6) expansion_map = ComponentMap() generate_finite_elements(model.t, 5) update_contset_indexed_component(model.fx, expansion_map) self.assertEqual(len(model.fx), 18) self.assertEqual(len(model.fx['A', 2].SOS2_constraint), 3)
def test_activate_deactivate_indexed(self): model = ConcreteModel() model.s = Set(initialize=[1]) model.y = Var(model.s) model.x = Var(model.s,bounds=(-1,1)) args = ([1],model.y,model.x) keywords = {'pw_pts':{1:[-1,0,1]},\ 'pw_constr_type':'EQ',\ 'f_rule':lambda model,i,x: x**2} model.c = Piecewise(*args,**keywords) self.assertTrue(len(model.c[1].component_map(Constraint)) > 0) self.assertTrue(len(model.c[1].component_map(Constraint, active=True)) > 0) self.assertEqual(model.c.active, True) self.assertEqual(model.c[1].active, True) model.c[1].deactivate() self.assertTrue(len(model.c[1].component_map(Constraint)) > 0) self.assertTrue(len(model.c[1].component_map(Constraint, active=True)) > 0) self.assertEqual(model.c.active, True) self.assertEqual(model.c[1].active, False) model.c[1].activate() self.assertTrue(len(model.c[1].component_map(Constraint)) > 0) self.assertTrue(len(model.c[1].component_map(Constraint, active=True)) > 0) self.assertEqual(model.c.active, True) self.assertEqual(model.c[1].active, True) model.c.deactivate() self.assertTrue(len(model.c[1].component_map(Constraint)) > 0) self.assertTrue(len(model.c[1].component_map(Constraint, active=True)) > 0) self.assertEqual(model.c.active, False) self.assertEqual(model.c[1].active, False)
def define_model(**kwds): model = ConcreteModel() model.x = Var(INDEX_SET, bounds=(-5, 4)) # domain variable model.Fx = Var(INDEX_SET) # range variable model.p = Param(INDEX_SET, initialize=1.0) model.obj = Objective(expr=sum_product(model.Fx), sense=kwds.pop('sense', maximize)) model.piecewise = Piecewise(INDEX_SET, model.Fx, model.x, pw_pts=DOMAIN_PTS, f_rule=F, **kwds) #Fix the answer for testing purposes model.set_answer_constraint1 = Constraint(expr=model.x[1] == -5.0) model.set_answer_constraint2 = Constraint(expr=model.x[2] == -3.0) model.set_answer_constraint3 = Constraint(expr=model.x[3] == -2.5) model.set_answer_constraint4 = Constraint(expr=model.x[4] == -1.5) model.set_answer_constraint5 = Constraint(expr=model.x[5] == 2.0) model.set_answer_constraint6 = Constraint(expr=model.x[6] == 3.5) model.set_answer_constraint7 = Constraint(expr=model.x[7] == 4.0) return model
def test_indexed_with_nonindexed_vars(self): model = ConcreteModel() model.range1 = Var() model.x = Var(bounds=(-1,1)) args = ([1],model.range1,model.x) keywords = {'pw_pts':{1:[-1,0,1]},\ 'pw_constr_type':'EQ',\ 'f_rule':lambda model,i,x: x**2} model.con1 = Piecewise(*args,**keywords) model.range2 = Var([1]) model.y = Var([1],bounds=(-1,1)) args = ([1],model.range2,model.y) model.con2 = Piecewise(*args,**keywords) args = ([1],model.range2,model.y[1]) model.con3 = Piecewise(*args,**keywords)
def test_bad_args_count(self): model = ConcreteModel() model.range = Var() model.x = Var(bounds=(-1,1)) args = (model.range,model.x) keywords = {'pw_pts':[-1,0,1],\ 'pw_constr_type':'EQ',\ 'f_rule':lambda model,x: x**2} model.con = Piecewise(*args,**keywords) try: args = (model.range,) model.con1 = Piecewise(*args,**keywords) except Exception: pass else: self.fail("Piecewise should fail when initialized "\ "with less than two arguments.")
def test_nonindexed_with_indexed_vars(self): model = ConcreteModel() model.range = Var([1]) model.x = Var([1],bounds=(-1,1)) args = (model.range[1],model.x[1]) keywords = {'pw_pts':[-1,0,1],\ 'pw_constr_type':'EQ',\ 'f_rule':lambda model,x: x**2} model.con1 = Piecewise(*args,**keywords)
def test_unsorted_pw_pts(self): model = ConcreteModel() model.range = Var() model.x = Var(bounds=(-1,1)) args = (model.range,model.x) keywords = {'pw_pts':[-1,0,1],\ 'pw_constr_type':'EQ',\ 'f_rule':lambda model,x: x**2} model.con = Piecewise(*args,**keywords) try: keywords['pw_pts'] = [0,-1,1] model.con3 = Piecewise(*args,**keywords) except Exception: pass else: self.fail("Piecewise should fail when initialized "\ "with unsorted domain points.")
def test_concrete_piecewise(self): model = ConcreteModel() model.range = Var() model.x = Var(bounds=(-1,1)) args = (model.range,model.x) keywords = {'pw_pts':[-1,0,1],\ 'pw_constr_type':'EQ',\ 'f_rule':lambda model,x: x**2} model.con = Piecewise(*args,**keywords)
def test_unbounded_var(self): model = ConcreteModel() model.range = Var() model.x = Var(bounds=(-1,1)) args = (model.range,model.x) keywords = {'pw_pts':[-1,0,1],\ 'pw_constr_type':'EQ',\ 'f_rule':lambda model,x: x**2} model.con = Piecewise(*args,**keywords) try: model.x.setlb(None) model.x.setub(None) model.con1 = Piecewise(*args,**keywords) except Exception: pass else: self.fail("Piecewise should fail when initialized "\ "with unbounded domain variable.") # Check that the unbounded_domain_var keyword works model.con1 = Piecewise(unbounded_domain_var=True,*args,**keywords) model.y = Var(bounds=(0,None)) try: args = (model.range,model.y) model.con2 = Piecewise(*args,**keywords) except Exception: pass else: self.fail("Piecewise should fail when initialized "\ "with unbounded domain variable.") model.z = Var(bounds=(None,10)) try: args = (model.range,model.z) model.con3 = Piecewise(*args,**keywords) except Exception: pass else: self.fail("Piecewise should fail when initialized "\ "with unbounded domain variable.")
def test_None_key(self): model = ConcreteModel() model.range = Var() model.x = Var(bounds=(-1,1)) args = (model.range,model.x) keywords = {'pw_pts':[-1,0,1],\ 'pw_constr_type':'EQ',\ 'f_rule':lambda model,x: x**2} model.con = Piecewise(*args,**keywords) self.assertEqual(id(model.con), id(model.con[None]))
def test_abstract_piecewise(self): model = AbstractModel() model.range = Var() model.x = Var(bounds=(-1,1)) args = (model.range,model.x) keywords = {'pw_pts':[-1,0,1],\ 'pw_constr_type':'EQ',\ 'f_rule':lambda model,x: x**2} model.con = Piecewise(*args,**keywords) instance = model.create_instance()
def test_log_bad_length(self): model = ConcreteModel() model.range = Var() model.x = Var(bounds=(-1,1)) args = (model.range,model.x) keywords = {'pw_pts':[-1,0,1],\ 'pw_constr_type':'EQ',\ 'pw_repn':'LOG',\ 'f_rule':lambda model,x: x**2} model.con = Piecewise(*args,**keywords) try: keywords['pw_pts'] = [-1,0,0.5,1] model.con3 = Piecewise(*args,**keywords) except Exception: pass else: self.fail("Piecewise should fail when initialized "\ "with LOG an pw_pts list with length not "\ "equal to (2^n)+1.")
def create_model(pw_repn, a): min_x, max_x = -10, 10 m = ConcreteModel() m.c = Var() m.x = Var(bounds=(min_x, max_x)) m.y = Var() m.pw = Piecewise(m.y, m.x, pw_repn=pw_repn, pw_constr_type='EQ', pw_pts=[min_x, 0, 1, max_x], f_rule=lambda m, x: pw(x)) m.constr1 = Constraint(expr=m.x == a) m.constr2 = Constraint(expr=m.c == 23) m.o = Objective(expr=m.c) return m
amodel.periods, rule=enforce_generator_output_limits_rule_part_a) amodel.EnforceGeneratorOutputLimitsPartB = Constraint( amodel.generator, amodel.periods, rule=enforce_generator_output_limits_rule_part_b) amodel.EnforceGeneratorOutputLimitsPartC = Constraint( amodel.generator, amodel.periods, rule=enforce_generator_output_limits_rule_part_c) #amodel.EnforceMaxAvailableRampUpRates = Constraint(amodel.generator, amodel.periods, rule=enforce_max_available_ramp_up_rates_rule) #amodel.EnforceMaxAvailableRampDownRates = Constraint(amodel.generator, amodel.periods, rule=enforce_max_available_ramp_down_rates_rule) amodel.ComputeProductionCosts = Piecewise(amodel.generator * amodel.periods, amodel.PowerGenerated, amodel.ProductionCost, pw_pts=amodel.createpoints, f_rule=production_cost_function, pw_constr_type='UB') #amodel.ComputeProductionCosts = Constraint(amodel.generator, amodel.periods, rule=production_cost_function) #%% total cost amodel.ComputeTotalProductionCost = Constraint( rule=compute_total_production_cost_rule) amodel.ComputeShutdownCosts = Constraint(amodel.generator, amodel.periods, rule=compute_shutdown_costs_rule) amodel.ComputeStartupCosts = Constraint(amodel.generator, amodel.periods, rule=compute_startup_costs_rule) amodel.ComputeTotalFixedCost = Constraint(rule=compute_total_fixed_cost_rule) amodel.EnforceUpTimeConstraintsInitial = Constraint(
def build_model(self): if self.bid_points is None: self.is_pwl = False self.constant_term = self.polynomial[0] if self.is_linear: return polynomial = list(self.polynomial) # use constant term in place of the 0th order term polynomial[0] = 0 self.add_variable('cost', index=self.times.set, low=0) def pw_rule(model, time, input_var): return polynomial_value(polynomial, input_var) self.discrete_input_points = discretize_range( self.num_breakpoints, self.min_input, self.max_input) in_pts = dict( (t, self.discrete_input_points) for t in self.times.set) pw_representation = Piecewise( self.times.set, self.get_variable('cost', time=None, indexed=True), self.input_variable(), f_rule=pw_rule, pw_pts=in_pts, pw_constr_type='LB', warn_domain_coverage=False, # unless warn_domain_coverage is set, pyomo will complain # gen lower power bounds are set to zero (status trick) # and Piecewise complains if Pmin>0, ) else: # custom bid points self.is_linear = False self.is_pwl = True self.add_variable('cost', index=self.times.set, low=0) self.discrete_input_points = self.bid_points.power.values.tolist() in_pts = dict( (t, self.discrete_input_points) for t in self.times.set) mapping = self.bid_points.set_index('power').to_dict()['cost'] def pw_rule_points(model, time, input_var): # just the input->output points mapping in this case # see coopr/examples/pyomo/piecewise/example3.py return mapping[input_var] pw_representation = Piecewise(self.times.set, self.get_variable( 'cost', time=None, indexed=True), self.input_variable(), pw_pts=in_pts, pw_constr_type='LB', pw_repn='DCC', # the disagregated convex combination method f_rule=pw_rule_points, warn_domain_coverage=False) pw_representation.name = self.iden() self.max_output = pw_representation._f_rule(None, None, self.max_input) self._parent_problem().add_component_to_problem(pw_representation)