예제 #1
0
def test_matrix_sq2norm_sqcon():
    """ Relaxed Form : Minimize(square(norm(v0))) with v0 >= square(v1) """

    v0 = Variable(name = 'v0')
    v1 = Variable(name = 'v1')

    p0 = Parameter(name = 'p0')
    p1 = Parameter(name = 'p1')

    obj = square(norm(v0 + v1))
    con = [v0 >= square(v1)]  # should become -v0 + square(v1) <= 0

    p = Problem(Minimize(obj), con)
    c = Canonicalize(p, verbose=True)

    sym3 = [v for vn, v in c.vars.items() if vn == 'sym3'][0]

    assert_A = {'row': [0, 0, 0],
         'col': [0, 1, 4],
         'val': [Constant(-1.0), Constant(-1.0), Constant(1.0)]}

    assert_b = [Constant(0.0)]
    #assert_G = {'row': [0, 0, 1, 2, 3, 4, 5, 6, 7, 8],
    #     'col': [0, 5, 3, 4, 2, 2, 3, 5, 5, 1],
    #     'val': [Constant(-1.0), Constant(0.0), Constant(-1.0), Constant(1.0),
    #             Constant(-1.0), Constant(-1.0), Constant(2.0), Constant(-1.0),
    #             Constant(-1.0), Constant(2.0)]}

    assert_G = {'row': [0, 0, 1, 2, 3, 4, 5, 6, 7, 8],
         'col': [0, 5, 3, 4, 2, 2, 3, 5, 5, 1],
         'val': [Constant(-1.0), Constant(1.0), Constant(-1.0), Constant(1.0),
                 Constant(-1.0), Constant(-1.0), Constant(2.0), Constant(-1.0),
                 Constant(-1.0), Constant(2.0)]}

    assert_h = [ (-1.0 * sym3), Constant(0.0), Constant(0.0), Constant(1.0),
                Constant(-1.0), Constant(0.0), Constant(1.0),
                Constant(-1.0), Constant(0.0)]

    assert_h = [Constant(0.0), Constant(0.0), Constant(0.0), Constant(1.0),
                Constant(-1.0), Constant(0.0), Constant(1.0), Constant(-1.0),
                Constant(0.0)]

    assert_dims = {'q': [2, 3, 3], 'l': 1}

    assert_A = COO_to_CS(assert_A, (len(assert_b), len(c.c)), 'col')
    assert_G = COO_to_CS(assert_G, (len(assert_h), len(c.c)), 'col')

    assert_c = [0.0, 0.0, 1.0, 0.0, 0.0, 0.0]

    assert(c.c == assert_c)
    assert(c.A == assert_A)
    assert(c.b == assert_b)

    assert(c.G == assert_G)
    assert(all([c.h[n].value == t.value for n, t in enumerate(assert_h)]))

    assert(c.dims == assert_dims)

    reset_symbols()
예제 #2
0
def test_elementwisesymbol_into_function():
    """ Test a shaped symbol input to a scalar function and
        the elementwise output """

    x = Variable((3, 1), name='x')

    expr = square(x)

    print(expr)

    constr = (expr <= 0)

    print(constr)

    expand = constr.expand()

    print(expand)

    assert (type(constr.expr) is sums.sum)
    assert (type(constr.expr.args[0]) is Vector)
    assert (len(constr.expr.args[0].args) == 3)
    assert (len(expand) == 3)
    assert (all([type(a) is le for a in expand]))

    reset_symbols()
예제 #3
0
def test_square_graph():

    v0 = Variable(name='v0')
    s0 = Symbol(name='s0')

    expr = square(v0)

    constr = (expr <= s0)  # effectively in relaxed smith form

    print('Graph form of', end='')
    print(constr)
    constr = constr.graph_form()
    print('-->', constr)
    print()

    n = max(v0.shape[0], v0.shape[1])
    assert (constr.dims == n + 2)
    assert (type(constr) is SecondOrderConeConstraint)
    assert (all([type(c) is le for c in constr.constraints]))

    c = constr  # keeps below names same as other tests

    string_equals = [
        """(-1.0 + (-1.0 * s0)) <= 0""", """(1.0 + (-1.0 * s0)) <= 0""",
        """((2.0 * v0)) <= 0"""
    ]

    for n, string in enumerate(string_equals):
        print(c.constraints[n], '==', string, '?')
        assert (str(c.constraints[n]) == str(string))
        print('SUCCESS!')

    reset_symbols()
예제 #4
0
def test_matrix_sq():
    """ Matrix Form of Minimize(square(v0)) """

    v0 = Variable(name = 'v0')
    v1 = Variable(name = 'v1')

    p0 = Parameter(name = 'p0')
    p1 = Parameter(name = 'p1')

    obj = square(v0)
    con = []

    p = Problem(Minimize(obj), con)
    c = Canonicalize(p, verbose=True)

    assert(c.c == [0, 0, 1])
    assert(c.A is None)
    assert(c.b is None)

    assert_G =  {
                    'row':[0, 1, 2],
                    'col':[2, 2, 0],
                    'val':[Constant(-1.0), Constant(-1.0), Constant(2.0)]
                }


    assert_h = [Constant(1.0), Constant(-1.0), Constant(0.0)]
    assert_G = COO_to_CS(assert_G, (len(assert_h), len(c.c)), 'col')

    assert(c.G == assert_G)
    assert(all([c.h[n].value == t.value for n, t in enumerate(assert_h)]))

    assert(c.dims == {'q': [3], 'l': 0})

    reset_symbols()
