Ejemplo n.º 1
0
 def __init__(self, *args, **kwargs):
     super(cMyTest, self).__init__(*args, **kwargs)
     self.N_ = N = 20  # np.random.randint(2, 10)
     self.dim_ = dim = 8  # np.random.randint(1, 8)
     self.wp_ = wp = np.random.rand(N + 1, dim)
     self.basis_ = basis = cBasis0010()
     self.cost_ = cJerkL2Norm(wp, basis)
Ejemplo n.º 2
0
    def test_l2_norms(self):
        ''' Test L2 norms '''
        tau = np.random.rand() * 5.0
        Bimpl = cBasis0010()
        Qd3 = np.zeros((6, 6))
        Qd1 = np.zeros((6, 6))
        Bimpl.l2_norm(tau, Qd3, 3)
        Bimpl.l2_norm(tau, Qd1, 1)
        y = np.random.rand(Bimpl.dim_)

        def qd3norm2(s):
            res = Bimpl.evalDerivOnWindow(s, tau, 3).dot(y)
            return np.power(res, 2.0) * tau / 2.0

        def qd1norm2(s):
            res = Bimpl.evalDerivOnWindow(s, tau, 1).dot(y)
            return np.power(res, 2.0) * tau / 2.0

        dt = 5.0e-6
        for f, Q in [(qd1norm2, Qd1), (qd3norm2, Qd3)]:
            fv = np.array([f(t) for t in np.arange(-1, 1, dt)])
            Itest = np.sum(fv[1:] + fv[:-1]) * dt / 2.0

            Inom = Q.dot(y).dot(y)

            err = np.abs(Itest - Inom)
            assert err < 5.0e-2, '''
            Error in integral = {:.4f}
            Nominal integral = {:.4f}
            Test integral = {:.4f}
            '''.format(err, Inom, Itest)
Ejemplo n.º 3
0
    def test_value(self):
        ''' Compare the value of the symbolic and the implemented Legendre
        Polynomials'''
        for item in range(0, 100):
            Bimpl = cBasis0010()
            tau_ = 10.0 * np.random.rand()
            s_ = np.random.rand() * 2.0 - 1.0
            B = [Bi.subs({self.tausym_: tau_}) for Bi in self.Bsym_]
            B = [sp.lambdify(self.ssym_, Bi) for Bi in B]
            B = np.array([Bi(s_) for Bi in B])

            e = np.max(np.abs(B - Bimpl.evalOnWindow(s_, tau_)))
            # print('error = {:.3e}\r'.format(e), end='')

            assert (e < 1.0e-10)
Ejemplo n.º 4
0
    def test_derivative_y_2(self):
        ''' Second test for the derivative of y wr.t. tau.
        First test the identity A*dydtau + dAdtau y = 0A
        Second test the identity of above using basis
        '''
        for i in range(40):
            np.random.seed()
            dim = np.random.randint(1, 3)
            N = np.random.randint(2, 6)
            a = np.random.rand()
            wp = (np.random.rand(N + 1, dim) - 0.5) * 2 * np.pi
            tauv = 0.5 + np.random.rand(N) * 2.0
            splcalc = cSplineCalc(dim, N, cBasis0010())
            basis = splcalc.basis_
            dydtauNom, y = splcalc.eval_dydtau(tauv, wp)
            A = splcalc.eval_A(tauv).todense()
            y = y.copy()

            # A*dydtau + dAdtau y = 0
            for i, _ in enumerate(tauv):
                v0 = A.dot(dydtauNom[:, i])
                v1 = splcalc.eval_dAdtiy(tauv, i, y).todense()
                res = v0 + v1.T
                e = np.abs(res)

                assert np.max(e) < 1.0e-10, '''
                e = {:14.7e}
                '''.format(e)

            for i, taui in enumerate(tauv):

                B = basis.evalDerivOnWindow(-1, taui, 0)
                dB_dtau = basis.evalDerivWrtTauOnWindow(-1, taui, 0)

                for idim in range(dim):
                    i0 = i * 6 * dim + 6 * idim
                    i1 = i0 + 6
                    e = dydtauNom[i0:i1, i].dot(B) + y[i0:i1].dot(dB_dtau)

                    assert np.abs(e) < 1.0e-10, '''
                    error computing dydtau^\\top B + y^\\top dB_dtau
                    e            = {:14.7e}
                    index of tau = {:d}
                    index of q   = {:d}
                    i0           = {:d}
                    i1           = {:d}
                    '''.format(e, i, idim, i0, i1)
Ejemplo n.º 5
0
    def test_derivatives_wrt_t(self):
        ''' Compare the derivative w.r.t. t of the symbolic and the implemented
        Legendre Polynomials'''
        for item in range(0, 100):
            Bimpl = cBasis0010()
            tau_ = 10.0 * np.random.rand()
            s_ = np.random.rand() * 2.0 - 1.0
            ddeg = np.random.randint(1, 6)
            B = [
                Bi.diff(self.ssym_, ddeg) * sp.Pow(2 / self.tausym_, ddeg)
                for Bi in self.Bsym_
            ]
            B = [Bi.subs({self.tausym_: tau_}) for Bi in B]
            B = [sp.lambdify(self.ssym_, Bi) for Bi in B]
            B = np.array([Bi(s_) for Bi in B])

            e = np.max(np.abs(B - Bimpl.evalDerivOnWindow(s_, tau_, ddeg)))

            assert (
                e < 5.0e-3
            ), 'Large error on derivatives wrt t,    error = {:+.3e}'.format(e)
Ejemplo n.º 6
0
    def test_values(self):

        server = xmlrpc.client.ServerProxy(self.url_)
        dim = 6  # np.random.randint(2, 6)
        N = 5  # np.random.randint(3, 120)
        T = 10.0
        box = np.pi
        unique_id = 'hola'
        q0 = rand_interpolate(N, dim, cBasis0010(), T, box)
        tspan = np.arange(0, T, 0.01)
        q0t = q0(tspan)
        q0dt = q0.deriv()(tspan)

        server.gspline_load(unique_id, piecewise2json(q0))

        q1t = np.array([
            server.gspline_desired_position(unique_id, float(t)) for t in tspan
        ])
        q1dt = np.array([
            server.gspline_desired_velocity(unique_id, float(t)) for t in tspan
        ])

        assert np.linalg.norm(q0t - q1t, ord=np.inf) < 1.0e-10
        assert np.linalg.norm(q0dt - q1dt, ord=np.inf) < 1.0e-10
Ejemplo n.º 7
0
    def test_container(self):

        dim = 6  # np.random.randint(2, 6)
        N = 5  # np.random.randint(3, 120)
        T = 10.0
        box = np.pi
        q0 = rand_interpolate(N, dim, cBasis0010(), T, box)

        server = xmlrpc.client.ServerProxy(self.url_)

        server.gspline_load('unique_id', piecewise2json(q0))

        res = server.gspline_get('u*')
        q1 = [json2piecewise(el) for el in res][0]

        timespan = np.arange(0, T, 0.01)

        assert np.linalg.norm(q0(timespan) - q1(timespan),
                              ord=np.inf) < 1.0e-10

        server.gspline_load('unique_id_2', piecewise2json(q0))
        res = server.gspline_get('u*')

        assert len(res) == 2