Exemplo n.º 1
0
    def _cvxmod_minimize(self, lb, ub, **kwargs):
        """ solve quadratic problem using CVXMOD interface

        Keyword arguments:

        lb, ub  -- vectors of lower and upper bounds (potentially modified by
                   added tolerance)

        Modified member variables:

        status, solution, obj_value

        Returns: nothing
        """
        # shorter names
        v = self.cvxmodV
        A = self.cvxmodMatrix
        minus_w = cvxmod.matrix(self.obj.f)  # negative wildtype solution
        lb = cvxmod.matrix(lb)
        ub = cvxmod.matrix(ub)
        if self.weights is not None:
            weights = cvxmod.matrix(diag(sqrt(self.weights)))
        if self.Aineq is not None:
            Aineq = cvxmod.matrix(array(self.Aineq))
            bineq = cvxmod.matrix(self.bineq)
            if self.weights is None:
                p = cvxmod.problem(cvxmod.minimize(
                    cvxmod.norm2(v + minus_w)), [
                        cvxmod.abs(A * v) < self.matrix_tol,
                        Aineq * v <= bineq + self.matrix_tol, v >= lb, v <= ub
                    ])
            else:
                p = cvxmod.problem(
                    cvxmod.minimize(cvxmod.norm2(weights * (v + minus_w))), [
                        cvxmod.abs(A * v) < self.matrix_tol,
                        Aineq * v <= bineq + self.matrix_tol, v >= lb, v <= ub
                    ])
        else:
            if self.weights is None:
                p = cvxmod.problem(
                    cvxmod.minimize(cvxmod.norm2(v + minus_w)),
                    [cvxmod.abs(A * v) < self.matrix_tol, v >= lb, v <= ub])
            else:
                p = cvxmod.problem(
                    cvxmod.minimize(cvxmod.norm2(weights * (v + minus_w))),
                    [cvxmod.abs(A * v) < self.matrix_tol, v >= lb, v <= ub])

        self.status = cvxToSolverStatus(p.solve())

        if not v.value:
            self.solution = []
        else:
            self.solution = array(list(v.value))
        try:
            self.obj_value = p.value
        except cvxmod.OptvarValueError:
            self.obj_value = inf
Exemplo n.º 2
0
        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
Exemplo n.º 3
0
        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
Exemplo n.º 4
0
        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
Exemplo n.º 5
0
    def compute_combinaison_safe(self,target,rcond = 0.0,regul = None):
        """
            Computes the combination of base targets allowing to reproduce
            'target' (or giving the best approximation), while keeping
            coefficients between 0 and 1.

            arguments :
            - target : target to fit
            - rcond : cut off on the singular values as a fraction of
                      the biggest one. Only base vectors corresponding
                      to singular values bigger than rcond*largest_singular_value
                      
            - regul : regularisation factor for least square fitting. This force
                      the algorithm to use fewer targets.
            
        """
        from cvxmod import optvar,param,norm2,norm1,problem,matrix,minimize
        if type(target) is str or type(target) is unicode :
            target = read_target(target)
        cond = self.s>= rcond*self.s[0]
        u = self.u[ : , cond ]
        vt = self.vt[ cond ]
        s = self.s[cond]
        
        t = target.flatten()
        dim,ntargets = self.vt.shape
        nvert = target.shape[0]
        
        pt = np.dot(u.T,t.reshape(nvert*3,1))
        A = param('A',value = matrix(s.reshape(dim,1)*vt))
        b = param('b',value = matrix(pt))
        x = optvar('x',ntargets)

        if regul is None : prob = problem(minimize(norm2(A*x-b)),[x>=0.,x<=1.])
        else : prob = problem(minimize(norm2(A*x-b) + regul * norm1(x)),[x>=0.,x<=1.])
        
        prob.solve()
        bs = np.array(x.value).flatten()
        # Body setting files have a precision of at most 1.e-3
        return bs*(bs>=1e-3)