예제 #5
0
def test_relax_sq2norm_constr():
    """ Relaxed Form of Minimize(square(norm(v0))) with v0 >= p0"""

    v0 = Variable(name = 'v0')
    v1 = Variable(name = 'v1')

    p0 = Parameter(name = 'p0')
    p1 = Parameter(name = 'p1')

    obj = square(norm(v0))
    con = [v0 >= p0]  # should become -v0 + p0 <= 0

    p = Problem(Minimize(obj), con)
    c = Canonicalize(p, verbose=True, only='relax')

    assert( type(c.constraints[-1]) is le)
    assert( type(c.constraints[-1].expr) is sums.sum)
    assert( type(c.constraints[-1].expr.args[0]) is muls.smul)
    assert( type(c.constraints[-1].expr.args[1]) is muls.smul)
    assert( c.objective is not obj   )
    assert( 'sym0' == c.objective.name )

    string_equals = [
        """((-1.0 * sym1) + (1.0 * norm2(v0))) <= 0""",
        """((-1.0 * sym0) + (1.0 * square(sym1))) <= 0""",
        """((-1.0 * v0) + (p0 * 1.0)) <= 0""",
    ]

    for n, string in enumerate(string_equals):
        print(c.constraints[n],'==',string,'?')
        assert(str(c.constraints[n]) == string)
        print('SUCCESS!')

    reset_symbols()
예제 #6
0
def test_relax_sq2norm():
    """ Relaxed Form of Minimize(square(norm(v0))) """

    v0 = Variable(name = 'v0')
    v1 = Variable(name = 'v1')

    p0 = Parameter(name = 'p0')
    p1 = Parameter(name = 'p1')

    obj = square(norm(v0))
    con = []

    p = Problem(Minimize(obj), con)
    c = Canonicalize(p, verbose=True, only='relax')

    assert( c.objective is not obj   )
    assert( 'sym0' == c.objective.name )

    # Let up on the testing rigor a bit, now that we checked core fundamentals

    string_equals = [
        """((-1.0 * sym1) + (1.0 * norm2(v0))) <= 0""",
        """((-1.0 * sym0) + (1.0 * square(sym1))) <= 0""",
    ]

    for n, string in enumerate(string_equals):
        print(c.constraints[n],'==',string,'?')
        assert(str(c.constraints[n]) == string)
        print('SUCCESS!')

    reset_symbols()
예제 #7
0
def test_relax_sq():
    """ Relaxed Form of Minimize(square(v0)) """

    v0 = Variable(name = 'v0')
    v1 = Variable(name = 'v1')

    p0 = Parameter(name = 'p0')
    p1 = Parameter(name = 'p1')

    obj = square(v0)
    con = []

    p = Problem(Minimize(obj), con)
    c = Canonicalize(p, verbose=True, only='relax')

    assert( len(c.constraints) == 1 )
    assert( c.objective is not obj   )
    assert( 'sym0' == c.objective.name )

    assert(type(c.constraints[0]) is le)

    assert(c.constraints[0].expr.args[0].curvature == 0)
    assert(c.constraints[0].expr.args[1].curvature == +1)
    assert(c.constraints[0].expr.args[1].symbol_groups()[0][0].value == 1.0)
    assert(c.constraints[0].expr.args[1].symbol_groups()[2][0].name == 'square')

    reset_symbols()
예제 #8
0
def test_relax_least_squares_constr():
    """ Relaxed Form : Minimize(square(norm(F*x - g))) with x >= p0 """

    x = Variable ((3,1),name='x')
    F = Parameter((3,3),name='F')
    g = Parameter((3,1),name='g')

    objective = square(norm( F*x - g ))

    objective = Minimize(objective)
    problem   = Problem(objective, [x >= 0])

    c = Canonicalize(problem, verbose=True, only='relax')

    string_equals = [
        """(sym2 + (-1.0 * matmul(F, x))) == 0""",
        """(sym3 + (-1.0 * sym2) + (g * 1.0)) == 0""",
        """((-1.0 * sym1) + (1.0 * norm2(sym3))) <= 0""",
        """((-1.0 * sym0) + (1.0 * square(sym1))) <= 0""",
        """((-1.0 * x)) <= 0""",
    ]

    for n, string in enumerate(string_equals):
        print(c.constraints[n],' ???? ',string, end=' ')
        assert(str(c.constraints[n]) == string)
        print('... SUCCESS!')

    reset_symbols()
