def test_associativity(self): x = parameter() y = parameter() pyomo.kernel.pprint( y + x*data_expression(data_expression(x*y))) pyomo.kernel.pprint( y + data_expression(data_expression(x*y))*x)
def test_division_behavior(self): # make sure integers involved in Pyomo expression # use __future__ behavior e = noclone(parameter(value=2)) self.assertIs(type(e.expr), parameter) self.assertEqual((1/e)(), 0.5) self.assertEqual((parameter(1)/e)(), 0.5) self.assertEqual((1/e.expr()), 0.5)
def test_division_behavior(self): # make sure integers involved in Pyomo expression # use __future__ behavior e = noclone(parameter(value=2)) self.assertIs(type(e.expr), parameter) self.assertEqual((1/e)(), 0.5) self.assertEqual((parameter(1)/e)(), 0.5) # since the type returned is int, this should result # in the behavior used by the interpreter if six.PY3: self.assertEqual((1/e.expr()), 0.5) else: self.assertEqual((1/e.expr()), 0)
def test_division_behavior(self): # make sure integers involved in Pyomo expression # use __future__ behavior e = noclone(parameter(value=2)) self.assertIs(type(e.expr), parameter) self.assertEqual((1 / e)(), 0.5) self.assertEqual((parameter(1) / e)(), 0.5) # since the type returned is int, this should result # in the behavior used by the interpreter if six.PY3: self.assertEqual((1 / e.expr()), 0.5) else: self.assertEqual((1 / e.expr()), 0)
def test_is_fixed(self): p = parameter() self.assertEqual(p.is_fixed(), True) self.assertEqual(is_fixed(p), True) p.value = 1.0 self.assertEqual(p.is_fixed(), True) self.assertEqual(is_fixed(p), True)
def test_is_constant(self): p = parameter() self.assertEqual(p.is_constant(), False) self.assertEqual(is_constant(p), False) p.value = 1.0 self.assertEqual(p.is_constant(), False) self.assertEqual(is_constant(p), False)
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_potentially_variable(self): p = parameter() self.assertEqual(p.is_potentially_variable(), False) self.assertEqual(is_potentially_variable(p), False) p.value = 1.0 self.assertEqual(p.is_potentially_variable(), False) self.assertEqual(is_potentially_variable(p), False)
def test_polynomial_degree(self): e = noclone(parameter()) self.assertEqual(e.polynomial_degree(), 0) e = noclone(parameter(value=1)) self.assertEqual(e.polynomial_degree(), 0) v = variable() v.value = 2 e = noclone(v + 1) self.assertEqual(e.polynomial_degree(), 1) e = noclone(v**2 + v + 1) self.assertEqual(e.polynomial_degree(), 2) v.fix() self.assertEqual(e.polynomial_degree(), 0) e = noclone(v**v) self.assertEqual(e.polynomial_degree(), 0) v.free() self.assertEqual(e.polynomial_degree(), None)
def test_call(self): e = self._ctype_factory() self.assertEqual(e(), None) e.expr = 1 self.assertEqual(e(), 1) p = parameter() p.value = 2 e.expr = p + 1 self.assertEqual(e(), 3)
def test_division_behavior(self): # make sure integers involved in Pyomo expression # use __future__ behavior e = self._ctype_factory() e.expr = 2 self.assertIs(type(e.expr), int) self.assertEqual((1/e)(), 0.5) self.assertEqual((parameter(1)/e)(), 0.5) self.assertEqual((1/e.expr), 0.5)
def test_init(self): p = parameter() self.assertTrue(p.parent is None) self.assertEqual(p.ctype, IParameter) self.assertEqual(p.value, None) self.assertEqual(p(), None) p.value = 1 self.assertEqual(p.value, 1) self.assertEqual(p(), 1)
def test_call(self): e = noclone(None) self.assertIs(e, None) e = noclone(1) self.assertEqual(e, 1) p = parameter() p.value = 2 e = noclone(p + 1) self.assertEqual(e(), 3)
def test_clone(self): p = parameter() e = noclone(p) self.assertTrue(e.clone() is e) self.assertTrue(e.clone().expr is p) sube = p**2 + 1 e = noclone(sube) self.assertTrue(e.clone() is e) self.assertTrue(e.clone().expr is sube)
def test_clone(self): e = self._ctype_factory() self.assertTrue(e.clone() is e) p = parameter() e.expr = p self.assertTrue(e.clone() is e) self.assertTrue(e.clone().expr is p) sube = p**2 + 1 e.expr = sube self.assertTrue(e.clone() is e) self.assertTrue(e.clone().expr is sube)
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_polynomial_degree(self): p = parameter() self.assertEqual(p.polynomial_degree(), 0) self.assertEqual((p**2).polynomial_degree(), 0) self.assertEqual(p.value, None) with self.assertRaises(ValueError): (p**2)() p.value = 1.0 self.assertEqual(p.polynomial_degree(), 0) self.assertEqual((p**2).polynomial_degree(), 0) self.assertEqual(p.value, 1.0) self.assertEqual((p**2)(), 1.0)
def test_is_fixed(self): v = variable() e = noclone(v + 1) self.assertEqual(e.is_fixed(), False) self.assertEqual(is_fixed(e), False) v.fix() self.assertEqual(e.is_fixed(), True) self.assertEqual(is_fixed(e), True) e = noclone(parameter()) self.assertEqual(e.is_fixed(), True) self.assertEqual(is_fixed(e), True)
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_division_behavior(self): # make sure integers involved in Pyomo expression # use __future__ behavior e = self._ctype_factory() e.expr = 2 self.assertIs(type(e.expr), int) self.assertEqual((1 / e)(), 0.5) self.assertEqual((parameter(1) / e)(), 0.5) # since the type returned is int, this should result # in the behavior used by the interpreter if six.PY3: self.assertEqual((1 / e.expr), 0.5) else: self.assertEqual((1 / e.expr), 0)
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_is_constant(self): e = self._ctype_factory() self.assertEqual(e.is_constant(), False) self.assertEqual(is_constant(e), False) e.expr = 1 self.assertEqual(e.is_constant(), False) self.assertEqual(is_constant(e), False) p = parameter() self.assertEqual(p.is_constant(), False) self.assertEqual(is_constant(p), False) p.value = 2 e.expr = p + 1 self.assertEqual(e.is_constant(), False) self.assertEqual(is_constant(e), False)
def test_division_behavior(self): # make sure integers involved in Pyomo expression # use __future__ behavior e = self._ctype_factory() e.expr = 2 self.assertIs(type(e.expr), int) self.assertEqual((1/e)(), 0.5) self.assertEqual((parameter(1)/e)(), 0.5) # since the type returned is int, this should result # in the behavior used by the interpreter if six.PY3: self.assertEqual((1/e.expr), 0.5) else: self.assertEqual((1/e.expr), 0)
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_is_constant(self): v = variable() e = noclone(v) self.assertEqual(e.is_constant(), False) self.assertEqual(is_constant(e), False) v.fix(1) self.assertEqual(e.is_constant(), False) self.assertEqual(is_constant(e), False) p = parameter() e = noclone(p) self.assertEqual(p.is_constant(), False) self.assertEqual(is_constant(p), False) self.assertEqual(is_constant(noclone(1)), True)
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_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_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_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_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() pyomo.kernel.pprint(p) b = block() b.p = p pyomo.kernel.pprint(p) pyomo.kernel.pprint(b) m = block() m.b = b pyomo.kernel.pprint(p) pyomo.kernel.pprint(b) pyomo.kernel.pprint(m)
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_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_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_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_polynomial_degree(self): e = self._ctype_factory() self.assertEqual(e.polynomial_degree(), 0) e.expr = 1 self.assertEqual(e.polynomial_degree(), 0) p = parameter() e.expr = p**2 self.assertEqual(e.polynomial_degree(), 0) a = self._ctype_factory() e.expr = (a*p)**2/(p + 5) self.assertEqual(e.polynomial_degree(), 0) a.expr = 2.0 p.value = 5.0 self.assertEqual(e.polynomial_degree(), 0) self.assertEqual(e(), 10.0) v = variable() with self.assertRaises(ValueError): e.expr = v + 1
def test_polynomial_degree(self): e = self._ctype_factory() self.assertEqual(e.polynomial_degree(), 0) e.expr = 1 self.assertEqual(e.polynomial_degree(), 0) p = parameter() e.expr = p**2 self.assertEqual(e.polynomial_degree(), 0) a = self._ctype_factory() e.expr = (a * p)**2 / (p + 5) self.assertEqual(e.polynomial_degree(), 0) a.expr = 2.0 p.value = 5.0 self.assertEqual(e.polynomial_degree(), 0) self.assertEqual(e(), 10.0) v = variable() with self.assertRaises(ValueError): e.expr = v + 1
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_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 testis_potentially_variable(self): e = self._ctype_factory() self.assertEqual(e.is_potentially_variable(), False) self.assertEqual(is_potentially_variable(e), False) e.expr = 1 self.assertEqual(e.is_potentially_variable(), False) self.assertEqual(is_potentially_variable(e), False) p = parameter() e.expr = p**2 self.assertEqual(e.is_potentially_variable(), False) self.assertEqual(is_potentially_variable(e), False) a = self._ctype_factory() e.expr = (a * p)**2 / (p + 5) self.assertEqual(e.is_potentially_variable(), False) self.assertEqual(is_potentially_variable(e), False) a.expr = 2.0 p.value = 5.0 self.assertEqual(e.is_potentially_variable(), False) self.assertEqual(is_potentially_variable(e), False) self.assertEqual(e(), 10.0) v = variable() with self.assertRaises(ValueError): e.expr = v + 1
def testis_potentially_variable(self): e = self._ctype_factory() self.assertEqual(e.is_potentially_variable(), False) self.assertEqual(is_potentially_variable(e), False) e.expr = 1 self.assertEqual(e.is_potentially_variable(), False) self.assertEqual(is_potentially_variable(e), False) p = parameter() e.expr = p**2 self.assertEqual(e.is_potentially_variable(), False) self.assertEqual(is_potentially_variable(e), False) a = self._ctype_factory() e.expr = (a*p)**2/(p + 5) self.assertEqual(e.is_potentially_variable(), False) self.assertEqual(is_potentially_variable(e), False) a.expr = 2.0 p.value = 5.0 self.assertEqual(e.is_potentially_variable(), False) self.assertEqual(is_potentially_variable(e), False) self.assertEqual(e(), 10.0) v = variable() with self.assertRaises(ValueError): e.expr = v + 1
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())
def test_domain(self): v = variable(domain=Reals) self.assertEqual(v.domain_type, RealSet) self.assertEqual(v.is_continuous(), True) self.assertEqual(v.is_discrete(), False) self.assertEqual(v.is_binary(), False) self.assertEqual(v.is_integer(), False) self.assertEqual(v.lb, None) self.assertEqual(v.ub, None) self.assertEqual(v.bounds, (None, None)) v = variable(domain=Reals, lb=0, ub=1) self.assertEqual(v.domain_type, RealSet) self.assertEqual(v.is_continuous(), True) self.assertEqual(v.is_discrete(), False) self.assertEqual(v.is_binary(), False) self.assertEqual(v.is_integer(), False) self.assertEqual(v.lb, 0) self.assertEqual(v.ub, 1) self.assertEqual(v.bounds, (0, 1)) lb_param = parameter() ub_param = parameter() lb = lb_param ub = ub_param v = variable(domain=Reals, lb=lb, ub=ub) self.assertEqual(v.domain_type, RealSet) self.assertEqual(v.is_continuous(), True) self.assertEqual(v.is_discrete(), False) self.assertEqual(v.is_binary(), False) self.assertEqual(v.is_integer(), False) self.assertIs(v.lb, lb) self.assertIs(v.ub, ub) self.assertIs(v.bounds[0], lb) self.assertIs(v.bounds[1], ub) lb = lb**2 ub = ub**2 v.lb = lb v.ub = ub lb_param.value = 2.0 ub_param.value = 3.0 self.assertIs(v.lb, lb) self.assertEqual(v.lb(), 4.0) self.assertIs(v.ub, ub) self.assertEqual(v.ub(), 9.0) self.assertIs(v.bounds[0], lb) self.assertEqual(v.bounds[0](), 4.0) self.assertIs(v.bounds[1], ub) self.assertEqual(v.bounds[1](), 9.0) # if the domain has a finite bound # then you can not use the same bound # keyword variable(domain=NonNegativeReals, ub=0) with self.assertRaises(ValueError): variable(domain=NonNegativeReals, lb=0) variable(domain=NonNegativeReals, ub=ub) with self.assertRaises(ValueError): variable(domain=NonNegativeReals, lb=lb) variable(domain=NonNegativeIntegers, ub=0) with self.assertRaises(ValueError): variable(domain=NonNegativeIntegers, lb=0) variable(domain=NonNegativeIntegers, ub=ub) with self.assertRaises(ValueError): variable(domain=NonNegativeIntegers, lb=lb) variable(domain=NegativeReals, lb=0) with self.assertRaises(ValueError): variable(domain=NegativeReals, ub=0) variable(domain=NegativeReals, lb=lb) with self.assertRaises(ValueError): variable(domain=NegativeReals, ub=ub) variable(domain=NegativeIntegers, lb=0) with self.assertRaises(ValueError): variable(domain=NegativeIntegers, ub=0) variable(domain=NegativeIntegers, lb=lb) with self.assertRaises(ValueError): variable(domain=NegativeIntegers, ub=ub) unit_interval = RealInterval(bounds=(0,1)) self.assertEqual(unit_interval.bounds(), (0,1)) v = variable(domain=unit_interval) self.assertEqual(v.domain_type, RealSet) self.assertEqual(v.is_continuous(), True) self.assertEqual(v.is_discrete(), False) self.assertEqual(v.is_binary(), False) self.assertEqual(v.is_integer(), False) self.assertEqual(v.lb, 0) self.assertEqual(v.ub, 1) self.assertEqual(v.bounds, (0, 1)) with self.assertRaises(ValueError): variable(domain=unit_interval, lb=0) with self.assertRaises(ValueError): variable(domain=unit_interval, ub=0) v = variable() v.domain = unit_interval self.assertEqual(v.domain_type, RealSet) self.assertEqual(v.is_continuous(), True) self.assertEqual(v.is_discrete(), False) self.assertEqual(v.is_binary(), False) self.assertEqual(v.is_integer(), False) self.assertEqual(v.lb, 0) self.assertEqual(v.ub, 1) self.assertEqual(v.bounds, (0, 1)) binary = IntegerInterval(bounds=(0,1)) self.assertEqual(binary.bounds(), (0,1)) v = variable(domain=binary) self.assertEqual(v.domain_type, IntegerSet) self.assertEqual(v.is_continuous(), False) self.assertEqual(v.is_discrete(), True) self.assertEqual(v.is_binary(), True) self.assertEqual(v.is_integer(), True) self.assertEqual(v.lb, 0) self.assertEqual(v.ub, 1) self.assertEqual(v.bounds, (0, 1)) with self.assertRaises(ValueError): variable(domain=binary, lb=0) with self.assertRaises(ValueError): variable(domain=binary, ub=0) v = variable() v.domain = binary self.assertEqual(v.domain_type, IntegerSet) self.assertEqual(v.is_continuous(), False) self.assertEqual(v.is_discrete(), True) self.assertEqual(v.is_binary(), True) self.assertEqual(v.is_integer(), True) self.assertEqual(v.lb, 0) self.assertEqual(v.ub, 1) self.assertEqual(v.bounds, (0, 1)) variable(domain_type=RealSet) variable(domain=Reals) with self.assertRaises(ValueError): variable(domain_type=RealSet, domain=Reals) with self.assertRaises(ValueError): variable(domain_type=BooleanSet)
def test_binary_type(self): v = variable() v.domain_type = IntegerSet self.assertEqual(v.domain_type, IntegerSet) self.assertEqual(v.is_continuous(), False) self.assertEqual(v.is_discrete(), True) self.assertEqual(v.is_binary(), False) self.assertEqual(v.is_integer(), True) self.assertEqual(v.lb, None) self.assertEqual(v.ub, None) self.assertEqual(v.bounds, (None, None)) self.assertEqual(v.has_lb(), False) self.assertEqual(v.has_ub(), False) v.lb = 0 v.ub = 1 self.assertEqual(v.domain_type, IntegerSet) self.assertEqual(v.is_continuous(), False) self.assertEqual(v.is_discrete(), True) self.assertEqual(v.is_binary(), True) self.assertEqual(v.is_integer(), True) self.assertEqual(v.lb, 0) self.assertEqual(type(v.lb), int) self.assertEqual(v.ub, 1) self.assertEqual(type(v.ub), int) self.assertEqual(v.bounds, (0,1)) v.lb = 0 v.ub = 0 self.assertEqual(v.domain_type, IntegerSet) self.assertEqual(v.is_continuous(), False) self.assertEqual(v.is_discrete(), True) self.assertEqual(v.is_binary(), True) self.assertEqual(v.is_integer(), True) self.assertEqual(v.lb, 0) self.assertEqual(v.ub, 0) self.assertEqual(v.bounds, (0,0)) v.lb = 1 v.ub = 1 self.assertEqual(v.domain_type, IntegerSet) self.assertEqual(v.is_continuous(), False) self.assertEqual(v.is_discrete(), True) self.assertEqual(v.is_binary(), True) self.assertEqual(v.is_integer(), True) self.assertEqual(v.lb, 1) self.assertEqual(v.ub, 1) self.assertEqual(v.bounds, (1,1)) v = variable(domain=Binary) self.assertEqual(v.domain_type, IntegerSet) self.assertEqual(v.is_continuous(), False) self.assertEqual(v.is_discrete(), True) self.assertEqual(v.is_binary(), True) self.assertEqual(v.is_integer(), True) self.assertEqual(v.lb, 0) self.assertEqual(v.ub, 1) self.assertEqual(v.bounds, (0,1)) v.ub = 2 self.assertEqual(v.domain_type, IntegerSet) self.assertEqual(v.is_continuous(), False) self.assertEqual(v.is_discrete(), True) self.assertEqual(v.is_binary(), False) self.assertEqual(v.is_integer(), True) self.assertEqual(v.lb, 0) self.assertEqual(v.ub, 2) self.assertEqual(v.bounds, (0,2)) v.lb = -1 self.assertEqual(v.domain_type, IntegerSet) self.assertEqual(v.is_continuous(), False) self.assertEqual(v.is_discrete(), True) self.assertEqual(v.is_binary(), False) self.assertEqual(v.is_integer(), True) self.assertEqual(v.lb, -1) self.assertEqual(v.ub, 2) self.assertEqual(v.bounds, (-1,2)) v.domain = Binary self.assertEqual(v.domain_type, IntegerSet) self.assertEqual(v.is_continuous(), False) self.assertEqual(v.is_discrete(), True) self.assertEqual(v.is_binary(), True) self.assertEqual(v.is_integer(), True) self.assertEqual(v.lb, 0) self.assertEqual(v.ub, 1) self.assertEqual(v.bounds, (0,1)) v.domain_type = RealSet self.assertEqual(v.domain_type, RealSet) self.assertEqual(v.is_continuous(), True) self.assertEqual(v.is_discrete(), False) self.assertEqual(v.is_binary(), False) self.assertEqual(v.is_integer(), False) self.assertEqual(v.lb, 0) self.assertEqual(v.ub, 1) self.assertEqual(v.bounds, (0,1)) v.domain_type = IntegerSet self.assertEqual(v.domain_type, IntegerSet) self.assertEqual(v.is_continuous(), False) self.assertEqual(v.is_discrete(), True) self.assertEqual(v.is_binary(), True) self.assertEqual(v.is_integer(), True) self.assertEqual(v.lb, 0) self.assertEqual(v.ub, 1) self.assertEqual(v.bounds, (0,1)) v.domain = Reals self.assertEqual(v.domain_type, RealSet) self.assertEqual(v.is_continuous(), True) self.assertEqual(v.is_discrete(), False) self.assertEqual(v.is_binary(), False) self.assertEqual(v.is_integer(), False) self.assertEqual(v.lb, None) self.assertEqual(v.ub, None) self.assertEqual(v.bounds, (None, None)) self.assertEqual(v.has_lb(), False) self.assertEqual(v.has_ub(), False) v.bounds = (float('-inf'), float('inf')) self.assertEqual(v.domain_type, RealSet) self.assertEqual(v.is_continuous(), True) self.assertEqual(v.is_discrete(), False) self.assertEqual(v.is_binary(), False) self.assertEqual(v.is_integer(), False) self.assertEqual(v.lb, float('-inf')) self.assertEqual(v.ub, float('inf')) self.assertEqual(v.bounds, (float('-inf'),float('inf'))) self.assertEqual(v.has_lb(), False) self.assertEqual(v.has_ub(), False) pL = parameter() v.lb = pL pU = parameter() v.ub = pU v.domain_type = IntegerSet self.assertEqual(v.domain_type, IntegerSet) self.assertEqual(v.is_continuous(), False) self.assertEqual(v.is_discrete(), True) with self.assertRaises(ValueError): v.is_binary() self.assertEqual(v.is_integer(), True) self.assertIs(v.lb, pL) with self.assertRaises(ValueError): v.has_lb() self.assertIs(v.ub, pU) with self.assertRaises(ValueError): v.has_ub() pL.value = 0 pU.value = 1 self.assertEqual(v.domain_type, IntegerSet) self.assertEqual(v.is_continuous(), False) self.assertEqual(v.is_discrete(), True) self.assertEqual(v.is_binary(), True) self.assertEqual(v.is_integer(), True) self.assertEqual(v.lb(), 0) self.assertEqual(v.ub(), 1) self.assertEqual(v.has_lb(), True) self.assertEqual(v.has_ub(), True) pL.value = 0 pU.value = 0 self.assertEqual(v.domain_type, IntegerSet) self.assertEqual(v.is_continuous(), False) self.assertEqual(v.is_discrete(), True) self.assertEqual(v.is_binary(), True) self.assertEqual(v.is_integer(), True) self.assertEqual(v.lb(), 0) self.assertEqual(v.ub(), 0) pL.value = 1 pU.value = 1 self.assertEqual(v.domain_type, IntegerSet) self.assertEqual(v.is_continuous(), False) self.assertEqual(v.is_discrete(), True) self.assertEqual(v.is_binary(), True) self.assertEqual(v.is_integer(), True) self.assertEqual(v.lb(), 1) self.assertEqual(v.ub(), 1) v.domain = Binary pU.value = 2 v.ub = pU self.assertEqual(v.domain_type, IntegerSet) self.assertEqual(v.is_continuous(), False) self.assertEqual(v.is_discrete(), True) self.assertEqual(v.is_binary(), False) self.assertEqual(v.is_integer(), True) self.assertEqual(v.lb, 0) self.assertEqual(v.ub(), 2) pL.value = -1 v.lb = pL self.assertEqual(v.domain_type, IntegerSet) self.assertEqual(v.is_continuous(), False) self.assertEqual(v.is_discrete(), True) self.assertEqual(v.is_binary(), False) self.assertEqual(v.is_integer(), True) self.assertEqual(v.lb(), -1) self.assertEqual(v.ub(), 2) pL.value = float('-inf') pU.value = float('inf') self.assertEqual(v.domain_type, IntegerSet) self.assertEqual(v.is_continuous(), False) self.assertEqual(v.is_discrete(), True) self.assertEqual(v.is_binary(), False) self.assertEqual(v.is_integer(), True) self.assertEqual(v.lb(), float('-inf')) self.assertEqual(v.ub(), float('inf')) self.assertEqual(v.has_lb(), False) self.assertEqual(v.has_ub(), False)
def test_slack_methods(self): x = variable(value=2) L = 1 U = 5 # equality x.bounds = L,L x.value = 4 self.assertEqual(x.value, 4) self.assertEqual(x.slack, -3) self.assertEqual(x.lslack, 3) self.assertEqual(x.uslack, -3) x.value = 6 self.assertEqual(x.value, 6) self.assertEqual(x.slack, -5) self.assertEqual(x.lslack, 5) self.assertEqual(x.uslack, -5) x.value = 0 self.assertEqual(x.value, 0) self.assertEqual(x.slack, -1) self.assertEqual(x.lslack, -1) self.assertEqual(x.uslack, 1) x.value = None self.assertEqual(x.value, None) self.assertEqual(x.slack, None) self.assertEqual(x.lslack, None) self.assertEqual(x.uslack, None) # equality x.bounds = U, U x.value = 4 self.assertEqual(x.value, 4) self.assertEqual(x.slack, -1) self.assertEqual(x.lslack, -1) self.assertEqual(x.uslack, 1) x.value = 6 self.assertEqual(x.value, 6) self.assertEqual(x.slack, -1) self.assertEqual(x.lslack, 1) self.assertEqual(x.uslack, -1) x.value = 0 self.assertEqual(x.value, 0) self.assertEqual(x.slack, -5) self.assertEqual(x.lslack, -5) self.assertEqual(x.uslack, 5) x.value = None self.assertEqual(x.value, None) self.assertEqual(x.slack, None) self.assertEqual(x.lslack, None) self.assertEqual(x.uslack, None) # lower finite x.bounds = L, None x.value = 4 self.assertEqual(x.value, 4) self.assertEqual(x.slack, 3) self.assertEqual(x.lslack, 3) self.assertEqual(x.uslack, float('inf')) x.value = 6 self.assertEqual(x.value, 6) self.assertEqual(x.slack, 5) self.assertEqual(x.lslack, 5) self.assertEqual(x.uslack, float('inf')) x.value = 0 self.assertEqual(x.value, 0) self.assertEqual(x.slack, -1) self.assertEqual(x.lslack, -1) self.assertEqual(x.uslack, float('inf')) x.value = None self.assertEqual(x.value, None) self.assertEqual(x.slack, None) self.assertEqual(x.lslack, None) self.assertEqual(x.uslack, None) # lower unbounded x.bounds = float('-inf'), None x.value = 4 self.assertEqual(x.value, 4) self.assertEqual(x.slack, float('inf')) self.assertEqual(x.lslack, float('inf')) self.assertEqual(x.uslack, float('inf')) x.value = 6 self.assertEqual(x.value, 6) self.assertEqual(x.slack, float('inf')) self.assertEqual(x.lslack, float('inf')) self.assertEqual(x.uslack, float('inf')) x.value = 0 self.assertEqual(x.value, 0) self.assertEqual(x.slack, float('inf')) self.assertEqual(x.lslack, float('inf')) self.assertEqual(x.uslack, float('inf')) x.value = None self.assertEqual(x.value, None) self.assertEqual(x.slack, None) self.assertEqual(x.lslack, None) self.assertEqual(x.uslack, None) # upper finite x.bounds = None, U x.value = 4 self.assertEqual(x.value, 4) self.assertEqual(x.slack, 1) self.assertEqual(x.lslack, float('inf')) self.assertEqual(x.uslack, 1) x.value = 6 self.assertEqual(x.value, 6) self.assertEqual(x.slack, -1) self.assertEqual(x.lslack, float('inf')) self.assertEqual(x.uslack, -1) x.value = 0 self.assertEqual(x.value, 0) self.assertEqual(x.slack, 5) self.assertEqual(x.lslack, float('inf')) self.assertEqual(x.uslack, 5) x.value = None self.assertEqual(x.value, None) self.assertEqual(x.slack, None) self.assertEqual(x.lslack, None) self.assertEqual(x.uslack, None) # upper unbounded x.bounds = None, float('inf') x.value = 4 self.assertEqual(x.value, 4) self.assertEqual(x.slack, float('inf')) self.assertEqual(x.lslack, float('inf')) self.assertEqual(x.uslack, float('inf')) x.value = 6 self.assertEqual(x.value, 6) self.assertEqual(x.slack, float('inf')) self.assertEqual(x.lslack, float('inf')) self.assertEqual(x.uslack, float('inf')) x.value = 0 self.assertEqual(x.value, 0) self.assertEqual(x.slack, float('inf')) self.assertEqual(x.lslack, float('inf')) self.assertEqual(x.uslack, float('inf')) x.value = None self.assertEqual(x.value, None) self.assertEqual(x.slack, None) self.assertEqual(x.lslack, None) self.assertEqual(x.uslack, None) # range finite x.bounds = L, U x.value = 4 self.assertEqual(x.value, 4) self.assertEqual(x.slack, 1) self.assertEqual(x.lslack, 3) self.assertEqual(x.uslack, 1) x.value = 6 self.assertEqual(x.value, 6) self.assertEqual(x.slack, -1) self.assertEqual(x.lslack, 5) self.assertEqual(x.uslack, -1) x.value = 0 self.assertEqual(x.value, 0) self.assertEqual(x.slack, -1) self.assertEqual(x.lslack, -1) self.assertEqual(x.uslack, 5) x.value = None self.assertEqual(x.value, None) self.assertEqual(x.slack, None) self.assertEqual(x.lslack, None) self.assertEqual(x.uslack, None) # range unbounded (None) x.bounds = None, None x.value = 4 self.assertEqual(x.value, 4) self.assertEqual(x.slack, float('inf')) self.assertEqual(x.lslack, float('inf')) self.assertEqual(x.uslack, float('inf')) x.value = 6 self.assertEqual(x.value, 6) self.assertEqual(x.slack, float('inf')) self.assertEqual(x.lslack, float('inf')) self.assertEqual(x.uslack, float('inf')) x.value = 0 self.assertEqual(x.value, 0) self.assertEqual(x.slack, float('inf')) self.assertEqual(x.lslack, float('inf')) self.assertEqual(x.uslack, float('inf')) x.value = None self.assertEqual(x.value, None) self.assertEqual(x.slack, None) self.assertEqual(x.lslack, None) self.assertEqual(x.uslack, None) # range unbounded x.bounds = float('-inf'), float('inf') x.value = 4 self.assertEqual(x.value, 4) self.assertEqual(x.slack, float('inf')) self.assertEqual(x.lslack, float('inf')) self.assertEqual(x.uslack, float('inf')) x.value = 6 self.assertEqual(x.value, 6) self.assertEqual(x.slack, float('inf')) self.assertEqual(x.lslack, float('inf')) self.assertEqual(x.uslack, float('inf')) x.value = 0 self.assertEqual(x.value, 0) self.assertEqual(x.slack, float('inf')) self.assertEqual(x.lslack, float('inf')) self.assertEqual(x.uslack, float('inf')) x.value = None self.assertEqual(x.value, None) self.assertEqual(x.slack, None) self.assertEqual(x.lslack, None) self.assertEqual(x.uslack, None) # range finite (parameter) x.bounds = parameter(L), parameter(U) x.value = 4 self.assertEqual(x.value, 4) self.assertEqual(x.slack, 1) self.assertEqual(x.lslack, 3) self.assertEqual(x.uslack, 1) x.value = 6 self.assertEqual(x.value, 6) self.assertEqual(x.slack, -1) self.assertEqual(x.lslack, 5) self.assertEqual(x.uslack, -1) x.value = 0 self.assertEqual(x.value, 0) self.assertEqual(x.slack, -1) self.assertEqual(x.lslack, -1) self.assertEqual(x.uslack, 5) x.value = None self.assertEqual(x.value, None) self.assertEqual(x.slack, None) self.assertEqual(x.lslack, None) self.assertEqual(x.uslack, None) # range unbounded (parameter) x.bounds = parameter(float('-inf')), parameter(float('inf')) x.value = 4 self.assertEqual(x.value, 4) self.assertEqual(x.slack, float('inf')) self.assertEqual(x.lslack, float('inf')) self.assertEqual(x.uslack, float('inf')) x.value = 6 self.assertEqual(x.value, 6) self.assertEqual(x.slack, float('inf')) self.assertEqual(x.lslack, float('inf')) self.assertEqual(x.uslack, float('inf')) x.value = 0 self.assertEqual(x.value, 0) self.assertEqual(x.slack, float('inf')) self.assertEqual(x.lslack, float('inf')) self.assertEqual(x.uslack, float('inf')) x.value = None self.assertEqual(x.value, None) self.assertEqual(x.slack, None) self.assertEqual(x.lslack, None) self.assertEqual(x.uslack, None)