def getQP(n=10, beta=1.0e-5, phi=0): 'Generate the QP data structure' prob = dict() prob['H'] = getH(n, beta) prob['c'] = getc(n) prob['Aeq'], prob['beq'] = geteq(n) prob['l'], prob['u'] = getlu(n, phi) prob['A'] = sp_rand(0, 2 * n**2, 0) prob['bl'] = sp_rand(0, 1, 0) prob['bu'] = sp_rand(0, 1, 0) #prob['x0'] = sp_rand(2*n**2,1,0.5) prob['x0'] = matrix(1.0, (2 * n**2, 1)) return QP(prob)
def __init__(self, prob=QP(), Free=[], **kwargs): 'Initialize the PDASc' # Initialize the superclass super(PDASc, self).__init__(prob, **kwargs) # Add additional data self.F = Free nf = len(self.F) m = self.QP.numeq Q1 = sparse([self.QP.H[self.F, self.F], self.QP.Aeq[:, self.F]]) Q2 = sparse([self.QP.Aeq[:, self.F].T, spmatrix([], [], [], (m, m))]) self.ipiv = matrix(1, (nf + m, 1)) self.Q = matrix([[Q1], [Q2]]) lapack.sytrf(self.Q, self.ipiv) # LDL factorization of Q, dense, option 1 # lapack.sytrf(self.Q,self.ipiv) # self.DiagQi = spdiag([self.Q[i,i] for i in range(nf+m)]) # self.LQ = copy(self.Q) # for i in range(nf+m): # self.LQ[i,i] = 1 # for j in range(i+1,nf+m): # self.LQ[i,j] = 0 # LDL factorization of Q, dense, option 2 by calling Matlab library # mlab = Matlab() # mlab.start() filename = 'temp/' + ''.join([ random.choice(string.ascii_letters + string.digits) for n in xrange(32) ]) s = dict() s['A'] = sparse([[Q1], [Q2]]) write(filename + '.mat', s) d = '/home/zhh210/workspace/pypdas/numeric/control/' # Memory failure when size is large # output = mlab.run_func('getldp.m',{'arg1':filename}) # self.LQ = matrix(output['result']['L']) # self.Dinv = matrix(output['result']['Dinv']) # self.P = matrix(output['result']['P']) # output = mlab.run_func('saveldp.m',{'arg1':filename}) # Execute shell command cmd = 'matlab -r ' + '"' + "saveldp('" + filename + "');quit" + '"' print cmd os.system(cmd) data = read(filename + '.mat') self.LQ = matrix(data['L']) self.Dinv = data['Dinv'] self.P = data['P']
def _generateQP(H, c, Aeq, beq, A, bl, bu, l, u, x0): 'Auxilliary function for generating QP blass' prob = dict() prob['H'] = H prob['c'] = c prob['Aeq'] = Aeq prob['beq'] = beq prob['A'] = A prob['bl'] = bl prob['bu'] = bu prob['l'] = l prob['u'] = u prob['x0'] = x0 qp = QP(prob) return qp
def test_qp(): 'Function to test QP class' prob = dict() n = 5 m = 1 prob['H'] = sprandsym(n) prob['c'] = sp_rand(n, 1, 0.8) prob['A'] = sp_rand(m, n, 0.8) prob['bl'] = sp_rand(m, 1, 1) prob['bu'] = prob['bl'] + 1 prob['Aeq'] = sp_rand(m, n, 0.8) prob['beq'] = sp_rand(m, 1, 1) prob['l'] = sp_rand(n, 1, 1) prob['u'] = prob['l'] + 1 prob['x0'] = sp_rand(n, 1, 0.8) qp = QP(prob) print(qp) return qp
def __init__(self, QP=QP(), **kwargs): #super(solver,self).__init__(self,QP,**kwargs) self.QP = QP n = QP.numvar m = QP.numeq mi = QP.numineq self.x = copy(QP.x0) self.czl = spmatrix([], [], [], (mi, 1)) self.czu = spmatrix([], [], [], (mi, 1)) self.y = matrix(1.0, (m, 1)) self.zl = spmatrix([], [], [], (n, 1)) self.zu = spmatrix([], [], [], (n, 1)) #self.zl = spmatrix([0],[max(0,n-1)],[0]) #self.zu = spmatrix([0],[max(0,n-1)],[0]) self.iter = 0 self.cAL = [] self.cAU = [] self.cI = [] self.AL = [] self.AU = [] self.I = [] self.violations = Violations() self.option = ut.optimset(**kwargs) self._guess() self._ObserverList = dict(PDAS.observerlist) self.state = 'Initial' self._CGiter = 0 self.TotalCG = 0 self.CG_r = matrix([10, 100, 1000]) self.correctV = Violations() if 'inv_norm' in self.option.options.keys(): self.inv_norm = self.option['inv_norm'] else: self.inv_norm = 10
def __init__(self, prob=QP(), **kwargs): # Initialize the solver self.prob = prob super().__init__() pass