예제 #9
0
def test_smith_sq():
    """ Smith Form of Minimize(square(v0)) """

    v0 = Variable(name='v0')
    v1 = Variable(name='v1')

    p0 = Parameter(name='p0')
    p1 = Parameter(name='p1')

    obj = square(v0)
    con = []

    p = Problem(Minimize(obj), con)
    c = Canonicalize(p, verbose=True, only='smith')

    assert (len(c.constraints) == 1)
    assert (c.objective is not obj)
    assert ('sym0' == c.objective.name)

    assert (hasattr(c.constraints[0].expr.args[0], 'name'))
    assert (c.constraints[0].expr.args[0].name == 'sym0')

    assert (hasattr(c.constraints[0].expr.args[1], 'args'))
    assert (hasattr(c.constraints[0].expr.args[1].args[0], 'value'))
    assert (c.constraints[0].expr.args[1].args[0].value == -1.0)

    assert (hasattr(c.constraints[0].expr.args[1].args[1], 'name'))
    assert (c.constraints[0].expr.args[1].args[1].name == 'square')

    reset_symbols()
예제 #10
0
def test_smith_sq2norm_sqcon():
    """ Smith Form : Minimize(square(norm(v0))) with v0 >= square(v1) """

    v0 = Variable(name='v0')
    v1 = Variable(name='v1')

    p0 = Parameter(name='p0')
    p1 = Parameter(name='p1')

    obj = square(norm(v0 + v1))
    con = [v0 >= square(v1)]  # should become -v0 + square(v1) <= 0

    p = Problem(Minimize(obj), con)
    c = Canonicalize(p, verbose=True, only='smith')

    assert (type(c.constraints[-1]) is le)
    assert (type(c.constraints[-1].expr) is sums.sum)
    assert (type(c.constraints[-1].expr.args[0]) is muls.smul)
    assert (type(c.constraints[-1].expr.args[1]) is muls.smul)
    assert (c.objective is not obj)
    assert ('sym0' == c.objective.name)

    string_equals = [
        """(sym2 + (-1.0 * v0) + (-1.0 * v1)) == 0""",
        """(sym1 + (-1.0 * norm2(sym2))) == 0""",
        """(sym0 + (-1.0 * square(sym1))) == 0""",
        """(sym3 + (-1.0 * square(v1))) == 0""",
        """((-1.0 * v0) + (1.0 * sym3)) <= 0""",
    ]

    for n, string in enumerate(string_equals):
        print(c.constraints[n], ' ???? ', string, end=' ')
        assert (str(c.constraints[n]) == string)
        print('... SUCCESS!')

    reset_symbols()
예제 #11
0
def test_matrix_least_squares_constr():
    """ Minimize(square(norm(F*x - g))) with x >= p0 """

    x = Variable ((3,1),name='x')
    F = Parameter((3,3),name='F')
    g = Parameter((3,1),name='g')

    objective = square(norm( F*x - g ))

    objective = Minimize(objective)
    problem   = Problem(objective, [x >= 1])

    c = Canonicalize(problem, verbose=True)

    # Test for errors, not asserts, and for solution outcome (via ecos_solution)

    reset_symbols()
예제 #12
0
def test_matrix_sq2norm_constr():
    """ Matrix Form of Minimize(square(norm(v0))) with v0 >= p0"""

    v0 = Variable(name = 'v0')
    v1 = Variable(name = 'v1')

    p0 = Parameter(name = 'p0')
    p1 = Parameter(name = 'p1')

    obj = square(norm(v0))
    con = [v0 >= p0]  # should become -v0 + p0 <= 0

    p = Problem(Minimize(obj), con)
    c = Canonicalize(p, verbose=True)


    assert_c = [0.0, 0.0, 1.0, 0.0]

    assert_A = COO_to_CS({'row':[],'col':[],'val':[]}, (0,4), 'col')

    assert_G =  {'row': [0, 1, 2, 3, 4, 5], 'col': [0, 3, 0, 2, 2, 3],
                'val': [Constant(-1.0), Constant(-1.0), Constant(1.0),
                        Constant(-1.0), Constant(-1.0), Constant(2.0)]}

    assert_h = [(-1.0 * p0), Constant(0.0), Constant(0.0),
                Constant(1.0), Constant(-1.0), Constant(0.0)]

    assert_G = COO_to_CS(assert_G, (len(assert_h), len(c.c)), 'col')

    assert(c.c == assert_c)
    assert(c.A is None)
    assert(c.b is None)

    assert(c.G == assert_G)
    assert(all([c.h[n].value == t.value for n, t in enumerate(assert_h)]))

    assert(c.dims == {'q': [2, 3], 'l': 1})

    reset_symbols()
예제 #13
0
def test_elementwiselist_into_function():
    """ Test a list input to a scalar function and the elementwise output """

    x = Variable(name='x')
    y = Variable(name='y')
    z = Variable(name='z')
    a = Variable(name='a')
    b = Variable(name='b')
    c = Variable(name='c')

    expr = square([x, y, z, a, b, c])

    print(expr)

    constr = (expr <= 0)

    print(constr)

    assert (type(constr.expr) is sums.sum)
    assert (type(constr.expr.args[0]) is Vector)
    assert (len(constr.expr.args[0].args) == 6)

    reset_symbols()