Exemple #1
0
 def test_check_result(self):
     """issue 361"""
     N = 5
     L = 5.
     dx = L / (N - 1)
     EI = Variable("EI", 10)
     p = VectorVariable(N, "p")
     p = p.sub(p, 100 * np.ones(N))
     V = VectorVariable(N, "V")
     M = VectorVariable(N, "M")
     th = VectorVariable(N, "th")
     w = VectorVariable(N, "w")
     eps = 1E-6
     substitutions = {var: eps for var in [V[-1], M[-1], th[0], w[0]]}
     objective = w[-1]
     constraints = [
         EI * V.left[1:N] >=
         EI * V[1:N] + 0.5 * dx * p.left[1:N] + 0.5 * dx * p[1:N],
         EI * M.left[1:N] >=
         EI * M[1:N] + 0.5 * dx * V.left[1:N] + 0.5 * dx * V[1:N],
         EI * th.right[0:N - 1] >= EI * th[0:N - 1] +
         0.5 * dx * M.right[0:N - 1] + 0.5 * dx * M[0:N - 1],
         EI * w.right[0:N - 1] >= EI * w[0:N - 1] +
         0.5 * dx * th.right[0:N - 1] + 0.5 * dx * th[0:N - 1]
     ]
     m = Model(objective, constraints, substitutions)
     sol = m.solve(verbosity=0)
Exemple #2
0
 def test_substition(self):
     x = VectorVariable(3, 'x', label='dummy variable')
     c = {x: [1, 2, 3]}
     self.assertEqual(x.sub(c), [Monomial({}, e) for e in [1, 2, 3]])
     p = x**2
     self.assertEqual(p.sub(c), [Monomial({}, e) for e in [1, 4, 9]])
     d = p.sum()
     self.assertEqual(d.sub(c), Monomial({}, 14))
Exemple #3
0
 def test_substition(self):
     x = VectorVariable(3, 'x', label='dummy variable')
     c = {x: [1, 2, 3]}
     self.assertEqual(x.sub(c), [Monomial({}, e) for e in [1, 2, 3]])
     p = x**2
     self.assertEqual(p.sub(c), [Monomial({}, e) for e in [1, 4, 9]])
     d = p.sum()
     self.assertEqual(d.sub(c), Monomial({}, 14))
Exemple #4
0
    def test_vector(self):
        x = Variable("x")
        y = Variable("y")
        z = VectorVariable(2, "z")
        p = x*y*z
        self.assertTrue(all(p.sub({x: 1, "y": 2}) == 2*z))
        self.assertTrue(all(p.sub({x: 1, y: 2, "z": [1, 2]}) ==
                            z.sub(z, [2, 4])))

        x = VectorVariable(3, "x", "m")
        xs = x[:2].sum()
        for x_ in ["x", x]:
            self.assertAlmostEqual(mag(xs.sub(x_, [1, 2, 3]).c), 3.0)
Exemple #5
0
    def test_vector(self):
        x = Variable("x")
        y = Variable("y")
        z = VectorVariable(2, "z")
        p = x*y*z
        self.assertTrue(all(p.sub({x: 1, "y": 2}) == 2*z))
        self.assertTrue(all(p.sub({x: 1, y: 2, "z": [1, 2]}) ==
                            z.sub({z: [2, 4]})))

        xvec = VectorVariable(3, "x", "m")
        xs = xvec[:2].sum()
        for x_ in ["x", xvec]:
            self.assertAlmostEqual(mag(xs.sub({x_: [1, 2, 3]}).c), 3.0)
Exemple #6
0
 def test_check_result(self):
     """issue 361"""
     N = 5
     L = 5.
     dx = L/(N-1)
     EI = Variable("EI",10)
     p = VectorVariable(N, "p")
     p = p.sub(p, 100*np.ones(N))
     V  = VectorVariable(N, "V")
     M  = VectorVariable(N, "M")
     th = VectorVariable(N, "th")
     w  = VectorVariable(N, "w")
     eps = 1E-6
     substitutions = {var: eps for var in [V[-1], M[-1], th[0], w[0]]}
     objective = w[-1]
     constraints = [EI*V.left[1:N]     >= EI*V[1:N]    + 0.5*dx*p.left[1:N]     + 0.5*dx*p[1:N],
                    EI*M.left[1:N]     >= EI*M[1:N]    + 0.5*dx*V.left[1:N]     + 0.5*dx*V[1:N],
                    EI*th.right[0:N-1] >= EI*th[0:N-1] + 0.5*dx*M.right[0:N-1]  + 0.5*dx*M[0:N-1],
                    EI*w.right[0:N-1]  >= EI*w[0:N-1]  + 0.5*dx*th.right[0:N-1] + 0.5*dx*th[0:N-1]]
     m = Model(objective, constraints, substitutions)
     sol = m.solve(verbosity=0)
Exemple #7
0
"Example substitution; adapted from t_sub.py/t_NomialSubs/test_Vector"
from gpkit import Variable, VectorVariable
x = Variable("x")
y = Variable("y")
z = VectorVariable(2, "z")
p = x * y * z
assert all(p.sub({x: 1, "y": 2}) == 2 * z)
assert all(p.sub({x: 1, y: 2, "z": [1, 2]}) == z.sub({z: [2, 4]}))