Ejemplo n.º 1
0
def CVXOPT_SOCP_Solver(p, solverName):
    if solverName == 'native_CVXOPT_SOCP_Solver': solverName = None
    cvxopt_solvers.options['maxiters'] = p.maxIter
    cvxopt_solvers.options['feastol'] = p.contol    
    cvxopt_solvers.options['abstol'] = p.ftol
    if p.iprint <= 0:
        cvxopt_solvers.options['show_progress'] = False
        cvxopt_solvers.options['LPX_K_MSGLEV'] = 0
        cvxopt_solvers.options['MSK_IPAR_LOG'] = 0
    xBounds2Matrix(p)
    #FIXME: if problem is search for MAXIMUM, not MINIMUM!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    f = copy(p.f).reshape(-1,1)

    # CVXOPT has some problems with x0 so currently I decided to avoid using the one
    
    Gq, hq = [], []
    C, d, q, s = p.C, p.d, p.q, p.s
    for i in range(len(q)):
        Gq.append(Matrix(Vstack((-atleast_1d(q[i]),-atleast_1d(C[i]) if not isspmatrix(C[i]) else C[i]))))
        hq.append(matrix(hstack((atleast_1d(s[i]), atleast_1d(d[i]))), tc='d'))
    sol = cvxopt_solvers.socp(Matrix(p.f), Gl=Matrix(p.A), hl = Matrix(p.b), Gq=Gq, hq=hq, A=Matrix(p.Aeq), b=Matrix(p.beq), solver=solverName)
    p.msg = sol['status']
    if p.msg == 'optimal' :  p.istop = SOLVED_WITH_UNIMPLEMENTED_OR_UNKNOWN_REASON
    else: p.istop = -100
    if sol['x'] is not None:
        p.xf = asarray(sol['x']).flatten()
        p.ff = sum(p.dotmult(p.f, p.xf))
        
    else:
        p.ff = nan
        p.xf = nan*ones(p.n)
Ejemplo n.º 2
0
def CVXOPT_LP_Solver(p, solverName):
    #os.close(1); os.close(2)
    if solverName == 'native_CVXOPT_LP_Solver': solverName = None
    cvxopt_solvers.options['maxiters'] = p.maxIter
    cvxopt_solvers.options['feastol'] = p.contol    
    cvxopt_solvers.options['abstol'] = p.ftol
    if p.iprint <= 0:
        cvxopt_solvers.options['show_progress'] = False
        cvxopt_solvers.options['LPX_K_MSGLEV'] = 0
        cvxopt_solvers.options['MSK_IPAR_LOG'] = 0
    xBounds2Matrix(p)
    #WholeRepr2LinConst(p)

    # CVXOPT have some problems with x0 so currently I decided to avoid using the one
    #if  p.x0.size>0 and p.x0.flatten()[0] != None and all(isfinite(p.x0)):
    #    sol= cvxopt_solvers.solvers.lp(Matrix(p.f), Matrix(p.A), Matrix(p.b), Matrix(p.Aeq), Matrix(p.beq), solverName)
    #else:

    if (len(p.intVars)>0 or len(p.binVars)>0) and solverName == 'glpk':
        from cvxopt.glpk import ilp
        c = Matrix(p.f)
        A, b = Matrix(p.Aeq),  Matrix(p.beq)
        G, h = Matrix(p.A),  Matrix(p.b)
        if A is None:
            A = matrix(0.0,  (0, p.n))
            b = matrix(0.0,(0,1))
        if G is None:
            G = matrix(0.0,  (0, p.n))
            h = matrix(0.0,(0,1))

        (status, x) = ilp(c, G, h, A, b, set(p.intVars), B=set(p.binVars))
        if status == 'optimal': p.istop = SOLVED_WITH_UNIMPLEMENTED_OR_UNKNOWN_REASON
        elif status == 'maxiters exceeded': p.istop = IS_MAX_ITER_REACHED
        elif status == 'time limit exceeded': p.istop = IS_MAX_TIME_REACHED
        elif status == 'unknown': p.istop = UNDEFINED
        else: p.istop = FAILED_WITH_UNIMPLEMENTED_OR_UNKNOWN_REASON
        if x is None:
            p.xf = nan*ones(p.n)
        else:
            p.xf = array(x).flatten()#w/o flatten it yields incorrect result in ff!
        p.ff = sum(p.dotmult(p.f, p.xf))
        p.msg = status
    else:
#        if len(p.b) != 0: 
#            s0 = matrix(p.b - dot(p.A, p.x0))
#        else:
#            s0 = matrix()
#        primalstart = {'x': matrix(p.x0), 's': s0}
#        sol = cvxopt_solvers.lp(Matrix(p.f), Matrix(p.A), Matrix(p.b), Matrix(p.Aeq), Matrix(p.beq), solverName, primalstart)
        sol = cvxopt_solvers.lp(Matrix(p.f), Matrix(p.A), Matrix(p.b), Matrix(p.Aeq), Matrix(p.beq), solverName)
        p.msg = sol['status']
        if p.msg == 'optimal' :  p.istop = SOLVED_WITH_UNIMPLEMENTED_OR_UNKNOWN_REASON
        else: p.istop = -100
        if sol['x'] is not None:
            p.xf = asarray(sol['x']).flatten()
            # ! don't involve p.ff  - it can be different because of goal and additional constant from FuncDesigner
            p.duals = concatenate((asarray(sol['y']).flatten(), asarray(sol['z']).flatten()))
        else:
            p.ff = nan
            p.xf = nan*ones(p.n)
Ejemplo n.º 3
0
def CVXOPT_QP_Solver(p, solverName):
    if solverName == 'native_CVXOPT_QP_Solver': solverName = None
    cvxopt_solvers.options['maxiters'] = p.maxIter
    cvxopt_solvers.options['feastol'] = p.contol
    cvxopt_solvers.options['abstol'] = p.ftol
    cvxopt_solvers.options['reltol'] = 1e-16
    if p.iprint <= 0:
        cvxopt_solvers.options['show_progress'] = False
        cvxopt_solvers.options['MSK_IPAR_LOG'] = 0
    xBounds2Matrix(p)

    f = copy(p.f).reshape(-1, 1)

    sol = cvxopt_solvers.qp(Matrix(p.H), Matrix(p.f), Matrix(p.A), Matrix(p.b),
                            Matrix(p.Aeq), Matrix(p.beq), solverName)

    p.msg = sol['status']
    if p.msg == 'optimal': p.istop = 1000
    else: p.istop = -100

    if sol['x'] is not None:
        p.xf = xf = asfarray(sol['x']).flatten()
        p.ff = asfarray(0.5 * dot(xf, p.matMultVec(p.H, xf)) +
                        p.dotmult(p.f, xf).sum()).flatten()
        p.duals = concatenate(
            (asfarray(sol['y']).flatten(), asfarray(sol['z']).flatten()))
    else:
        p.ff = nan
        p.xf = nan * ones(p.n)
Ejemplo n.º 4
0
def CVXOPT_QP_Solver(p, solverName):
    if solverName == 'native_CVXOPT_QP_Solver': solverName = None
    cvxopt_solvers.options['maxiters'] = p.maxIter
    cvxopt_solvers.options['feastol'] = p.contol
    cvxopt_solvers.options['abstol'] = p.ftol
    cvxopt_solvers.options['reltol'] = 1e-16
    if p.iprint <= 0: 
        cvxopt_solvers.options['show_progress'] = False
        cvxopt_solvers.options['MSK_IPAR_LOG'] = 0
    xBounds2Matrix(p)
    
    f = copy(p.f).reshape(-1,1)
    
    sol = cvxopt_solvers.qp(Matrix(p.H), Matrix(p.f), Matrix(p.A), Matrix(p.b), Matrix(p.Aeq), Matrix(p.beq), solverName)
    
    p.msg = sol['status']
    if p.msg == 'optimal' :  p.istop = 1000
    else: p.istop = -100
    
    
    if sol['x'] is not None:
        p.xf = xf = asfarray(sol['x']).flatten()
        p.ff = asfarray(0.5*dot(xf, p.matMultVec(p.H, xf)) + p.dotmult(p.f, xf).sum()).flatten()
        p.duals = concatenate((asfarray(sol['y']).flatten(), asfarray(sol['z']).flatten()))
    else:
        p.ff = nan
        p.xf = nan*ones(p.n)