Exemplo n.º 6
0
def reconstruct_target(target_file,base_prefix,regul = None):
	"""
		Reconstruct the target in 'target_file' using constrained, 
		and optionally regularized, least square optimisation.
		
		arguments :
			target_file : file contaiing the target to fit
			base_prefix : prefix for the files of the base.
	"""
	
	vlist = read_vertex_list(base_prefix+'_vertices.dat')
	t = read_target(target_file,vlist)
	U = load(base_prefix+"_U.npy").astype('float')
	S = load(base_prefix+"_S.npy").astype('float')
	V = load(base_prefix+"_V.npy").astype('float')

	ntargets,dim = V.shape
	nvert = len(t)
	pt = dot(U.T,t.reshape(nvert*3,1))
	pbase = S[:dim].reshape(dim,1)*V.T
	A = param('A',value = matrix(pbase))
	b = param('b',value = matrix(pt))
	x = optvar('x',ntargets)

	if regul is None : prob = problem(minimize(norm2(A*x-b)),[x>=0.,x<=1.])
	else : prob = problem(minimize(norm2(A*x-b) + regul * norm1(x)),[x>=0.,x<=1.])
	
	prob.solve()
	
	targ_names_file = base_prefix+"_names.txt"
	with open(targ_names_file) as f :
		tnames = [line.strip() for line in f.readlines() ]
	tnames.sort()
	
	base,ext = os.path.splitext(target_file)
	bs_name = base+".bs"
	with open(bs_name,"w") as f :
		for tn,v in zip(tnames,x.value):
			if v >= 1e-3 : f.write("%s %0.3f\n"%(tn,v))
Exemplo n.º 7
0
def reconstruct_target(target_file,base_prefix,regul = None):
	"""
		Reconstruct the target in 'target_file' using constrained, 
		and optionally regularized, least square optimisation.
		
		arguments :
			target_file : file contaiing the target to fit
			base_prefix : prefix for the files of the base.
	"""
	
	vlist = read_vertex_list(base_prefix+'_vertices.dat')
	t = read_target(target_file,vlist)
	U = load(base_prefix+"_U.npy").astype('float')
	S = load(base_prefix+"_S.npy").astype('float')
	V = load(base_prefix+"_V.npy").astype('float')

	ntargets,dim = V.shape
	nvert = len(t)
	pt = dot(U.T,t.reshape(nvert*3,1))
	pbase = S[:dim].reshape(dim,1)*V.T
	A = param('A',value = matrix(pbase))
	b = param('b',value = matrix(pt))
	x = optvar('x',ntargets)

	if regul is None : prob = problem(minimize(norm2(A*x-b)),[x>=0.,x<=1.])
	else : prob = problem(minimize(norm2(A*x-b) + regul * norm1(x)),[x>=0.,x<=1.])
	
	prob.solve()
	
	targ_names_file = base_prefix+"_names.txt"
	with open(targ_names_file) as f :
		tnames = [line.strip() for line in f.readlines() ]
	tnames.sort()
	
	base,ext = os.path.splitext(target_file)
	bs_name = base+".bs"
	with open(bs_name,"w") as f :
		for tn,v in zip(tnames,x.value):
			if v >= 1e-3 : f.write("%s %0.3f\n"%(tn,v))
Exemplo n.º 8
0
 def cost(self, dgvel):
     """Returns the cost function of the task.
     
     TODO: add a (possibly singular) weighting matrix (thus allow to control the orientation)
     
     """
     J_ = self._controlled_frame.jacobian[3:6, :]
     J = param(value=matrix(J_))
     dJ = self._controlled_frame.djacobian[3:6, :]
     gvel = self._world.gvel
     Pdes = self._target_frame.pose[0:3, 3]
     cf = self._controlled_frame
     dVdes = 10.0 * dot(cf.pose[0:3, 0:3].T, Pdes - cf.pose[0:3, 3]) - 2.0 * sqrt(10.0) * dot(J_, self._world.gvel)
     return norm2(J * dgvel + param(value=matrix(dot(dJ, gvel) - dVdes)))
Exemplo n.º 9
0
    def cost(self, dgvel):
        """Returns the cost function of the task.

        TODO: add a (possibly singular) weighting matrix (thus allow to control the orientation)

        """
        J_ = self._controlled_frame.jacobian[3:6, :]
        J = param(value=matrix(J_))
        dJ = self._controlled_frame.djacobian[3:6, :]
        gvel = self._world.gvel
        Pdes = self._target_frame.pose[0:3, 3]
        cf = self._controlled_frame
        dVdes = 10.*dot(cf.pose[0:3,0:3].T, Pdes - cf.pose[0:3,3]) -\
            2.*sqrt(10.)*dot(J_, self._world.gvel)
        return norm2(J * dgvel + param(value=matrix(dot(dJ, gvel) - dVdes)))
