Example #1
0
def solveLSQ(b, A, B, bxl, bxu, bcl, bcu):
    """
    Solves a constrained linear least squares problem

    minimize   2-norm (A*x-b)^2
    subject to  bcl <= B*x <= bcu
                bxl <=  x  <= bxu
    """
    (n, m) = A.shape

    q = numpy.zeros(m + n)
    q[m:m + n] = 1

    C1 = numpy.hstack((A, -numpy.eye(n, n)))               # A*x - I*u = [A, -I]*[x;u]
    C2 = numpy.hstack((B, numpy.zeros((B.shape[0], n))))

    qp = QuadraticObjectiveProgram(Q=numpy.diag(v=q, k=0),
                                   p=numpy.zeros(n + m),
                                   A=numpy.vstack((C1, C2)))

    qp.lp.bc.lower = numpy.concatenate((b, bcl))
    qp.lp.bc.upper = numpy.concatenate((b, bcu))

    qp.lp.bx.lower = numpy.concatenate((bxl, -numpy.inf * numpy.ones(n)))
    qp.lp.bx.upper = numpy.concatenate((bxu, +numpy.inf * numpy.ones(n)))

    return qp.solve()[0][0:m]
Example #2
0
def solveLASSO(b, A, B, bxl, bxu, bcl, bcu, tau):
    """
    Solves a constrained linear least squares problem

    minimize   2-norm (A*x-b)^2 + tau 1-norm(x)
    subject to  bcl <= B*x <= bcu
                bxl <=  x  <= bxu
    """
    (n, m) = A.shape

    q = numpy.zeros(2 * m + n)   #[x,z,t]
    q[m:m + n] = 1

    C1 = numpy.hstack((A, -numpy.eye(n, n), numpy.zeros((n, m))))               # A*x - I*u = [A, -I]*[x;u]
    C2 = numpy.hstack((B, numpy.zeros((B.shape[0], n + m))))
    E = numpy.eye(m, m)
    C3 = numpy.hstack((E, numpy.zeros((m, n)), -E))
    C4 = numpy.hstack((-E, numpy.zeros((m, n)), -E))

    qp = QuadraticObjectiveProgram(Q=numpy.diag(v=q, k=0),
                                   p=numpy.concatenate((numpy.zeros(n + m), tau * numpy.ones(m))),
                                   A=numpy.vstack((C1, C2, C3, C4)))

    qp.lp.bc.lower = numpy.concatenate((b, bcl, -numpy.inf * numpy.ones(2 * m)))
    qp.lp.bc.upper = numpy.concatenate((b, bcu, numpy.zeros(2 * m)))

    qp.lp.bx.lower = numpy.concatenate((bxl, -numpy.inf * numpy.ones(n + m)))
    qp.lp.bx.upper = numpy.concatenate((bxu, +numpy.inf * numpy.ones(n + m)))

    return qp.solve()[0][0:m]
Example #3
0
def solveQPobj(c, A, Q, bxl, bxu, bcl, bcu):
    """
    Solves a convex program with a quadratic objective

    maximize    c'*x - (1/2) x'*Q*x
    subject to    bcl <= A*x <= bcu
                  bxl <=  x  <= bxu
    """
    qp = QuadraticObjectiveProgram(Q=Q, p=-c, A=A)
    qp.lp.bc.lower = bcl
    qp.lp.bc.upper = bcu
    qp.lp.bx.lower = bxl
    qp.lp.bx.upper = bxu
    return qp.solve()[0]