Esempio n. 1
0
def do_generate(args):
    K, D = 2, 2
    hyper = {
        'k': K,
        'd': D,
    }
    params = {
        (1, ): 0.4,
        (2, ): 0.6,
        (1, 1): 1,
        (1, 2): 0,
        (2, 1): 0,
        (2, 2): 1,
    }

    params = generate_parameters(hyper)
    params_pol = generate_poly(hyper, params)
    mom = generate_moments(hyper, params)
    pol = create_mom_poly(params_pol, mom)
    sol = polyopt.optimize_polynomial(pol)

    m = pol.monoms()

    print zip(m[:(K + K * D)], sol['x'][:(K + K * D)])
    return sol
Esempio n. 2
0
def do_generate(args):
    K, D = 2, 2
    hyper = {
            'k' : K,
            'd' : D,
            }
    params = {
            (1,) : 0.4,
            (2,) : 0.6,
            (1,1) : 1,
            (1,2) : 0,
            (2,1) : 0,
            (2,2) : 1,
            }

    params = generate_parameters(hyper)
    params_pol = generate_poly(hyper, params)
    mom = generate_moments(hyper, params)
    pol = create_mom_poly(params_pol,mom)
    sol = polyopt.optimize_polynomial(pol)

    m = pol.monoms()

    print zip(m[:(K+K*D)], sol['x'][:(K+K*D)])
    return sol
Esempio n. 3
0
def test_independent_quadratic():
    """
    x = 1, y = 2
    Test the simple quadratic (x - 3)^2 + (y - 5)^2.
    """

    a, b = 2., 3.
    x, y = symbols('x,y')
    pol = polyopt.by_evaluation(x, y, x=a, y=b)
    sol = polyopt.optimize_polynomial(pol)
    x_sol = sol[x], sol[y]
    assert np.allclose([a, b], x_sol, 1e-3)
Esempio n. 4
0
def test_symmetric_quadratic():
    """
    x = 1, y = 2
    Test the simple quadratic (x + y - 3)^2 + (x^2 + y^2 - 5)^2.
    """

    a, b = 1., 2.
    x, y = symbols('x,y')
    pol = polyopt.by_evaluation(x + y, x**2 + y**2, x=a, y=b)
    sol = polyopt.optimize_polynomial(pol)
    x_sol = sol[x], sol[y]
    # This doesn't work.
    assert np.allclose([a, b], x_sol, 1e-3) == False
Esempio n. 5
0
def test_cubic():
    """
    Test the simple quadratic (x-a)^2.
    """
    a = 2.

    x = symbols('x')
    pol = poly((x - a)**6)
    x_opt = a

    sol = polyopt.optimize_polynomial(pol)
    x_sol = sol[x]

    assert np.allclose(x_opt, x_sol, 1e-1)
Esempio n. 6
0
def test_cubic():
    """
    Test the simple quadratic (x-a)^2.
    """
    a = 2.

    x = symbols('x')
    pol = poly( (x - a)**6 )
    x_opt = a

    sol = polyopt.optimize_polynomial(pol)
    x_sol = sol[x]

    assert np.allclose( x_opt, x_sol, 1e-1 )
Esempio n. 7
0
def do_generate(args):
    hyper = { 'k': args.k, 'd' : args.d, 'v' : args.v }

    # TODO: Support arbitrary number of moments.
    if hyper['v'] != 3: 
        raise NotImplementedError() 

    params = generate_parameters(hyper)
    params_pol = generate_poly(hyper, params)
    mom = generate_moments(hyper, params)
    pol = create_mom_poly(params_pol,mom)
    sol = polyopt.optimize_polynomial(pol)

    print sol
    return sol
Esempio n. 8
0
def test_independent_quadratic():
    """
    x = 1, y = 2
    Test the simple quadratic (x - 3)^2 + (y - 5)^2.
    """

    a, b = 2., 3.
    x, y = symbols('x,y')
    pol = polyopt.by_evaluation(
            x,
            y,
            x = a,
            y = b)
    sol = polyopt.optimize_polynomial(pol)
    x_sol = sol[x], sol[y]
    assert np.allclose( [a, b], x_sol, 1e-3 )
Esempio n. 9
0
def test_symmetric_quadratic():
    """
    x = 1, y = 2
    Test the simple quadratic (x + y - 3)^2 + (x^2 + y^2 - 5)^2.
    """

    a, b = 1., 2.
    x, y = symbols('x,y')
    pol = polyopt.by_evaluation(
            x + y,
            x**2 + y**2,
            x = a,
            y = b)
    sol = polyopt.optimize_polynomial(pol)
    x_sol = sol[x], sol[y]
    # This doesn't work.
    assert np.allclose( [a, b], x_sol, 1e-3 ) == False
Esempio n. 10
0
def test_broken_quadratic():
    """
    x = 1, y = 2
    Test the simple quadratic (pi * x + (1-pi) * y - 3)^2 + (x^2 + y^2 - 5)^2.
    """

    a, b, pi = 1., 2., 0.4,
    x, y = symbols('x,y')
    pol = polyopt.by_evaluation(pi * x + (1 - pi) * y,
                                pi * x**2 + (1 - pi) * y**2,
                                x=a,
                                y=b)
    sol = polyopt.optimize_polynomial(pol)
    x_opt = np.array([a, b])
    x_sol = sol[x], sol[y]

    diff = np.sqrt((x_opt - x_sol).dot(x_opt - x_sol))
    print diff
    # This doesn't work.
    assert np.allclose(x_opt, x_sol, 1e-1)
Esempio n. 11
0
def test_broken_quadratic():
    """
    x = 1, y = 2
    Test the simple quadratic (pi * x + (1-pi) * y - 3)^2 + (x^2 + y^2 - 5)^2.
    """

    a, b, pi = 1., 2., 0.4,
    x, y = symbols('x,y')
    pol = polyopt.by_evaluation(
            pi * x + (1-pi) * y,
            pi * x**2 + (1-pi) * y**2,
            x = a,
            y = b)
    sol = polyopt.optimize_polynomial(pol)
    x_opt = np.array([a,b])
    x_sol = sol[x], sol[y]

    diff = np.sqrt((x_opt - x_sol).dot(x_opt - x_sol))
    print diff
    # This doesn't work.
    assert np.allclose( x_opt, x_sol, 1e-1 )