Exemplo n.º 1
0
def _solve_cvxopt(E, f, G, h, A, b):
    """
    """
    P = dot(E.T, E)
    q = dot(f, E)
    Pp = matrix(P)
    qp = matrix(q)
    Gp = matrix(G)
    hp = matrix(h)
    Ap = matrix(A)
    bp = matrix(b)

    for degenerate_rank in range(-16, -5):
        try:
            results = qpsolver(Pp, qp, Gp, hp, Ap, bp)
        except ValueError as err:
            print "Exception Error:", err.args
            if err.args[0] == "Rank(A) < p or Rank([P; A; G]) < n":
                print 'Try to degenerate P to ensure rank([P; A; G])=n'
                P += eye(P.shape[0]) * 10**(degenerate_rank)
                Pp = matrix(P)
            elif err.args[0] == "domain error":
                print "Should delete some constraints"
        else:
            break

    X_sol = array(results['x']).flatten()
    return X_sol
Exemplo n.º 2
0
def _solve_cvxopt(E, f, G, h, A, b):
    """
    """
    P = dot(E.T, E)
    q = dot(f, E)
    Pp = matrix(P)
    qp = matrix(q)
    Gp = matrix(G)
    hp = matrix(h)
    Ap = matrix(A)
    bp = matrix(b)

    for degenerate_rank in range(-16, -5):
        try:
            results = qpsolver(Pp, qp, Gp, hp, Ap, bp)
        except ValueError as err:
            print "Exception Error:", err.args
            if err.args[0] == "Rank(A) < p or Rank([P; A; G]) < n":
                print 'Try to degenerate P to ensure rank([P; A; G])=n'
                P += eye(P.shape[0])*10**(degenerate_rank)
                Pp = matrix(P)
            elif err.args[0] == "domain error":
                print "Should delete some constraints"
        else:
            break

    X_sol = array(results['x']).flatten()
    return X_sol
Exemplo n.º 3
0
h = zeros(nInequality)
if "G:" in Problist:
    G = array([float(v) for v in Problist[Problist.index('G:')+1 : Problist.index('G:')+ nInequality*nProblem +1]]).reshape(nInequality,nProblem)
if "h:" in Problist:
    h = array([float(v) for v in Problist[Problist.index('h:')+1 : Problist.index('h:')+ nInequality +1]]).reshape(nInequality)

print "P:\n", P
print "q:\n", q
print "A:\n", A
print "b:\n", b
print "G:\n", G
print "h:\n", h

Pp = matrix(P)
qp = matrix(q)
Gp = matrix(G)
hp = matrix(h)
Ap = matrix(A)
bp = matrix(b)

solvers.options.update({'abstol': 1e-12, 'reltol': 1e-12, 'feastol': 1e-12,})

results = qpsolver(Pp, qp, Gp, hp, Ap, bp)

X_sol = array(results['x']).flatten()

print "cost:", .5*dot(X_sol, dot(P, X_sol)) + dot(q, X_sol)
print X_sol