def test_model_composition_units(self): class Above(Model): """A simple upper bound on x Lower Unbounded --------------- x """ def setup(self): x = self.x = Variable("x", "ft") x_max = Variable("x_{max}", 1, "yard") self.cost = 1 / x return [x <= x_max] class Below(Model): """A simple lower bound on x Upper Unbounded --------------- x """ def setup(self): x = self.x = Variable("x", "m") x_min = Variable("x_{min}", 1, "cm") self.cost = x return [x >= x_min] a, b = Above(), Below() concatm = Model(a.cost * b.cost, [a, b]) concat_cost = concatm.solve(verbosity=0)["cost"] almostequal = self.assertAlmostEqual yard, cm = gpkit.ureg("yard"), gpkit.ureg("cm") if not isinstance(a["x"].key.units, str): almostequal(1 / yard / a.solve(verbosity=0)["cost"], 1, 5) almostequal(1 * cm / b.solve(verbosity=0)["cost"], 1, 5) almostequal(1 * cm / yard / concat_cost, 1, 5) reset_modelnumbers() a1, b1 = Above(), Below() self.assertEqual(a1["x"].key.modelnums, [0]) b1.subinplace({b1["x"]: a1["x"]}) m = Model(a1["x"], [a1, b1]) sol = m.solve(verbosity=0) if not isinstance(m["x"].key.units, str): almostequal(1 * cm / sol["cost"], 1, 5) a1, b1 = Above(), Below() self.assertEqual(a1["x"].key.modelnums, [1]) a1.subinplace({a1["x"]: b1["x"]}) m = Model(b1["x"], [a1, b1]) m.cost = m["x"] sol = m.solve(verbosity=0) if not isinstance(m["x"].key.units, str): almostequal(1 * gpkit.ureg.cm / sol["cost"], 1, 5) self.assertIn(m["x"], sol["variables"]) self.assertIn(a1["x"], sol["variables"]) self.assertIn(b1["x"], sol["variables"]) self.assertNotIn(a["x"], sol["variables"]) self.assertNotIn(b["x"], sol["variables"])
def test_reassigned_constant_cost(self): # for issue 1131 x = Variable('x') x_min = Variable('x_min', 1) y = Variable('y') with SignomialsEnabled(): m = Model(y, [y + 0.5 >= x, x >= x_min, 6 >= y]) m.localsolve(verbosity=0, solver=self.solver) del m.substitutions[x_min] m.cost = 1 / x_min self.assertNotIn(x_min, m.sp().substitutions)
def test_reassigned_constant_cost(self): # for issue 1131 x = Variable('x') x_min = Variable('x_min', 1) y = Variable('y') with SignomialsEnabled(): m = Model(y, [y + 0.5 >= x, x >= x_min, 6 >= y]) m.localsolve(verbosity=0, solver=self.solver) del m.substitutions[x_min] m.cost = 1/x_min self.assertNotIn(x_min, m.sp().substitutions)
def test_reassigned_constant_cost(self): # for issue 1131 x = Variable("x") x_min = Variable("x_min", 1) y = Variable("y") with SignomialsEnabled(): m = Model(y, [y + 0.5 >= x, x >= x_min, 6 >= y]) m.localsolve(verbosity=0, solver=self.solver) del m.substitutions[x_min] m.cost = 1 / x_min self.assertNotIn(x_min, m.sp().gp().substitutions) # pylint: disable=no-member