def test_expand_multiple_indexed(self): m = ConcreteModel() m.x = Var([1, 2], domain=Binary) m.y = Var(bounds=(1, 3)) m.CON = Connector() m.CON.add(m.x) m.CON.add(m.y) m.a1 = Var([1, 2]) m.a2 = Var([1, 2]) m.b1 = Var() m.b2 = Var() m.ECON2 = Connector() m.ECON2.add(m.a1, 'x') m.ECON2.add(m.b1, 'y') m.ECON1 = Connector() m.ECON1.add(m.a2, 'x') m.ECON1.add(m.b2, 'y') # 2 constraints: one has a connector, the other doesn't. The # former should be deactivated and expanded, the latter should # be left untouched. m.c = Constraint(expr=m.CON == m.ECON1) m.d = Constraint(expr=m.ECON2 == m.ECON1) m.nocon = Constraint(expr=m.x[1] == 2) self.assertEqual(len(list(m.component_objects(Constraint))), 3) self.assertEqual(len(list(m.component_data_objects(Constraint))), 3) TransformationFactory('core.expand_connectors').apply_to(m) #m.pprint() self.assertEqual(len(list(m.component_objects(Constraint))), 5) self.assertEqual(len(list(m.component_data_objects(Constraint))), 9) self.assertTrue(m.nocon.active) self.assertFalse(m.c.active) self.assertTrue(m.component('c.expanded').active) self.assertFalse(m.d.active) self.assertTrue(m.component('d.expanded').active) os = StringIO() m.component('c.expanded').pprint(ostream=os) self.assertEqual( os.getvalue(), """c.expanded : Size=3, Index='c.expanded_index', Active=True Key : Lower : Body : Upper : Active 1 : 0.0 : x[1] - a2[1] : 0.0 : True 2 : 0.0 : x[2] - a2[2] : 0.0 : True 3 : 0.0 : y - b2 : 0.0 : True """) os = StringIO() m.component('d.expanded').pprint(ostream=os) self.assertEqual( os.getvalue(), """d.expanded : Size=3, Index='d.expanded_index', Active=True Key : Lower : Body : Upper : Active 1 : 0.0 : a1[1] - a2[1] : 0.0 : True 2 : 0.0 : a1[2] - a2[2] : 0.0 : True 3 : 0.0 : b1 - b2 : 0.0 : True """)
def test_expand_empty_expression(self): m = ConcreteModel() m.x = Var() m.y = Var() m.CON = Connector() m.CON.add(-m.x, 'x') m.CON.add(1 + m.y, 'y') m.ECON = Connector() # 2 constraints: one has a connector, the other doesn't. The # former should be deactivated and expanded, the latter should # be left untouched. m.c = Constraint(expr=m.CON == m.ECON) m.nocon = Constraint(expr=m.x == 2) self.assertEqual(len(list(m.component_objects(Constraint))), 2) self.assertEqual(len(list(m.component_data_objects(Constraint))), 2) TransformationFactory('core.expand_connectors').apply_to(m) self.assertEqual(len(list(m.component_objects(Constraint))), 3) self.assertEqual(len(list(m.component_data_objects(Constraint))), 4) self.assertTrue(m.nocon.active) self.assertFalse(m.c.active) self.assertTrue(m.component('c.expanded').active) os = StringIO() m.component('c.expanded').pprint(ostream=os) self.assertEqual( os.getvalue(), """c.expanded : Size=2, Index='c.expanded_index', Active=True Key : Lower : Body : Upper : Active 1 : 0.0 : - x - 'ECON.auto.x' : 0.0 : True 2 : 0.0 : 1 + y - 'ECON.auto.y' : 0.0 : True """)
def test_expand_indexed(self): m = ConcreteModel() m.x = Var([1, 2]) m.y = Var() m.CON = Connector() m.CON.add(m.x) m.CON.add(m.y) # 2 constraints: one has a connector, the other doesn't. The # former should be deactivated and expanded, the latter should # be left untouched. m.c = Constraint(expr=m.CON == 1) m.nocon = Constraint(expr=m.x[1] == 2) self.assertEqual(len(list(m.component_objects(Constraint))), 2) self.assertEqual(len(list(m.component_data_objects(Constraint))), 2) TransformationFactory('core.expand_connectors').apply_to(m) self.assertEqual(len(list(m.component_objects(Constraint))), 3) self.assertEqual(len(list(m.component_data_objects(Constraint))), 5) self.assertTrue(m.nocon.active) self.assertFalse(m.c.active) self.assertTrue(m.component('c.expanded').active) os = StringIO() m.component('c.expanded').pprint(ostream=os) self.assertEqual( os.getvalue(), """c.expanded : Size=3, Index='c.expanded_index', Active=True Key : Lower : Body : Upper : Active 1 : 1.0 : x[1] : 1.0 : True 2 : 1.0 : x[2] : 1.0 : True 3 : 1.0 : y : 1.0 : True """)
def test_expand_empty_indexed(self): m = ConcreteModel() m.x = Var([1, 2], domain=Binary) m.y = Var(bounds=(1, 3)) m.CON = Connector() m.CON.add(m.x) m.CON.add(m.y) m.ECON = Connector() # 2 constraints: one has a connector, the other doesn't. The # former should be deactivated and expanded, the latter should # be left untouched. m.c = Constraint(expr=m.CON == m.ECON) m.nocon = Constraint(expr=m.x[1] == 2) self.assertEqual(len(list(m.component_objects(Constraint))), 2) self.assertEqual(len(list(m.component_data_objects(Constraint))), 2) TransformationFactory('core.expand_connectors').apply_to(m) #m.pprint() self.assertEqual(len(list(m.component_objects(Constraint))), 3) self.assertEqual(len(list(m.component_data_objects(Constraint))), 5) self.assertTrue(m.nocon.active) self.assertFalse(m.c.active) self.assertTrue(m.component('c.expanded').active) self.assertIs(m.x[1].domain, m.component('ECON.auto.x')[1].domain) self.assertIs(m.x[2].domain, m.component('ECON.auto.x')[2].domain) self.assertIs(m.y.domain, m.component('ECON.auto.y').domain) self.assertEqual(m.x[1].bounds, m.component('ECON.auto.x')[1].bounds) self.assertEqual(m.x[2].bounds, m.component('ECON.auto.x')[2].bounds) self.assertEqual(m.y.bounds, m.component('ECON.auto.y').bounds) os = StringIO() m.component('c.expanded').pprint(ostream=os) self.assertEqual( os.getvalue(), """c.expanded : Size=3, Index='c.expanded_index', Active=True Key : Lower : Body : Upper : Active 1 : 0.0 : x[1] - 'ECON.auto.x'[1] : 0.0 : True 2 : 0.0 : x[2] - 'ECON.auto.x'[2] : 0.0 : True 3 : 0.0 : y - 'ECON.auto.y' : 0.0 : True """)
def test_varlist_aggregator(self): m = ConcreteModel() m.flow = VarList() m.phase = Var(bounds=(1, 3)) m.CON = Connector() m.CON.add(m.flow, aggregate=lambda m, v: sum(v[i] for i in v) == 0) m.CON.add(m.phase) m.ECON2 = Connector() m.ECON1 = Connector() # 2 constraints: one has a connector, the other doesn't. The # former should be deactivated and expanded, the latter should # be left untouched. m.c = Constraint(expr=m.CON == m.ECON1) m.d = Constraint(expr=m.ECON2 == m.CON) self.assertEqual(len(list(m.component_objects(Constraint))), 2) self.assertEqual(len(list(m.component_data_objects(Constraint))), 2) TransformationFactory('core.expand_connectors').apply_to(m) #m.pprint() self.assertEqual(len(list(m.component_objects(Constraint))), 5) self.assertEqual(len(list(m.component_data_objects(Constraint))), 7) self.assertFalse(m.c.active) self.assertTrue(m.component('c.expanded').active) self.assertFalse(m.d.active) self.assertTrue(m.component('d.expanded').active) self.assertEqual(len(m.flow), 2) os = StringIO() m.component('c.expanded').pprint(ostream=os) self.assertEqual( os.getvalue(), """c.expanded : Size=2, Index='c.expanded_index', Active=True Key : Lower : Body : Upper : Active 1 : 0.0 : flow[1] - 'ECON1.auto.flow' : 0.0 : True 2 : 0.0 : phase - 'ECON1.auto.phase' : 0.0 : True """) os = StringIO() m.component('d.expanded').pprint(ostream=os) self.assertEqual( os.getvalue(), """d.expanded : Size=2, Index='d.expanded_index', Active=True Key : Lower : Body : Upper : Active 1 : 0.0 : 'ECON2.auto.flow' - flow[2] : 0.0 : True 2 : 0.0 : 'ECON2.auto.phase' - phase : 0.0 : True """) os = StringIO() m.component('CON.flow.aggregate').pprint(ostream=os) self.assertEqual( os.getvalue(), """CON.flow.aggregate : Size=1, Index=None, Active=True Key : Lower : Body : Upper : Active None : 0.0 : flow[1] + flow[2] : 0.0 : True """) os = StringIO() m.CON.pprint(ostream=os) self.assertEqual( os.getvalue(), """CON : Size=1, Index=None Key : Name : Size : Variable None : flow : * : flow : phase : 1 : phase """)