def test_init(self): """Test VarKey initialization""" # test no-name init _ = ArrayVariable(1) # test protected field with self.assertRaises(ValueError): _ = ArrayVariable(1, idx=5) # test type x = VarKey('x') self.assertEqual(type(x), VarKey) # test no args x = VarKey() self.assertEqual(type(x), VarKey) y = VarKey(**x.descr) self.assertEqual(x, y) # test special 'name' keyword overwriting behavior x = VarKey('x', flavour='vanilla') self.assertEqual(x.name, 'x') x = VarKey(name='x') self.assertEqual(x.name, 'x') # pylint: disable=redundant-keyword-arg self.assertRaises(TypeError, lambda: VarKey('x', name='y')) self.assertIsInstance(x.latex(), str) self.assertIsInstance(x.latex_unitstr(), str) # test index latex printing y = VectorVariable(2, "y") self.assertEqual(y[0].key.latex(), "{\\vec{y}}_{0}")
def test_relaxation(self): x = Variable("x") y = Variable("y") with SignomialsEnabled(): constraints = [y + x >= 2, y <= x] objective = x m = Model(objective, constraints) m.localsolve(verbosity=0) # issue #257 A = VectorVariable(2, "A") B = ArrayVariable([2, 2], "B") C = VectorVariable(2, "C") with SignomialsEnabled(): constraints = [A <= B.dot(C), B <= 1, C <= 1] obj = 1 / A[0] + 1 / A[1] m = Model(obj, constraints) m.localsolve(verbosity=0)
def test_relaxation(self): x = Variable("x") y = Variable("y") with SignomialsEnabled(): constraints = [y + x >= 2, y <= x] objective = x m = Model(objective, constraints) m.localsolve(verbosity=0) # issue #257 A = VectorVariable(2, "A") B = ArrayVariable([2, 2], "B") C = VectorVariable(2, "C") with SignomialsEnabled(): constraints = [A <= B.dot(C), B <= 1, C <= 1] obj = 1/A[0] + 1/A[1] m = Model(obj, constraints) m.localsolve(verbosity=0)
def test_str(self): """Make sure string looks something like a numpy array""" x = ArrayVariable((2, 4), 'x') self.assertEqual(str(x), "x[:]")
def test_str(self): """Make sure string looks something like a numpy array""" x = ArrayVariable((2, 4), 'x') strx = str(x) self.assertEqual(strx.count("["), 3) self.assertEqual(strx.count("]"), 3)