def solveLeastSquare(A, b, lb=None, ub=None, A_in=None, lb_in=None, ub_in=None): ''' Solve the least square problem: minimize || A*x-b ||^2 subject to lb_in <= A_in*x <= ub_in lb <= x <= ub ''' n = A.shape[1] m_in = 0 if A_in is not None: m_in = A_in.shape[0] if lb_in is None: lb_in = np.array(m_in * [-1e99]) if ub_in is None: ub_in = np.array(m_in * [1e99]) if lb is None: lb = np.array(n * [-1e99]) if ub is None: ub = np.array(n * [1e99]) Hess = np.dot(A.transpose(), A) grad = -np.dot(A.transpose(), b) maxActiveSetIter = np.array([100 + 2 * m_in + 2 * n]) maxComputationTime = np.array([600.0]) options = Options() options.printLevel = PrintLevel.LOW # NONE, LOW, MEDIUM options.enableRegularisation = True print('Gonna solve QP...') if m_in == 0: qpOasesSolver = QProblemB(n) # , HessianType.SEMIDEF); qpOasesSolver.setOptions(options) imode = qpOasesSolver.init(Hess, grad, lb, ub, maxActiveSetIter, maxComputationTime) else: qpOasesSolver = SQProblem(n, m_in) # , HessianType.SEMIDEF); qpOasesSolver.setOptions(options) imode = qpOasesSolver.init(Hess, grad, A_in, lb, ub, lb_in, ub_in, maxActiveSetIter, maxComputationTime) print('QP solved in %f seconds and %d iterations' % (maxComputationTime[0], maxActiveSetIter[0])) if imode != 0 and imode != 63: print("ERROR Qp oases %d " % (imode)) x_norm = np.zeros(n) # solution of the normalized problem qpOasesSolver.getPrimalSolution(x_norm) return x_norm
def solveLeastSquare(A, b, lb=None, ub=None, A_in=None, lb_in=None, ub_in=None): n = A.shape[1] m_in = 0 if (A_in != None): m_in = A_in.shape[0] if (lb_in == None): lb_in = np.array(m_in * [-1e99]) if (ub_in == None): ub_in = np.array(m_in * [1e99]) if (lb == None): lb = np.array(n * [-1e99]) if (ub == None): ub = np.array(n * [1e99]) Hess = np.dot(A.transpose(), A) grad = -np.dot(A.transpose(), b) maxActiveSetIter = np.array([100 + 2 * m_in + 2 * n]) maxComputationTime = np.array([600.0]) options = Options() options.printLevel = PrintLevel.LOW #NONE, LOW, MEDIUM options.enableRegularisation = True print 'Gonna solve QP...' if (m_in == 0): qpOasesSolver = QProblemB(n) #, HessianType.SEMIDEF); qpOasesSolver.setOptions(options) imode = qpOasesSolver.init(Hess, grad, lb, ub, maxActiveSetIter, maxComputationTime) else: qpOasesSolver = SQProblem(n, m_in) #, HessianType.SEMIDEF); qpOasesSolver.setOptions(options) imode = qpOasesSolver.init(Hess, grad, A_in, lb, ub, lb_in, ub_in, maxActiveSetIter, maxComputationTime) print 'QP solved in %f seconds and %d iterations' % (maxComputationTime[0], maxActiveSetIter[0]) if (imode != 0 and imode != 63): print "ERROR Qp oases %d " % (imode) x_norm = np.zeros(n) # solution of the normalized problem qpOasesSolver.getPrimalSolution(x_norm) return x_norm
def solveLeastSquare(A, b, lb=None, ub=None, A_in=None, lb_in=None, ub_in=None): n = A.shape[1]; m_in = 0; if(A_in!=None): m_in = A_in.shape[0]; if(lb_in==None): lb_in = np.array(m_in*[-1e99]); if(ub_in==None): ub_in = np.array(m_in*[1e99]); if(lb==None): lb = np.array(n*[-1e99]); if(ub==None): ub = np.array(n*[1e99]); Hess = np.dot(A.transpose(),A); grad = -np.dot(A.transpose(),b); maxActiveSetIter = np.array([100+2*m_in+2*n]); maxComputationTime = np.array([600.0]); options = Options(); options.printLevel = PrintLevel.LOW; #NONE, LOW, MEDIUM options.enableRegularisation = True; print 'Gonna solve QP...'; if(m_in==0): qpOasesSolver = QProblemB(n); #, HessianType.SEMIDEF); qpOasesSolver.setOptions(options); imode = qpOasesSolver.init(Hess, grad, lb, ub, maxActiveSetIter, maxComputationTime); else: qpOasesSolver = SQProblem(n, m_in); #, HessianType.SEMIDEF); qpOasesSolver.setOptions(options); imode = qpOasesSolver.init(Hess, grad, A_in, lb, ub, lb_in, ub_in, maxActiveSetIter, maxComputationTime); # print 'QP solved in %f seconds and %d iterations' % (maxComputationTime[0],maxActiveSetIter[0]); if(imode!=0 and imode!=63): print "ERROR Qp oases %d " % (imode); x_norm = np.zeros(n); # solution of the normalized problem qpOasesSolver.getPrimalSolution(x_norm); return x_norm;
def solveLeastSquare(A, b, lb=None, ub=None, A_in=None, lb_in=None, ub_in=None, maxIterations=None, maxComputationTime=60.0, regularization=1e-8): n = A.shape[1] m_in = 0 A = np.asarray(A) b = np.asarray(b).squeeze() if (A_in is not None): m_in = A_in.shape[0] if (lb_in is None): lb_in = np.array(m_in * [-1e99]) else: lb_in = np.asarray(lb_in).squeeze() if (ub_in is None): ub_in = np.array(m_in * [1e99]) else: ub_in = np.asarray(ub_in).squeeze() if (lb is None): lb = np.array(n * [-1e99]) else: lb = np.asarray(lb).squeeze() if (ub is None): ub = np.array(n * [1e99]) else: ub = np.asarray(ub).squeeze() # 0.5||Ax-b||^2 = 0.5(x'A'Ax - 2b'Ax + b'b) = 0.5x'A'Ax - b'Ax +0.5b'b Hess = np.dot(A.T, A) + regularization * np.identity(n) grad = -np.dot(A.T, b) if (maxIterations is None): maxActiveSetIter = np.array([100 + 2 * m_in + 2 * n]) else: maxActiveSetIter = np.array([maxIterations]) maxComputationTime = np.array([maxComputationTime]) options = Options() options.printLevel = PrintLevel.NONE #NONE, LOW, MEDIUM options.enableRegularisation = False if (m_in == 0): qpOasesSolver = QProblemB(n) #, HessianType.SEMIDEF); qpOasesSolver.setOptions(options) # beware that the Hessian matrix may be modified by this function imode = qpOasesSolver.init(Hess, grad, lb, ub, maxActiveSetIter, maxComputationTime) else: qpOasesSolver = SQProblem(n, m_in) #, HessianType.SEMIDEF); qpOasesSolver.setOptions(options) imode = qpOasesSolver.init(Hess, grad, A_in, lb, ub, lb_in, ub_in, maxActiveSetIter, maxComputationTime) x = np.empty(n) qpOasesSolver.getPrimalSolution(x) #print "QP cost:", 0.5*(np.linalg.norm(np.dot(A, x)-b)**2); return (imode, np.asmatrix(x).T)