Ejemplo n.º 5
0
def CVXOPT_SDP_Solver(p, solverName):
    if solverName == 'native_CVXOPT_SDP_Solver': solverName = None
    cvxopt_solvers.options['maxiters'] = p.maxIter
    cvxopt_solvers.options['feastol'] = p.contol
    cvxopt_solvers.options['abstol'] = p.ftol
    if p.iprint <= 0:
        cvxopt_solvers.options['show_progress'] = False
        cvxopt_solvers.options['LPX_K_MSGLEV'] = 0
        #cvxopt_solvers.options['MSK_IPAR_LOG'] = 0
    xBounds2Matrix(p)

    #FIXME: if problem is search for MAXIMUM, not MINIMUM!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    f = copy(p.f).reshape(-1, 1)

    # CVXOPT have some problems with x0 so currently I decided to avoid using the one

    sol = cvxopt_solvers.sdp(Matrix(p.f), Matrix(p.A), Matrix(p.b), \
                             converter_to_CVXOPT_SDP_Matrices_from_OO_SDP_Class(p.S, p.n), \
                             DictToList(p.d), Matrix(p.Aeq), Matrix(p.beq), solverName)
    p.msg = sol['status']
    if p.msg == 'optimal':
        p.istop = SOLVED_WITH_UNIMPLEMENTED_OR_UNKNOWN_REASON
    else:
        p.istop = -100
    if sol['x'] is not None:
        p.xf = asarray(sol['x']).flatten()
        p.ff = sum(p.dotmult(p.f, p.xf))
        #p.duals = concatenate((asarray(sol['y']).flatten(), asarray(sol['z']).flatten()))
    else:
        p.ff = nan
        p.xf = nan * ones(p.n)
Ejemplo n.º 6
0
def CVXOPT_SDP_Solver(p, solverName):
    if solverName == 'native_CVXOPT_SDP_Solver': solverName = None
    cvxopt_solvers.options['maxiters'] = p.maxIter
    cvxopt_solvers.options['feastol'] = p.contol    
    cvxopt_solvers.options['abstol'] = p.ftol
    if p.iprint <= 0:
        cvxopt_solvers.options['show_progress'] = False
        cvxopt_solvers.options['LPX_K_MSGLEV'] = 0
        #cvxopt_solvers.options['MSK_IPAR_LOG'] = 0
    xBounds2Matrix(p)

    #FIXME: if problem is search for MAXIMUM, not MINIMUM!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    f = copy(p.f).reshape(-1,1)

    # CVXOPT have some problems with x0 so currently I decided to avoid using the one

    sol = cvxopt_solvers.sdp(Matrix(p.f), Matrix(p.A), Matrix(p.b), \
                             converter_to_CVXOPT_SDP_Matrices_from_OO_SDP_Class(p.S, p.n), \
                             DictToList(p.d), Matrix(p.Aeq), Matrix(p.beq), solverName)
    p.msg = sol['status']
    if p.msg == 'optimal' :  p.istop = SOLVED_WITH_UNIMPLEMENTED_OR_UNKNOWN_REASON
    else: p.istop = -100
    if sol['x'] is not None:
        p.xf = asarray(sol['x']).flatten()
        p.ff = sum(p.dotmult(p.f, p.xf))
        #p.duals = concatenate((asarray(sol['y']).flatten(), asarray(sol['z']).flatten()))
    else:
        p.ff = nan
        p.xf = nan*ones(p.n)
Ejemplo n.º 7
0
    def __solver__(self, p):
        #p.kernelIterFuncs.pop(SMALL_DELTA_X)
        #p.kernelIterFuncs.pop(SMALL_DELTA_F)
        xBounds2Matrix(p)
        p.cobyla = EmptyClass()
        if p.userProvided.c: p.cobyla.nc = p.c(p.x0).size
        else: p.cobyla.nc = 0
        if p.userProvided.h: p.cobyla.nh = p.h(p.x0).size
        else: p.cobyla.nh = 0

        det_arr = cumsum(array((p.cobyla.nc, p.cobyla.nh, p.b.size, p.beq.size, p.cobyla.nh, p.beq.size)))

        cons = []
        for i in range(det_arr[-1]):
            if i < det_arr[0]:
                c = lambda x, i=i: - p.c(x)[i] # cobyla requires positive constraints!
            elif det_arr[0] <= i < det_arr[1]:
                j = i - det_arr[0]
                c = lambda x, j=j: p.h(x)[j]
            elif det_arr[1] <= i < det_arr[2]:
                j = i - det_arr[1]
                #assert 0<= j <p.cobyla.nb
                c = lambda x, j=j: p.b[j] - p.dotmult(p.A[j], x).sum() # cobyla requires positive constraints!
            elif det_arr[2] <= i < det_arr[3]:
                j = i - det_arr[2]
                #assert 0<= j <p.cobyla.nbeq
                c = lambda x, j=j: p.dotmult(p.Aeq[j], x).sum() - p.beq[j]
            elif det_arr[3] <= i < det_arr[4]:
                j = i - det_arr[3]
                c = lambda x, j=j: - p.h(x)[j]
            elif det_arr[4] <= i < det_arr[5]:
                j = i - det_arr[4]
                #assert 0<= j <p.cobyla.nbeq
                c = lambda x, j=j: p.dotmult(p.Aeq[j], x).sum() - p.beq[j]
            else:
                p.err('error in connection cobyla to openopt')
            cons.append(c)
