Beispiel #1
0
 def test_vector_sweep(self):
     """Test sweep involving VectorVariables"""
     x = Variable("x")
     x_min = Variable("x_min", 1)
     y = VectorVariable(2, "y")
     m = Model(x, [x >= y.prod()])
     m.substitutions.update({y: ('sweep', [[2, 3], [5, 7], [9, 11]])})
     a = m.solve(verbosity=0)["cost"]
     b = [6, 15, 27, 14, 35, 63, 22, 55, 99]
     self.assertTrue(all(abs(a - b) / (a + b) < 1e-7))
     x_min = Variable("x_min", 1)  # constant to check array indexing
     m = Model(x, [x >= y.prod(), x >= x_min])
     m.substitutions.update({y: ('sweep', [[2, 3], [5, 7, 11]])})
     sol = m.solve(verbosity=0)
     a = sol["cost"]
     b = [10, 15, 14, 21, 22, 33]
     self.assertTrue(all(abs(a - b) / (a + b) < 1e-7))
     self.assertEqual(sol["constants"][x_min], 1)
     for i, bi in enumerate(b):
         self.assertEqual(sol.atindex(i)["constants"][x_min], 1)
         ai = m.solution.atindex(i)["cost"]
         self.assertTrue(abs(ai - bi) / (ai + bi) < 1e-7)
     m = Model(x, [x >= y.prod()])
     m.substitutions.update({y: ('sweep', [[2, 3, 9], [5, 7, 11]])})
     self.assertRaises(ValueError, m.solve, verbosity=0)
Beispiel #2
0
 def test_vector_sweep(self):
     x = Variable("x")
     y = VectorVariable(2, "y")
     m = Model(x, [x >= y.prod()])
     m.substitutions.update({y: ('sweep', [[2, 3], [5, 7, 11]])})
     a = m.solve(verbosity=0)["cost"]
     b = [10, 14, 22, 15, 21, 33]
     # below line fails with changing dictionary keys in py3
     # self.assertTrue(all(abs(a-b)/(a+b) < 1e-7))
     m = Model(x, [x >= y.prod()])
     m.substitutions.update({y: ('sweep',
                                 [[2, 3], [5, 7], [9, 11], [13, 15]])})
     self.assertRaises(ValueError, m.solve)
Beispiel #3
0
 def test_vector_sweep(self):
     """Test sweep involving VectorVariables"""
     x = Variable("x")
     y = VectorVariable(2, "y")
     m = Model(x, [x >= y.prod()])
     m.substitutions.update({y: ('sweep', [[2, 3], [5, 7, 11]])})
     a = m.solve(verbosity=0)["cost"]
     b = [10, 14, 22, 15, 21, 33]
     # below line fails with changing dictionary keys in py3
     # self.assertTrue(all(abs(a-b)/(a+b) < 1e-7))
     m = Model(x, [x >= y.prod()])
     m.substitutions.update(
         {y: ('sweep', [[2, 3], [5, 7], [9, 11], [13, 15]])})
     self.assertRaises(ValueError, m.solve)
Beispiel #4
0
 def test_constraintget(self):
     x = Variable("x")
     x_ = Variable("x", model="_")
     xv = VectorVariable(2, "x")
     xv_ = VectorVariable(2, "x", model="_")
     self.assertEqual(Model(x, [x >= 1])["x"], x)
     with self.assertRaises(ValueError):
         _ = Model(x, [x >= 1, x_ >= 1])["x"]
     with self.assertRaises(ValueError):
         _ = Model(x, [x >= 1, xv >= 1])["x"]
     self.assertTrue(all(Model(xv.prod(), [xv >= 1])["x"] == xv))
     with self.assertRaises(ValueError):
         _ = Model(xv.prod(), [xv >= 1, xv_ >= 1])["x"]
     with self.assertRaises(ValueError):
         _ = Model(xv.prod(), [xv >= 1, x_ >= 1])["x"]
Beispiel #5
0
 def test_constraintget(self):
     x = Variable("x")
     x_ = Variable("x", models=["_"])
     xv = VectorVariable(2, "x")
     xv_ = VectorVariable(2, "x", models=["_"])
     self.assertEqual(Model(x, [x >= 1])["x"], x)
     with self.assertRaises(ValueError):
         _ = Model(x, [x >= 1, x_ >= 1])["x"]
     with self.assertRaises(ValueError):
         _ = Model(x, [x >= 1, xv >= 1])["x"]
     self.assertTrue(all(Model(xv.prod(), [xv >= 1])["x"] == xv))
     with self.assertRaises(ValueError):
         _ = Model(xv.prod(), [xv >= 1, xv_ >= 1])["x"]
     with self.assertRaises(ValueError):
         _ = Model(xv.prod(), [xv >= 1, x_ >= 1])["x"]
Beispiel #6
0
    def setup(self, goods=None, bads=None, taylor_order=6):
        goods = goods if goods else {}
        goods.update({1 / k: 1 / v for k, v in bads.items()})
        N = check_values_length(goods)

        exp_S = VectorVariable(N, "e^{S}")
        VectorVariable(N, "S")

        self.cost = 1
        constraints = [[exp_S >= 1, exp_S.prod() == np.e]]
        for monomial, options in goods.items():
            m_nd = Variable("|%s|" %
                            monomial.latex(excluded=["models", "units"]))
            exp_m = Variable("e^{%s}" % m_nd.latex(excluded=["models"]))
            self.cost /= monomial
            if hasattr(options, "units"):
                monomial = monomial / (1 * options.units)
                options = options.magnitude
            options_scale = options.mean()
            options /= options_scale
            constraints.append([
                m_nd == monomial / options_scale,
                (exp_S**options).prod() == exp_m,
                exp_m >= 1 + te_exp_minus1(m_nd, taylor_order),
            ])

        return constraints