Exemplo n.º 10
0
    def update(self, dt=None):
        """
        """
        self.initialize_LQP()

        self.get_situation()

        self.compute_objectives()

        self.write_tasks()

        self.write_constraints()

        self.solve_LQP()

        M = param("M", value=matrix(self.world.mass))
        N = param("N", value=matrix(self.world.nleffects))
        # variables:
        dgvel = optvar("dgvel", self._wndof)
        tau = optvar("tau", self._wndof)
        # fc    = optvar('tau', self._wndof)
        gvel = param("gvel", value=matrix(self.world.gvel))
        taumax = param("taumax", value=matrix(array([10.0, 10.0, 10.0])))

        ### resolution ###
        cost = norm2(tau)
        for task in self._tasks:
            cost += 100.0 * task.cost(dgvel)

        p = problem(minimize(cost))
        p.constr.append(M * dgvel + N * gvel == tau)
        p.constr.append(-taumax <= tau)
        p.constr.append(tau <= taumax)
        p.solve(True)
        tau = array(tau.value).reshape(self._wndof)
        self._rec_tau.append(tau)
        gforce = tau
        impedance = zeros((self._wndof, self._wndof))
        return (gforce, impedance)
Exemplo n.º 11
0
    def update(self, dt=None):
        """
        """
        self.initialize_LQP()

        self.get_situation()

        self.compute_objectives()

        self.write_tasks()

        self.write_constraints()

        self.solve_LQP()

        M = param('M', value=matrix(self.world.mass))
        N = param('N', value=matrix(self.world.nleffects))
        # variables:
        dgvel = optvar('dgvel', self._wndof)
        tau = optvar('tau', self._wndof)
        #fc    = optvar('tau', self._wndof)
        gvel = param('gvel', value=matrix(self.world.gvel))
        taumax = param('taumax', value=matrix(array([10., 10., 10.])))

        ### resolution ###
        cost = norm2(tau)
        for task in self._tasks:
            cost += 100. * task.cost(dgvel)

        p = problem(minimize(cost))
        p.constr.append(M * dgvel + N * gvel == tau)
        p.constr.append(-taumax <= tau)
        p.constr.append(tau <= taumax)
        p.solve(True)
        tau = array(tau.value).reshape(self._wndof)
        self._rec_tau.append(tau)
        gforce = tau
        impedance = zeros((self._wndof, self._wndof))
        return (gforce, impedance)
Exemplo n.º 12
0
def Main():
    options, _ = MakeOpts().parse_args(sys.argv)
    assert options.genes_filename
    assert options.protein_levels_a and options.protein_levels_b
    
    
    print 'Reading genes list from', options.genes_filename
    gene_ids = util.ReadProteinIDs(options.genes_filename)
    
    print 'Reading protein data A from', options.protein_levels_a
    gene_counts_a = util.ReadProteinCounts(options.protein_levels_a)
    print 'Reading protein data B from', options.protein_levels_b
    gene_counts_b = util.ReadProteinCounts(options.protein_levels_b)

    my_counts_a = dict((id, (count, name)) for id, name, count in
                       util.ExtractCounts(gene_counts_a, gene_ids))
    my_counts_b = dict((id, (count, name)) for id, name, count in
                       util.ExtractCounts(gene_counts_b, gene_ids))
        
    overlap_ids = set(my_counts_a.keys()).intersection(my_counts_b.keys())
    x = pylab.matrix([my_counts_a[id][0] for id in overlap_ids])
    y = pylab.matrix([my_counts_b[id][0] for id in overlap_ids])
    labels = [my_counts_b[id][1] for id in overlap_ids]
    
    xlog = pylab.log10(x)
    ylog = pylab.log10(y)
    a = cvxmod.optvar('a', 1)
    mx = cvxmod.matrix(xlog.T)
    my = cvxmod.matrix(ylog.T)
    
    p = cvxmod.problem(cvxmod.minimize(cvxmod.norm2(my - a - mx)))
    p.solve(quiet=True)
    offset = cvxmod.value(a)
    lin_factor = 10**offset
    lin_label = 'Y = %.2g*X' % lin_factor
    log_label = 'log10(Y) = %.2g + log10(X)' % offset
    
    f1 = pylab.figure(0)
    pylab.title('Linear scale')
    xylim = max([x.max(), y.max()]) + 5000
    linxs = pylab.arange(0.0, xylim, 0.1)
    linys = linxs * lin_factor
    pylab.plot(x.tolist()[0], y.tolist()[0], 'g.', label='Protein Data')
    pylab.plot(linxs, linys, 'b-', label=lin_label)
    for x_val, y_val, label in zip(x.tolist()[0], y.tolist()[0], labels):
        pylab.text(x_val, y_val, label, fontsize=8)

    pylab.xlabel(options.a_label)
    pylab.ylabel(options.b_label)
    pylab.legend()
    pylab.xlim((0.0, xylim))
    pylab.ylim((0.0, xylim))
    
    f2 = pylab.figure(1)
    pylab.title('Log10 scale')
    xylim = max([xlog.max(), ylog.max()]) + 1.0    
    pylab.plot(xlog.tolist()[0], ylog.tolist()[0], 'g.', label='Log10 Protein Data')
    linxs = pylab.arange(0.0, xylim, 0.1)
    linys = linxs + offset
    pylab.plot(linxs, linys, 'b-', label=log_label)
    
    for x_val, y_val, label in zip(xlog.tolist()[0], ylog.tolist()[0], labels):
        pylab.text(x_val, y_val, label, fontsize=8)
    
    pylab.xlabel(options.a_label + ' (log10)')
    pylab.ylabel(options.b_label + ' (log10)')
    pylab.legend()
    pylab.xlim((0.0, xylim))
    pylab.ylim((0.0, xylim))
    
    
    
    pylab.show()
