def __init__(self, f, input=None, output=None, bound='eq'): super(TransformedPiecewiseLinearFunctionND, self).__init__() assert isinstance(f, PiecewiseLinearFunctionND) if bound not in ('lb', 'ub', 'eq'): raise ValueError("Invalid bound type %r. Must be " "one of: ['lb','ub','eq']" % (bound)) self._bound = bound self._f = f _, ndim = f._tri.points.shape if input is None: input = [None] * ndim self._input = expression_tuple( expression(input[i]) for i in range(ndim)) self._output = expression(output)
def test_build_linking_constraints(self): c = _build_linking_constraints([], []) self.assertIs(type(c), constraint_tuple) self.assertEqual(len(c), 0) v = [1, data_expression(), variable(), expression(expr=1.0)] vaux = [variable(), variable(), variable(), variable()] c = _build_linking_constraints(v, vaux) self.assertIs(type(c), constraint_tuple) self.assertEqual(len(c), 4) self.assertIs(type(c[0]), linear_constraint) self.assertEqual(c[0].rhs, 1) self.assertEqual(len(list(c[0].terms)), 1) self.assertIs(list(c[0].terms)[0][0], vaux[0]) self.assertEqual(list(c[0].terms)[0][1], 1) self.assertIs(type(c[1]), linear_constraint) self.assertIs(c[1].rhs, v[1]) self.assertEqual(len(list(c[1].terms)), 1) self.assertIs(list(c[1].terms)[0][0], vaux[1]) self.assertEqual(list(c[1].terms)[0][1], 1) self.assertIs(type(c[2]), linear_constraint) self.assertEqual(c[2].rhs, 0) self.assertEqual(len(list(c[2].terms)), 2) self.assertIs(list(c[2].terms)[0][0], vaux[2]) self.assertEqual(list(c[2].terms)[0][1], 1) self.assertIs(list(c[2].terms)[1][0], v[2]) self.assertEqual(list(c[2].terms)[1][1], -1) self.assertIs(type(c[3]), constraint) self.assertEqual(c[3].rhs, 0) from pyomo.repn import generate_standard_repn repn = generate_standard_repn(c[3].body) self.assertEqual(len(repn.linear_vars), 1) self.assertIs(repn.linear_vars[0], vaux[3]) self.assertEqual(repn.linear_coefs[0], 1) self.assertEqual(repn.constant, -1)
def test_bad_alpha_type(self): c = dual_power( r1=variable(lb=0), r2=variable(lb=0), x=[variable(), variable()], alpha=parameter()) c = dual_power( r1=variable(lb=0), r2=variable(lb=0), x=[variable(), variable()], alpha=data_expression()) with self.assertRaises(TypeError): c = dual_power( r1=variable(lb=0), r2=variable(lb=0), x=[variable(), variable()], alpha=variable()) with self.assertRaises(TypeError): c = dual_power( r1=variable(lb=0), r2=variable(lb=0), x=[variable(), variable()], alpha=expression())
def test_arg(self): e = expression() self.assertEqual(e.arg(0), None) e.expr = 1 self.assertEqual(e.arg(0), 1) with self.assertRaises(KeyError): e.arg(1)
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 __init__(self, f, input=None, output=None, bound='eq', validate=True, **kwds): super(TransformedPiecewiseLinearFunction, self).__init__() assert isinstance(f, PiecewiseLinearFunction) if bound not in ('lb', 'ub', 'eq'): raise ValueError("Invalid bound type %r. Must be " "one of: ['lb','ub','eq']" % (bound)) self._bound = bound self._f = f self._inout = expression_tuple([expression(input), expression(output)]) if validate: self.validate(**kwds)
def __init__(self, f, input=None, output=None, bound='eq'): super(TransformedPiecewiseLinearFunctionND, self).__init__() assert isinstance(f, PiecewiseLinearFunctionND) if bound not in ('lb', 'ub', 'eq'): raise ValueError("Invalid bound type %r. Must be " "one of: ['lb','ub','eq']" % (bound)) self._bound = bound self._f = f _,ndim = f._tri.points.shape if input is None: input = [None]*ndim self._input = expression_tuple( expression(input[i]) for i in range(ndim)) self._output = expression(output)
def test_init_NumericValue(self): v = variable() p = parameter() e = expression() d = data_expression() o = objective() for obj in (v, v + 1, v**2, p, p + 1, p**2, e, e + 1, e**2, d, d + 1, d**2, o, o + 1, o**2): self.assertTrue(isinstance(noclone(obj), NumericValue)) self.assertTrue(isinstance(noclone(obj), IIdentityExpression)) self.assertTrue(isinstance(noclone(obj), noclone)) self.assertIs(noclone(obj).expr, obj)
def testis_potentially_variable(self): e = noclone(variable()) self.assertEqual(e.is_potentially_variable(), True) self.assertEqual(is_potentially_variable(e), True) e = noclone(parameter()) self.assertEqual(e.is_potentially_variable(), False) self.assertEqual(is_potentially_variable(e), False) e = noclone(expression()) self.assertEqual(e.is_potentially_variable(), True) self.assertEqual(is_potentially_variable(e), True) e = noclone(data_expression()) self.assertEqual(e.is_potentially_variable(), False) self.assertEqual(is_potentially_variable(e), False)
def test_init_NumericValue(self): v = variable() p = parameter() e = expression() d = data_expression() o = objective() for obj in (v, v+1, v**2, p, p+1, p**2, e, e+1, e**2, d, d+1, d**2, o, o+1, o**2): self.assertTrue(isinstance(noclone(obj), NumericValue)) self.assertTrue(isinstance(noclone(obj), IIdentityExpression)) self.assertTrue(isinstance(noclone(obj), noclone)) self.assertIs(noclone(obj).expr, obj)
def test_bad_weights(self): v = variable() with self.assertRaises(ValueError): s = sos([v], weights=[v]) v.fix(1.0) with self.assertRaises(ValueError): s = sos([v], weights=[v]) e = expression() with self.assertRaises(ValueError): s = sos([v], weights=[e]) de = data_expression() s = sos([v], weights=[de]) p = parameter() s = sos([v], weights=[p])
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_build_linking_constraints(self): c = _build_linking_constraints([],[]) self.assertIs(type(c), constraint_tuple) self.assertEqual(len(c), 0) c = _build_linking_constraints([None],[variable()]) self.assertIs(type(c), constraint_tuple) self.assertEqual(len(c), 0) v = [1, data_expression(), variable(), expression(expr=1.0)] vaux = [variable(), variable(), variable(), variable()] c = _build_linking_constraints(v, vaux) self.assertIs(type(c), constraint_tuple) self.assertEqual(len(c), 4) self.assertIs(type(c[0]), linear_constraint) self.assertEqual(c[0].rhs, 1) self.assertEqual(len(list(c[0].terms)), 1) self.assertIs(list(c[0].terms)[0][0], vaux[0]) self.assertEqual(list(c[0].terms)[0][1], 1) self.assertIs(type(c[1]), linear_constraint) self.assertIs(c[1].rhs, v[1]) self.assertEqual(len(list(c[1].terms)), 1) self.assertIs(list(c[1].terms)[0][0], vaux[1]) self.assertEqual(list(c[1].terms)[0][1], 1) self.assertIs(type(c[2]), linear_constraint) self.assertEqual(c[2].rhs, 0) self.assertEqual(len(list(c[2].terms)), 2) self.assertIs(list(c[2].terms)[0][0], vaux[2]) self.assertEqual(list(c[2].terms)[0][1], 1) self.assertIs(list(c[2].terms)[1][0], v[2]) self.assertEqual(list(c[2].terms)[1][1], -1) self.assertIs(type(c[3]), constraint) self.assertEqual(c[3].rhs, 0) from pyomo.repn import generate_standard_repn repn = generate_standard_repn(c[3].body) self.assertEqual(len(repn.linear_vars), 1) self.assertIs(repn.linear_vars[0], vaux[3]) self.assertEqual(repn.linear_coefs[0], 1) self.assertEqual(repn.constant, -1)
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))
class Test_expression_tuple(_TestActiveTupleContainerBase, unittest.TestCase): _container_type = expression_tuple _ctype_factory = lambda self: expression()
def test_is_parameter_type(self): for obj in (variable(), parameter(), objective(), expression(), data_expression()): self.assertEqual(noclone(obj).is_parameter_type(), False)
def test_is_named_expression_type(self): e = expression() self.assertEqual(e.is_named_expression_type(), True)
def test_ctype(self): e = expression() self.assertIs(e.ctype, IExpression) self.assertIs(type(e), expression) self.assertIs(type(e)._ctype, IExpression)
def test_associativity(self): x = variable() y = variable() pyomo.kernel.pprint(y + x * expression(expression(x * y))) pyomo.kernel.pprint(y + expression(expression(x * y)) * x)
class TestComponentMap(unittest.TestCase): _components = [(variable(), "v"), (variable_dict(), "vdict"), (variable_list(), "vlist"), (constraint(), "c"), (constraint_dict(), "cdict"), (constraint_list(), "clist"), (objective(), "o"), (objective_dict(), "odict"), (objective_list(), "olist"), (expression(), "e"), (expression_dict(), "edict"), (expression_list(), "elist"), (block(), "b"), (block_dict(), "bdict"), (block_list(), "blist"), (suffix(), "s")] 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_init(self): cmap = ComponentMap() cmap = ComponentMap(self._components) with self.assertRaises(TypeError): cmap = ComponentMap(*self._components) def test_type(self): cmap = ComponentMap() self.assertTrue(isinstance(cmap, collections_Mapping)) self.assertTrue(isinstance(cmap, collections_MutableMapping)) self.assertTrue(issubclass(type(cmap), collections_Mapping)) self.assertTrue(issubclass(type(cmap), collections_MutableMapping)) def test_str(self): cmap = ComponentMap() self.assertEqual(str(cmap), "ComponentMap({})") cmap.update(self._components) str(cmap) def test_len(self): cmap = ComponentMap() self.assertEqual(len(cmap), 0) cmap.update(self._components) self.assertEqual(len(cmap), len(self._components)) cmap = ComponentMap(self._components) self.assertEqual(len(cmap), len(self._components)) self.assertTrue(len(self._components) > 0) def test_getsetdelitem(self): cmap = ComponentMap() for c, val in self._components: self.assertTrue(c not in cmap) for c, val in self._components: cmap[c] = val self.assertEqual(cmap[c], val) self.assertEqual(cmap.get(c), val) del cmap[c] with self.assertRaises(KeyError): cmap[c] with self.assertRaises(KeyError): del cmap[c] self.assertEqual(cmap.get(c), None) def test_iter(self): cmap = ComponentMap() self.assertEqual(list(iter(cmap)), []) cmap.update(self._components) ids_seen = set() for c in cmap: ids_seen.add(id(c)) self.assertEqual(ids_seen, set(id(c) for c,val in self._components)) def test_keys(self): cmap = ComponentMap(self._components) self.assertEqual(sorted(cmap.keys(), key=id), sorted(list(c for c,val in self._components), key=id)) def test_values(self): cmap = ComponentMap(self._components) self.assertEqual(sorted(cmap.values()), sorted(list(val for c,val in self._components))) def test_items(self): cmap = ComponentMap(self._components) for x in cmap.items(): self.assertEqual(type(x), tuple) self.assertEqual(len(x), 2) self.assertEqual(sorted(cmap.items(), key=lambda _x: (id(_x[0]), _x[1])), sorted(self._components, key=lambda _x: (id(_x[0]), _x[1]))) def test_update(self): cmap = ComponentMap() self.assertEqual(len(cmap), 0) cmap.update(self._components) self.assertEqual(len(cmap), len(self._components)) for c, val in self._components: self.assertEqual(cmap[c], val) def test_clear(self): cmap = ComponentMap() self.assertEqual(len(cmap), 0) cmap.update(self._components) self.assertEqual(len(cmap), len(self._components)) cmap.clear() self.assertEqual(len(cmap), 0) def test_setdefault(self): cmap = ComponentMap() for c,_ in self._components: with self.assertRaises(KeyError): cmap[c] self.assertTrue(c not in cmap) cmap.setdefault(c, []).append(1) self.assertEqual(cmap[c], [1]) del cmap[c] with self.assertRaises(KeyError): cmap[c] self.assertTrue(c not in cmap) cmap[c] = [] cmap.setdefault(c, []).append(1) self.assertEqual(cmap[c], [1]) def test_eq(self): cmap1 = ComponentMap() self.assertNotEqual(cmap1, set()) self.assertFalse(cmap1 == set()) self.assertNotEqual(cmap1, list()) self.assertFalse(cmap1 == list()) self.assertNotEqual(cmap1, tuple()) self.assertFalse(cmap1 == tuple()) self.assertEqual(cmap1, dict()) self.assertTrue(cmap1 == dict()) cmap1.update(self._components) self.assertNotEqual(cmap1, set()) self.assertFalse(cmap1 == set()) self.assertNotEqual(cmap1, list()) self.assertFalse(cmap1 == list()) self.assertNotEqual(cmap1, tuple()) self.assertFalse(cmap1 == tuple()) self.assertNotEqual(cmap1, dict()) self.assertFalse(cmap1 == dict()) self.assertTrue(cmap1 == cmap1) self.assertEqual(cmap1, cmap1) cmap2 = ComponentMap(self._components) self.assertTrue(cmap2 == cmap1) self.assertFalse(cmap2 != cmap1) self.assertEqual(cmap2, cmap1) self.assertTrue(cmap1 == cmap2) self.assertFalse(cmap1 != cmap2) self.assertEqual(cmap1, cmap2) del cmap2[self._components[0][0]] self.assertFalse(cmap2 == cmap1) self.assertTrue(cmap2 != cmap1) self.assertNotEqual(cmap2, cmap1) self.assertFalse(cmap1 == cmap2) self.assertTrue(cmap1 != cmap2) self.assertNotEqual(cmap1, cmap2)
def test_nondata_bounds(self): A = numpy.ones((5, 4)) ctuple = matrix_constraint(A, rhs=1) eL = expression() eU = expression() with self.assertRaises(ValueError): ctuple.rhs = eL for c in ctuple: with self.assertRaises(ValueError): c.rhs = eL self.assertTrue((ctuple.rhs == 1).all()) self.assertTrue((ctuple.equality == True).all()) vL = variable() vU = variable() with self.assertRaises(ValueError): ctuple.rhs = vL for c in ctuple: with self.assertRaises(ValueError): c.rhs = vL self.assertTrue((ctuple.rhs == 1).all()) self.assertTrue((ctuple.equality == True).all()) vL.value = 1.0 vU.value = 1.0 with self.assertRaises(ValueError): ctuple.rhs = vL for c in ctuple: with self.assertRaises(ValueError): c.rhs = vL self.assertTrue((ctuple.rhs == 1).all()) self.assertTrue((ctuple.equality == True).all()) # the fixed status of a variable # does not change this restriction vL.fixed = True vU.fixed = True with self.assertRaises(ValueError): ctuple.rhs = vL for c in ctuple: with self.assertRaises(ValueError): c.rhs = vL self.assertTrue((ctuple.rhs == 1).all()) self.assertTrue((ctuple.equality == True).all()) p = parameter(value=0) with self.assertRaises(ValueError): ctuple.rhs = p for c in ctuple: with self.assertRaises(ValueError): c.rhs = p self.assertTrue((ctuple.rhs == 1).all()) self.assertTrue((ctuple.equality == True).all()) ctuple.equality = False self.assertTrue((ctuple.lb == 1).all()) self.assertTrue((ctuple.ub == 1).all()) self.assertTrue((ctuple.equality == False).all()) eL = expression() eU = expression() with self.assertRaises(ValueError): ctuple.lb = eL with self.assertRaises(ValueError): ctuple.ub = eU for c in ctuple: with self.assertRaises(ValueError): c.lb = eL with self.assertRaises(ValueError): c.ub = eU with self.assertRaises(ValueError): c.bounds = (eL, eU) self.assertTrue((ctuple.lb == 1).all()) self.assertTrue((ctuple.ub == 1).all()) self.assertTrue((ctuple.equality == False).all()) vL = variable() vU = variable() with self.assertRaises(ValueError): ctuple.lb = vL with self.assertRaises(ValueError): ctuple.ub = vU for c in ctuple: with self.assertRaises(ValueError): c.lb = vL with self.assertRaises(ValueError): c.ub = vU with self.assertRaises(ValueError): c.bounds = (vL, vU) self.assertTrue((ctuple.lb == 1).all()) self.assertTrue((ctuple.ub == 1).all()) self.assertTrue((ctuple.equality == False).all()) vL.value = 1.0 vU.value = 1.0 with self.assertRaises(ValueError): ctuple.lb = vL with self.assertRaises(ValueError): ctuple.ub = vU for c in ctuple: with self.assertRaises(ValueError): c.lb = vL with self.assertRaises(ValueError): c.ub = vU with self.assertRaises(ValueError): c.bounds = (vL, vU) self.assertTrue((ctuple.lb == 1).all()) self.assertTrue((ctuple.ub == 1).all()) self.assertTrue((ctuple.equality == False).all()) # the fixed status of a variable # does not change this restriction vL.fixed = True vU.fixed = True with self.assertRaises(ValueError): ctuple.lb = vL with self.assertRaises(ValueError): ctuple.ub = vU for c in ctuple: with self.assertRaises(ValueError): c.lb = vL with self.assertRaises(ValueError): c.ub = vU with self.assertRaises(ValueError): c.bounds = (vL, vU) self.assertTrue((ctuple.lb == 1).all()) self.assertTrue((ctuple.ub == 1).all()) self.assertTrue((ctuple.equality == False).all()) p = parameter(value=0) with self.assertRaises(ValueError): ctuple.lb = p with self.assertRaises(ValueError): ctuple.ub = p for c in ctuple: with self.assertRaises(ValueError): c.lb = p with self.assertRaises(ValueError): c.ub = p with self.assertRaises(ValueError): c.bounds = (p, p) self.assertTrue((ctuple.lb == 1).all()) self.assertTrue((ctuple.ub == 1).all()) self.assertTrue((ctuple.equality == False).all())
class Test_expression_list(_TestActiveListContainerBase, unittest.TestCase): _container_type = expression_list _ctype_factory = lambda self: expression()
def test_nondata_bounds(self): A = numpy.ones((5,4)) ctuple = matrix_constraint(A, rhs=1) eL = expression() eU = expression() with self.assertRaises(ValueError): ctuple.rhs = eL for c in ctuple: with self.assertRaises(ValueError): c.rhs = eL self.assertTrue((ctuple.rhs == 1).all()) self.assertTrue((ctuple.equality == True).all()) vL = variable() vU = variable() with self.assertRaises(ValueError): ctuple.rhs = vL for c in ctuple: with self.assertRaises(ValueError): c.rhs = vL self.assertTrue((ctuple.rhs == 1).all()) self.assertTrue((ctuple.equality == True).all()) vL.value = 1.0 vU.value = 1.0 with self.assertRaises(ValueError): ctuple.rhs = vL for c in ctuple: with self.assertRaises(ValueError): c.rhs = vL self.assertTrue((ctuple.rhs == 1).all()) self.assertTrue((ctuple.equality == True).all()) # the fixed status of a variable # does not change this restriction vL.fixed = True vU.fixed = True with self.assertRaises(ValueError): ctuple.rhs = vL for c in ctuple: with self.assertRaises(ValueError): c.rhs = vL self.assertTrue((ctuple.rhs == 1).all()) self.assertTrue((ctuple.equality == True).all()) p = parameter(value=0) with self.assertRaises(ValueError): ctuple.rhs = p for c in ctuple: with self.assertRaises(ValueError): c.rhs = p self.assertTrue((ctuple.rhs == 1).all()) self.assertTrue((ctuple.equality == True).all()) ctuple.equality = False self.assertTrue((ctuple.lb == 1).all()) self.assertTrue((ctuple.ub == 1).all()) self.assertTrue((ctuple.equality == False).all()) eL = expression() eU = expression() with self.assertRaises(ValueError): ctuple.lb = eL with self.assertRaises(ValueError): ctuple.ub = eU for c in ctuple: with self.assertRaises(ValueError): c.lb = eL with self.assertRaises(ValueError): c.ub = eU with self.assertRaises(ValueError): c.bounds = (eL, eU) self.assertTrue((ctuple.lb == 1).all()) self.assertTrue((ctuple.ub == 1).all()) self.assertTrue((ctuple.equality == False).all()) vL = variable() vU = variable() with self.assertRaises(ValueError): ctuple.lb = vL with self.assertRaises(ValueError): ctuple.ub = vU for c in ctuple: with self.assertRaises(ValueError): c.lb = vL with self.assertRaises(ValueError): c.ub = vU with self.assertRaises(ValueError): c.bounds = (vL, vU) self.assertTrue((ctuple.lb == 1).all()) self.assertTrue((ctuple.ub == 1).all()) self.assertTrue((ctuple.equality == False).all()) vL.value = 1.0 vU.value = 1.0 with self.assertRaises(ValueError): ctuple.lb = vL with self.assertRaises(ValueError): ctuple.ub = vU for c in ctuple: with self.assertRaises(ValueError): c.lb = vL with self.assertRaises(ValueError): c.ub = vU with self.assertRaises(ValueError): c.bounds = (vL, vU) self.assertTrue((ctuple.lb == 1).all()) self.assertTrue((ctuple.ub == 1).all()) self.assertTrue((ctuple.equality == False).all()) # the fixed status of a variable # does not change this restriction vL.fixed = True vU.fixed = True with self.assertRaises(ValueError): ctuple.lb = vL with self.assertRaises(ValueError): ctuple.ub = vU for c in ctuple: with self.assertRaises(ValueError): c.lb = vL with self.assertRaises(ValueError): c.ub = vU with self.assertRaises(ValueError): c.bounds = (vL, vU) self.assertTrue((ctuple.lb == 1).all()) self.assertTrue((ctuple.ub == 1).all()) self.assertTrue((ctuple.equality == False).all()) p = parameter(value=0) with self.assertRaises(ValueError): ctuple.lb = p with self.assertRaises(ValueError): ctuple.ub = p for c in ctuple: with self.assertRaises(ValueError): c.lb = p with self.assertRaises(ValueError): c.ub = p with self.assertRaises(ValueError): c.bounds = (p, p) self.assertTrue((ctuple.lb == 1).all()) self.assertTrue((ctuple.ub == 1).all()) self.assertTrue((ctuple.equality == False).all())
class TestComponentSet(unittest.TestCase): _components = [ variable(), variable_dict(), variable_list(), constraint(), constraint_dict(), constraint_list(), objective(), objective_dict(), objective_list(), expression(), expression_dict(), expression_list(), block(), block_dict(), block_list(), suffix() ] def test_pickle(self): c = ComponentSet() self.assertEqual(len(c), 0) cup = pickle.loads(pickle.dumps(c)) self.assertIsNot(cup, c) self.assertEqual(len(cup), 0) v = variable() c.add(v) self.assertEqual(len(c), 1) self.assertTrue(v in c) cup = pickle.loads(pickle.dumps(c)) vup = cup.pop() cup.add(vup) self.assertIsNot(cup, c) self.assertIsNot(vup, v) self.assertEqual(len(cup), 1) self.assertTrue(vup in cup) 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.assertTrue(v in c) 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.assertTrue(vup in cup) self.assertIs(vup.parent, Vup) self.assertIs(Vup.parent, bup) self.assertIs(bup.parent, None) self.assertEqual(len(c), 1) self.assertTrue(v in c) def test_init(self): cset = ComponentSet() cset = ComponentSet(self._components) with self.assertRaises(TypeError): cset = ComponentSet(*self._components) def test_type(self): cset = ComponentSet() self.assertTrue(isinstance(cset, collections.abc.Set)) self.assertTrue(isinstance(cset, collections.abc.MutableSet)) self.assertTrue(issubclass(type(cset), collections.abc.Set)) self.assertTrue(issubclass(type(cset), collections.abc.MutableSet)) def test_str(self): cset = ComponentSet() self.assertEqual(str(cset), "ComponentSet([])") cset.update(self._components) str(cset) def test_len(self): cset = ComponentSet() self.assertEqual(len(cset), 0) cset.update(self._components) self.assertEqual(len(cset), len(self._components)) cset = ComponentSet(self._components) self.assertEqual(len(cset), len(self._components)) self.assertTrue(len(self._components) > 0) def test_iter(self): cset = ComponentSet() self.assertEqual(list(iter(cset)), []) cset.update(self._components) ids_seen = set() for c in cset: ids_seen.add(id(c)) self.assertEqual(ids_seen, set(id(c) for c in self._components)) def set_add(self): cset = ComponentSet() self.assertEqual(len(cset), 0) for i, c in enumerate(self._components): self.assertTrue(c not in cset) cset.add(c) self.assertTrue(c in cset) self.assertEqual(len(cset), i + 1) self.assertEqual(len(cset), len(self._components)) for c in self._components: self.assertTrue(c in cset) cset.add(c) self.assertTrue(c in cset) self.assertEqual(len(cset), len(self._components)) def test_pop(self): cset = ComponentSet() self.assertEqual(len(cset), 0) with self.assertRaises(KeyError): cset.pop() v = variable() cset.add(v) self.assertTrue(v in cset) self.assertEqual(len(cset), 1) v_ = cset.pop() self.assertIs(v, v_) self.assertTrue(v not in cset) self.assertEqual(len(cset), 0) def test_update(self): cset = ComponentSet() self.assertEqual(len(cset), 0) cset.update(self._components) self.assertEqual(len(cset), len(self._components)) for c in self._components: self.assertTrue(c in cset) def test_clear(self): cset = ComponentSet() self.assertEqual(len(cset), 0) cset.update(self._components) self.assertEqual(len(cset), len(self._components)) cset.clear() self.assertEqual(len(cset), 0) def test_remove(self): cset = ComponentSet() self.assertEqual(len(cset), 0) cset.update(self._components) self.assertEqual(len(cset), len(self._components)) for i, c in enumerate(self._components): cset.remove(c) self.assertEqual(len(cset), len(self._components) - (i + 1)) for c in self._components: self.assertTrue(c not in cset) with self.assertRaises(KeyError): cset.remove(c) def test_discard(self): cset = ComponentSet() self.assertEqual(len(cset), 0) cset.update(self._components) self.assertEqual(len(cset), len(self._components)) for i, c in enumerate(self._components): cset.discard(c) self.assertEqual(len(cset), len(self._components) - (i + 1)) for c in self._components: self.assertTrue(c not in cset) cset.discard(c) def test_isdisjoint(self): cset1 = ComponentSet() cset2 = ComponentSet() self.assertTrue(cset1.isdisjoint(cset2)) self.assertTrue(cset2.isdisjoint(cset1)) v = variable() cset1.add(v) self.assertTrue(cset1.isdisjoint(cset2)) self.assertTrue(cset2.isdisjoint(cset1)) cset2.add(v) self.assertFalse(cset1.isdisjoint(cset2)) self.assertFalse(cset2.isdisjoint(cset1)) def test_misc_set_ops(self): v1 = variable() cset1 = ComponentSet([v1]) v2 = variable() cset2 = ComponentSet([v2]) cset3 = ComponentSet([v1, v2]) empty = ComponentSet([]) self.assertEqual(cset1 | cset2, cset3) self.assertEqual((cset1 | cset2) - cset3, empty) self.assertEqual(cset1 ^ cset2, cset3) self.assertEqual(cset1 ^ cset3, cset2) self.assertEqual(cset2 ^ cset3, cset1) self.assertEqual(cset1 & cset2, empty) self.assertEqual(cset1 & cset3, cset1) self.assertEqual(cset2 & cset3, cset2) def test_eq(self): cset1 = ComponentSet() self.assertEqual(cset1, set()) self.assertTrue(cset1 == set()) self.assertNotEqual(cset1, list()) self.assertFalse(cset1 == list()) self.assertNotEqual(cset1, tuple()) self.assertFalse(cset1 == tuple()) self.assertNotEqual(cset1, dict()) self.assertFalse(cset1 == dict()) cset1.update(self._components) self.assertNotEqual(cset1, set()) self.assertFalse(cset1 == set()) self.assertNotEqual(cset1, list()) self.assertFalse(cset1 == list()) self.assertNotEqual(cset1, tuple()) self.assertFalse(cset1 == tuple()) self.assertNotEqual(cset1, dict()) self.assertFalse(cset1 == dict()) self.assertTrue(cset1 == cset1) self.assertEqual(cset1, cset1) cset2 = ComponentSet(self._components) self.assertTrue(cset2 == cset1) self.assertFalse(cset2 != cset1) self.assertEqual(cset2, cset1) self.assertTrue(cset1 == cset2) self.assertFalse(cset1 != cset2) self.assertEqual(cset1, cset2) cset2.remove(self._components[0]) self.assertFalse(cset2 == cset1) self.assertTrue(cset2 != cset1) self.assertNotEqual(cset2, cset1) self.assertFalse(cset1 == cset2) self.assertTrue(cset1 != cset2) self.assertNotEqual(cset1, cset2)