Beispiel #7
0
    def test_vector_sub(self):
        x = VectorVariable(3, "x")
        y = VectorVariable(3, "y")
        ymax = VectorVariable(3, "ymax")

        with SignomialsEnabled():
            # issue1077 links to a case that failed for SPs only
            m = Model(x.prod(), [x + y >= 1, y <= ymax])

        m.substitutions["ymax"] = [0.3, 0.5, 0.8]
        m.localsolve(verbosity=0)
Beispiel #8
0
    def test_vector_sub(self):
        x = VectorVariable(3, "x")
        y = VectorVariable(3, "y")
        ymax = VectorVariable(3, "ymax")

        with SignomialsEnabled():
            # issue1077 links to a case that failed for SPs only
            m = Model(x.prod(), [x + y >= 1, y <= ymax])

        m.substitutions["ymax"] = [0.3, 0.5, 0.8]
        m.localsolve(verbosity=0)
Beispiel #9
0
 def test_quantity_sub(self):
     if gpkit.units:
         x = Variable("x", 1, "cm")
         y = Variable("y", 1)
         self.assertEqual(x.sub({x: 1 * gpkit.units.m}).c.magnitude, 100)
         # NOTE: uncomment the below if requiring Quantity substitutions
         # self.assertRaises(ValueError, x.sub, x, 1)
         self.assertRaises(ValueError, x.sub, {x: 1 * gpkit.ureg.N})
         self.assertRaises(ValueError, y.sub, {y: 1 * gpkit.ureg.N})
         v = gpkit.VectorVariable(3, "v", "cm")
         subbed = v.sub({v: [1, 2, 3] * gpkit.ureg.m})
         self.assertEqual([z.c.magnitude for z in subbed], [100, 200, 300])
         v = VectorVariable(1, "v", "km")
         v_min = VectorVariable(1, "v_min", "km")
         m = Model(v.prod(), [v >= v_min],
                   {v_min: [2 * gpkit.units("nmi")]})
         cost = m.solve(verbosity=0)["cost"]
         self.assertAlmostEqual(cost / (3.704 * gpkit.ureg("km")), 1.0)
         m = Model(v.prod(), [v >= v_min],
                   {v_min: np.array([2]) * gpkit.units("nmi")})
         cost = m.solve(verbosity=0)["cost"]
         self.assertAlmostEqual(cost / (3.704 * gpkit.ureg("km")), 1.0)
Beispiel #10
0
 def test_quantity_sub(self):
     if gpkit.units:
         x = Variable("x", 1, "cm")
         y = Variable("y", 1)
         self.assertEqual(x.sub({x: 1*gpkit.units.m}).c.magnitude, 100)
         # NOTE: uncomment the below if requiring Quantity substitutions
         # self.assertRaises(ValueError, x.sub, x, 1)
         self.assertRaises(ValueError, x.sub, {x: 1*gpkit.ureg.N})
         self.assertRaises(ValueError, y.sub, {y: 1*gpkit.ureg.N})
         v = gpkit.VectorVariable(3, "v", "cm")
         subbed = v.sub({v: [1, 2, 3]*gpkit.ureg.m})
         self.assertEqual([z.c.magnitude for z in subbed], [100, 200, 300])
         v = VectorVariable(1, "v", "km")
         v_min = VectorVariable(1, "v_min", "km")
         m = Model(v.prod(), [v >= v_min],
                   {v_min: [2*gpkit.units("nmi")]})
         cost = m.solve(verbosity=0)["cost"]
         self.assertAlmostEqual(cost/(3.704*gpkit.ureg("km")), 1.0)
         m = Model(v.prod(), [v >= v_min],
                   {v_min: np.array([2])*gpkit.units("nmi")})
         cost = m.solve(verbosity=0)["cost"]
         self.assertAlmostEqual(cost/(3.704*gpkit.ureg("km")), 1.0)
Beispiel #11
0
 def test_phantoms(self):
     x = Variable("x")
     x_ = Variable("x", 1, models=["test"])
     xv = VectorVariable(2, "x", [1, 1], models=["vec"])
     m = Model(x, [x >= x_, x_ == xv.prod()])
     m.solve(verbosity=0)
     with self.assertRaises(ValueError):
         _ = m.substitutions["x"]
     with self.assertRaises(KeyError):
         _ = m.substitutions["y"]
     with self.assertRaises(ValueError):
         _ = m["x"]
     self.assertIn(x, m.variables_byname("x"))
     self.assertIn(x_, m.variables_byname("x"))
Beispiel #12
0
 def test_phantoms(self):
     x = Variable("x")
     x_ = Variable("x", 1, models=["test"])
     xv = VectorVariable(2, "x", [1, 1], models=["vec"])
     m = Model(x, [x >= x_, x_ == xv.prod()])
     m.solve(verbosity=0)
     with self.assertRaises(ValueError):
         _ = m.substitutions["x"]
     with self.assertRaises(KeyError):
         _ = m.substitutions["y"]
     with self.assertRaises(ValueError):
         _ = m["x"]
     self.assertIn(x, m.variables_byname("x"))
     self.assertIn(x_, m.variables_byname("x"))
Beispiel #13
0
 def test_prod(self):
     x = VectorVariable(3, 'x')
     m = x.prod()
     self.assertTrue(isinstance(m, Monomial))
     self.assertEqual(m, x[0] * x[1] * x[2])
     self.assertEqual(m, np.prod(x))
Beispiel #14
0
 def test_prod(self):
     x = VectorVariable(3, 'x')
     m = x.prod()
     self.assertTrue(isinstance(m, Monomial))
     self.assertEqual(m, x[0]*x[1]*x[2])
     self.assertEqual(m, np.prod(x))