Exemplo n.º 13
0
def Main():
    options, _ = MakeOpts().parse_args(sys.argv)
    assert options.genes_filename
    assert options.protein_levels_a and options.protein_levels_b

    print 'Reading genes list from', options.genes_filename
    gene_ids = util.ReadProteinIDs(options.genes_filename)

    print 'Reading protein data A from', options.protein_levels_a
    gene_counts_a = util.ReadProteinCounts(options.protein_levels_a)
    print 'Reading protein data B from', options.protein_levels_b
    gene_counts_b = util.ReadProteinCounts(options.protein_levels_b)

    my_counts_a = dict(
        (id, (count, name))
        for id, name, count in util.ExtractCounts(gene_counts_a, gene_ids))
    my_counts_b = dict(
        (id, (count, name))
        for id, name, count in util.ExtractCounts(gene_counts_b, gene_ids))

    overlap_ids = set(my_counts_a.keys()).intersection(my_counts_b.keys())
    x = pylab.matrix([my_counts_a[id][0] for id in overlap_ids])
    y = pylab.matrix([my_counts_b[id][0] for id in overlap_ids])
    labels = [my_counts_b[id][1] for id in overlap_ids]

    xlog = pylab.log10(x)
    ylog = pylab.log10(y)
    a = cvxmod.optvar('a', 1)
    mx = cvxmod.matrix(xlog.T)
    my = cvxmod.matrix(ylog.T)

    p = cvxmod.problem(cvxmod.minimize(cvxmod.norm2(my - a - mx)))
    p.solve(quiet=True)
    offset = cvxmod.value(a)
    lin_factor = 10**offset
    lin_label = 'Y = %.2g*X' % lin_factor
    log_label = 'log10(Y) = %.2g + log10(X)' % offset

    f1 = pylab.figure(0)
    pylab.title('Linear scale')
    xylim = max([x.max(), y.max()]) + 5000
    linxs = pylab.arange(0.0, xylim, 0.1)
    linys = linxs * lin_factor
    pylab.plot(x.tolist()[0], y.tolist()[0], 'g.', label='Protein Data')
    pylab.plot(linxs, linys, 'b-', label=lin_label)
    for x_val, y_val, label in zip(x.tolist()[0], y.tolist()[0], labels):
        pylab.text(x_val, y_val, label, fontsize=8)

    pylab.xlabel(options.a_label)
    pylab.ylabel(options.b_label)
    pylab.legend()
    pylab.xlim((0.0, xylim))
    pylab.ylim((0.0, xylim))

    f2 = pylab.figure(1)
    pylab.title('Log10 scale')
    xylim = max([xlog.max(), ylog.max()]) + 1.0
    pylab.plot(xlog.tolist()[0],
               ylog.tolist()[0],
               'g.',
               label='Log10 Protein Data')
    linxs = pylab.arange(0.0, xylim, 0.1)
    linys = linxs + offset
    pylab.plot(linxs, linys, 'b-', label=log_label)

    for x_val, y_val, label in zip(xlog.tolist()[0], ylog.tolist()[0], labels):
        pylab.text(x_val, y_val, label, fontsize=8)

    pylab.xlabel(options.a_label + ' (log10)')
    pylab.ylabel(options.b_label + ' (log10)')
    pylab.legend()
    pylab.xlim((0.0, xylim))
    pylab.ylim((0.0, xylim))

    pylab.show()