Exemple #1
0
def test_lwr_quadratic():
    """Test LWLR on 2nd order polynomial models of dimensions from 1 to 1.
     (should pass most of the time)"""
    result = True

    for i in range(20):

        a = random.random()
        b = random.random()
        c = random.random()
        f = lambda x: [a*x[0]*x[0] + b*x[0] + c]
        model = LWLRForwardModel(1, 1, 0.1)
        robot = robots.fun.RobotFunction(f, 1, 1)

        for i in range(100):
            x = np.random.rand(1)
            y = f(x)
            model.add_xy(x, y)

        tb = models.testbed.Testbed(robot, fmodel = model)
        tb.uniform_motor(20)
        errors = tb.run_forward()
        result = result and sum(errors) < len(errors)*1e-3

        # for i in xrange(10):
        #     x = np.random.rand(1)
        #     y = f(x)
        #     yp = model.predict_y(x)
        #     # import tools
        #     # print tools.dist(y, yp)
        #     result = result and np.allclose(y, yp, rtol = 1e-1, atol = 1e-1)

    return result
Exemple #2
0
def test_lwr_linear():
    """Test LWLR on random linear models of dimensions from 1 to 20.
     It should return exact results, give of take floating point imprecisions."""
    result = True

    for i in range(20):
        n = random.randint(1, 20)
        m = random.randint(1, 5)
        f = random_linear(n, m)
        model = LWLRForwardModel(n, m, 1.0)

        for i in range(2*n):
            x = np.random.rand(n)
            y = f(x)
            model.add_xy(x, y)

        for i in range(10):
            x = np.random.rand(n).ravel()
            y = f(x)
            yp = model.predict_y(x)
            #print 'y ', y
            #print 'yp', yp
            if not np.allclose(y, yp, rtol = 1e-10, atol = 1e-10):
                print(n, m)
            result = result and np.allclose(y, yp, rtol = 1e-10, atol = 1e-10)

    return result
Exemple #3
0
def test_lwr_linear_testbed():
    """Same test as previously, this time using testbed api."""
    result = True

    for i in range(20):
        n = random.randint(1, 20)
        m = random.randint(1, 20)
        f = random_linear(n, m)
        robot = robots.fun.RobotFunction(f, n, m)
        model = LWLRForwardModel(n, m, 1.0)

        for i in range(2*n):
            x = np.random.rand(n)
            y = f(x)
            model.add_xy(x, y)

        tb = models.testbed.Testbed(robot, fmodel = model)
        tb.uniform_motor(10)
        errors = tb.run_forward()
        result = result and sum(errors) < len(errors)*1e-10

    return result
Exemple #4
0
def test_lwr1D_linear():
    """Simplest test possible (well, not quite, but close)."""
    result = True

    f = lambda x : 2.0*x
    model = LWLRForwardModel(1, 1, k = 3, sigma = 0.1)

    for i in range(10):
        x = np.random.rand(1)
        y = f(x)
        #print x, y
        model.add_xy(x, y)

    for i in range(10):
        x = np.random.rand(1).ravel()
        y = f(x)
        yp = model.predict_y(x)
        #print 'y ', y
        #print 'yp', yp
        result = result and np.allclose(y, yp, rtol = 1e-5, atol = 1e-5)

    return result