def test_Expr_if_quadratic(self): model = ConcreteModel() model.x = Var() rep = generate_canonical_repn( Expr_if(IF=1.0, THEN=model.x**2, ELSE=-1.0)) self.assertTrue(isinstance(rep, GeneralCanonicalRepn) == True) self.assertEqual(canonical_degree(rep), 2) rep = generate_canonical_repn( Expr_if(IF=0.0, THEN=1.0, ELSE=-model.x**2)) self.assertTrue(isinstance(rep, GeneralCanonicalRepn) == True) self.assertEqual(canonical_degree(rep), 2)
def test_Expr_if_nonlinear(self): model = ConcreteModel() model.x = Var() rep = generate_canonical_repn(Expr_if(IF=model.x, THEN=1.0, ELSE=-1.0)) self.assertTrue(isinstance(rep, GeneralCanonicalRepn) == True) self.assertEqual(canonical_degree(rep), None) rep = generate_canonical_repn( Expr_if(IF=1.0, THEN=Expr_if(IF=model.x**2, THEN=1.0, ELSE=-1.0), ELSE=-1.0)) self.assertTrue(isinstance(rep, GeneralCanonicalRepn) == True) self.assertEqual(canonical_degree(rep), None) rep = generate_canonical_repn( Expr_if(IF=model.x**2, THEN=1.0, ELSE=-1.0)) self.assertTrue(isinstance(rep, GeneralCanonicalRepn) == True) self.assertEqual(canonical_degree(rep), None)
def test_Expr_if_constant(self): model = ConcreteModel() model.x = Var() model.x.fix(2.0) rep = generate_canonical_repn(Expr_if(IF=model.x, THEN=1, ELSE=-1)) self.assertTrue(isinstance(rep, LinearCanonicalRepn) == True) self.assertTrue(rep.linear == None) self.assertTrue(rep.constant != None) self.assertTrue(rep.variables == None) baseline = {None: 1} self.assertEqual(baseline, linear_repn_to_dict(rep)) rep = generate_canonical_repn(Expr_if(IF=model.x**2, THEN=1, ELSE=-1)) self.assertTrue(isinstance(rep, LinearCanonicalRepn) == True) self.assertTrue(rep.linear == None) self.assertTrue(rep.constant != None) self.assertTrue(rep.variables == None) baseline = {None: 1} self.assertEqual(baseline, linear_repn_to_dict(rep)) rep = generate_canonical_repn( Expr_if(IF=(1 - cos(model.x - 1)) > 0.5, THEN=1, ELSE=-1)) self.assertTrue(isinstance(rep, LinearCanonicalRepn) == True) self.assertTrue(rep.linear == None) self.assertTrue(rep.constant != None) self.assertTrue(rep.variables == None) baseline = {None: -1} self.assertEqual(baseline, linear_repn_to_dict(rep)) rep = generate_canonical_repn(Expr_if(IF=1, THEN=model.x, ELSE=-1)) self.assertTrue(isinstance(rep, LinearCanonicalRepn) == True) self.assertTrue(rep.linear == None) self.assertTrue(rep.constant != None) self.assertTrue(rep.variables == None) baseline = {None: value(model.x)} self.assertEqual(baseline, linear_repn_to_dict(rep)) rep = generate_canonical_repn(Expr_if(IF=0, THEN=1, ELSE=model.x)) self.assertTrue(isinstance(rep, LinearCanonicalRepn) == True) self.assertTrue(rep.linear == None) self.assertTrue(rep.constant != None) self.assertTrue(rep.variables == None) baseline = {None: value(model.x)} self.assertEqual(baseline, linear_repn_to_dict(rep))
def test_Expr_if_linear(self): model = ConcreteModel() model.x = Var() model.y = Var() rep = generate_canonical_repn( Expr_if(IF=1, THEN=model.x + 3 * model.y + 10, ELSE=-1)) self.assertTrue(isinstance(rep, LinearCanonicalRepn) == True) self.assertTrue(rep.linear != None) self.assertTrue(rep.constant != None) self.assertTrue(rep.variables != None) baseline = {id(model.x): 1, id(model.y): 3, None: 10} self.assertEqual(baseline, linear_repn_to_dict(rep)) rep = generate_canonical_repn(Expr_if(IF=0.0, THEN=1.0, ELSE=-model.x)) self.assertTrue(isinstance(rep, LinearCanonicalRepn) == True) self.assertTrue(rep.linear != None) self.assertTrue(rep.constant == None) self.assertTrue(rep.variables != None) baseline = {id(model.x): -1} self.assertEqual(baseline, linear_repn_to_dict(rep))
model = ConcreteModel() model.vTrue = Var(initialize=1) model.vFalse = Var(initialize=-1) model.pTrue = Param(initialize=1) model.pFalse = Param(initialize=-1) model.vN1 = Var(initialize=-1) model.vP1 = Var(initialize=1) model.v0 = Var(initialize=0) model.vN2 = Var(initialize=-2) model.vP2 = Var(initialize=2) model.obj = Objective( expr=10.0 * Expr_if(IF=model.v0, THEN=model.vTrue, ELSE=model.vFalse)) # True/False model.c1 = Constraint( expr=Expr_if(IF=(0), THEN=(model.vTrue), ELSE=( model.vFalse)) == model.pFalse) model.c2 = Constraint( expr=Expr_if(IF=(1), THEN=(model.vTrue), ELSE=( model.vFalse)) == model.pTrue) # x <= 0 model.c3 = Constraint( expr=Expr_if(IF=(model.vN1 <= 0), THEN=(model.vTrue), ELSE=( model.vFalse)) == model.pTrue) model.c4 = Constraint( expr=Expr_if(IF=(model.v0 <= 0), THEN=(model.vTrue), ELSE=(
def f_rule(model): return sum(0.5*Expr_if(IF=abs(model.a*model.x[i]+model.b-model.y[i])>15, THEN=1.5*abs(model.a*model.x[i]+model.b-model.y[i])-0.5*1.5**2, ELSE=0.5*abs(model.a*model.x[i]+model.b-model.y[i])**2) \ for i in range(1,6))