##        def oo_cobyla_cons(x):
##            c0 = -p.c(x)
##            c1 = p.h(x)
##            c2 = -(p.matmult(p.A, x) - p.b)
##            c3 = p.matmult(p.Aeq, x) - p.beq
##            return hstack((c0, c1, -c1, c2, c3, -c3))


#        p.xk = p.x0.copy()
#        p.fk = p.f(p.x0)
#
#        p.iterfcn()
#        if p.istop:
#            p.xf = p.xk
#            p.ff = p.fk
#            return


        xf = fmin_cobyla(p.f, p.x0, cons = tuple(cons), iprint = 0, maxfun = p.maxFunEvals, rhoend = p.xtol )

        p.xk = xf
        p.fk = p.f(xf)
        p.istop = 1000
Ejemplo n.º 8
0
def CVXOPT_SOCP_Solver(p, solverName):
    if solverName == 'native_CVXOPT_SOCP_Solver': solverName = None
    cvxopt_solvers.options['maxiters'] = p.maxIter
    cvxopt_solvers.options['feastol'] = p.contol
    cvxopt_solvers.options['abstol'] = p.ftol
    if p.iprint <= 0:
        cvxopt_solvers.options['show_progress'] = False
        cvxopt_solvers.options['LPX_K_MSGLEV'] = 0
        cvxopt_solvers.options['MSK_IPAR_LOG'] = 0
    xBounds2Matrix(p)
    #FIXME: if problem is search for MAXIMUM, not MINIMUM!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    f = copy(p.f).reshape(-1, 1)

    # CVXOPT has some problems with x0 so currently I decided to avoid using the one

    Gq, hq = [], []
    C, d, q, s = p.C, p.d, p.q, p.s
    for i in range(len(q)):
        Gq.append(
            Matrix(
                Vstack((-atleast_1d(q[i]),
                        -atleast_1d(C[i]) if not isspmatrix(C[i]) else C[i]))))
        hq.append(matrix(hstack((atleast_1d(s[i]), atleast_1d(d[i]))), tc='d'))
    sol = cvxopt_solvers.socp(Matrix(p.f),
                              Gl=Matrix(p.A),
                              hl=Matrix(p.b),
                              Gq=Gq,
                              hq=hq,
                              A=Matrix(p.Aeq),
                              b=Matrix(p.beq),
                              solver=solverName)
    p.msg = sol['status']
    if p.msg == 'optimal':
        p.istop = SOLVED_WITH_UNIMPLEMENTED_OR_UNKNOWN_REASON
    else:
        p.istop = -100
    if sol['x'] is not None:
        p.xf = asarray(sol['x']).flatten()
        p.ff = sum(p.dotmult(p.f, p.xf))

    else:
        p.ff = nan
        p.xf = nan * ones(p.n)
Ejemplo n.º 9
0
    def __solver__(self, p):
        xBounds2Matrix(p)
        n = p.n
        
        
        Ind_unbounded = logical_and(isinf(p.lb), isinf(p.ub))
        ind_unbounded = where(Ind_unbounded)[0]
        n_unbounded = ind_unbounded.size
        
        # Cast linear inequality constraints into linear equality constraints using slack variables
        nLinInEq, nLinEq = p.b.size, p.beq.size
        
        #p.lb, p.ub = empty(n+nLinInEq+n_unbounded), empty(n+nLinInEq+n_unbounded)
        #p.lb.fill(-inf)
        #p.ub.fill(inf)
        
#        for fn in ['_A', '_Aeq']:
#            if hasattr(p, fn): delattr(p, fn)

        # TODO: handle p.useSparse parameter
        
#        if n_unbounded != 0:
#            R = SparseMatrixConstructor((n_unbounded, n))
#            R[range(n_unbounded), ind_unbounded] = 1.0
#            R2 = Diag([1]*nLinInEq+[-1]*n_unbounded)
#            _A = Hstack((Vstack((p.A, R)), R2))
#        else:
#            _A = Hstack((p.A, Eye(nLinInEq)))

        _A = Hstack((p.A, Eye(nLinInEq), -Vstack([p.A[:, i] for i in ind_unbounded]).T if isPyPy else -p.A[:, ind_unbounded]))
        

