def test_getname_relativeto(self): m = block() m.b = block() m.b.v = variable() self.assertEqual(m.b.v.getname(fully_qualified=True), 'b.v') self.assertEqual(m.b.v.getname(fully_qualified=True, relative_to=m.b), 'v')
def test_active(self): s = suffix() with self.assertRaises(AttributeError): s.active = False model = block() model.s = s b = block() b.model = model bdict = block_dict() bdict[0] = b bdict[None] = block() m = block() m.bdict = bdict self.assertEqual(m.active, True) self.assertEqual(bdict.active, True) self.assertEqual(bdict[None].active, True) self.assertEqual(b.active, True) self.assertEqual(model.active, True) self.assertEqual(s.active, True) m.deactivate(shallow=False, descend_into=True) self.assertEqual(m.active, False) self.assertEqual(bdict.active, False) self.assertEqual(bdict[None].active, False) self.assertEqual(b.active, False) self.assertEqual(model.active, False) self.assertEqual(s.active, False) m.activate(shallow=False, descend_into=True) self.assertEqual(m.active, True) self.assertEqual(bdict.active, True) self.assertEqual(bdict[None].active, True) self.assertEqual(b.active, True) self.assertEqual(model.active, True) self.assertEqual(s.active, True) m.deactivate(shallow=True, descend_into=True) self.assertEqual(m.active, False) self.assertEqual(bdict.active, True) self.assertEqual(bdict[None].active, False) self.assertEqual(b.active, False) self.assertEqual(model.active, False) self.assertEqual(s.active, True) m.activate(shallow=True, descend_into=True) self.assertEqual(m.active, True) self.assertEqual(bdict.active, True) self.assertEqual(bdict[None].active, True) self.assertEqual(b.active, True) self.assertEqual(model.active, True) self.assertEqual(s.active, True)
def test_import_suffix_generator(self): m = block() m.s0 = suffix(direction=suffix.LOCAL) m.s0i = suffix(direction=suffix.LOCAL, datatype=suffix.INT) m.s1 = suffix(direction=suffix.IMPORT_EXPORT) m.s1i = suffix(direction=suffix.IMPORT_EXPORT, datatype=suffix.INT) m.s2 = suffix(direction=suffix.IMPORT) m.s2i = suffix(direction=suffix.IMPORT, datatype=suffix.INT) m.s3 = suffix(direction=suffix.EXPORT) m.s3i = suffix(direction=suffix.EXPORT, datatype=suffix.INT) m.b = block() m.b.s0 = suffix(direction=suffix.LOCAL) m.b.s0i = suffix(direction=suffix.LOCAL, datatype=suffix.INT) m.b.s1 = suffix(direction=suffix.IMPORT_EXPORT) m.b.s1i = suffix(direction=suffix.IMPORT_EXPORT, datatype=suffix.INT) m.b.s2 = suffix(direction=suffix.IMPORT) m.b.s2i = suffix(direction=suffix.IMPORT, datatype=suffix.INT) m.b.s3 = suffix(direction=suffix.EXPORT) m.b.s3i = suffix(direction=suffix.EXPORT, datatype=suffix.INT) # default self.assertEqual([id(c_) for c_ in import_suffix_generator(m)], [id(m.s1), id(m.s1i), id(m.s2), id(m.s2i), id(m.b.s1), id(m.b.s1i), id(m.b.s2), id(m.b.s2i)]) # descend_into=False self.assertEqual([id(c_) for c_ in import_suffix_generator(m, descend_into=False)], [id(m.s1), id(m.s1i), id(m.s2), id(m.s2i)]) # datatype=INT self.assertEqual([id(c_) for c_ in import_suffix_generator(m, datatype=suffix.INT)], [id(m.s1i), id(m.s2i), id(m.b.s1i), id(m.b.s2i)]) # active=True m.s1.deactivate() m.b.deactivate() self.assertEqual([id(c_) for c_ in import_suffix_generator(m, active=True)], [id(m.s1i), id(m.s2), id(m.s2i)])
def test_local_suffix_generator(self): m = block() m.s0 = suffix(direction=suffix.LOCAL) m.s0i = suffix(direction=suffix.LOCAL, datatype=suffix.INT) m.s1 = suffix(direction=suffix.IMPORT_EXPORT) m.s1i = suffix(direction=suffix.IMPORT_EXPORT, datatype=suffix.INT) m.s2 = suffix(direction=suffix.IMPORT) m.s2i = suffix(direction=suffix.IMPORT, datatype=suffix.INT) m.s3 = suffix(direction=suffix.EXPORT) m.s3i = suffix(direction=suffix.EXPORT, datatype=suffix.INT) m.b = block() m.b.s0 = suffix(direction=suffix.LOCAL) m.b.s0i = suffix(direction=suffix.LOCAL, datatype=suffix.INT) m.b.s1 = suffix(direction=suffix.IMPORT_EXPORT) m.b.s1i = suffix(direction=suffix.IMPORT_EXPORT, datatype=suffix.INT) m.b.s2 = suffix(direction=suffix.IMPORT) m.b.s2i = suffix(direction=suffix.IMPORT, datatype=suffix.INT) m.b.s3 = suffix(direction=suffix.EXPORT) m.b.s3i = suffix(direction=suffix.EXPORT, datatype=suffix.INT) # default self.assertEqual( [(ck_, c_.name) for ck_, c_ in local_suffix_generator(m, return_key=True)], [("s0", m.s0.name), ("s0i", m.s0i.name), ("s0", m.b.s0.name), ("s0i", m.b.s0i.name)]) self.assertEqual( [id(c_) for c_ in local_suffix_generator(m, return_key=False)], [id(m.s0), id(m.s0i), id(m.b.s0), id(m.b.s0i)]) # descend_into=False self.assertEqual([(ck_, c_.name) for ck_, c_ in local_suffix_generator( m, descend_into=False, return_key=True)], [("s0", m.s0.name), ("s0i", m.s0i.name)]) self.assertEqual([ id(c_) for c_ in local_suffix_generator( m, descend_into=False, return_key=False) ], [id(m.s0), id(m.s0i)]) # datatype=INT self.assertEqual([(ck_, c_.name) for ck_, c_ in local_suffix_generator( m, datatype=suffix.INT, return_key=True)], [("s0i", m.s0i.name), ("s0i", m.b.s0i.name)]) self.assertEqual([ id(c_) for c_ in local_suffix_generator( m, datatype=suffix.INT, return_key=False) ], [id(m.s0i), id(m.b.s0i)]) # active=True m.s0.deactivate() m.b.deactivate() self.assertEqual([(ck_, c_.name) for ck_, c_ in local_suffix_generator( m, active=True, return_key=True)], [("s0i", m.s0i.name)]) self.assertEqual([ id(c_) for c_ in local_suffix_generator(m, active=True, return_key=False) ], [id(m.s0i)])
def test_preorder_visit(self): # test that we can use the advanced preorder_visit # function on a block to efficiently check for these # constraint containers (without iterating over # their children) A = numpy.ones((3, 3)) m = block() m.c = matrix_constraint(A) m.B = block_list() m.B.append(tiny_block()) m.B[0].c = matrix_constraint(A) m.b = block() m.b.c = constraint_dict() m.b.c[None] = matrix_constraint(A) m.b.c[1] = matrix_constraint(A) m.b.c[2] = constraint() # don't visit things below a matrix constraint # (e.g., cases where we want to handle it in bulk) def visit_mc(x): visit_mc.count += 1 if isinstance(x, matrix_constraint): return False return True visit_mc.count = 0 def visit_all(x): visit_all.count += 1 return True visit_all.count = 0 m.preorder_visit(visit_mc, ctype=matrix_constraint.ctype, include_all_parents=False) self.assertEqual(visit_mc.count, 6) visit_mc.count = 0 m.preorder_visit(visit_all, ctype=matrix_constraint.ctype, include_all_parents=False) # number of matrix constraints (4) # + number of plain constraints (4*3 + 1) # + number of other constraint containers (1) self.assertEqual(visit_all.count, 18) self.assertEqual(len(list(m.components(ctype=constraint.ctype))), 13)
def test_pprint(self): # Not really testing what the output is, just that # an error does not occur. The pprint functionality # is still in the early stages. p = parameter() pyomo.core.kernel.pprint(p) b = block() b.p = p pyomo.core.kernel.pprint(p) pyomo.core.kernel.pprint(b) m = block() m.b = b pyomo.core.kernel.pprint(p) pyomo.core.kernel.pprint(b) pyomo.core.kernel.pprint(m)
def test_pprint(self): # Not really testing what the output is, just that # an error does not occur. The pprint functionality # is still in the early stages. cdict = self._container_type({None: self._ctype_factory()}) pyomo.core.kernel.pprint(cdict) b = block() b.cdict = cdict pyomo.core.kernel.pprint(cdict) pyomo.core.kernel.pprint(b) m = block() m.b = b pyomo.core.kernel.pprint(cdict) pyomo.core.kernel.pprint(b) pyomo.core.kernel.pprint(m)
def test_pprint(self): # Not really testing what the output is, just that # an error does not occur. The pprint functionality # is still in the early stages. vlist = variable_list([variable(), variable()]) s = sos(vlist) pyomo.core.kernel.pprint(s) b = block() b.s = s pyomo.core.kernel.pprint(s) pyomo.core.kernel.pprint(b) m = block() m.b = b pyomo.core.kernel.pprint(s) pyomo.core.kernel.pprint(b) pyomo.core.kernel.pprint(m)
def test_pickle(self): v = variable(lb=1, ub=2, domain_type=IntegerSet, fixed=True) self.assertEqual(v.lb, 1) self.assertEqual(type(v.lb), int) self.assertEqual(v.ub, 2) self.assertEqual(type(v.ub), int) self.assertEqual(v.domain_type, IntegerSet) self.assertEqual(v.fixed, True) self.assertEqual(v.parent, None) vup = pickle.loads( pickle.dumps(v)) self.assertEqual(vup.lb, 1) self.assertEqual(type(vup.lb), int) self.assertEqual(vup.ub, 2) self.assertEqual(type(vup.ub), int) self.assertEqual(vup.domain_type, IntegerSet) self.assertEqual(vup.fixed, True) self.assertEqual(vup.parent, None) b = block() b.v = v self.assertIs(v.parent, b) bup = pickle.loads( pickle.dumps(b)) vup = bup.v self.assertEqual(vup.lb, 1) self.assertEqual(vup.ub, 2) self.assertEqual(vup.domain_type, IntegerSet) self.assertEqual(vup.fixed, True) self.assertIs(vup.parent, bup)
def test_pprint(self): import pyomo.kernel # Not really testing what the output is, just that # an error does not occur. The pprint functionality # is still in the early stages. v = variable() pyomo.kernel.pprint(v) b = block() b.v = v pyomo.kernel.pprint(v) pyomo.kernel.pprint(b) m = block() m.b = b pyomo.kernel.pprint(v) pyomo.kernel.pprint(b) pyomo.kernel.pprint(m)
def test_pickle(self): for key in transforms.registered_transforms: v = variable(lb=1,ub=3) p = transforms.piecewise([1,2,3], [1,2,1], input=v, validate=False, repn=key) self.assertEqual(p.parent, None) self.assertEqual(p.input.expr.parent, None) self.assertIs(p.input.expr, v) pup = pickle.loads( pickle.dumps(p)) self.assertEqual(pup.parent, None) self.assertEqual(pup.input.expr.parent, None) self.assertIsNot(pup.input.expr, v) b = block() b.v = v b.p = p self.assertIs(p.parent, b) self.assertEqual(p.input.expr.parent, b) bup = pickle.loads( pickle.dumps(b)) pup = bup.p self.assertIs(pup.parent, bup) self.assertEqual(pup.input.expr.parent, bup) self.assertIs(pup.input.expr, bup.v) self.assertIsNot(pup.input.expr, b.v)
def test_active(self): o = objective() self.assertEqual(o.active, True) o.deactivate() self.assertEqual(o.active, False) o.activate() self.assertEqual(o.active, True) b = block() self.assertEqual(b.active, True) b.deactivate() self.assertEqual(b.active, False) b.o = o self.assertEqual(o.active, True) self.assertEqual(b.active, False) o.deactivate() self.assertEqual(o.active, False) self.assertEqual(b.active, False) b.activate() self.assertEqual(o.active, False) self.assertEqual(b.active, True) b.activate(shallow=False) self.assertEqual(o.active, True) self.assertEqual(b.active, True) b.deactivate(shallow=False) self.assertEqual(o.active, False) self.assertEqual(b.active, False)
def test_active(self): s = sos([]) self.assertEqual(s.active, True) s.deactivate() self.assertEqual(s.active, False) s.activate() self.assertEqual(s.active, True) b = block() self.assertEqual(b.active, True) b.deactivate() self.assertEqual(b.active, False) b.s = s self.assertEqual(s.active, True) self.assertEqual(b.active, False) s.deactivate() self.assertEqual(s.active, False) self.assertEqual(b.active, False) b.activate() self.assertEqual(s.active, False) self.assertEqual(b.active, True) b.activate(shallow=False) self.assertEqual(s.active, True) self.assertEqual(b.active, True) b.deactivate(shallow=False) self.assertEqual(s.active, False) self.assertEqual(b.active, False)
def test_pickle(self): v = variable() e = noclone(v) self.assertEqual(type(e), noclone) self.assertIs(type(e.expr), variable) eup = pickle.loads(pickle.dumps(e)) self.assertEqual(type(eup), noclone) self.assertTrue(e is not eup) self.assertIs(type(eup.expr), variable) self.assertIs(type(e.expr), variable) self.assertTrue(eup.expr is not e.expr) del e del v v = variable(value=1) b = block() b.v = v eraw = b.v + 1 b.e = 1 + noclone(eraw) bup = pickle.loads(pickle.dumps(b)) self.assertTrue(isinstance(bup.e, NumericValue)) self.assertEqual(value(bup.e), 3.0) b.v.value = 2 self.assertEqual(value(b.e), 4.0) self.assertEqual(value(bup.e), 3.0) bup.v.value = -1 self.assertEqual(value(b.e), 4.0) self.assertEqual(value(bup.e), 1.0) self.assertIs(b.v.parent, b) self.assertIs(bup.v.parent, bup) del b.v
def test_pickle(self): s = suffix(direction=suffix.EXPORT, datatype=suffix.FLOAT) self.assertEqual(s.direction, suffix.EXPORT) self.assertEqual(s.datatype, suffix.FLOAT) self.assertEqual(s.parent, None) sup = pickle.loads( pickle.dumps(s)) self.assertEqual(sup.direction, suffix.EXPORT) self.assertEqual(sup.datatype, suffix.FLOAT) self.assertEqual(sup.parent, None) b = block() b.s = s self.assertIs(s.parent, b) bup = pickle.loads( pickle.dumps(b)) sup = bup.s self.assertEqual(sup.direction, suffix.EXPORT) self.assertEqual(sup.datatype, suffix.FLOAT) self.assertIs(sup.parent, bup) b.v = variable(lb=1) b.s[b.v] = 1.0 bup = pickle.loads( pickle.dumps(b)) sup = bup.s vup = bup.v self.assertEqual(sup[vup], 1.0)
def test_pickle(self): vlist = create_variable_list(3) ctuple = matrix_constraint( numpy.array([[0,0,0],[0,0,0]]), x=vlist) self.assertTrue((ctuple.lb == -numpy.inf).all()) self.assertTrue((ctuple.ub == numpy.inf).all()) self.assertTrue((ctuple.equality == False).all()) self.assertEqual(ctuple.parent, None) ctuple_up = pickle.loads( pickle.dumps(ctuple)) self.assertTrue((ctuple_up.lb == -numpy.inf).all()) self.assertTrue((ctuple_up.ub == numpy.inf).all()) self.assertTrue((ctuple_up.equality == False).all()) self.assertEqual(ctuple_up.parent, None) b = block() b.ctuple = ctuple self.assertIs(ctuple.parent, b) bup = pickle.loads( pickle.dumps(b)) ctuple_up = bup.ctuple self.assertTrue((ctuple_up.lb == -numpy.inf).all()) self.assertTrue((ctuple_up.ub == numpy.inf).all()) self.assertTrue((ctuple_up.equality == False).all()) self.assertIs(ctuple_up.parent, bup)
def test_to_string(self): b = block() p = parameter() e = noclone(p**2) self.assertEqual(str(e.expr), "<parameter>**2.0") self.assertEqual(str(e), "{<parameter>**2.0}") e.to_string() out = StringIO() e.to_string(ostream=out) self.assertEqual(out.getvalue(), "<parameter>**2.0") e.to_string(verbose=False) out = StringIO() e.to_string(ostream=out, verbose=False) self.assertEqual(out.getvalue(), "<parameter>**2.0") e.to_string(verbose=True) out = StringIO() e.to_string(ostream=out, verbose=True) self.assertEqual(out.getvalue(), "{pow( <parameter> , 2.0 )}") b.e = e b.p = p self.assertNotEqual(p.name, None) e.to_string(verbose=True) out = StringIO() e.to_string(ostream=out, verbose=True) self.assertEqual(out.getvalue(), "{pow( " + p.name + " , 2.0 )}") self.assertEqual(out.getvalue(), "{pow( p , 2.0 )}") del b.e del b.p
def test_pprint(self): # Not really testing what the output is, just that # an error does not occur. The pprint functionality # is still in the early stages. v = variable() o = objective(expr=v**2) pyomo.core.kernel.pprint(o) b = block() b.o = o pyomo.core.kernel.pprint(o) pyomo.core.kernel.pprint(b) m = block() m.b = b pyomo.core.kernel.pprint(o) pyomo.core.kernel.pprint(b) pyomo.core.kernel.pprint(m)
def test_init(self): v = variable() self.assertTrue(v.parent is None) self.assertEqual(v.ctype, Var) self.assertEqual(v.domain_type, RealSet) self.assertEqual(v.lb, None) self.assertEqual(v.ub, None) self.assertEqual(v.fixed, False) self.assertEqual(v.value, None) self.assertEqual(v.stale, True) b = block() b.v = v self.assertTrue(v.parent is b) del b.v self.assertTrue(v.parent is None) v = variable(domain_type=IntegerSet, value=1, lb=0, ub=2, fixed=True) self.assertTrue(v.parent is None) self.assertEqual(v.ctype, Var) self.assertEqual(v.domain_type, IntegerSet) self.assertEqual(v.lb, 0) self.assertEqual(v.ub, 2) self.assertEqual(v.fixed, True) self.assertEqual(v.value, 1) self.assertEqual(v.stale, True)
def test_pickle(self): v = variable() s = sos([v], weights=[1]) self.assertEqual(len(s), 1) self.assertIs(s.variables[0], v) self.assertTrue(v in s) self.assertEqual(s.weights[0], 1) self.assertEqual(s.level, 1) self.assertEqual(s.parent, None) sup = pickle.loads(pickle.dumps(s)) self.assertEqual(len(sup), 1) self.assertIsNot(sup.variables[0], v) self.assertFalse(v in sup) self.assertEqual(sup.weights[0], 1) self.assertEqual(sup.level, 1) self.assertEqual(sup.parent, None) b = block() b.v = v self.assertIs(v.parent, b) b.s = s self.assertIs(s.parent, b) bup = pickle.loads(pickle.dumps(b)) sup = bup.s self.assertEqual(len(sup), 1) self.assertIs(sup.variables[0], bup.v) self.assertTrue(bup.v in sup) self.assertEqual(sup.weights[0], 1) self.assertEqual(sup.level, 1) self.assertIs(sup.parent, bup)
def test_pprint(self): # Not really testing what the output is, just that # an error does not occur. The pprint functionality # is still in the early stages. v = variable() e = noclone(v**2) pyomo.core.kernel.pprint(e) pyomo.core.kernel.pprint(e, indent=1) b = block() b.e = expression(expr=e) pyomo.core.kernel.pprint(e) pyomo.core.kernel.pprint(b) m = block() m.b = b pyomo.core.kernel.pprint(e) pyomo.core.kernel.pprint(b) pyomo.core.kernel.pprint(m)
def test_pprint(self): import pyomo.kernel # Not really testing what the output is, just that # an error does not occur. The pprint functionality # is still in the early stages. p = parameter() e = self._ctype_factory(p**2) pyomo.kernel.pprint(e) b = block() b.e = e pyomo.kernel.pprint(e) pyomo.kernel.pprint(b) m = block() m.b = b pyomo.kernel.pprint(e) pyomo.kernel.pprint(b) pyomo.kernel.pprint(m)
def test_pprint(self): # Not really testing what the output is, just that # an error does not occur. The pprint functionality # is still in the early stages. m, n = 3, 2 vlist = create_variable_list(2) A = numpy.random.rand(m, n) ctuple = matrix_constraint(A, lb=1, ub=2, x=vlist) pyomo.core.kernel.pprint(ctuple) b = block() b.c = ctuple pyomo.core.kernel.pprint(ctuple) pyomo.core.kernel.pprint(b) m = block() m.b = b pyomo.core.kernel.pprint(ctuple) pyomo.core.kernel.pprint(b) pyomo.core.kernel.pprint(m)
def test_init_non_NumericValue(self): types = [None, 1, 1.1, True, ""] if numpy_available: types.extend([numpy.float32(1), numpy.bool_(True), numpy.int32(1)]) types.append(block()) types.append(block) for obj in types: self.assertEqual(noclone(obj), obj) self.assertIs(type(noclone(obj)), type(obj))
def test_active(self): v = variable() b = block() self.assertEqual(b.active, True) b.deactivate() self.assertEqual(b.active, False) b.v = v self.assertEqual(b.active, False) b.activate() self.assertEqual(b.active, True)
def test_remove(self): model = block() index = range(5) c = self._container_type( self._ctype_factory() for i in index) for i in index: cdata = c[0] self.assertEqual(cdata in c, True) c.remove(cdata) self.assertEqual(cdata in c, False)
def test_pickle(self): for key in transforms_nd.registered_transforms: p = transforms_nd.piecewise_nd(_test_tri, _test_values, repn=key) self.assertEqual(p.parent, None) pup = pickle.loads(pickle.dumps(p)) self.assertEqual(pup.parent, None) b = block() b.p = p self.assertIs(p.parent, b) bup = pickle.loads(pickle.dumps(b)) pup = bup.p self.assertIs(pup.parent, bup)
def test_pprint(self): # Not really testing what the output is, just that # an error does not occur. The pprint functionality # is still in the early stages. v = variable() clist = constraint_list([constraint()]) s = suffix() s[v] = 1 s[clist] = None pyomo.core.kernel.pprint(s) b = block() b.s = s pyomo.core.kernel.pprint(s) pyomo.core.kernel.pprint(b) m = block() m.b = b pyomo.core.kernel.pprint(s) pyomo.core.kernel.pprint(b) pyomo.core.kernel.pprint(m) pyomo.core.kernel.pprint({'a': 1, 'b': 2})
def test_pickle(self): p = parameter(value=1.0) self.assertEqual(p.value, 1.0) self.assertEqual(p.parent, None) pup = pickle.loads(pickle.dumps(p)) self.assertEqual(pup.value, 1.0) self.assertEqual(pup.parent, None) b = block() b.p = p self.assertIs(p.parent, b) bup = pickle.loads(pickle.dumps(b)) pup = bup.p self.assertEqual(pup.value, 1.0) self.assertIs(pup.parent, bup)
def test_to_string(self): b = block() p = parameter() e = noclone(p**2) self.assertEqual(str(e.expr), "<parameter>**2") self.assertEqual(str(e), "{(<parameter>**2)}") self.assertEqual(e.to_string(), "(<parameter>**2)") self.assertEqual(e.to_string(verbose=False), "(<parameter>**2)") self.assertEqual(e.to_string(verbose=True), "{pow(<parameter>, 2)}") b.e = e b.p = p self.assertNotEqual(p.name, None) self.assertEqual(e.to_string(verbose=True), "{pow(" + p.name + ", 2)}") self.assertEqual(e.to_string(verbose=True), "{pow(p, 2)}") del b.e del b.p