def SDPTDoALocate(self, RN1, RN2, TDoA, TDoAStd): """ Apply SDP approximation and localization """ RN1 = cvxm.matrix(RN1) RN2 = cvxm.matrix(RN2) TDoA = cvxm.matrix(TDoA) c = 3e08 RDoA = c*TDoA RDoAStd=cvxm.matrix(c*TDoAStd) mtdoa,ntdoa=cvxm.size(RN1) Im = cvxm.eye(mtdoa) Y=cvxm.optvar('Y',mtdoa+1,mtdoa+1) t=cvxm.optvar('t',ntdoa,1) prob=cvxm.problem(cvxm.minimize(cvxm.norm2(t))) prob.constr.append(Y>=0) prob.constr.append(Y[mtdoa,mtdoa]==1) for i in range(ntdoa): X0=cvxm.matrix([[Im, -cvxm.transpose(RN1[:,i])],[-RN1[:,i], cvxm.transpose(RN1[:,i])*RN1[:,i]]]) X1=cvxm.matrix([[Im, -cvxm.transpose(RN2[:,i])],[-RN2[:,i], cvxm.transpose(RN2[:,i])*RN2[:,i]]]) prob.constr.append(-RDoAStd[i,0]*t[i]<cvxm.trace(X0*Y)+cvxm.trace(X1*Y)-RDoA[i,0]**2) prob.constr.append(RDoAStd[i,0]*t[i]>cvxm.trace(X0*Y)+cvxm.trace(X1*Y)-RDoA[i,0]**2) prob.solve() Pval=Y.value X_cvx=Pval[:2,-1] return X_cvx
def SDPRSSLocate(self, RN, PL0, d0, RSS, RSSnp, RSSStd, Rest): RoA=self.getRange(RN, PL0, d0, RSS, RSSnp, RSSStd, Rest) RN=cvxm.matrix(RN) RSS=cvxm.matrix(RSS) RSSnp=cvxm.matrix(RSSnp) RSSStd=cvxm.matrix(RSSStd) PL0=cvxm.matrix(PL0) RoA=cvxm.matrix(RoA) mrss,nrss=cvxm.size(RN) Si = array([(1/d0**2)*10**((RSS[0,0]-PL0[0,0])/(5.0*RSSnp[0,0])),(1/d0**2)*10**((RSS[1,0]-PL0[1,0])/(5.0*RSSnp[1,0])),(1/d0**2)*10**((RSS[2,0]-PL0[2,0])/(5.0*RSSnp[2,0])),(1/d0**2)*10**((RSS[3,0]-PL0[3,0])/(5.0*RSSnp[3,0]))]) #Si = array([(1/d0**2)*10**(-(RSS[0,0]-PL0[0,0])/(5.0*RSSnp[0,0])),(1/d0**2)*10**(-(RSS[0,1]-PL0[1,0])/(5.0*RSSnp[0,1])),(1/d0**2)*10**(-(RSS[0,2]-PL0[2,0])/(5.0*RSSnp[0,2])),(1/d0**2)*10**(-(RSS[0,3]-PL0[3,0])/(5.0*RSSnp[0,3]))]) Im = cvxm.eye(mrss) Y=cvxm.optvar('Y',mrss+1,mrss+1) t=cvxm.optvar('t',nrss,1) prob=cvxm.problem(cvxm.minimize(cvxm.norm2(t))) prob.constr.append(Y>=0) prob.constr.append(Y[mrss,mrss]==1) for i in range(nrss): X0=cvxm.matrix([[Im, -cvxm.transpose(RN[:,i])],[-RN[:,i], cvxm.transpose(RN[:,i])*RN[:,i]]]) prob.constr.append(-RSSStd[i,0]*t[i]<Si[i]*cvxm.trace(X0*Y)-1) prob.constr.append(RSSStd[i,0]*t[i]>Si[i]*cvxm.trace(X0*Y)-1) prob.solve() Pval=Y.value X_cvx=Pval[:2,-1] return X_cvx
def SDPToALocate(self, RN, ToA, ToAStd): """ Apply SDP approximation and localization """ RN = cvxm.matrix(RN) ToA = cvxm.matrix(ToA) c = 3e08 # Speed of light RoA = c*ToA RoAStd = c*ToAStd RoAStd = cvxm.matrix(RoAStd) mtoa,ntoa=cvxm.size(RN) Im = cvxm.eye(mtoa) Y=cvxm.optvar('Y',mtoa+1,mtoa+1) t=cvxm.optvar('t',ntoa,1) prob=cvxm.problem(cvxm.minimize(cvxm.norm2(t))) prob.constr.append(Y>=0) prob.constr.append(Y[mtoa,mtoa]==1) for i in range(ntoa): X0=cvxm.matrix([[Im, -cvxm.transpose(RN[:,i])],[-RN[:,i], cvxm.transpose(RN[:,i])*RN[:,i]]]) prob.constr.append(-t[i]<(cvxm.trace(X0*Y)-RoA[i]**2)*(1/RoAStd[i])) prob.constr.append(t[i]>(cvxm.trace(X0*Y)-RoA[i]**2)*(1/RoAStd[i])) prob.solve() Pval=Y.value X_cvx=Pval[:2,-1] return X_cvx
def solve_nu_svm(out, labels, nu, solver, reg): ''' solve boosting formulation used by gelher and nowozin @param out: matrix (N,F) of predictions (for each f_i) for all examples @param labels: vector (N,1) label for each example @param nu: regularization constant @param solver: which solver to use. options: 'mosek', 'glpk' ''' # get dimension N = out.size[0] F = out.size[1] assert N == len(labels), str(N) + " " + str(len(labels)) norm_fact = 1.0 / (nu * float(N)) print "normalization factor %f" % (norm_fact) # avoid point-wise product label_matrix = cvxmod.zeros((N, N)) for i in xrange(N): label_matrix[i, i] = labels[i] #### parameters f = cvxmod.param("f", N, F) y = cvxmod.param("y", N, N, symm=True) norm = cvxmod.param("norm", 1) #### varibales # rho rho = cvxmod.optvar("rho", 1) # dim = (N x 1) chi = cvxmod.optvar("chi", N) # dim = (F x 1) beta = cvxmod.optvar("beta", F) # Q Q = cvxmod.eye(F) # regularize vs ones if reg: objective = 0.5 * cvxmod.atoms.quadform(beta, Q) - (1.0 / float( F)) * cvxmod.sum(beta) - rho * nu + norm_fact * cvxmod.sum(chi) #objective = 0.5*cvxmod.atoms.quadform(beta, Q) - (1.0/float(F))*cvxmod.sum(beta) -rho*nu + norm_fact*cvxmod.sum(chi) else: objective = 0.5 * cvxmod.atoms.quadform( beta, Q) - rho * nu + norm_fact * cvxmod.sum(chi) print objective # create problem p = cvxmod.problem(cvxmod.minimize(objective)) # create contraints for probability simplex #p.constr.append(beta |cvxmod.In| probsimp(F)) #p.constr.append(cvxmod.sum(beta)==1.0) p.constr.append(beta >= 0.0) p.constr.append(chi >= 0.0) # attempt to perform non-sparse boosting #p.constr.append(square(norm2(beta)) <= 1.0) # y f beta y f*beta y*f*beta # (N x N) (N x F) (F x 1) --> (N x N) (N x 1) --> (N x 1) p.constr.append(y * (f * beta) + chi >= rho) # set values for parameters f.value = out y.value = label_matrix norm.value = norm_fact print "solving problem" print "=============================================" print p print "=============================================" # start solver p.solve(lpsolver=solver) # print variables cvxmod.printval(chi) cvxmod.printval(beta) cvxmod.printval(rho) return numpy.array(cvxmod.value(beta))