#        if isspmatrix(_A): 
#            _A = _A.A
        
        # add lin eq cons
        if nLinEq != 0:
            Constructor = SparseMatrixConstructor if scipyInstalled and nLinInEq > 100 else DenseMatrixConstructor
            _A = Vstack((_A, Hstack((p.Aeq, Constructor((nLinEq, nLinInEq)), \
                                                         -Vstack([p.Aeq[:, i] for i in ind_unbounded]).T if isPyPy else -p.Aeq[:, ind_unbounded]))))
            
        if isspmatrix(_A): 
            if _A.size > 0.3 * prod(_A.shape):
                _A = _A.A
            else:
                _A = _A.tolil()
            #_A = _A.A
        
        #p.A, p.b = zeros((0, p.n)), array([])
        #_f = hstack((p.f, zeros(nLinInEq+n_unbounded)))
        
        #_f = Hstack((p.f, zeros(nLinInEq), -p.f[ind_unbounded]))
        #_b = hstack((p.b, [0]*n_unbounded, p.beq))
        
        _f = hstack((p.f, zeros(nLinInEq), -p.f[Ind_unbounded]))
        
        _b = hstack((p.b, p.beq))
        
        
        if p.useSparse is False and isspmatrix(_A): _A = _A.A
        p.debugmsg('handling as sparse: ' + str(isspmatrix(_A)))
        optx,zmin,is_bounded,sol,basis = lp_engine(_f, _A, _b)
        p.xf = optx[:n] -optx[-n:] if len(optx) != 0 else nan
        p.istop = 1000 if p.xf is not nan else -1000
Ejemplo n.º 10
0
    def __solver__(self, p):
        xBounds2Matrix(p)
        n = p.n

        Ind_unbounded = logical_and(isinf(p.lb), isinf(p.ub))
        ind_unbounded = where(Ind_unbounded)[0]
        n_unbounded = ind_unbounded.size

        # Cast linear inequality constraints into linear equality constraints using slack variables
        nLinInEq, nLinEq = p.b.size, p.beq.size

        #p.lb, p.ub = empty(n+nLinInEq+n_unbounded), empty(n+nLinInEq+n_unbounded)
        #p.lb.fill(-inf)
        #p.ub.fill(inf)

        #        for fn in ['_A', '_Aeq']:
        #            if hasattr(p, fn): delattr(p, fn)

        # TODO: handle p.useSparse parameter

        #        if n_unbounded != 0:
        #            R = SparseMatrixConstructor((n_unbounded, n))
        #            R[range(n_unbounded), ind_unbounded] = 1.0
        #            R2 = Diag([1]*nLinInEq+[-1]*n_unbounded)
        #            _A = Hstack((Vstack((p.A, R)), R2))
        #        else:
        #            _A = Hstack((p.A, Eye(nLinInEq)))

        _A = Hstack(
            (p.A, Eye(nLinInEq), -Vstack([p.A[:, i] for i in ind_unbounded]).T
             if isPyPy else -p.A[:, ind_unbounded]))

        #        if isspmatrix(_A):
        #            _A = _A.A

        # add lin eq cons
        if nLinEq != 0:
            Constructor = SparseMatrixConstructor if scipyInstalled and nLinInEq > 100 else DenseMatrixConstructor
            _A = Vstack((_A, Hstack((p.Aeq, Constructor((nLinEq, nLinInEq)), \
                                                         -Vstack([p.Aeq[:, i] for i in ind_unbounded]).T if isPyPy else -p.Aeq[:, ind_unbounded]))))

        if isspmatrix(_A):
            if _A.size > 0.3 * prod(_A.shape):
                _A = _A.A
            else:
                _A = _A.tolil()
            #_A = _A.A

        #p.A, p.b = zeros((0, p.n)), array([])
        #_f = hstack((p.f, zeros(nLinInEq+n_unbounded)))

        #_f = Hstack((p.f, zeros(nLinInEq), -p.f[ind_unbounded]))
        #_b = hstack((p.b, [0]*n_unbounded, p.beq))

        _f = hstack((p.f, zeros(nLinInEq), -p.f[Ind_unbounded]))

        _b = hstack((p.b, p.beq))

        if p.useSparse is False and isspmatrix(_A): _A = _A.A
        p.debugmsg('handling as sparse: ' + str(isspmatrix(_A)))
        optx, zmin, is_bounded, sol, basis = lp_engine(_f, _A, _b)
        p.xf = optx[:n] - optx[-n:] if len(optx) != 0 else nan
        p.istop = 1000 if p.xf is not nan else -1000