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() e = noclone(v**2) pyomo.kernel.pprint(e) pyomo.kernel.pprint(e, indent=1) b = block() b.e = expression(expr=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) # tests compatibility with _ToStringVisitor pyomo.kernel.pprint(noclone(v) + 1) pyomo.kernel.pprint(noclone(v + 1)) x = variable() y = variable() pyomo.kernel.pprint(y + x * noclone(noclone(x * y))) pyomo.kernel.pprint(y + noclone(noclone(x * y)) * x)
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_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.v = variable() m.V = variable_list() m.V.append(variable()) m.B = block_list() m.B.append(block()) m.B[0].c = matrix_constraint(A) m.B[0].v = variable() m.B[0].V = variable_list() m.B[0].V.append(variable()) 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() m.b.c[3] = constraint_list() # don't visit things below a matrix constraint # (e.g., cases where we want to handle it in bulk) def no_mc_descend(x): if isinstance(x, matrix_constraint): return False return True cnt = 0 for obj in m.preorder_traversal(ctype=IConstraint, descend=no_mc_descend): self.assertTrue(type(obj.parent) is not matrix_constraint) self.assertTrue((obj.ctype is block._ctype) or \ (obj.ctype is constraint._ctype)) cnt += 1 self.assertEqual(cnt, 11) cnt = 0 mc_child_cnt = 0 for obj in m.preorder_traversal(ctype=IConstraint): self.assertTrue((obj.ctype is block._ctype) or \ (obj.ctype is constraint._ctype)) if type(obj.parent) is matrix_constraint: mc_child_cnt += 1 cnt += 1 self.assertEqual(cnt, 23) self.assertEqual(mc_child_cnt, 12) self.assertEqual( len(list(m.components(ctype=IConstraint))), 13)
def test_preorder_traversal(self): A = numpy.ones((3,3)) m = block() m.c = matrix_constraint(A) m.v = variable() m.V = variable_list() m.V.append(variable()) m.B = block_list() m.B.append(block()) m.B[0].c = matrix_constraint(A) m.B[0].v = variable() m.B[0].V = variable_list() m.B[0].V.append(variable()) 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() m.b.c[3] = constraint_list() # don't visit things below a matrix constraint # (e.g., cases where we want to handle it in bulk) def no_mc_descend(x): if isinstance(x, matrix_constraint): return False return True cnt = 0 for obj in pmo.preorder_traversal(m, ctype=IConstraint, descend=no_mc_descend): self.assertTrue(type(obj.parent) is not matrix_constraint) self.assertTrue((obj.ctype is block._ctype) or \ (obj.ctype is constraint._ctype)) cnt += 1 self.assertEqual(cnt, 11) cnt = 0 mc_child_cnt = 0 for obj in pmo.preorder_traversal(m, ctype=IConstraint): self.assertTrue((obj.ctype is block._ctype) or \ (obj.ctype is constraint._ctype)) if type(obj.parent) is matrix_constraint: mc_child_cnt += 1 cnt += 1 self.assertEqual(cnt, 23) self.assertEqual(mc_child_cnt, 12) self.assertEqual( len(list(m.components(ctype=IConstraint))), 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. c = self._object_factory() pprint(c) b = block() b.c = c pprint(c) pprint(b) m = block() m.b = b pprint(c) pprint(b) 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. f = functional_value() pprint(f) b = block() b.f = f pprint(f) pprint(b) m = block() m.b = b pprint(f) pprint(b) 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. p = parameter() pprint(p) b = block() b.p = p pprint(p) pprint(b) m = block() m.b = b pprint(p) pprint(b) pprint(m)
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): 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_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_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): 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() o = objective(expr=v**2) pprint(o) b = block() b.o = o pprint(o) pprint(b) m = block() m.b = b pprint(o) pprint(b) 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. 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): 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_init(self): v = variable() self.assertTrue(v.parent is None) self.assertEqual(v.ctype, IVariable) 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, IVariable) 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(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_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_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): 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_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. cdict = self._container_type({None: self._ctype_factory()}) pyomo.kernel.pprint(cdict) b = block() b.cdict = cdict pyomo.kernel.pprint(cdict) pyomo.kernel.pprint(b) m = block() m.b = b pyomo.kernel.pprint(cdict) pyomo.kernel.pprint(b) pyomo.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. c = self._object_factory() pyomo.kernel.pprint(c) b = block() b.c = c pyomo.kernel.pprint(c) pyomo.kernel.pprint(b) m = block() m.b = b pyomo.kernel.pprint(c) pyomo.kernel.pprint(b) pyomo.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): 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. vlist = variable_list([variable(), variable()]) s = sos(vlist) pyomo.kernel.pprint(s) b = block() b.s = s pyomo.kernel.pprint(s) pyomo.kernel.pprint(b) m = block() m.b = b pyomo.kernel.pprint(s) 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) pmo.pprint(ctuple) b = block() b.c = ctuple pmo.pprint(ctuple) pmo.pprint(b) m = block() m.b = b pmo.pprint(ctuple) pmo.pprint(b) pmo.pprint(m)
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_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_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() e = noclone(v**2) pyomo.kernel.pprint(e) pyomo.kernel.pprint(e, indent=1) b = block() b.e = expression(expr=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_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_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() e = noclone(v**2) pyomo.kernel.pprint(e) pyomo.kernel.pprint(e, indent=1) b = block() b.e = expression(expr=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) # tests compatibility with _ToStringVisitor pyomo.kernel.pprint(noclone(v)+1) pyomo.kernel.pprint(noclone(v+1))
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 pprint(s) b = block() b.s = s pprint(s) pprint(b) m = block() m.b = b pprint(s) pprint(b) pprint(m) pprint({'a': 1, 'b': 2})
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() clist = constraint_list([constraint()]) s = suffix() s[v] = 1 s[clist] = None pyomo.kernel.pprint(s) b = block() b.s = s pyomo.kernel.pprint(s) pyomo.kernel.pprint(b) m = block() m.b = b pyomo.kernel.pprint(s) pyomo.kernel.pprint(b) pyomo.kernel.pprint(m) pyomo.kernel.pprint({'a': 1, 'b': 2})
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. 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) pmo.pprint(ctuple) b = block() b.c = ctuple pmo.pprint(ctuple) pmo.pprint(b) m = block() m.b = b pmo.pprint(ctuple) pmo.pprint(b) pmo.pprint(m)
def test_name(self): s = suffix() self.assertTrue(s.parent is None) self.assertEqual(s.local_name, None) self.assertEqual(s.name, None) model = block() model.s = s self.assertTrue(model.parent is None) self.assertTrue(s.parent is model) self.assertEqual(s.local_name, "s") self.assertEqual(s.name, "s") b = block() b.model = model self.assertTrue(b.parent is None) self.assertTrue(model.parent is b) self.assertTrue(s.parent is model) self.assertEqual(s.local_name, "s") self.assertEqual(s.name, "model.s") bdict = block_dict() bdict[0] = b self.assertTrue(bdict.parent is None) self.assertTrue(b.parent is bdict) self.assertTrue(model.parent is b) self.assertTrue(s.parent is model) self.assertEqual(s.local_name, "s") self.assertEqual(s.name, "[0].model.s") m = block() m.bdict = bdict self.assertTrue(m.parent is None) self.assertTrue(bdict.parent is m) self.assertTrue(b.parent is bdict) self.assertTrue(model.parent is b) self.assertTrue(s.parent is model) self.assertEqual(s.local_name, "s") self.assertEqual(s.name, "bdict[0].model.s")
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_pickle(self): f = functional_value() self.assertIs(f.fn, None) self.assertIs(f.parent, None) fup = pickle.loads( pickle.dumps(f)) self.assertIs(fup.fn, None) self.assertIs(fup.parent, None) b = block() b.f = f self.assertIs(f.parent, b) bup = pickle.loads( pickle.dumps(b)) fup = bup.f self.assertIs(fup.fn, None) self.assertIs(fup.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
def test_pickle(self): p = parameter(value=1.0) self.assertEqual(p.value, 1.0) self.assertIs(p.parent, None) pup = pickle.loads( pickle.dumps(p)) self.assertEqual(pup.value, 1.0) self.assertIs(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_pickle(self): c = ComponentMap() self.assertEqual(len(c), 0) cup = pickle.loads( pickle.dumps(c)) self.assertIsNot(cup, c) self.assertEqual(len(cup), 0) v = variable() c[v] = 1.0 self.assertEqual(len(c), 1) self.assertEqual(c[v], 1.0) cup = pickle.loads( pickle.dumps(c)) vup = list(cup.keys())[0] self.assertIsNot(cup, c) self.assertIsNot(vup, v) self.assertEqual(len(cup), 1) self.assertEqual(cup[vup], 1) self.assertEqual(vup.parent, None) b = block() V = b.V = variable_list() b.V.append(v) b.c = c self.assertEqual(len(c), 1) self.assertEqual(c[v], 1.0) self.assertIs(v.parent, b.V) self.assertIs(V.parent, b) self.assertIs(b.parent, None) bup = pickle.loads( pickle.dumps(b)) Vup = bup.V vup = Vup[0] cup = bup.c self.assertIsNot(cup, c) self.assertIsNot(vup, v) self.assertIsNot(Vup, V) self.assertIsNot(bup, b) self.assertEqual(len(cup), 1) self.assertEqual(cup[vup], 1) self.assertIs(vup.parent, Vup) self.assertIs(Vup.parent, bup) self.assertIs(bup.parent, None) self.assertEqual(len(c), 1) self.assertEqual(c[v], 1)
def test_active(self): A = numpy.ones((2,2)) ctuple = matrix_constraint(A) self.assertEqual(ctuple.active, True) for c in ctuple: self.assertEqual(c.active, True) ctuple.deactivate() self.assertEqual(ctuple.active, False) for c in ctuple: self.assertEqual(c.active, True) ctuple.deactivate(shallow=False) self.assertEqual(ctuple.active, False) for c in ctuple: self.assertEqual(c.active, False) ctuple[0].activate() self.assertEqual(ctuple.active, False) self.assertEqual(ctuple[0].active, True) self.assertEqual(ctuple[1].active, False) ctuple.activate() self.assertEqual(ctuple.active, True) self.assertEqual(ctuple[0].active, True) self.assertEqual(ctuple[1].active, False) ctuple.activate(shallow=False) self.assertEqual(ctuple.active, True) for c in ctuple: self.assertEqual(c.active, True) b = block() self.assertEqual(b.active, True) b.deactivate() self.assertEqual(b.active, False) b.c = ctuple self.assertEqual(ctuple.active, True) self.assertEqual(b.active, False) ctuple.deactivate() self.assertEqual(ctuple.active, False) self.assertEqual(b.active, False) b.activate() self.assertEqual(ctuple.active, False) self.assertEqual(b.active, True) b.activate(shallow=False) self.assertEqual(ctuple.active, True) self.assertEqual(b.active, True) b.deactivate(shallow=False) self.assertEqual(ctuple.active, False) self.assertEqual(b.active, False)
def test_to_string(self): b = block() e = self._ctype_factory() label = str(e) self.assertNotEqual(label, None) self.assertEqual(e.name, None) self.assertEqual(str(e.expr), "None") self.assertEqual(str(e), label) self.assertEqual(e.to_string(), label+"{Undefined}") self.assertEqual(e.to_string(verbose=False), label+"{Undefined}") self.assertEqual(e.to_string(verbose=True), label+"{Undefined}") b.e = e self.assertNotEqual(e.name, None) self.assertEqual(e.to_string(verbose=True), "e{Undefined}") del b.e self.assertEqual(e.name, None) e.expr = 1 self.assertEqual(str(e.expr), "1") self.assertEqual(str(e), label) self.assertEqual(e.to_string(), "1") self.assertEqual(e.to_string(verbose=False), "1") self.assertEqual(e.to_string(verbose=True), label+"{1}") b.e = e self.assertNotEqual(e.name, None) self.assertEqual(e.to_string(verbose=True), "e{1}") del b.e self.assertEqual(e.name, None) p = parameter() e.expr = p**2 self.assertEqual(str(e.expr), "<parameter>**2") self.assertEqual(str(e), label) self.assertEqual(e.to_string(), "(<parameter>**2)") self.assertEqual(e.to_string(verbose=False), "(<parameter>**2)") self.assertEqual(e.to_string(verbose=True), label+"{pow(<parameter>, 2)}") b.e = e b.p = p self.assertNotEqual(e.name, None) self.assertNotEqual(p.name, None) self.assertEqual(e.to_string(verbose=True), e.name+"{pow("+p.name+", 2)}") self.assertEqual(e.to_string(verbose=True), "e{pow(p, 2)}") del b.e del b.p
def test_pickle(self): e = self._ctype_factory(expr=1.0) self.assertEqual(type(e.expr), float) self.assertEqual(e.expr, 1.0) self.assertEqual(e.parent, None) eup = pickle.loads( pickle.dumps(e)) self.assertEqual(type(eup.expr), float) self.assertEqual(eup.expr, 1.0) self.assertEqual(eup.parent, None) b = block() b.e = e self.assertIs(e.parent, b) bup = pickle.loads( pickle.dumps(b)) eup = bup.e self.assertEqual(type(eup.expr), float) self.assertEqual(eup.expr, 1.0) self.assertIs(eup.parent, bup)
def test_dill(self): p = parameter(1) f = functional_value(lambda: p()) self.assertEqual(f(), 1) fup = dill.loads( dill.dumps(f)) p.value = 2 self.assertEqual(f(), 2) self.assertEqual(fup(), 1) b = block() b.p = p b.f = f self.assertEqual(b.f(), 2) bup = dill.loads( dill.dumps(b)) fup = bup.f b.p.value = 4 self.assertEqual(b.f(), 4) self.assertEqual(bup.f(), 2) bup.p.value = 4 self.assertEqual(bup.f(), 4)
def test_pickle(self): c = self._object_factory() self.assertIs(c.lb, None) self.assertEqual(c.ub, 0) self.assertIsNot(c.body, None) self.assertIs(c.parent, None) cup = pickle.loads( pickle.dumps(c)) self.assertIs(cup.lb, None) self.assertEqual(cup.ub, 0) self.assertIsNot(cup.body, None) self.assertIs(cup.parent, None) b = block() b.c = c self.assertIs(c.parent, b) bup = pickle.loads( pickle.dumps(b)) cup = bup.c self.assertIs(cup.lb, None) self.assertEqual(cup.ub, 0) self.assertIsNot(cup.body, None) self.assertIs(cup.parent, bup)
def as_domain(cls, r, x): """Builds a conic domain. Input arguments take the same form as those of the conic constraint, but in place of each variable, one can optionally supply a constant, linear expression, or None. Returns ------- block A block object with the core conic constraint (block.q) expressed using auxiliary variables (block.r, block.x) linked to the input arguments through auxiliary constraints (block.c). """ b = block() b.r = variable(lb=0) b.x = variable_tuple( [variable() for i in range(len(x))]) b.c = _build_linking_constraints([r] + list(x), [b.r] + list(b.x)) b.q = cls(r=b.r, x=b.x) return b
def as_domain(cls, r, x1, x2): """Builds a conic domain. Input arguments take the same form as those of the conic constraint, but in place of each variable, one can optionally supply a constant, linear expression, or None. Returns ------- block A block object with the core conic constraint (block.q) expressed using auxiliary variables (block.r, block.x1, block.x2) linked to the input arguments through auxiliary constraints (block.c). """ b = block() b.r = variable(lb=0) b.x1 = variable() b.x2 = variable(ub=0) b.c = _build_linking_constraints([r,x1,x2], [b.r,b.x1,b.x2]) b.q = cls(r=b.r, x1=b.x1, x2=b.x2) return b