Example #1
0
def exact_wbp_cvx(Operator, y, w, s, eta, maxiter):
    x = cvx.Variable(Operator.n)

    cvx.Problem(cvx.Minimize(cvx.norm(np.diag(w) * x, 1)),
                [cvx.norm(Operator.A * x - y, 1) <= eps]).solve()

    return Result(np.array(x.value).flatten(), -1, 'Exact CVX')
Example #2
0
def TRCPA_v1(M,n,tf,q,lam):
	Avars = []
	for i in range(tf-1):
		Avars.append(cvx.Variable(n,n))
	Ltop = cvx.Variable(n,q)
	Lbottom = np.zeros((n*(tf-1),q))
	L = cvx.vstack(Ltop,Lbottom)
	S = cvx.Variable(n*tf,q)

	print("Check 1")
	objective = cvx.Minimize(cvx.norm(L,"nuc") + lam*cvx.norm(S,1))
	identity = np.eye(n)
	print("Check 2")
	constraints = [np.dot(identity,M[0:n,:])==Ltop+S[0:n,:]]

	for i in range(tf-1):
		constraints.append(-1*Avars[i]*M[(i*n):(i+1)*n,:] + np.dot(identity,M[((i+1)*n):(i+2)*n,:])==L[((i+1)*n):(i+2)*n,:]+S[((i+1)*n):(i+2)*n,:])
		
	print("Check 3")
	print(constraints)
	prob = cvx.Problem(objective,constraints)
	print("Check 4")
	result = prob.solve(verbose = True)
	print("Check 5")
	Ltop_final = Ltop.value
	Avars_final = []
	for x in Avars:
		Avars_final.append(x.value)
	S_final = S.value
	return (Avars_final,Ltop_final,Lbottom,S_final)
def GetMinimalSpeedToReachEpsilonNeighbordhoodVector(dt, epsilon, W, dW, dF):
        Ndim = W.shape[0]
        Nsamples = W.shape[1]

        dist_w = np.zeros((Nsamples-1))
        for i in range(0,Nsamples-1):
                dist_w[i] = np.linalg.norm(W[:,i]-W[:,i+1])

        p = Variable(Nsamples-1)
        sM = Variable(Nsamples-1)

        constraints = []
        objfunc = 0.0
        for i in range(0,Nsamples-1):
                #constraints.append( norm(p*dt*dW0[0:2] + dF +np.dot(dw,np.array((1,0))) ) < epsilon )
                constraints.append( norm(p[i]*dt*dW[:,i] + dt*dt/2*dF[:,i] + np.dot(dist_w[i],np.array((1,0,0,0))) ) < epsilon )
                constraints.append( sM[i] >= p[i] )
                constraints.append( sM[i] >= 0.0)
                constraints.append( p[i] >= 0.0 )
                objfunc += norm(sM[i])

        objective = Minimize(objfunc)

        prob = Problem(objective, constraints)

        print "solve minimal speed"
        result = prob.solve(solver=SCS)
        print "done.(",prob.value,"|",np.min(sM.value),")"

        if prob.value < inf:
                return np.array(sM.value).flatten()
        else:
                return np.array(sM.value).flatten()
def sparseCoefRecovery(X, Opt, l=0.001):
	d, n = X.shape
	C = np.zeros((n, n))
	# Minimize(sum_squares(X - X*C))

	for i in xrange(n):
		print i

		y = X[:, i]
		Y = np.delete(X, (i), axis=1)

		c = cp.Variable(n-1,1)
		if Opt == 'Lasso':
			objective = cp.Minimize(cp.norm(c,1) + l*cp.norm(Y*c -y))
			constraints = []

		elif Opt =='L1Perfect':
			objective = cp.Minimize(cp.norm(c, 1))
			constraints = [Y * c  == y]

		prob = cp.Problem(objective, constraints)
		prob.solve()

		c_val = np.array(c.value)[:,0]

		if i > 1:
			C[:i-1,i] = c_val[:i-1]
		if i < n:
			C[i+1:n,i] = c_val[i:n]
		C[i,i] = 0

	return C
Example #5
0
def qc_wbp_cvx(Operator, y, w, s, eta, maxiter):
    x = cvx.Variable(Operator.n)

    cvx.Problem(cvx.Minimize(cvx.norm(np.diag(w) * x, 1)),
                [cvx.norm(Operator.A * x - y, 2) <= eta]).solve()

    return Result(np.array(x.value).flatten(), -1, 'Quadratically Constrained CVX')
Example #6
0
def GroupGraphicLasso(X, rho, alpha ,groups):
    """
        S is the empirical covariance matrix.
    """
    S = np.cov(X.T)
    assert S.shape[0] == S.shape[1], "Matrix must be square"
    n = S.shape[0]

    #Phi = cvx.Variable(n, n)
    Phi = cvx.Semidef(n)
    rest = cvx.Semidef(n)
    group_pennal=[]
    non_one_pennal=[]
    A=set()
    for group in groups:
        group_pennal.append(cvx.norm(Phi[group,group],"fro"))
        non_index=nonGroupIndex(group, n)
        non_one_pennal.append(cvx.norm(Phi[group][:,non_index],1))
        A.update(set(group))
    non_block = [i for i in range(n) if i not in A]
    if len(non_block) > 0:
        block_onenorm = cvx.norm(Phi[:,non_block],1)
        obj = cvx.Minimize(-(cvx.log_det(Phi) - cvx.trace(S*Phi) - rho*sum(group_pennal) - alpha * sum(non_one_pennal)-
                        alpha * block_onenorm))
    else:
        obj = cvx.Minimize(-(cvx.log_det(Phi) - cvx.trace(S*Phi) - rho*sum(group_pennal) - alpha * sum(non_one_pennal)))
    constraints = []

    prob = cvx.Problem(obj,constraints)
    prob.solve(solver=cvx.SCS, eps=1e-5)
    return Phi.value
Example #7
0
def CS(h,const=5.0,noise=0.0000001):
    """Compressed Sensing replacement of Fourier Transform on 1D array h
       * REQUIRES CVXPY PACKAGE *
         h       = sampled time signal
         const   = scalar multiple dimension of h, larger values give greater
                     resolution albeit with increased cost.
         noise   = scalar constant to account for numerical noise

         returns:
         g       = fourier transform h to frequency domain using CS technique
    """

    try:
        import cvxpy as cvx
    except ImportError as e:
        print "You need CVXPY: http://www.cvxpy.org/"
        sys.exit()

    h = np.asarray(h, dtype=float)
    Nt = len(h)
    Nw = int(const*Nt)
    t = np.arange(Nt)
    w = np.arange(Nw)
    F = np.sin(2 * np.pi * np.outer(t,w) / Nw)
    g = cvx.Variable(Nw)

    # min |g|_1 subject to |F.g - h|_2 < noise

    objective = cvx.Minimize(cvx.norm(g,1))
    constraints = [cvx.norm(F*g - h,2) <= noise]
    prob = cvx.Problem(objective, constraints)
    prob.solve(solver='SCS',verbose=True)
    g = np.asarray(g.value)
    g = g[:,0]
    return g
def ForceCanBeCounteractedByAccelerationVector(dt, Fp, u1min, u1max, u2min, u2max, plot=False) :

        ### question (1) : can we produce an acceleration ddW, such that it counteracts F?

        ## dynamics projected onto identity element, it becomes obvious that in an infinitesimal neighborhood, 
        ## we can only counteract forces along the x and the theta axes due to non-holonomicity

        dt2 = dt*dt/2

        ## span dt2-hyperball in Ndim
        F = dt2*Fp
        thetamin = dt2*u2min
        thetamax = dt2*u2max
        xmin = 0.0
        xmax = dt2*u1max

        Xlow = np.dot(np.dot(Rz(-pi/2),Rz(thetamin)),np.array((1,0,0)))
        Xhigh = np.dot(np.dot(Rz(pi/2),Rz(thetamax)),np.array((1,0,0)))

        Ndim = Fp.shape[0]
        if Fp.ndim <= 1:
                Nsamples = 1
        else:
                Nsamples = Fp.shape[1]
        p = Variable(3,Nsamples)

        constraints = []
        objfunc = 0.0
        for i in range(0,Nsamples):
                constraints.append( norm(p[:,i]) <= xmax )
                constraints.append( np.matrix(Xlow[0:3])*p[:,i] <= 0 )
                constraints.append( np.matrix(Xhigh[0:3])*p[:,i] <= 0 )
                if Fp.ndim <= 1:
                        objfunc += norm(p[:,i]-F[0:3])
                else:
                        objfunc += norm(p[:,i]-F[0:3,i])
                #objfunc.append(norm(p[:,i]-F[:,i]))

        objective = Minimize(objfunc)
        prob = Problem(objective, constraints)

        result = prob.solve(solver=SCS, eps=1e-7)

        #nearest_ddq = np.array(p.value)
        nearest_ddq = np.array(p.value/dt2)

        codimension = Ndim-nearest_ddq.shape[0]

        #print Ndim, nearest_ddq.shape
        #print codimension
        zero_rows = np.zeros((codimension,Nsamples))

        if nearest_ddq.shape[0] < Ndim:
                nearest_ddq = np.vstack((nearest_ddq,zero_rows))

        if plot:
                PlotReachableSetForceDistance(dt, u1min, u1max, u2min, u2max, -F, dt2*nearest_ddq)

        return nearest_ddq
Example #9
0
def example3():
    x = cvx.Variable(3)
    y = cvx.Variable()

    prob = cvx.Problem(cvx.Minimize(cvx.norm(x) + cvx.norm(2*y)))
    x_vars = dict(x=x,y=y)
    
    return prob, x_vars
Example #10
0
    def test_half_quadratic_splitting(self):


        """Test half quadratic splitting.
        """
        X = Variable((10,5))
        B = np.reshape(np.arange(50), (10,5))
        prox_fns = [sum_squares(X, b=B)]
        sltn = hqs.solve(prox_fns, [], eps_rel=1e-4, max_iters = 100, max_inner_iters = 50)
        self.assertItemsAlmostEqual(X.value, B, places=2)
        self.assertAlmostEqual(sltn, 0)

        prox_fns = [norm1(X, b=B, beta=2)]
        sltn = hqs.solve(prox_fns, [], eps_rel=1e-4, max_iters = 100, max_inner_iters = 50)
        self.assertItemsAlmostEqual(X.value, B/2., places=2)
        self.assertAlmostEqual(sltn, 0)

        prox_fns = [norm1(X), sum_squares(X, b=B)]
        sltn = hqs.solve(prox_fns, [], eps_rel=1e-7,
                     rho_0 = 1.0, rho_scale = np.sqrt(2.0) * 2.0, rho_max = 2**16,
                    max_iters = 20, max_inner_iters = 500)

        cvx_X = cvx.Variable(10, 5)
        cost = cvx.sum_squares(cvx_X - B) + cvx.norm(cvx_X, 1)
        prob = cvx.Problem(cvx.Minimize(cost))
        prob.solve()
        self.assertItemsAlmostEqual(X.value, cvx_X.value, places=2)
        self.assertAlmostEqual(sltn, prob.value, places=3)

        psi_fns, omega_fns = hqs.partition(prox_fns)
        sltn = hqs.solve(psi_fns, omega_fns, eps_rel=1e-7,
                     rho_0 = 1.0, rho_scale = np.sqrt(2.0) * 2.0, rho_max = 2**16,
                    max_iters = 20, max_inner_iters = 500)
        self.assertItemsAlmostEqual(X.value, cvx_X.value, places=2)
        self.assertAlmostEqual(sltn, prob.value, places=3)

        # With linear operators.
        kernel = np.array([1,2,3])
        kernel_mat = np.matrix("2 1 3; 3 2 1; 1 3 2")
        L = np.linalg.norm(kernel_mat)
        x = Variable(3)
        b = np.array([-41,413,2])
        prox_fns = [nonneg(x), sum_squares(conv(kernel, x), b=b)]
        hqs.solve(prox_fns, [], eps_rel=1e-9, rho_0 = 4, rho_scale = np.sqrt(2.0)*1.0,
                  rho_max = 2**16, max_iters = 30, max_inner_iters = 500)

        cvx_X = cvx.Variable(3)
        cost = cvx.norm(kernel_mat*cvx_X - b)
        prob = cvx.Problem(cvx.Minimize(cost), [cvx_X >= 0])
        prob.solve()
        self.assertItemsAlmostEqual(x.value, cvx_X.value, places=0)

        psi_fns, omega_fns = hqs.partition(prox_fns)
        hqs.solve(psi_fns, omega_fns, eps_rel=1e-9, rho_0 = 4, rho_scale = np.sqrt(2.0)*1.0,
                  rho_max = 2**16, max_iters = 30, max_inner_iters = 500)
        self.assertItemsAlmostEqual(x.value, cvx_X.value, places=0)
def cbp_polar(y, F, Fprev, Fnext, Delta, penalty=0.1, order=1):
    """
    CBP with polar interpolation
    """

    # compute r and theta
    a = 0.5 * np.linalg.norm(Fnext-Fprev, axis=0)[int(F.shape[1]/2)]
    b = np.linalg.norm(F-Fprev, axis=0)[int(F.shape[1]/2)]
    theta = np.pi - 2 * np.arcsin(a/b)      # radians
    r = a / np.sin(theta)

    # build the polar transformation matrix
    P = np.array([[1,r*np.cos(theta),-r*np.sin(theta)], [1,r,0], [1,r*np.cos(theta),r*np.sin(theta)]])

    # get C, U, and V
    pol = np.linalg.inv(P).dot(np.vstack((Fprev.ravel(),F.ravel(),Fnext.ravel())))
    C = pol[0,:].reshape(F.shape)
    U = pol[1,:].reshape(F.shape)
    V = pol[2,:].reshape(F.shape)

    ## construct the problem

    # discretized matrices
    Cp = cvxopt.matrix(C)
    Up = cvxopt.matrix(U)
    Vp = cvxopt.matrix(V)
    yp = cvxopt.matrix(y)

    # sparsity penalty
    gamma = cp.Parameter(sign="positive", name='gamma')
    gamma.value = penalty

    # variables
    dx = cp.Variable(F.shape[1],name='x')
    dy = cp.Variable(F.shape[1],name='y')
    dz = cp.Variable(F.shape[1],name='z')

    # objective and constraints
    objective = cp.Minimize(sum(cp.square(yp - Cp*dx - Up*dy - Vp*dz)) + gamma*cp.norm(dx, 1))
    #constraints = [0 <= x, cp.sqrt(cp.square(y)+cp.square(z)) <= r*x, r*np.cos(theta)*x <= y, y <= r*x]
    sqcon = [cp.norm(cp.vstack(yi,zi),2) <= xi*r for xi, yi, zi in zip(dx,dy,dz)]
    constraints = [0 <= dx, dy <= r*dx, r*np.cos(theta)*dx <= dy]
    constraints.extend(sqcon)
    p = cp.Problem(objective, constraints)

    # solve
    result = p.solve()

    # reconstruct
    yhat = C.dot(np.array(dx.value)) + U.dot(np.array(dy.value)) + V.dot(np.array(dz.value))

    return np.array(dx.value), yhat, p.value
Example #12
0
  def __solve__(self):
    """ Solve the optimization problem associated with each point """
    
    # Cones on the plus and minus side
    sup_plus_vars = cvx.Variable(len(self.points), self.dim)
    inf_plus_vars = cvx.Variable(len(self.points), self.dim)
    sup_minus_vars = cvx.Variable(len(self.points), self.dim)
    inf_minus_vars = cvx.Variable(len(self.points), self.dim)
    constraints = [sup_plus_vars >= 0, inf_plus_vars >= 0, sup_minus_vars >= 0, sup_plus_vars >= 0]

    # inf/sup relational constraints
    constraints += [sup_plus_vars >= inf_plus_vars]
    constraints += [sup_minus_vars <= inf_minus_vars]

    # Cone constraints
    for (i,(pone,vone)) in enumerate(self.points.iteritems()):
      # point i has to be able to project into point j
      for (j,(ptwo,vtwo)) in enumerate(self.points.iteritems()):
        if i==j: continue
        lhs_sup = 0.0
        lhs_inf = 0.0
        for (di,(poned,ptwod)) in enumerate(zip(pone,ptwo)):
          run = ptwod - poned
          supvar = 0.0
          infvar = 0.0
          if ptwod > poned:
            supvar = sup_plus_vars[i,di]
            infvar = inf_plus_vars[i,di]
          elif ptwod < poned:
            supvar = sup_minus_vars[i,di]
            infvar = inf_minus_vars[i,di]
          lhs_sup += run*supvar
          lhs_inf += run*infvar
        constraints += [lhs_sup >= vtwo-vone, lhs_inf <= vtwo-vone]

    # Optimization : minimize cone width
    objective = cvx.Minimize(cvx.norm(sup_plus_vars-inf_plus_vars)+cvx.norm(inf_minus_vars-sup_minus_vars))
    p = cvx.Problem(objective, constraints)
    # Run it!
    try:
      p.solve(verbose=False)
    except Exception as e:
      print e
    # Post-mortem...
    if p.status != 'optimal' and p.status != 'optimal_inaccurate':
      raise ScoreCreationError("Could not create scoring function: Optimization Failed")
      return None
    # Pull out the coefficients from the variables
    plus_vars = [[(sup_plus_vars[i,j].value,inf_plus_vars[i,j].value) for j in xrange(self.dim)] for i in xrange(len(self.points))]
    minus_vars = [[(sup_minus_vars[i,j].value,inf_minus_vars[i,j].value) for j in xrange(self.dim)] for i in xrange(len(self.points))]
    return plus_vars, minus_vars
Example #13
0
	def train(self, x, y, p, A, alpha, beta, lambda1, lambda2):
		w = cvxpy.Variable(x.shape[1])
		objective = cvxpy.Minimize(
						cvxpy.sum_squares(y - x * w)
						- alpha * p * w
						+ beta * cvxpy.norm(A * w , 1)
						+ lambda1 * cvxpy.norm(w, 2) ** 2
						+ lambda2 * cvxpy.norm(w, 1)
					)

		prob = cvxpy.Problem(objective)
		result = prob.solve(solver = cvxpy.CVXOPT, verbose = True)

		self.w = w.value
Example #14
0
    def test_pock_chambolle(self):
        """Test pock chambolle algorithm.
        """
        X = Variable((10,5))
        B = np.reshape(np.arange(50), (10,5))
        prox_fns = [sum_squares(X, b=B)]
        sltn = pc.solve(prox_fns, [], 1.0, 1.0, 1.0, eps_rel=1e-5, eps_abs=1e-5)
        self.assertItemsAlmostEqual(X.value, B, places=2)
        self.assertAlmostEqual(sltn, 0)

        prox_fns = [norm1(X, b=B, beta=2)]
        sltn = pc.solve(prox_fns, [], 1.0, 1.0, 1.0, eps_rel=1e-5, eps_abs=1e-5)
        self.assertItemsAlmostEqual(X.value, B/2., places=2)
        self.assertAlmostEqual(sltn, 0, places=2)

        prox_fns = [norm1(X), sum_squares(X, b=B)]
        sltn = pc.solve(prox_fns, [], 0.5, 1.0, 1.0, eps_rel=1e-5, eps_abs=1e-5)

        cvx_X = cvx.Variable(10, 5)
        cost = cvx.sum_squares(cvx_X - B) + cvx.norm(cvx_X, 1)
        prob = cvx.Problem(cvx.Minimize(cost))
        prob.solve()
        self.assertItemsAlmostEqual(X.value, cvx_X.value, places=2)
        self.assertAlmostEqual(sltn, prob.value)

        psi_fns, omega_fns = pc.partition(prox_fns)
        sltn = pc.solve(psi_fns, omega_fns, 0.5, 1.0, 1.0,
            eps_abs=1e-5, eps_rel=1e-5)
        self.assertItemsAlmostEqual(X.value, cvx_X.value, places=2)
        self.assertAlmostEqual(sltn, prob.value)

        # With linear operators.
        kernel = np.array([1,2,3])
        kernel_mat = np.matrix("2 1 3; 3 2 1; 1 3 2")
        L = np.linalg.norm(kernel_mat)
        x = Variable(3)
        b = np.array([-41,413,2])
        prox_fns = [nonneg(x), sum_squares(conv(kernel, x), b=b)]
        sltn = pc.solve(prox_fns, [], 0.1, 0.1, 1.0, max_iters=3000,
            eps_abs=1e-5, eps_rel=1e-5)
        cvx_X = cvx.Variable(3)
        cost = cvx.norm(kernel_mat*cvx_X - b)
        prob = cvx.Problem(cvx.Minimize(cost), [cvx_X >= 0])
        prob.solve()
        self.assertItemsAlmostEqual(x.value, cvx_X.value, places=2)

        psi_fns, omega_fns = pc.partition(prox_fns)
        sltn = pc.solve(psi_fns, omega_fns, 0.1, 0.1, 1.0, max_iters=3000,
            eps_abs=1e-5, eps_rel=1e-5)
        self.assertItemsAlmostEqual(x.value, cvx_X.value, places=2)
def lasso(A, b, gam):
    import cvxpy as cp
    import numpy as np
    import cvxopt
    #from multiprocessing import Pool

    # Problem data.
    gamma = cp.Parameter(sign="positive")

    # Construct the problem.
    x = cp.Variable(A.shape[1])
    objective = cp.Minimize(cp.sum_squares(A * x - b) + gamma * cp.norm(x, 1))
    p = cp.Problem(objective)

    # Assign a value to gamma and find the optimal x.
    def get_x(gamma_value):
        gamma.value = gamma_value
        result = p.solve()
        return x.value

    #gammas = np.logspace(-1, 2, num=100)
    # Serial computation.
    #x_values = [get_x(value) for value in gammas]

    # Parallel computation.
    #pool = Pool(processes=4)
    #par_x = pool.map(get_x, gammas)

    #for v1, v2 in zip(x_values, par_x):
        #if np.linalg.norm(v1 - v2) > 1e-5:
            #print "error"

    return get_x(gam)
Example #16
0
def disc_one(x,y,ncuts,k=1,segs=None,lnorm=1,tune=50,constant=True,eps=0.01,cvx_solver=2):
	# possible solvers to choose from: typically CVXOPT works better for simulations thus far.  
	solvers = [cvx.SCS,cvx.ECOS,cvx.CVXOPT] # 0 is SCS, 1 is ECOS, 2 CVXOPT
	
	n = x.size
	# Create D-matrix: specify n and k
	if segs==None:
		segs = np.linspace(min(x)-eps,max(x)+eps,ncuts)
	D = utils.form_Dk(n=ncuts,k=k)

	# Create O-matrix: specify x and number of desired cuts
	O = utils.Omatrix(x,ncuts,constant=constant,eps=eps,segs=segs)
	
	# Solve convex problem.
	theta = cvx.Variable(ncuts)
	obj = cvx.Minimize(0.5 * cvx.sum_squares(y - O*theta)
                   + tune * cvx.norm(D*theta, lnorm) )
	prob = cvx.Problem(obj)
	# Use CVXOPT as the solver
	prob.solve(solver=solvers[cvx_solver],verbose=False)

	print 'Solver status: ', prob.status
	# Check for error.
	if prob.status != cvx.OPTIMAL:
		raise Exception("Solver did not converge!")
	if constant==True: return [segs, np.array(theta.value),x,y,segs,constant,eps]
	if constant==False: return [x, O.dot(np.array(theta.value)),x,y,segs,constant,eps] 
Example #17
0
    def solve_sparse(self, y, w, x_mask=None):
        assert y.ndim == 1 and w.ndim == 1 and y.shape == w.shape
        assert w.shape[0] == self.n

        x = cp.Variable(np.count_nonzero(x_mask))
        inv_mask = np.logical_not(x_mask)

        term1 = cp.square(cp.norm(x-y[x_mask]))
        term2 = self.c * cp.norm1(cp.diag(w[x_mask]) * x)

        objective = cp.Minimize(term1 + term2)
        constraints = [cp.quad_form(x, self.B[np.ix_(x_mask, x_mask)]) <= 1]
        problem = cp.Problem(objective, constraints)

        result = problem.solve(solver=cp.SCS)
        if problem.status != cp.OPTIMAL:
            warnings.warn(problem.status)
        if problem.status not in (cp.OPTIMAL, cp.OPTIMAL_INACCURATE,
                                  cp.UNBOUNDED_INACCURATE):
            raise ValueError(problem.status)

        out = np.zeros(self.n)
        x = np.asarray(x.value).flatten()
        out[x_mask] = x
        return out
Example #18
0
def lars_one(x,y,k=1,tune=50,eps=0.01,cvx_solver=0):
	# possible solvers to choose from: typically CVXOPT works better for simulations thus far.  
	solvers = [cvx.SCS,cvx.CVXOPT] # 0 is SCS, 1 CVXOPT
	default_iter = [160000,3200][cvx_solver] # defaults are 2500 and 100

	# Create T: the knots
	T = knots(x=x,k=k)
	D = utils.form_Dk(n=x.size,k=k)

	# Create G-matrix (truncated power basis): specify n and k
	G = tpb(x=x,t=T,k=k)

	# Solve convex problem.

	theta = cvx.Variable(x.size)
	obj = cvx.Minimize(0.5 * cvx.sum_squares(y - G*theta)
                   + tune * cvx.norm(D*theta, 1) )
	prob = cvx.Problem(obj)
	prob.solve(solver=solvers[cvx_solver],verbose=False,max_iters = default_iter)

	# Check for error.
	# This while loop only works for SCS. CVXOPT terminates after 1 iteration. 
	counter = 0
	while prob.status != cvx.OPTIMAL:
		maxit = 2*default_iter
		prob.solve(solver=solvers[cvx_solver],verbose=False,max_iters=maxit)
		default_iter = maxit
		counter = counter +1
		if counter>4:
			raise Exception("Solver did not converge with %s iterations! (N=%s,d=%s,k=%s)" % (default_iter,n,ncuts,k) )
	
	output = {'theta.hat':np.array(theta.value),'fitted': G.dot(np.array(theta.value)),'x':x,'y':y,'eps':eps}  
	return output
def cbp_taylor(y, F, Delta, penalty=0.1, order=1):
    """
    1st order taylor approximation
    """

    # generate derivative matrices
    dF = list()
    current = F
    for i in range(order):
        dF.append( deriv(current,t) )
        current = dF[-1]

    # Construct the problem.
    Fp = cvxopt.matrix(F)
    dFp = cvxopt.matrix(dF[0])
    yp = cvxopt.matrix(y)
    gamma = cp.Parameter(sign="positive", name='gamma')
    gamma.value = penalty

    x = cp.Variable(F.shape[1],name='x')
    d = cp.Variable(F.shape[1],name='d')
    objective = cp.Minimize(sum(cp.square(yp - Fp*x - dFp*d)) + gamma*cp.norm(x, 1))
    constraints = [0 <= x, cp.abs(d) <= 0.5*Delta*x]
    p = cp.Problem(objective, constraints)

    # solve
    result = p.solve()

    # reconstruct
    yhat = F.dot(np.array(x.value)) + dF[0].dot(np.array(d.value))

    return np.array(x.value), yhat, np.array(d.value), p.value
Example #20
0
def test_consensus():
    n = 100
    k = 4
    np.random.seed(0)

    proxes = []
    for _ in range(k):
        A = np.random.randn(n,n)
        b = np.random.randn(n)
        x = cvx.Variable(n)
        
        obj = cvx.Minimize(cvx.norm(A*x-b))
        prob = cvx.Problem(obj)
        
        prox = Prox(prob, {'x':x})
        proxes += [prox]

    admm = ADMM(proxes, rho=1)
    admm.step(100)

    # check that the residuals are relatively small
    assert 1e-7 <= admm.infos[-1]['r'] <= 1e-4
    assert 1e-7 <= admm.infos[-1]['s'] <= 1e-4

    # a few more admm steps should make the residuals very small
    admm.step(100)

    assert admm.infos[-1]['r'] <= 1e-8
    assert admm.infos[-1]['s'] <= 1e-8
Example #21
0
def blockCompressedSenseL1(block, rho, alpha, basis_premultiplied, mixing_matrix):
    """ Run L1 compressed sensing given alpha and a basis."""

    # Get block size.
    block_len = block.shape[0] * block.shape[1]

    # Unravel this image into a single column vector.
    img_vector = np.asmatrix(block.ravel()).T

    # Determine m (samples)
    img_measured = mixing_matrix * img_vector

    # Construct the problem.
    coefficients = cvx.Variable(block_len)
    coefficients_premultiplied = basis_premultiplied * coefficients
    L2 = cvx.sum_squares(coefficients_premultiplied - img_measured)
    REG = cvx.sum_squares(coefficients)
    L1 = cvx.norm(coefficients, 1)
    objective = cvx.Minimize(L2 + rho * REG + alpha * L1)
    constraints = []
    problem = cvx.Problem(objective, constraints)

    # Solve.
    problem.solve(verbose=False, solver="SCS")

    # Print problem status.
    print "Problem status: " + str(problem.status)
    sys.stdout.flush()

    return coefficients.value
Example #22
0
    def test_problem(self):
        """Test problem object.
        """
        X = Variable((4, 2))
        B = np.reshape(np.arange(8), (4, 2)) * 1.
        prox_fns = [norm1(X), sum_squares(X, b=B)]
        prob = Problem(prox_fns)
        # prob.partition(quad_funcs = [prox_fns[0], prox_fns[1]])
        prob.set_automatic_frequency_split(False)
        prob.set_absorb(False)
        prob.set_implementation(Impl['halide'])
        prob.set_solver('admm')
        prob.solve()

        true_X = norm1(X).prox(2, B.copy())
        self.assertItemsAlmostEqual(X.value, true_X, places=2)
        prob.solve(solver="pc")
        self.assertItemsAlmostEqual(X.value, true_X, places=2)
        prob.solve(solver="hqs", eps_rel=1e-6,
                   rho_0=1.0, rho_scale=np.sqrt(2.0) * 2.0, rho_max=2**16,
                   max_iters=20, max_inner_iters=500, verbose=False)
        self.assertItemsAlmostEqual(X.value, true_X, places=2)

        # CG
        prob = Problem(prox_fns)
        prob.set_lin_solver("cg")
        prob.solve(solver="admm")
        self.assertItemsAlmostEqual(X.value, true_X, places=2)
        prob.solve(solver="hqs", eps_rel=1e-6,
                   rho_0=1.0, rho_scale=np.sqrt(2.0) * 2.0, rho_max=2**16,
                   max_iters=20, max_inner_iters=500, verbose=False)
        self.assertItemsAlmostEqual(X.value, true_X, places=2)

        # Quad funcs.
        prob = Problem(prox_fns)
        prob.solve(solver="admm")
        self.assertItemsAlmostEqual(X.value, true_X, places=2)

        # Absorbing lin ops.
        prox_fns = [norm1(5 * mul_elemwise(B, X)), sum_squares(-2 * X, b=B)]
        prob = Problem(prox_fns)
        prob.set_absorb(True)
        prob.solve(solver="admm", eps_rel=1e-6, eps_abs=1e-6)

        cvx_X = cvx.Variable(4, 2)
        cost = cvx.sum_squares(-2 * cvx_X - B) + cvx.norm(5 * cvx.mul_elemwise(B, cvx_X), 1)
        cvx_prob = cvx.Problem(cvx.Minimize(cost))
        cvx_prob.solve(solver=cvx.SCS)
        self.assertItemsAlmostEqual(X.value, cvx_X.value, places=2)

        prob.set_absorb(False)
        prob.solve(solver="admm", eps_rel=1e-6, eps_abs=1e-6)
        self.assertItemsAlmostEqual(X.value, cvx_X.value, places=2)

        # Constant offsets.
        prox_fns = [norm1(5 * mul_elemwise(B, X)), sum_squares(-2 * X - B)]
        prob = Problem(prox_fns)
        prob.solve(solver="admm", eps_rel=1e-6, eps_abs=1e-6)
        self.assertItemsAlmostEqual(X.value, cvx_X.value, places=2)
Example #23
0
def constraint_norm1(Ftilde, Sigma_F, positivity=False):
    """ Constrain with optimization strategy """
    import cvxpy as cvx
    m, n = Sigma_F.shape
    Sigma_F_inv = np.linalg.inv(Sigma_F)
    zeros_F = np.zeros_like(Ftilde[:, np.newaxis])
    F = cvx.Variable(n)      # Construct the problem. PRIMAL
    expression = cvx.quad_form(F - Ftilde[:, np.newaxis], Sigma_F_inv)
    objective = cvx.Minimize(expression)
    if positivity:
        constraints = [F[0] == 0, F[-1] == 0, F >= zeros_F,
                       cvx.square(cvx.norm(F, 2)) <= 1]
    else:
        constraints = [F[0] == 0, F[-1] == 0, cvx.square(cvx.norm(F, 2)) <= 1]
    prob = cvx.Problem(objective, constraints)
    prob.solve(verbose=0, solver=cvx.CVXOPT)
    return np.squeeze(np.array((F.value)))
Example #24
0
    def test_convexify_constr(self):
        """
        Test convexify constraint
        """
        constr = cvx.norm(self.x) >= 1
        self.x.value = [1,1]
        constr_conv = convexify_constr(constr)
        prob_conv = cvx.Problem(cvx.Minimize(cvx.norm(self.x)), [constr_conv[0]])
        prob_conv.solve()
        self.assertAlmostEqual(prob_conv.value,1)

        constr = cvx.sqrt(self.a) <= 1
        self.a.value = [1]
        constr_conv = convexify_constr(constr)
        prob_conv = cvx.Problem(cvx.Minimize(self.a), [constr_conv[0],constr_conv[1][0]])
        prob_conv.solve()
        self.assertAlmostEqual(self.a.value[0],0)
Example #25
0
    def test_log_det(self):
        # TODO
        # Generate data
        x = np.matrix("0.55  0.0;"
                      "0.25  0.35;"
                      "-0.2   0.2;"
                      "-0.25 -0.1;"
                      "-0.0  -0.3;"
                      "0.4  -0.2").T
        (n, m) = x.shape

        # Create and solve the model
        A = cp.Variable(n, n);
        b = cp.Variable(n);
        obj = cp.Maximize( cp.log_det(A) )
        constraints = []
        for i in range(m):
            constraints.append( cp.norm(A*x[:, i] + b) <= 1 )
        p = cp.Problem(obj, constraints)
        result = p.solve()
        self.assertAlmostEqual(result, 1.9746)

    # # Risk return tradeoff curve
    # def test_risk_return_tradeoff(self):
    #     from math import sqrt
    #     from cvxopt import matrix
    #     from cvxopt.blas import dot
    #     from cvxopt.solvers import qp, options
    #     import scipy

    #     n = 4
    #     S = matrix( [[ 4e-2,  6e-3, -4e-3,   0.0 ],
    #                  [ 6e-3,  1e-2,  0.0,    0.0 ],
    #                  [-4e-3,  0.0,   2.5e-3, 0.0 ],
    #                  [ 0.0,   0.0,   0.0,    0.0 ]] )
    #     pbar = matrix([.12, .10, .07, .03])

    #     N = 100
    #     # CVXPY
    #     Sroot = numpy.asmatrix(scipy.linalg.sqrtm(S))
    #     x = cp.Variable(n, name='x')
    #     mu = cp.Parameter(name='mu')
    #     mu.value = 1 # TODO cp.Parameter("positive")
    #     objective = cp.Minimize(-pbar*x + mu*quad_over_lin(Sroot*x,1))
    #     constraints = [sum(x) == 1, x >= 0]
    #     p = cp.Problem(objective, constraints)

    #     mus = [ 10**(5.0*t/N-1.0) for t in range(N) ]
    #     xs = []
    #     for mu_val in mus:
    #         mu.value = mu_val
    #         p.solve()
    #         xs.append(x.value)
    #     returns = [ dot(pbar,x) for x in xs ]
    #     risks = [ sqrt(dot(x, S*x)) for x in xs ]

    #     # QP solver
Example #26
0
def sobbynorm(k1,k2, lnorm,theta,delta1,delta2,m):
	D1 = []
	D2 = []
	norm_list = []
	for i in range(len(k1)):
		D1.append(utils.form_Dk(n=m,k=k1[i])*(delta1**(1/lnorm-k1[i])))
		D2.append(utils.form_Dk(n=m,k=k2[i])*(delta2**(1/lnorm-k2[i])))
		norm_list.append(cvx.norm(D1[i]*theta*D2[i].T,lnorm))
	return np.sum(norm_list)
Example #27
0
def basic_glasso(target_dim, cov_mat, reg=0.1, norm=1, return_inv=False, verbose=False):
    theta = cvx.Semidef(target_dim)
    objective = cvx.Minimize(-cvx.log_det(theta) + cvx.trace(cov_mat*theta) + reg*cvx.norm(theta, norm))
    problem = cvx.Problem(objective)
    problem.solve(verbose=verbose)
    out = theta.value
    if return_inv:
        out = np.linalg.inv(out)
    return out
    def run(self, util_rate_generator, load):
        """
        Given a load and a pricing scheme, computes the optimal
        charge/discharge schedule for the battery which minimizes
        total cost.
        """

        # renamaing vars for readibility
        T = const.HORIZON
        # battery constraint params
        C = self.max_capacity
        M = self.max_power_output
        # energy storage of the battery
        s = cvx.Variable(T+1)
        # power output of the battery
        u = cvx.Variable(T)
        # state matrix
        A = np.diag([1]*(T), k=-1)

        # cost metrics for enery and demand
        energy_metric = lambda x : cvx.norm(x,1)
        demand_metric = lambda x : cvx.norm(x, 'inf')

        cost = cvx.Minimize(self.cost_function(u, load , util_rate_generator, energy_metric, demand_metric))
        #cost = cvx.Minimize(.2*load.T*(load + u))
        constraints = [
            s == A*s + cvx.vstack(0, self.acdc_eff*u),
            s <= C,
            s >= 0,
            u <= M,
            u >= -M,
            u + load >=0
        ]
        # sums problem objectives and concatenates constraints.
        prob = cvx.Problem(cost, constraints)
        # add final stopping conditions on the battery
        prob.constraints += [s[T] == 0, s[0] == 0]

        optval = prob.solve()
        self.optimal_cost = optval
        self.optimal_s = s
        self.optimal_u = u
        self.problem = prob
Example #29
0
def l1_solution(A, b, lam=0.5):
    N = A.shape[0]
    x = Variable(N)
    objective = Minimize(sum_entries(square(A * x - b)) + lam * norm(x, 1))
    constraints = []
    prob = Problem(objective, constraints)

    prob.solve()
    xhat = x.value
    return xhat
Example #30
0
def transition_to_action(s0,s1, dynamics, max_dtheta, max_ddx, max_iter=30,
                         max_line_searches=10):
  """recovers a control signal u that can cause a transition from s0 to s1.

  specifically, minimize || f(s0,u) - s1 ||^2 as a Sequential
  Quadratic Program.
  """
  # the current step size along the search direction recovered by the QP
  step = 1.

  # initial guess
  s0 = array(s0)
  s1 = array(s1)
  u0 = array((0,0))

  def cost(u):
    "|| f(s0,u) - s1 ||^2"
    return sum((dynamics(s0,u)['val'] - s1)**2)

  # the value of the initial guess
  best_cost = cost(u0)

  for it in xrange(max_iter):
    f = dynamics(s0, u0, derivs={'du'})

    # linearize || f(s0,u) - s1 ||^2 about u0 and solve as a QP
    u = CX.Variable(len(u0), name='u')
    objective = CX.square(CX.norm( array(f['val']) + vstack(f['du'])*(u-u0) - s1 ))
    p = CX.Problem(CX.Minimize(objective),
                   [CX.abs(u[0]) <= max_ddx,
                    CX.abs(u[1]) <= max_dtheta])
    r = p.solve()
    unew = array(u.value.flat)

    # line search along unew-u0 from u0
    line_search_success = False
    for line_searches in xrange(max_line_searches):
      new_cost = cost(u0 + step*(unew-u0))
      if new_cost < best_cost:
        # accept the step
        best_cost = new_cost
        u0 = u0 + step*(unew-u0)
        # grow the step for the next iteration
        step *= 1.2
        line_search_success = True
      else:
        # shrink the step size and try again
        step *= 0.5
    if not line_search_success:
      # convergence is when line search fails.
      return u0

  print 'Warning: failed to converge'
  return u0
Example #31
0
def dist(lh_set, rh_set):
    objective = cvx.Minimize(cvx.norm(lh_set - rh_set, 2))
    return cvx.Problem(objective).solve(solver=cvx.CVXOPT)
Example #32
0
x_0[7][0] = 1.887818931604597 ;

printf("Solution was :\n");
printf("   Objective : %f \n", solver_work->info->pcost);
"""

states = []
constraints  = [ x[:,T] == 0 ]
constraints += [ x[:,0] == x_0 ]

for t in range(T):

    cost = cvx.sum_squares(x[:,t+1]) + cvx.sum_squares(u[:,t])

    constraints += [ x[:,t+1] == A*x[:,t] + B*u[:,t] ]
    constraints += [ cvx.norm(u[:,t], 'inf') <= 1 ]

    states.append( cost )

# sums problem objectives and concatenates constraints.
objective = cvx.sum(states)
objective = cvx.Minimize(objective)

problem = cvx.Problem(objective, constraints)

data = problem.get_problem_data('ECOS')[0]

"""
print('c =',data['c'])
print('dims', 'soc:',data['dims'].soc,'l:',data['dims'].nonpos)
Example #33
0
    def __init__(self, m, K):
        # Variables:
        self.var = dict()
        self.var['X'] = cvxpy.Variable((m.n_x, K))
        self.var['U'] = cvxpy.Variable((m.n_u, K))
        self.var['sigma'] = cvxpy.Variable(nonneg=True)
        self.var['nu'] = cvxpy.Variable((m.n_x, K - 1))
        self.var['delta_norm'] = cvxpy.Variable(nonneg=True)
        self.var['sigma_norm'] = cvxpy.Variable(nonneg=True)

        # Parameters:
        self.par = dict()
        self.par['A_bar'] = cvxpy.Parameter((m.n_x * m.n_x, K - 1))
        self.par['B_bar'] = cvxpy.Parameter((m.n_x * m.n_u, K - 1))
        self.par['C_bar'] = cvxpy.Parameter((m.n_x * m.n_u, K - 1))
        self.par['S_bar'] = cvxpy.Parameter((m.n_x, K - 1))
        self.par['z_bar'] = cvxpy.Parameter((m.n_x, K - 1))

        self.par['X_last'] = cvxpy.Parameter((m.n_x, K))
        self.par['U_last'] = cvxpy.Parameter((m.n_u, K))
        self.par['sigma_last'] = cvxpy.Parameter(nonneg=True)

        self.par['weight_sigma'] = cvxpy.Parameter(nonneg=True)
        self.par['weight_delta'] = cvxpy.Parameter(nonneg=True)
        self.par['weight_delta_sigma'] = cvxpy.Parameter(nonneg=True)
        self.par['weight_nu'] = cvxpy.Parameter(nonneg=True)

        # Constraints:
        constraints = []

        # Model:
        constraints += m.get_constraints(self.var['X'], self.var['U'],
                                         self.par['X_last'],
                                         self.par['U_last'])

        # Dynamics:
        # x_t+1 = A_*x_t+B_*U_t+C_*U_T+1*S_*sigma+zbar+nu
        constraints += [
            self.var['X'][:, k + 1] == cvxpy.reshape(self.par['A_bar'][:, k],
                                                     (m.n_x, m.n_x)) *
            self.var['X'][:, k] +
            cvxpy.reshape(self.par['B_bar'][:, k],
                          (m.n_x, m.n_u)) * self.var['U'][:, k] +
            cvxpy.reshape(self.par['C_bar'][:, k],
                          (m.n_x, m.n_u)) * self.var['U'][:, k + 1] +
            self.par['S_bar'][:, k] * self.var['sigma'] +
            self.par['z_bar'][:, k] + self.var['nu'][:, k]
            for k in range(K - 1)
        ]

        # Trust regions:
        dx = cvxpy.sum(cvxpy.square(self.var['X'] - self.par['X_last']),
                       axis=0)
        du = cvxpy.sum(cvxpy.square(self.var['U'] - self.par['U_last']),
                       axis=0)
        ds = self.var['sigma'] - self.par['sigma_last']
        constraints += [cvxpy.norm(dx + du, 1) <= self.var['delta_norm']]
        constraints += [cvxpy.norm(ds, 'inf') <= self.var['sigma_norm']]

        # Flight time positive:
        constraints += [self.var['sigma'] >= 0.1]

        # Objective:
        sc_objective = cvxpy.Minimize(
            self.par['weight_sigma'] * self.var['sigma'] +
            self.par['weight_nu'] * cvxpy.norm(self.var['nu'], 'inf') +
            self.par['weight_delta'] * self.var['delta_norm'] +
            self.par['weight_delta_sigma'] * self.var['sigma_norm'])

        objective = sc_objective

        self.prob = cvxpy.Problem(objective, constraints)
def sparse_kmeans(AllDataMatrix, s, niter, group):

    w = [1 / np.sqrt(AllDataMatrix.shape[1])] * AllDataMatrix.shape[1]

    wx = np.zeros((len(AllDataMatrix), AllDataMatrix.shape[1]))
    for j in range(AllDataMatrix.shape[1]):
        wx[:, j] = AllDataMatrix[:, j] * (np.sqrt(w)[j])
    alpha_group = s
    s_orig = s
    nclust = 6

    #kmt = KMeans(n_clusters=nclust, init='random', n_init=100,verbose=False, tol=0.0000000001)
    #kmt.fit(wx)
    #print kmt.labels_
    #print adjusted_rand_score(labels, kmt.labels_)

    kmt = rkmeans(x=numpy2ri(wx), centers=nclust, nstart=100)
    kmlabels = np.array(kmt[0])
    print adjusted_rand_score(labels, np.array(kmt[0]))
    #overall iterations
    for i in range(niter):
        #print i
        #1.get bcssj

        aj_list = []
        for j in range(AllDataMatrix.shape[1]):
            dat_j = AllDataMatrix[:, j].reshape((len(AllDataMatrix), 1))
            djall = euclidean_distances(dat_j, dat_j)
            sumd_all = np.sum(djall**2) / len(AllDataMatrix)
            nk_list = []
            sumd_k_list = []

            for k in range(nclust):
                dat_j = AllDataMatrix[kmlabels == k, j]
                dat_j = dat_j.reshape((len(dat_j), 1))
                if (len(dat_j) < 1):
                    d = 0
                else:
                    d = euclidean_distances(dat_j, dat_j)
                nk = len(dat_j)
                sumd_k = np.sum(d**2)
                nk_list.append(nk)
                sumd_k_list.append(sumd_k)

            nk_list = np.array(nk_list)
            sumd_k_list = np.array(sumd_k_list)
            #compute within-sum of squares over feature j
            nk_list[nk_list == 0] = -1
            one_nk_list = 1. / nk_list
            one_nk_list[np.sign(one_nk_list) == -1] = 0
            withinssj = np.sum(one_nk_list * sumd_k_list)
            #aj = totalss/n - wss/nk
            aj = sumd_all - withinssj
            aj_list.append(aj)
        #2. get w
        a = np.array(aj_list)
        lenseq = np.array([256, 128, 64, 32, 16, 8, 4, 2, 1, 1])
        lenseq = np.array([256, 128, 64, 32, 16, 8, 8])
        nlevels = len(lenseq)

        sqrtlenseq = np.sqrt(lenseq)
        indseq = np.cumsum(lenseq)
        wvar = cvx.Variable(len(a))

        t = cvx.Variable(nlevels)
        ## Form objective.

        if group:
            ####GROUP SPARSE
            #obj = cvx.Minimize(sum(-1*a*wvar) + alpha_group*sum(t))
            obj = cvx.Minimize(sum(-1 * a * wvar))

            group0 = [cvx.norm(wvar[0:(indseq[0] - 1)], 2) <= t[0]]
            group1 = [cvx.norm(wvar[indseq[0]:(indseq[1])], 2) <= t[1]]
            group2 = [cvx.norm(wvar[indseq[1]:(indseq[2])], 2) <= t[2]]
            group3 = [cvx.norm(wvar[indseq[2]:(indseq[3])], 2) <= t[3]]
            group4 = [cvx.norm(wvar[indseq[3]:(indseq[4])], 2) <= t[4]]
            group5 = [cvx.norm(wvar[indseq[4]:(indseq[5])], 2) <= t[5]]
            group6 = [cvx.norm(wvar[indseq[5]:(indseq[6])], 2) <= t[6]]

            #        group0 = [cvx.norm(wvar[0:indseq[0]],2)<=t[0]]
            #        group1 = [cvx.norm(wvar[indseq[0]:indseq[1]],2)<=t[1]]
            #            group0 = [sqrtlenseq[0]*cvx.norm(wvar[0:(indseq[0]-1)],2)<=t[0]]
            #            group1 = [sqrtlenseq[1]*cvx.norm(wvar[indseq[0]:(indseq[1])],2)<=t[1]]
            #            group2 = [sqrtlenseq[2]*cvx.norm(wvar[indseq[1]:(indseq[2])],2)<=t[2]]
            #            group3 = [sqrtlenseq[3]*cvx.norm(wvar[indseq[2]:(indseq[3])],2)<=t[3]]
            #            group4 = [sqrtlenseq[4]*cvx.norm(wvar[indseq[3]:(indseq[4])],2)<=t[4]]
            #            group5 = [sqrtlenseq[5]*cvx.norm(wvar[indseq[4]:(indseq[5])],2)<=t[5]]
            #            group6 = [sqrtlenseq[6]*cvx.norm(wvar[indseq[5]:(indseq[6])],2)<=t[6]]
            #
            #group7 = [cvx.norm(wvar[indseq[6]:(indseq[7])],2)<=t[7]]
            #        group8 = [cvx.norm(wvar[indseq[7]:(indseq[8])],2)<=t[8]]
            #        group9 = [cvx.norm(wvar[indseq[8]:(indseq[9])],2)<=t[9]]

            ###"correct" constraints
            #constr = [wvar>=0,sum(wvar)==1] + group0 + group1 + group2 + group3 + group4 + group5 + group6
            ##l2 constraints
            #constr = [cvx.square(cvx.norm2(wvar))<=1,wvar>=0] + group0 + group1 + group2 + group3 + group4 + group5 + group6 + group7 + group8 + group9
            #constr = [cvx.square(cvx.norm2(wvar))<=1,wvar>=0] + group0 + group1 + group2 + group3 + group4 + group5 + group6 + group7
            constr = [
                cvx.square(cvx.norm2(wvar)) <= 1, wvar >= 0
            ] + group0 + group1 + group2 + group3 + group4 + group5 + group6
            constr = constr + [sum(t) <= alpha_group]  #cvx.norm1(wvar)<=s_orig

    ####GROUP NORM AS IN LASSO
    #        groupnormvec = [cvx.norm(wvar[0:(indseq[0]-1)],2),cvx.norm(wvar[indseq[0]:(indseq[1])],2),
    #                    cvx.norm(wvar[indseq[1]:(indseq[2])],2),cvx.norm(wvar[indseq[2]:(indseq[3])],2),
    #                    cvx.norm(wvar[indseq[3]:(indseq[4])],2),cvx.norm(wvar[indseq[4]:(indseq[5])],2),
    #                    cvx.norm(wvar[indseq[5]:(indseq[6])],2)]
    #        obj = cvx.Minimize(sum(-1*a*wvar) + alpha_group*sum(groupnormvec))
    #        constr = [cvx.square(cvx.norm2(wvar))<=1,wvar>=0]
        else:
            ####ORIGINAL SPARSE KMEANS PROBLEM
            #obj = cvx.Minimize(cvx.sum(cvx.mul_elemwise(-1*a,wvar)))
            obj = cvx.Minimize(sum(-1 * a * wvar))

            constr = [
                cvx.square(cvx.norm2(wvar)) <= 1,
                cvx.norm1(wvar) <= s_orig, wvar >= 0
            ]
            #constr = [cvx.square(cvx.norm2(wvar))<=1, wvar>=0]

        prob = cvx.Problem(obj, constr)
        #prob.solve()

        try:
            prob.solve()
            #print "default solver"
        except:

            #print "SCS SOLVER"
            #prob.solve(solver =cvx.CVXOPT)
            prob.solve(solver=cvx.SCS, verbose=False)  #use_indirect=True
            #print prob.value
        w = wvar.value

        #3. update kmeans
        wx = np.zeros((len(AllDataMatrix), AllDataMatrix.shape[1]))
        for j in range(AllDataMatrix.shape[1]):
            wj = np.sqrt(w[j][0, 0])
            #wj = w[j][0,0]
            if np.isnan(wj):
                #print "bad"
                wj = 10**-20
    #        else:
    #            #print "yes"
    #            #print wj
            wx[:, j] = AllDataMatrix[:, j] * wj

    #    kmt = KMeans(n_clusters=nclust, init='random', n_init=100,verbose=False,tol=0.0000000001)
    #    kmt.fit(wx)
        kmt = rkmeans(x=numpy2ri(wx), centers=nclust, nstart=100)
        kmlabels = np.array(kmt[0])
    return prob.value, kmlabels
Example #35
0
        point_x_e[i] = -1 * length + length_setion * (i %
                                                      n_srt) + length_setion
        point_y_e[i] = length - length_setion - length_setion * (i // n_srt)

    radius_p = 2 * length / np.sqrt(n * np.pi)
    r = [radius_p for i in range(n)]

    radius = 1.5

    c = cvx.Variable((n, 2))
    constr = []

    for i in range(n - 1):
        for j in range(i + 1, n):
            constr.append(
                cvx.norm(cvx.vec(c[i, :] - c[j, :]), 2) >= r[i] + r[j])
    prob = cvx.Problem(cvx.Minimize(cvx.max(cvx.max(cvx.abs(c), axis=1) + r)),
                       constr)
    prob.solve(method='dccp', solver='ECOS', ep=1e-2, max_slack=1e-2)

    point_e = np.column_stack((point_x_e, point_y_e))
    point_t = np.column_stack((point_x_t, point_y_t))
    point_c = np.column_stack((point_x_c, point_y_c))

    z_t = np.zeros((n, 1))
    z_e = np.zeros((n, 1))
    z_c = np.zeros((n, 1))

    firing_position_x_t = np.zeros((n, nsteps))
    firing_position_y_t = np.zeros((n, nsteps))
    firing_position_x_e = np.zeros((n, nsteps))
X = spimg.zoom(rgb_to_gray(Xorig), 0.2)
ny, nx = X.shape

k = round(nx * ny * 0.5)  # 50% sample
ri = np.random.choice(nx * ny, k, replace=False)  # random sample of indices
b = X.T.flat[ri]
#b = np.expand_dims(b, axis=1)

# create dct matrix operator using kron (memory errors for large ny*nx)
A = np.kron(spfft.idct(np.identity(nx), norm='ortho', axis=0),
            spfft.idct(np.identity(ny), norm='ortho', axis=0))
A = A[ri, :]  # same as phi times kron

# do L1 optimization
vx = cvx.Variable(nx * ny)
objective = cvx.Minimize(cvx.norm(vx, 1))
constraints = [A * vx == b]
prob = cvx.Problem(objective, constraints)
result = prob.solve(verbose=True)
Xat2 = np.array(vx.value).squeeze()

Xat = Xat2.reshape(nx, ny).T  # stack columns
Xa = idct2(Xat)

mask = np.zeros(X.shape)
mask.T.flat[ri] = 255
Xm = 255 * np.ones(X.shape)
Xm.T.flat[ri] = X.T.flat[ri]

plt.imshow(Xorig)
plt.show()
Example #37
0
 def relax(self):
     """The convex relaxation.
     """
     constr = super(Annulus, self).relax()
     return constr + [cp.norm(self, 2) <= self.R]
Example #38
0
 def _restrict(self, matrix):
     # Add restriction that beyond hyperplane at projection onto
     # n-sphere of radius r.
     return [matrix.T * self >= self.r * cp.norm(matrix, 2).value]
Example #39
0
def proj(cvx_set, value):
    objective = cvx.Minimize(cvx.norm(cvx_set - value, 2))
    cvx.Problem(objective).solve(solver=cvx.CVXOPT)
    return cvx_set.value
Example #40
0
def cvx_fit(data,
            basis_matrix,
            weights=None,
            PSD=True,
            trace=None,
            trace_preserving=False,
            **kwargs):
    """
    Reconstruct a quantum state using CVXPY convex optimization.

    Args:
        data (vector like): vector of expectation values
        basis_matrix (matrix like): matrix of measurement operators
        weights (vector like, optional): vector of weights to apply to the
                                         objective function (default: None)
        PSD (bool, optional): Enforced the fitted matrix to be positive
                              semidefinite (default: True)
        trace (int, optional): trace constraint for the fitted matrix
                               (default: None).
        trace_preserving (bool, optional): Enforce the fitted matrix to be
            trace preserving when fitting a Choi-matrix in quantum process
            tomography (default: False).
        **kwargs (optional): kwargs for cvxpy solver.

    Returns:
        The fitted matrix rho that minimizes
        ||basis_matrix * vec(rho) - data||_2.

    Additional Information:

        Objective function
        ------------------
        This fitter solves the constrained least-squares minimization:

            minimize: ||a * x - b ||_2
            subject to: x >> 0 (PSD, optional)
                        trace(x) = t (trace, optional)
                        partial_trace(x) = identity (trace_preserving,
                                                     optional)

        where:
            a is the matrix of measurement operators a[i] = vec(M_i).H
            b is the vector of expectation value data for each projector
              b[i] ~ Tr[M_i.H * x] = (a * x)[i]
            x is the vectorized density matrix (or Choi-matrix) to be fitted

        PSD constraint
        --------------
        The PSD keyword constrains the fitted matrix to be
        postive-semidefinite, which makes the optimization problem a SDP. If
        PSD=False the fitted matrix will still be constrained to be Hermitian,
        but not PSD. In this case the optimization problem becomes a SOCP.

        Trace constraint
        ----------------
        The trace keyword constrains the trace of the fitted matrix. If
        trace=None there will be no trace constraint on the fitted matrix.
        This constraint should not be used for process tomography and the
        trace preserving constraint should be used instead.

        Trace preserving (TP) constraint
        --------------------------------
        The trace_preserving keyword constrains the fitted matrix to be TP.
        This should only be used for process tomography, not state tomography.
        Note that the TP constraint implicitly enforces the trace of the fitted
        matrix to be equal to the square-root of the matrix dimension. If a
        trace constraint is also specified that differs from this value the fit
        will likely fail.

        CVXPY Solvers:
        -------
        Various solvers can be called in CVXPY using the `solver` keyword
        argument. Solvers included in CVXPY are:
            'CVXOPT': SDP and SOCP (default solver)
            'SCS'   : SDP and SOCP
            'ECOS'  : SOCP only
        See the documentation on CVXPY for more information on solvers.
    """

    # Check if CVXPY package is installed
    if cvxpy is None:
        raise Exception('CVXPY is not installed. Use `mle_fit` instead.')
    # Check CVXPY version
    else:
        version = cvxpy.__version__
        if not (version[0] == '1' or version[:3] == '0.4'):
            raise Exception('Incompatible CVXPY version. Install 1.0 or 0.4')

    # SDP VARIABLES

    # Since CVXPY only works with real variables we must specify the real
    # and imaginary parts of rho seperately: rho = rho_r + 1j * rho_i

    dim = int(np.sqrt(basis_matrix.shape[1]))
    if version[:3] == '0.4':
        # Compatibility with legacy 0.4
        rho_r = cvxpy.Variable(dim, dim)
        rho_i = cvxpy.Variable(dim, dim)
    else:
        rho_r = cvxpy.Variable((dim, dim))
        rho_i = cvxpy.Variable((dim, dim))

    # CONSTRAINTS

    # The constraint that rho is Hermitian (rho.H = rho)
    # transforms to the two constraints
    #   1. rho_r.T = rho_r.T  (real part is symmetric)
    #   2. rho_i.T = -rho_i.T  (imaginary part is anti-symmetric)

    cons = [rho_r == rho_r.T, rho_i == -rho_i.T]

    # Trace constraint: note this should not be used at the same
    # time as the trace preserving constraint.

    if trace is not None:
        cons.append(cvxpy.trace(rho_r) == trace)

    # Since we can only work with real matrices in CVXPY we can specify
    # a complex PSD constraint as
    #   rho >> 0 iff [[rho_r, -rho_i], [rho_i, rho_r]] >> 0

    if PSD is True:
        rho = cvxpy.bmat([[rho_r, -rho_i], [rho_i, rho_r]])
        cons.append(rho >> 0)

    # Trace preserving constraint when fiting Choi-matrices for
    # quantum process tomography. Note that this adds an implicity
    # trace constraint of trace(rho) = sqrt(len(rho)) = dim
    # if a different trace constraint is specified above this will
    # cause the fitter to fail.

    if trace_preserving is True:
        sdim = int(np.sqrt(dim))
        ptr = partial_trace_super(sdim, sdim)
        cons.append(ptr * cvxpy.vec(rho_r) == np.identity(sdim).ravel())

    # OBJECTIVE FUNCTION

    # The function we wish to minimize is || arg ||_2 where
    #   arg =  bm * vec(rho) - data
    # Since we are working with real matrices in CVXPY we expand this as
    #   bm * vec(rho) = (bm_r + 1j * bm_i) * vec(rho_r + 1j * rho_i)
    #                 = bm_r * vec(rho_r) - bm_i * vec(rho_i)
    #                   + 1j * (bm_r * vec(rho_i) + bm_i * vec(rho_r))
    #                 = bm_r * vec(rho_r) - bm_i * vec(rho_i)
    # where we drop the imaginary part since the expectation value is real

    bm_r = np.real(basis_matrix)
    bm_i = np.imag(basis_matrix)

    # CVXPY doesn't seem to handle sparse matrices very well so we convert
    # sparse matrices to Numpy arrays.

    if isinstance(basis_matrix, sps.spmatrix):
        bm_r = bm_r.todense()
        bm_i = bm_i.todense()

    arg = bm_r * cvxpy.vec(rho_r) - bm_i * cvxpy.vec(rho_i) - np.array(data)

    # Add weights vector if specified
    if weights is not None:

        # normalie weights?
        weights = np.array(weights)
        weights = weights / np.sqrt(sum(weights**2))

        arg = cvxpy.diag(weights) * arg

    # SDP objective function
    obj = cvxpy.Minimize(cvxpy.norm(arg, p=2))

    # Solve SDP
    prob = cvxpy.Problem(obj, cons)
    iters = 5000
    max_iters = kwargs.get('max_iters', 20000)

    if 'solver' not in kwargs:
        kwargs['solver'] = 'CVXOPT'  # make CVXOPT default solver

    problem_solved = False
    while not problem_solved:
        kwargs['max_iters'] = iters
        kwargs['solver'] = 'CVXOPT'
        prob.solve(**kwargs)
        if prob.status in ["optimal_inaccurate", "optimal"]:
            problem_solved = True
        elif prob.status == "unbounded_inaccurate":
            if iters < max_iters:
                iters *= 2
            else:
                raise RuntimeError(
                    "CVX fit failed, probably not enough iterations for the "
                    "solver")
        elif prob.status in ["infeasible", "unbounded"]:
            raise RuntimeError(
                "CVX fit failed, problem status {} which should not "
                "happen".format(prob.status))
        else:
            raise RuntimeError("CVX fit failed, reason unknown")
    rho_fit = rho_r.value + 1j * rho_i.value
    return rho_fit
Example #41
0
def svm(data):
    slack = [pos(1 - b * (a.T * w[:-1] - w[-1])) for (b, a) in data]
    return norm(w, 2) + sum(slack)
Example #42
0
    def seb_lmi_init(self,
                     epsilon=1E-3,
                     gamma=1e3,
                     init_var=1.5,
                     custom_seed=None):
        n = self.nx
        m = self.nx
        p = self.nx  # Take the all states as measurements
        W = self.width

        L = self.layers
        solver_res = 1E-5

        if custom_seed is not None:
            np.random.seed(custom_seed)

        constraints = []
        objective = 0.0

        P0 = cp.Variable((n, n), 'P0')
        P = cp.Variable((self.layers + 1, W), 'P')
        E = []
        H = []
        B = np.eye(n)

        # C = cp.Variable((p, n), 'C')
        C = np.eye(p)
        # C = np.array([[1, 0, 0, 0], [0, 0, 1, 0]])

        M = []
        for layer in range(self.layers + 2):

            # First layer
            if layer == 0:
                # Initialize the forward dynamics
                W_star = np.random.normal(0, init_var / np.sqrt(n), (W, n))

                E += [cp.Variable((n, n), 'E{0:d}'.format(layer))]
                H += [cp.Variable((W, n), 'H{0:d}'.format(layer))]

                Pc = (P0)
                # Pc = cp.diag(P0)
                Pn = cp.diag(P[0])
                M = cp.bmat([[E[0] + E[0].T - Pc, H[0].T, C.T],
                             [H[0], Pn, np.zeros((W, p))],
                             [C, np.zeros((p, W)),
                              np.eye(p)]])

            # Last Layer
            elif layer == self.layers + 1:
                # Initialize the forward dynamics
                W_star = np.random.normal(0, init_var / np.sqrt(n), (n, W))

                E += [cp.Variable((W, W), 'E{0:d}'.format(layer))]
                H += [cp.Variable((n, W), 'H{0:d}'.format(layer))]

                # Bad variable names :(
                # Indexing seems off becuase P0 is the first P not P[0]
                Pc = cp.diag(P[layer - 1])
                # Pn = cp.diag(P0)
                Pn = (P0)
                M = cp.bmat([[
                    E[layer] + E[layer].T - Pc,
                    np.zeros((W, m)), H[layer].T
                ], [np.zeros((m, W)), gamma**2 * np.eye(m), E[0].T],
                             [H[layer], E[0], Pn]])

            # Middle layers
            else:
                # Initialize the forward dynamics
                W_star = np.random.normal(0, init_var / np.sqrt(W), (W, W))
                E += [cp.Variable((W, W), 'E{0:d}'.format(layer))]
                H += [cp.Variable((W, W), 'H{0:d}'.format(layer))]

                # Indexing seems off because the first P is actually P0
                Pc = cp.diag(P[layer - 1])
                Pn = cp.diag(P[layer])
                M = cp.bmat([[E[layer] + E[layer].T - Pc, H[layer].T],
                             [H[layer], Pn]])

            q = M.shape[0]
            constraints += [M - (epsilon + solver_res) * np.eye(q) >> 0]
            objective += cp.norm(W_star @ E[layer] - H[layer], "fro")**2

        prob = cp.Problem(cp.Minimize(objective), constraints)

        print("Beginning Initialization l2")
        prob.solve(verbose=False, solver=cp.SCS, max_iters=5000)

        if prob.status in ["infeasible", "unbounded"]:
            print("Unable to solve problem")
        else:
            print('Init Complete - status:' + prob.status)

        # Reassign the values after projecting

        # Must convert from cvxpy -> numpy -> tensor
        for layer in range(self.layers + 2):
            self.E[layer].data = torch.Tensor(E[layer].value)
            self.H[layer].weight.data = torch.Tensor(H[layer].value)

            if layer == 0:
                self.P[layer].data = torch.Tensor(P0.value)
            else:
                self.P[layer].data = torch.Tensor(P[layer - 1].value)
Example #43
0
B = cp.Parameter((n, m))
x0 = cp.Parameter((n))
u0 = cp.Parameter((m))

# Loop on the horizon.
cost = 0
constr = []

for t in range(T):

	# Add the cost.
	cost += cp.sum_squares( (x[:,t+1] + x0 - s0[:,0]) * beta ) + cp.sum_squares( 0.001 * u[:,t])

	# Set the constraint.
	constr += [x[:,t+1] == A * x[:,t] + B[:,0] * u[:,t],
	cp.norm(u[:,t] + u0, "inf") <= 2.0]

# Set the initial constraint.
constr += [ x[:,0] == 0]

# Build the problem.
problem = cp.Problem(cp.Minimize(cost), constr)

# -------------------------------------------- #

def controller(x1=None, u1=None, A=None, B=None, new=True):
	"""
	"""

	# Solve the problem.
	problem.solve(warm_start=True)
Example #44
0
    def get_params(sig, lam=10):
        """
        Estimate the fitted parameters of the Poisson model.
        Code taken from Aaron Rumack, with minor modifications.

        We model

           log(y_t) = alpha_{wd(t)} + phi_t

        where alpha is a vector of fixed effects for each weekday. For
        identifiability, we constrain \sum alpha_j = 0, and to enforce this we set
        Sunday's fixed effect to be the negative sum of the other weekdays.


        We estimate this as a penalized Poisson GLM problem with log link. We
        rewrite the problem as

            log(y_t) = X beta + log(denominator_t)

        and set a design matrix X with one row per time point. The first six columns
        of X are weekday indicators; the remaining columns are the identity matrix,
        so that each time point gets a unique phi. Hence, the first six entries of beta
        correspond to alpha, and the remaining entries to phi.

         The penalty is on the L1 norm of third differences of phi (so the third
        differences of the corresponding columns of beta), to enforce smoothness.
        Third differences ensure smoothness without removing peaks or valleys.

        Return a matrix of parameters: the entire vector of betas, for each time
        series column in the data.

        Args:
            sig: signal to adjust, array
            lam: penalty parameter, scalar

        Returns:
            beta: array of fitted parameters

        """
        # construct design matrix
        X = np.zeros((sig.shape[0], 6 + sig.shape[0]))
        not_sunday = np.where(sig.index.dayofweek != 6)[0]
        X[not_sunday, np.array(sig.index.dayofweek)[not_sunday]] = 1
        X[np.where(sig.index.dayofweek == 6)[0], :6] = -1
        X[:, 6:] = np.eye(X.shape[0])

        npsig = np.array(sig)
        beta = cp.Variable((X.shape[1]))
        lam_var = cp.Parameter(nonneg=True)
        lam_var.value = lam

        ll = ((cp.matmul(npsig, cp.matmul(X, beta)) -
               cp.sum(cp.exp(cp.matmul(X, beta)))
               ) / X.shape[0]
              )
        penalty = (lam_var * cp.norm(cp.diff(beta[6:], 3), 1) / (X.shape[0] - 2)
                   )  # L-1 Norm of third differences, rewards smoothness

        try:
            prob = cp.Problem(cp.Minimize(-ll + lam_var * penalty))
            _ = prob.solve()
        except:
            # If the magnitude of the objective function is too large, an error is
            # thrown; Rescale the objective function
            prob = cp.Problem(cp.Minimize((-ll + lam_var * penalty) / 1e5))
            _ = prob.solve()

        return beta.value
b_p = cvx.Bool(4*num_obst,(precision-1)*(N))
b_p1 = cvx.Bool(4*num_obst,N)
b_p2 = cvx.Bool(4*num_obst,N)
b_qq = cvx.Bool(3)

# Big-M
M = 2000

# Define dynamic constraints

## Initial condition
con = [X[:,0] == np.matrix('0;0;0;0')]
## Dynamics
con.extend([X[:,i+1] == A*X[:,i] + B*U[:,i] for i in range(N)])
## Input constraints
con.extend([cvx.norm(U[:,i],np.inf) <= max_inp for i in range(N)])
## Obstacle avoidance
for obstacle in range(num_obst):
    for k in range(precision):
        weight1=(k)/precision
        weight2=1-(k)/precision
        if k==0:
            #For the state positions
            weight1=1
            weight2=0
            con.extend([obsLHS * (weight1*X[0:2,i]+weight2*X[0:2,i+1]) >= obsRHS_list[obstacle] + buffer*cvx.norm(X[0:2,i+1]-X[0:2,i]) - M*b_p1[4*obstacle:4*(obstacle+1),i] for i in range(1,N)])
            con.extend([obsLHS * (weight1*X[0:2,i]+weight2*X[0:2,i+1]) >= obsRHS_list[obstacle] + buffer*cvx.norm(X[0:2,i]-X[0:2,i-1]) - M*b_p2[4*obstacle:4*(obstacle+1),i] for i in range(1,N)])
            #Initial state
            con.extend([obsLHS * (weight1*X[0:2,0]+weight2*X[0:2,0+1]) >= obsRHS_list[obstacle] + buffer*cvx.norm(X[0:2,0+1]-X[0:2,0]) - M*b_p1[4*obstacle:4*(obstacle+1),0] ])
            #Final state
            con.extend([obsLHS * (weight1*X[0:2,N]+weight2*X[0:2,N-1]) >= obsRHS_list[obstacle] + buffer*cvx.norm(X[0:2,N]-X[0:2,N-1]) - M*b_p2[4*obstacle:4*(obstacle+1),0] ])
Example #46
0
#20161795 고가해(顧家楷)
import cvxpy as cp
import numpy as np

# Generate data.
np.random.seed(1)
A = np.random.randn(10, 20)
b = np.random.randn(10)
x = cp.Variable(20)
constraints = [x >= 0]
# Define and solve the CVXPY problem.
cost = cp.sum_squares(A*x + b)
prob = cp.Problem(cp.Minimize(cost), constraints)
prob.solve()
# Print result.
print("\nThe optimal value is", prob.value)
print("The optimal x is")
print(x.value)
print("The norm of the residual is ", cp.norm(A*x + b, p=2).value)
Example #47
0
import numpy as np
import cvxpy as cp
import pandas as pd

df_X = pd.read_csv('Xsvm.csv',header=None)
df_Y = pd.read_csv('ysvm.csv',header=None)
X = np.array(df_X,dtype=np.float64)
Y = np.array(df_Y,dtype=np.float64)
print(X.shape,Y.shape)

# Convex Optimization
a = cp.Variable(len(Y))
R1 = cp.matmul(cp.diag(a),Y)
R2 = cp.matmul(X.T,R1)
R4 = cp.norm(R2)**2
R4.shape

P1 = cp.sum(a)
Const1 = P1 - 0.5*R4
Const2 = cp.matmul(a.T,Y)
Const3 = [0<=a,Const2 == 0]
obj = cp.Maximize(Const1)
prob = cp.Problem(obj, Const3)
prob.solve(verbose=True)
# print(a.value)

A = (np.array(a.value)).reshape(500,1)
W = np.zeros((2,))
for i in range(len(Y)):
    W += A[i]*Y[i]*(X[i].T)
    if(A[i]>1e-4):
    rad.append(rdf.loc[rdf['ID'] == node]['Radius (mm)'].iloc[0])
    names.append(rdf.loc[rdf['ID'] == node]['Name'].iloc[0])
    

##############################
# Setup optimization problem #
##############################
c = cvx.Variable((n, 3))  # c is [n] sets of 3D coordinates
dists = [[0 for x in range(n)] for y in range(n)]  # Generate 26x26 array to hold variable calculations for distance constraints
constr = []  # Constraints list
for i in range(n):
    for j in range(n):
        if i == j:
            continue
        # Calculate the distance between two spheres
        dists[i][j] = cvx.norm(cvx.vec(c[i, :] - c[j, :]), 2)
        # The distance between 2 spheres (represented as 3D coords) must be greater than the radii of the two spheres combined
        constr.append(dists[i][j] >= rad[i] + rad[j])
        #print("%i, %i: dist= >= %f" % (i, j, r[i] + r[j]))

#################################################################################
# Problem solver - takes a long time!
# prob = cvx.Problem(cvx.Minimize(cvx.sum(cvx.multiply(cvx.bmat(dists), Iij))), constr)  # Minimize the value (dists) * (intensities)... Should be the same minimization as intensities/dist?
# prob.solve(method="dccp", solver="ECOS", ep=1e-2, max_slack=1e-2)
#################################################################################

#########################
# Analyze dat data baby #
#########################

# Reload or save data to file so we don't have to run the optimization every time
Example #49
0
def linear_mpc_control_angela(xref, xbar, x0, dref):
    """
    linear mpc control

    xref: reference point
    xbar: operational point
    x0: initial state
    dref: reference steer angle
    """

    x = cvxpy.Variable((NX, T + 1))
    u = cvxpy.Variable((NU, T))

    cost = 0.0
    constraints = []

    for t in range(T):

        # Terminal state constraint - must be able to track the reference
        # position at the end of the horizon within some margin of error
        # measured by Euclidean (x,y) distance
        DIST_TOL = 2.0
        constraints += [cvxpy.norm(x[0:2, T] - xref[0:2, T]) <= DIST_TOL]
        constraints += [x[2, :] <= MAX_SPEED]
        constraints += [x[2, :] >= MIN_SPEED]
        constraints += [cvxpy.abs(u[0, :]) <= MAX_ACCEL]
        constraints += [cvxpy.abs(u[1, :]) <= MAX_STEER]

        for tk in range(T - t):
            if cvxpy.abs(x[:, t]) >= DIST_TOL:
                cost += cvxpy.quad_form(u[:, t], R)
                print('I am here')
                if t != 0:
                    cost += cvxpy.quad_form(xref[:, t] - x[:, t], Q)

        A, B, C = get_linear_model_matrix(xbar[2, t], xbar[3, t], dref[0, t])
        # NOTE: Should not add noise directly do constraint since cvxpy
        # should solve on the model free dynamics
        # noise = np.random.normal(loc=0.0, scale=0.7, size=(NX))
        constraints += [
            x[:, t + 1] >= A * x[:, t] + B * u[:, t] + C - MAX_NOISE
        ]
        constraints += [
            x[:, t + 1] <= A * x[:, t] + B * u[:, t] + C + MAX_NOISE
        ]
        # constraints += [x[:, t + 1] == A * x[:, t] + B * u[:, t] + C]

        if t < (T - 1):
            cost += cvxpy.quad_form(u[:, t + 1] - u[:, t], Rd)
            constraints += [
                cvxpy.abs(u[1, t + 1] - u[1, t]) <= MAX_DSTEER * DT
            ]

    cost += cvxpy.quad_form(xref[:, T] - x[:, T], Qf)

    constraints += [x[:, 0] == x0]

    prob = cvxpy.Problem(cvxpy.Minimize(cost), constraints)
    prob.solve(solver=cvxpy.ECOS, verbose=False)

    if prob.status == cvxpy.OPTIMAL or prob.status == cvxpy.OPTIMAL_INACCURATE:
        ox = get_nparray_from_matrix(x.value[0, :])
        oy = get_nparray_from_matrix(x.value[1, :])
        ov = get_nparray_from_matrix(x.value[2, :])
        oyaw = get_nparray_from_matrix(x.value[3, :])
        oa = get_nparray_from_matrix(u.value[0, :])
        odelta = get_nparray_from_matrix(u.value[1, :])

    else:
        print("ERROR: Cannot solve MPC! CVXPY failed with status {}".format(
            prob.status))
        date = "-".join([str(datetime.now().month), str(datetime.now().day)])
        fname = "pathTrackPlot_{}".format(date)
        plt.savefig(fname)
        sys.exit(1)
        oa, odelta, ox, oy, oyaw, ov = None, None, None, None, None, None

    return oa, odelta, ox, oy, oyaw, ov
Example #50
0
def stabilize(desiredShot, aspectRatio, noDataFrames, imageSize, fps, lambda1=0.002, lambda2=0.0001, zoomSmooth=1):
    """From a time sequence of unstabilized frame boxes, compute a stabilized frame.

    All parameters are normalized with respect to frame size and time, so that simultaneaously doubling the imageSize and the desiredShot does not change the solution, and neither does using twice as many frames and doubling the fps.

    The main differences with the paper are:
    - only D, L11 and L13 terms are implemented
    - zoomSmooth was added

    If a frame in desiredShot goes outside of the original image, it is cropped.

    Reference: Gandhi Vineet, Ronfard Remi, Gleicher Michael
    Multi-Clip Video Editing from a Single Viewpoint
    European Conference on Visual Media Production (CVMP) 2014
    http://imagine.inrialpes.fr/people/vgandhi/GRG_CVMP_2014.pdf

    Keyword arguments:
    desiredShot -- a n x 4 numpy array containing on each line the box as [xmin, ymin, xmax, ymax]
    lambda1 -- see eq. (10) in paper
    lambda2 -- see eq. (10) in paper
    zoomSmooth -- a factor applied on the terms that deal with frame size in the regularization term: raise if the stabilized frame zooms in and out too much
    aspectRatio -- the desired output aspect ration (e.g. 16/9.)
    noDataFrames -- the list of frames that have no desiredShot information - only regularization is used to stabilize these frames
    imageSize -- [xmin, ymin, xmax, ymax] for the original image (typically [0,0,1920,1080] for HD)
    fps -- number of frames per seconds in the video - used for normalization
    """

    # print "noDataFrames:", noDataFrames

    # set to
    desiredShot[noDataFrames, :] = 0.

    imageHeight = float(imageSize[1])
    imageWidth = float(imageSize[0])

    # crop the desiredShot to the image window
    # we keep a 1-pixel margin to be sure that constraints can be satisfied
    margin = 1
    low_x1_flags = desiredShot[:, 0] < (0. + margin)
    desiredShot[low_x1_flags, 0] = 0. + margin
    low_x2_flags = desiredShot[:, 2] < (0. + margin)
    desiredShot[low_x2_flags, 2] = 0. + margin
    high_x1_flags = desiredShot[:, 0] > (imageWidth - margin)
    desiredShot[high_x1_flags, 0] = imageWidth - margin
    high_x2_flags = desiredShot[:, 2] > (imageWidth - margin)
    desiredShot[high_x2_flags, 2] = imageWidth - margin
    low_y1_flags = desiredShot[:, 1] < (0. + margin)
    desiredShot[low_y1_flags, 1] = 0. + margin
    low_y2_flags = desiredShot[:, 3] < (0. + margin)
    desiredShot[low_y2_flags, 3] = 0. + margin
    high_y1_flags = desiredShot[:, 1] > (imageHeight - margin)
    desiredShot[high_y1_flags, 1] = imageHeight - margin
    high_y2_flags = desiredShot[:, 3] > (imageHeight - margin)
    desiredShot[high_y2_flags, 3] = imageHeight - margin

    # Make sure that a crop of the given aspectRatio can be contained in imageSize and can contain the desiredShot.
    # This may be an issue eg. when doing a 16/9 or a 4/3 movie from 2K.
    # else, we must cut the desiredshot on both sides.
    for k in range(desiredShot.shape[0]):
        if (desiredShot[k, 2] - desiredShot[k, 0]) > (imageHeight * aspectRatio - margin):
            xcut = (desiredShot[k, 2] - desiredShot[k, 0]) - \
                    (imageHeight * aspectRatio - margin)
            desiredShot[k, 2] -= xcut / 2
            desiredShot[k, 0] += xcut / 2
        if (desiredShot[k, 3] - desiredShot[k, 1]) > (imageWidth / aspectRatio - margin):
            ycut = (desiredShot[k, 3] - desiredShot[k, 1]) - \
                    (imageWidth / aspectRatio - margin)
            desiredShot[k, 3] -= ycut / 2
            desiredShot[k, 1] += ycut / 2

    x_center = (desiredShot[:, 0] + desiredShot[:, 2]) / 2.
    y_center = (desiredShot[:, 1] + desiredShot[:, 3]) / 2.
    # elementwise maximum of each array
    half_height_opt = np.maximum((desiredShot[:, 2] - desiredShot[:, 0]) /
                                 aspectRatio, ((desiredShot[:, 3] - desiredShot[:, 1]))) / 2

    # smooth x_center y_center and half_height_opt using a binomial filter (Marchand and Marmet 1983)
    # eg [1 2 1]/4 or [1 4 6 4 1]/16 (obtained by applying it twice)
    # TODO: ignore noDataFrames when smoothing!
    x_center_residual = x_center
    # binomial_3(x_center_residual)
    # binomial_3(x_center_residual)
    y_center_residual = y_center
    # binomial_3(y_center_residual)
    # binomial_3(y_center_residual)
    half_height_opt_residual = half_height_opt
    # binomial_3(half_height_opt_residual)
    # binomial_3(half_height_opt_residual)

    # we subtract 0.001 pixel to be sure that constraints can be satisfied
    half_width = (desiredShot[:, 2] - desiredShot[:, 0]) / 2. - 0.001
    zero_flags = half_width[:] < 0
    half_width[zero_flags] = 0.
    half_height = (desiredShot[:, 3] - desiredShot[:, 1]) / 2. - 0.001
    zero_flags = half_height[:] < 0
    half_height[zero_flags] = 0.

    # now trick the constraints so that there are no inner inclusion constraints at noDataFrames
    x_center[noDataFrames] = imageWidth / 2.
    half_width[noDataFrames] = -imageWidth / 2.  # negative on purpose
    y_center[noDataFrames] = imageHeight / 2.
    half_height[noDataFrames] = -imageHeight / 2.  # negative on purpose
    half_height_opt[noDataFrames] = imageHeight / 2.

    assert ((x_center - half_width) >=
            0).all() and ((x_center + half_width) <= imageWidth).all()
    assert ((y_center - half_height) >=
            0).all() and ((y_center + half_height) <= imageHeight).all()

    n = x_center.size
    print ("n:", n)

    e = np.ones(shape=(n))

    x = cvx.Variable(n)
    y = cvx.Variable(n)
    h = cvx.Variable(n)  # half height (see sec. 4 in the paper)

    # compute the opposite of noDataFrames
    weights = np.ones(n)
    weights[noDataFrames] = 0.  # do not use residuals on the optimal frame where there's no data

    # for f in [97, 98, 99, 100]:
    #    print f, weights[f], x_center[f], y_center[f], half_height_opt[f]

    # normalize with image height
    # version 1:
    weights /= imageHeight
    expr = cvx.sum_squares(weights*(x_center_residual - x)) + cvx.sum_squares(weights*(
        y_center_residual - y)) + cvx.sum_squares((weights/zoomSmooth)*(half_height_opt_residual - h))
    expr /= n  # normalize by the number of images, get a cost per image
    # end of version 1
    # version 2:
    # dataFrames = np.nonzero(weights)
    # expr = cvx.sum_squares(x_center[dataFrames] - x[dataFrames]) + \
    #       cvx.sum_squares(y_center[dataFrames] - y[dataFrames]) + \
    #       cvx.sum_squares(half_height_opt[dataFrames] - h[dataFrames]) / (zoomSmooth*zoomSmooth)
    # expr /= (imageHeight*imageHeight)*n # normalize by the number of images, get a cost per image
    # end of version 2

    if lambda1 != 0.:
        lambda1Factor = lambda1 * fps / imageHeight
        # expr += lambda1Factor * (cvx.norm(D1 * x, 1) + cvx.norm(D1 * y, 1) + cvx.norm(D1 * h, 1) * zoomSmooth)
        if n > 1:
            expr += lambda1Factor * \
                (cvx.tv(x) + cvx.tv(y) + cvx.tv(h) * zoomSmooth)
    if lambda2 != 0.:
        lambda2Factor = lambda2 * fps * fps * fps / imageHeight
        # expr += lambda2Factor * (cvx.norm(D3 * x, 1) + cvx.norm(D3 * y, 1) + cvx.norm(D3 * h, 1) * zoomSmooth)
        if n > 2:
            expr += lambda2Factor * (cvx.norm(x[3:] - 3*x[2:n-1] + 3*x[1:n-2] - x[0:n-3], 1) +
                                     cvx.norm(y[3:] - 3*y[2:n-1] + 3*y[1:n-2] - y[0:n-3], 1) +
                                     cvx.norm(h[3:] - 3*h[2:n-1] + 3*h[1:n-2] - h[0:n-3], 1) * zoomSmooth)
    obj = cvx.Minimize(expr)

    # print expr
    print ("H=%d, W=%d lambda1=%f lambda2=%f zoomSmooth=%f fps=%f imageHeight=%f" % (
        imageHeight, imageWidth, lambda1, lambda2, zoomSmooth, fps, imageHeight))
    # note that the following constraints are tricked (see above) at noDataFrames, using negative values for half_width and half_height
    constraints = [h >= 0,
                   (x - aspectRatio * h) >= 0,
                   (x - aspectRatio * h) <= (x_center - half_width),
                   (x + aspectRatio * h) >= (x_center + half_width),
                   (x + aspectRatio * h) <= imageWidth,
                   (y - h) >= 0,
                   (y - h) <= (y_center - half_height),
                   (y + h) >= (y_center + half_height),
                   (y + h) <= imageHeight]

    prob = cvx.Problem(obj, constraints)
    tryagain = True
    tryreason = ""
    if tryagain or prob.status == cvx.INFEASIBLE or prob.status == cvx.UNBOUNDED:
        tryagain = False
        try:
            # ECOS, the default solver, is much better at solving our problems, especially at handling frames where the actor is not visible
            result = prob.solve(solver=cvx.ECOS, verbose=True)
        except cvx.SolverError as e:
            tryagain = True
            tryreason = str(e)

    if tryagain or prob.status == cvx.INFEASIBLE or prob.status == cvx.UNBOUNDED:
        tryagain = False
        try:
            result = prob.solve(solver=cvx.SCS, verbose=True)
        except cvx.SolverError as e:
            tryagain = True
            tryreason = str(e)

    if tryagain or prob.status == cvx.INFEASIBLE or prob.status == cvx.UNBOUNDED:
        tryagain = False
        try:
            result = prob.solve(solver=cvx.CVXOPT, verbose=True)
        except cvx.SolverError as e:
            tryagain = True
            tryreason = str(e)

    if tryagain or prob.status == cvx.INFEASIBLE or prob.status == cvx.UNBOUNDED:
        tryagain = False
        try:
            result = prob.solve(solver=cvx.CVXOPT, kktsolver=cvx.ROBUST_KKTSOLVER, verbose=True)
        except cvx.SolverError as e:
            tryagain = True
            tryreason = str(e)

    if tryagain:
        raise cvx.solverError(tryreason)
    if prob.status == cvx.INFEASIBLE or prob.status == cvx.UNBOUNDED:
        raise cvx.SolverError('Problem is infeasible or unbounded')

    print ("result=", result, "\n")
    optimised_xcenter = x.value #.reshape(n, 1)
    optimised_ycenter = y.value #.reshape(n, 1)
    optimised_height = h.value #.reshape(n, 1)

    return np.hstack([optimised_xcenter - aspectRatio * optimised_height, optimised_ycenter - optimised_height, optimised_xcenter + aspectRatio * optimised_height, optimised_ycenter + optimised_height])
Example #51
0
def max_objective_1(k, xref, xbar, x0, dref):
    x = cvxpy.Variable((NX, T + 1))
    u = cvxpy.Variable((NU, T))

    cost = 0.0
    constraints = []
    g = 0.0
    for t in range(T):
        cost += cvxpy.quad_form(u[:, t], R)

        if t != 0:
            cost += cvxpy.quad_form(xref[:, t] - x[:, t], Q)

        A, B, C = get_linear_model_matrix(xbar[2, t], xbar[3, t], dref[0, t])
        # NOTE: Should not add noise directly do constraint since cvxpy
        # should solve on the model free dynamics
        # noise = np.random.normal(loc=0.0, scale=0.7, size=(NX))
        constraints += [x[:, t + 1] == A * x[:, t] + B * u[:, t] + C]

        if t < (T - 1):
            cost += cvxpy.quad_form(u[:, t + 1] - u[:, t], Rd)
            constraints += [cvxpy.abs(u[1, t + 1] - u[1, t]) <= \
                            MAX_DSTEER * DT]

    cost += cvxpy.quad_form(xref[:, T] - x[:, T], Qf)

    constraints += [x[:, 0] == x0]
    # Terminal state constraint - must be able to track the reference
    # position at the end of the horizon within some margin of error
    # measured by Euclidean (x,y) distance
    DIST_TOL = 2.0
    constraints += [cvxpy.norm(x[0:2, T] - xref[0:2, T]) <= DIST_TOL]
    constraints += [x[2, :] <= MAX_SPEED]
    constraints += [x[2, :] >= MIN_SPEED]
    constraints += [cvxpy.abs(u[0, :]) <= MAX_ACCEL]
    constraints += [cvxpy.abs(u[1, :]) <= MAX_STEER]

    prob = cvxpy.Problem(cvxpy.Minimize(cost), constraints)
    prob.solve(solver=cvxpy.ECOS, verbose=False)

    if prob.status == cvxpy.OPTIMAL or prob.status == cvxpy.OPTIMAL_INACCURATE:
        ox = get_nparray_from_matrix(x.value[0, :])
        oy = get_nparray_from_matrix(x.value[1, :])
        ov = get_nparray_from_matrix(x.value[2, :])
        oyaw = get_nparray_from_matrix(x.value[3, :])
        oa = get_nparray_from_matrix(u.value[0, :])
        odelta = get_nparray_from_matrix(u.value[1, :])

        ox_all = x.value[:, :]
        ou_all = u.value[:, :]

    else:
        print("ERROR: Cannot solve MPC! CVXPY failed with status {}".format(
            prob.status))
        date = "-".join([str(datetime.now().month), str(datetime.now().day)])
        fname = "pathTrackPlot_{}".format(date)
        print('failed in max_objective')
        # plt.savefig(fname)
        sys.exit(1)
        oa, odelta, ox, oy, oyaw, ov = None, None, None, None, None, None

    for tk in range(k, T):
        print(np.linalg.norm(ox_all[0:2, tk] - xref[0:2, tk]))
        if np.linalg.norm(ox_all[0:2, tk] - xref[0:2, tk]) >= DIST_TOL:
            g = g + np.matmul(np.transpose(
                ox_all[:, tk]), np.matmul(Q, ox_all[:, tk])) + np.matmul(
                    np.transpose(ou_all[:, tk]), np.matmul(R, ou_all[:, tk]))

    return g
Example #52
0
    def main(self):
        # cvx
        w_lasso = cv.Variable((2, 1))
        J = cv.quad_form(w_lasso - self.mu,
                         self.A) + self.lamda * cv.norm(w_lasso, 1)
        objective = cv.Minimize(J)
        constraints = []
        prob = cv.Problem(objective, constraints)
        result = prob.solve(solver=cv.CVXOPT)
        w_lasso = w_lasso.value

        plt.contour(self.W1, self.W2, self.Value)

        wt = self.w_init
        # r smoothのr
        L = 1.0 * np.max(np.linalg.eig(2 * self.A)[0])
        # 更新式におけるeta
        eta = 500 / L
        # 値格納用配列
        w_history = []
        fvalues = []
        g_history = []

        for t in range(100):
            w_history.append(wt.T)
            # 勾配
            grad = 2 * np.dot(self.A, wt - self.mu)
            g_history.append(grad.flatten().tolist())
            # ヘシアン行列の計算
            Ht = np.sqrt(np.sum(np.array(g_history)**2, axis=0).T) + self.delta
            Ht = Ht.reshape(2, 1)
            # etaの更新
            eta_t = eta
            # 重みの更新式
            wth = wt - eta_t * (Ht**-1 * grad)
            Ht_inv = Ht**-1
            wt = np.array([
                self.proximal_operation(wth[0],
                                        self.lamda * eta_t * Ht_inv[0]),
                self.proximal_operation(wth[1], self.lamda * eta_t * Ht_inv[1])
            ])
            # 評価関数値の計算
            J = np.dot(
                np.dot((wt - self.mu).T, self.A),
                (wt - self.mu)) + self.lamda * (np.abs(wt[0]) + np.abs(wt[1]))
            fvalues.append(J)

        fvalues = np.vstack(fvalues)
        w_history = np.vstack(w_history)

        minfvalue = np.dot(
            np.dot((w_lasso - self.mu).T, self.A),
            (w_lasso - self.mu)) + self.lamda * np.sum(np.abs(w_lasso))
        minOfMin = np.min([minfvalue, np.min(fvalues)])
        # Drow Graph
        plt.figure(1)
        plt.plot(w_history[:, 0],
                 w_history[:, 1],
                 'ro-',
                 markersize=3,
                 linewidth=0.5)
        plt.plot(w_lasso[0], w_lasso[1], 'ko')
        plt.xlim(-1.5, 3)
        plt.ylim(-1.5, 3)
        plt.xlabel("w1")
        plt.ylabel("w2")
        plt.grid()

        plt.figure(2)
        plt.semilogy(fvalues - minOfMin, 'bs-', markersize=1, linewidth=0.5)
        plt.xlabel("iteration")
        plt.ylabel("J(w_t) - J(w_hat)")
        plt.grid()
Example #53
0
 Ik = model.Iks / 255
 # pp = np.linalg.inv(A.transpose()@A)@A.transpose()@Ik
 p1 = cp.Variable(numberOfLambs)
 objective = cp.Minimize(cp.sum_squares(A @ p1 - Ik))
 constraints = [0 <= p1, p1 <= 2]
 prob = cp.Problem(objective, constraints)
 var_s1 = prob.solve()
 """
 方法2
     线性规划
 """
 A = model.A_
 Ik = model.Iks / 255
 # pp = np.linalg.inv(A.transpose()@A)@A.transpose()@Ik
 p2 = cp.Variable(numberOfLambs)
 objective = cp.Minimize(cp.norm(A @ p2 - Ik))
 constraints = [0 <= p2, p2 <= 2]
 prob = cp.Problem(objective, constraints)
 var_s2 = prob.solve()
 """
     方法3
        Chebyshev APPROXIMATION
     """
 A = model.A_
 Ik = model.Iks / 255
 # pp = np.linalg.inv(A.transpose()@A)@A.transpose()@Ik
 p3 = cp.Variable(numberOfLambs)
 objective = cp.Minimize(cp.max(A @ p3 - Ik))
 constraints = [0 <= p3, p3 <= 2]
 prob = cp.Problem(objective, constraints)
 var_s3 = prob.solve()
Example #54
0
    def test_pnorm(self):
        atom = cp.pnorm(self.x, p=1.5)
        self.assertEqual(atom.shape, tuple())
        self.assertEqual(atom.curvature, s.CONVEX)
        self.assertEqual(atom.sign, s.NONNEG)

        atom = cp.pnorm(self.x, p=1)
        self.assertEqual(atom.shape, tuple())
        self.assertEqual(atom.curvature, s.CONVEX)
        self.assertEqual(atom.sign, s.NONNEG)

        atom = cp.pnorm(self.x, p=2)
        self.assertEqual(atom.shape, tuple())
        self.assertEqual(atom.curvature, s.CONVEX)
        self.assertEqual(atom.sign, s.NONNEG)
        expr = cp.norm(self.A, 2, axis=0)
        self.assertEqual(expr.shape, (2, ))

        atom = cp.pnorm(self.x, p='inf')
        self.assertEqual(atom.shape, tuple())
        self.assertEqual(atom.curvature, s.CONVEX)
        self.assertEqual(atom.sign, s.NONNEG)

        atom = cp.pnorm(self.x, p='Inf')
        self.assertEqual(atom.shape, tuple())
        self.assertEqual(atom.curvature, s.CONVEX)
        self.assertEqual(atom.sign, s.NONNEG)

        atom = cp.pnorm(self.x, p=np.inf)
        self.assertEqual(atom.shape, tuple())
        self.assertEqual(atom.curvature, s.CONVEX)
        self.assertEqual(atom.sign, s.NONNEG)

        atom = cp.pnorm(self.x, p=.5)
        self.assertEqual(atom.shape, tuple())
        self.assertEqual(atom.curvature, s.CONCAVE)
        self.assertEqual(atom.sign, s.NONNEG)

        atom = cp.pnorm(self.x, p=.7)
        self.assertEqual(atom.shape, tuple())
        self.assertEqual(atom.curvature, s.CONCAVE)
        self.assertEqual(atom.sign, s.NONNEG)

        atom = cp.pnorm(self.x, p=-.1)
        self.assertEqual(atom.shape, tuple())
        self.assertEqual(atom.curvature, s.CONCAVE)
        self.assertEqual(atom.sign, s.NONNEG)

        atom = cp.pnorm(self.x, p=-1)
        self.assertEqual(atom.shape, tuple())
        self.assertEqual(atom.curvature, s.CONCAVE)
        self.assertEqual(atom.sign, s.NONNEG)

        atom = cp.pnorm(self.x, p=-1.3)
        self.assertEqual(atom.shape, tuple())
        self.assertEqual(atom.curvature, s.CONCAVE)
        self.assertEqual(atom.sign, s.NONNEG)

        # Test copy with args=None
        copy = atom.copy()
        self.assertTrue(type(copy) is type(atom))
        # A new object is constructed, so copy.args == atom.args but copy.args
        # is not atom.args.
        self.assertEqual(copy.args, atom.args)
        self.assertFalse(copy.args is atom.args)
        self.assertEqual(copy.get_data(), atom.get_data())
        # Test copy with new args
        copy = atom.copy(args=[self.y])
        self.assertTrue(type(copy) is type(atom))
        self.assertTrue(copy.args[0] is self.y)
        self.assertEqual(copy.get_data(), atom.get_data())
Example #55
0
    def test_admm(self):
        """Test ADMM algorithm.
        """
        X = px.Variable((10, 5))
        B = np.reshape(np.arange(50), (10, 5)) * 1.
        prox_fns = [px.sum_squares(X, b=B)]
        sltn = admm.solve(prox_fns, [], 1.0, eps_abs=1e-4, eps_rel=1e-4)
        self.assertItemsAlmostEqual(X.value, B, places=2)
        self.assertAlmostEqual(sltn, 0)

        prox_fns = [px.norm1(X, b=B, beta=2)]
        sltn = admm.solve(prox_fns, [], 1.0)
        self.assertItemsAlmostEqual(X.value, B / 2., places=2)
        self.assertAlmostEqual(sltn, 0)

        prox_fns = [px.norm1(X), px.sum_squares(X, b=B)]
        sltn = admm.solve(prox_fns, [], 1.0, eps_rel=1e-5, eps_abs=1e-5)

        cvx_X = cvx.Variable(10, 5)
        cost = cvx.sum_squares(cvx_X - B) + cvx.norm(cvx_X, 1)
        prob = cvx.Problem(cvx.Minimize(cost))
        prob.solve()
        self.assertItemsAlmostEqual(X.value, cvx_X.value, places=2)
        self.assertAlmostEqual(sltn, prob.value)

        psi_fns, omega_fns = admm.partition(prox_fns)
        sltn = admm.solve(psi_fns, omega_fns, 1.0, eps_rel=1e-5, eps_abs=1e-5)
        self.assertItemsAlmostEqual(X.value, cvx_X.value, places=2)
        self.assertAlmostEqual(sltn, prob.value)

        prox_fns = [px.norm1(X)]
        quad_funcs = [px.sum_squares(X, b=B)]
        sltn = admm.solve(prox_fns,
                          quad_funcs,
                          1.0,
                          eps_rel=1e-5,
                          eps_abs=1e-5)
        self.assertItemsAlmostEqual(X.value, cvx_X.value, places=2)
        self.assertAlmostEqual(sltn, prob.value)

        # With parameters for px.sum_squares
        prox_fns = [px.norm1(X)]
        quad_funcs = [px.sum_squares(X, b=B, alpha=0.1, beta=2., gamma=1, c=B)]
        sltn = admm.solve(prox_fns,
                          quad_funcs,
                          1.0,
                          eps_rel=1e-5,
                          eps_abs=1e-5)

        cvx_X = cvx.Variable(10, 5)
        cost = 0.1 * cvx.sum_squares(2 * cvx_X - B) + cvx.sum_squares(cvx_X) + \
            cvx.norm(cvx_X, 1) + cvx.trace(B.T * cvx_X)
        prob = cvx.Problem(cvx.Minimize(cost))
        prob.solve()
        self.assertItemsAlmostEqual(X.value, cvx_X.value, places=2)
        self.assertAlmostEqual(sltn, prob.value, places=3)

        prox_fns = [px.norm1(X)]
        quad_funcs = [px.sum_squares(X - B, alpha=0.1, beta=2., gamma=1, c=B)]
        quad_funcs[0] = absorb_offset(quad_funcs[0])
        sltn = admm.solve(prox_fns,
                          quad_funcs,
                          1.0,
                          eps_rel=1e-5,
                          eps_abs=1e-5)
        self.assertItemsAlmostEqual(X.value, cvx_X.value, places=2)
        self.assertAlmostEqual(sltn, prob.value, places=3)

        prox_fns = [px.norm1(X)]
        cvx_X = cvx.Variable(10, 5)
        # With linear operators.
        kernel = np.array([1, 2, 3])
        x = px.Variable(3)
        b = np.array([-41, 413, 2])
        prox_fns = [px.nonneg(x), px.sum_squares(px.conv(kernel, x), b=b)]
        sltn = admm.solve(prox_fns, [], 1.0, eps_abs=1e-5, eps_rel=1e-5)

        kernel_mat = np.matrix("2 1 3; 3 2 1; 1 3 2")
        cvx_X = cvx.Variable(3)
        cost = cvx.norm(kernel_mat * cvx_X - b)
        prob = cvx.Problem(cvx.Minimize(cost), [cvx_X >= 0])
        prob.solve()
        self.assertItemsAlmostEqual(x.value, cvx_X.value, places=2)
        self.assertAlmostEqual(np.sqrt(sltn), prob.value, places=2)

        prox_fns = [px.nonneg(x)]
        quad_funcs = [px.sum_squares(px.conv(kernel, x), b=b)]
        sltn = admm.solve(prox_fns,
                          quad_funcs,
                          1.0,
                          eps_abs=1e-5,
                          eps_rel=1e-5)
        self.assertItemsAlmostEqual(x.value, cvx_X.value, places=2)
        self.assertAlmostEqual(np.sqrt(sltn), prob.value, places=2)
Example #56
0
def stabilize_chunk(desiredShot, aspectRatio, noDataFrames, imageSize, fps, crop_factor, apparent_motion, external_boundaries, screen_pos, lambda1=0.002, lambda2=0.0001, zoomSmooth=1.5, lambda3=0.005):
    """From a time sequence of unstabilized frame boxes, compute a stabilized frame.

    All parameters are normalized with respect to frame size and time, so that simultaneaously doubling the imageSize and the desiredShot does not change the solution, and neither does using twice as many frames and doubling the fps.

    The main differences with the paper are:
    - only D, L11 and L13 terms are implemented
    - zoomSmooth was added
    - the shots are optimized over chunks of shot_s*2 = 10s, and the five first seconds are kept. This makes the problem a lot more tractable.

    If a frame in desiredShot goes outside of the original image, it is cropped.

    Reference: Gandhi Vineet, Ronfard Remi, Gleicher Michael
    Multi-Clip Video Editing from a Single Viewpoint
    European Conference on Visual Media Production (CVMP) 2014
    http://imagine.inrialpes.fr/people/vgandhi/GRG_CVMP_2014.pdf

    Keyword arguments:
    desiredShot -- a n x 4 numpy array containing on each line the box as [xmin, ymin, xmax, ymax]
    lambda1 -- see eq. (10) in paper
    lambda2 -- see eq. (10) in paper
    zoomSmooth -- a factor applied on the terms that deal with frame size in the regularization term: raise if the stabilized frame zooms in and out too much
    aspectRatio -- the desired output aspect ration (e.g. 16/9.)
    noDataFrames -- the list of frames that have no desiredShot information - only regularization is used to stabilize these frames
    imageSize -- [xmin, ymin, xmax, ymax] for the original image (typically [0,0,1920,1080] for HD)
    fps -- number of frames per seconds in the video - used for normalization
    """

    #print "noDataFrames:", noDataFrames
    # print(noDataFrames, desiredShot)
    # set to
    desiredShot[noDataFrames, :] = 0.

    imageHeight = float(imageSize[1])
    imageWidth = float(imageSize[0])

    len_w = imageWidth/2
    len_h = imageHeight/2
    if imageHeight* aspectRatio < imageWidth:
        len_w = round((imageHeight*aspectRatio)/2)
    elif imageWidth / aspectRatio < imageHeight:
        len_h = round((imageWidth / aspectRatio)/2)



    # crop the desiredShot to the image window
    # we keep a 1-pixel margin to be sure that constraints can be satisfied
    margin = 1
    low_x1_flags = desiredShot[:, 0] < (0. + margin)
    desiredShot[low_x1_flags, 0] = 0. + margin
    low_x2_flags = desiredShot[:, 2] < (0. + margin)
    desiredShot[low_x2_flags, 2] = 0. + margin
    high_x1_flags = desiredShot[:, 0] > (imageWidth - margin)
    desiredShot[high_x1_flags, 0] = imageWidth - margin
    high_x2_flags = desiredShot[:, 2] > (imageWidth - margin)
    desiredShot[high_x2_flags, 2] = imageWidth - margin
    low_y1_flags = desiredShot[:, 1] < (0. + margin)
    desiredShot[low_y1_flags, 1] = 0. + margin
    low_y2_flags = desiredShot[:, 3] < (0. + margin)
    desiredShot[low_y2_flags, 3] = 0. + margin
    high_y1_flags = desiredShot[:, 1] > (imageHeight - margin)
    desiredShot[high_y1_flags, 1] = imageHeight - margin
    high_y2_flags = desiredShot[:, 3] > (imageHeight - margin)
    desiredShot[high_y2_flags, 3] = imageHeight - margin

    # Make sure that a crop of the given aspectRatio can be contained in imageSize and can contain the desiredShot.
    # This may be an issue eg. when doing a 16/9 or a 4/3 movie from 2K.
    # else, we must cut the desiredshot on both sides.
    for k in range(desiredShot.shape[0]):
        if (desiredShot[k, 2] - desiredShot[k, 0]) > (imageHeight * aspectRatio - margin):
            xcut = (desiredShot[k, 2] - desiredShot[k, 0]) - \
                (imageHeight * aspectRatio - margin)
            desiredShot[k, 2] -= xcut / 2
            desiredShot[k, 0] += xcut / 2
        if (desiredShot[k, 3] - desiredShot[k, 1]) > (imageWidth / aspectRatio - margin):
            ycut = (desiredShot[k, 3] - desiredShot[k, 1]) - \
                (imageWidth / aspectRatio - margin)
            desiredShot[k, 3] -= ycut / 2
            desiredShot[k, 1] += ycut / 2

    # print("desiredShot:", desiredShot)
    # print("noDataFrames:", noDataFrames)

    x_center = (desiredShot[:, 0] + desiredShot[:, 2]) / 2.
    y_center = (desiredShot[:, 1] + desiredShot[:, 3]) / 2.
    # elementwise maximum of each array
    half_height_opt = np.maximum((desiredShot[:, 2] - desiredShot[:, 0]) /
                                 aspectRatio, ((desiredShot[:, 3] - desiredShot[:, 1]))) / 2

    # smooth x_center y_center and half_height_opt using a binomial filter (Marchand and Marmet 1983)
    # eg [1 2 1]/4 or [1 4 6 4 1]/16 (obtained by applying it twice)
    # TODO: ignore noDataFrames when smoothing!
    x_center_residual = x_center
    # binomial_3(x_center_residual)
    # binomial_3(x_center_residual)
    y_center_residual = y_center
    # binomial_3(y_center_residual)
    # binomial_3(y_center_residual)
    half_height_opt_residual = half_height_opt
    # binomial_3(half_height_opt_residual)
    # binomial_3(half_height_opt_residual)

    half_width = (desiredShot[:, 2] - desiredShot[:, 0]) / 2.
    zero_flags = half_width[:] < 0
    half_width[zero_flags] = 0.
    half_height = (desiredShot[:, 3] - desiredShot[:, 1]) / 2.
    zero_flags = half_height[:] < 0
    half_height[zero_flags] = 0.

    # now trick the constraints so that there are no inner inclusion constraints at noDataFrames
    x_center[noDataFrames] = imageWidth / 2.
    half_width[noDataFrames] = -imageWidth / 2.  # negative on purpose
    y_center[noDataFrames] = imageHeight / 2.
    half_height[noDataFrames] = -imageHeight / 2.  # negative on purpose
    half_height_opt[noDataFrames] = imageHeight / 2.

    # print(half_height[noDataFrames], noDataFrames, imageHeight)
    # print(np.isnan(half_height))

    # for i in range(len(desiredShot)):
    #     if (y_center[i] - half_height[i]) < 0 or (y_center[i] + half_height[i]) > imageHeight:
    #         print('indice ',i)

    external_boundaries[noDataFrames] = [0, 0, 0, 0, 0, 0]
    l_tl = []
    for t in external_boundaries[:, 4]:
        if t == 1:
            l_tl.append(0.)
        else:
            l_tl.append(1.)
    tl_inv = np.array(l_tl)
    l_tr = []
    for t in external_boundaries[:, 5]:
        if t == 1:
            l_tr.append(0.)
        else:
            l_tr.append(1.)
    tr_inv = np.array(l_tr)

    tl_inv[noDataFrames] = 0
    tr_inv[noDataFrames] = 0

    assert ((x_center - half_width) >=
            0).all() and ((x_center + half_width) <= imageWidth).all()
    assert ((y_center - half_height) >=
            0).all() and ((y_center + half_height) <= imageHeight).all()

    n = x_center.size
    #print "n:", n

    # We split the problem into chunks of fixed duration.
    # We compute a subsolution for the current chunk and the next chunk, and ensure continuity for the
    # variation (1st derivative) and jerk (3rd derivative) terms.
    # Then we only keep the solution for the current chunk and advance.

    # compute the opposite of noDataFrames
    # normalize with image height
    weightsAll = np.ones(n) / imageHeight
    weightsAll[noDataFrames] = 0.  # do not use residuals on the optimal frame where there's no data
    #print "weightsAll:", weightsAll
    #print "half_height_opt:", half_height_opt

    optimised_xcenter = np.zeros(n)
    optimised_ycenter = np.zeros(n)
    optimised_height = np.zeros(n)

    chunk_s = 5  # size of a chunk in seconds
    chunk_n = int(chunk_s * fps)  # number of samples in a chunk
    full_chunk_n = chunk_n * 2  # number of samples in a subproblem
    # starting index for the chunk (also used to check if it is the first chunk)
    chunk_start = 0

    while chunk_start < n:
        chunk_end = min(n, chunk_start + chunk_n)
        chunk_size = chunk_end - chunk_start
        full_chunk_end = min(n, chunk_start + full_chunk_n)
        full_chunk_size = full_chunk_end - chunk_start
        # print("chunk:", chunk_start, chunk_end, full_chunk_end)

        x = cvx.Variable(full_chunk_size)
        y = cvx.Variable(full_chunk_size)
        # half height (see sec. 4 in the paper)
        h = cvx.Variable(full_chunk_size)

        weights = weightsAll[chunk_start:full_chunk_end]
        x_center_chunk = x_center[chunk_start:full_chunk_end]
        y_center_chunk = y_center[chunk_start:full_chunk_end]
        half_height_chunk = half_height[chunk_start:full_chunk_end]
        half_width_chunk = half_width[chunk_start:full_chunk_end]
        x_center_residual_chunk = x_center[chunk_start:full_chunk_end]
        y_center_residual_chunk = y_center[chunk_start:full_chunk_end]
        half_height_opt_residual_chunk = half_height_opt_residual[chunk_start:full_chunk_end]

        #Vector screen pos
        h_vector = screen_pos[chunk_start:full_chunk_end]

        #M1 term see paper 4.5
        c_x_factor = crop_factor[:, chunk_start:full_chunk_end-1, 0]
        c_y_factor = crop_factor[:, chunk_start:full_chunk_end-1, 1]
        c_h_factor = crop_factor[:, chunk_start:full_chunk_end-1, 2]

        #M2 term see paper 4.5
        b_x_factor = apparent_motion[:, chunk_start:full_chunk_end-1, 0]
        b_y_factor = apparent_motion[:, chunk_start:full_chunk_end-1, 1]
        b_h_factor = apparent_motion[:, chunk_start:full_chunk_end-1, 2]

        #E term [xl, xl1, xr, xr1, tl, tr]
        tl = external_boundaries[chunk_start:full_chunk_end, 4]
        tr = external_boundaries[chunk_start:full_chunk_end, 5]
        inv_tl = tl_inv[chunk_start:full_chunk_end]
        inv_tr = tr_inv[chunk_start:full_chunk_end]
        xl = external_boundaries[chunk_start:full_chunk_end, 0]
        xl1 = external_boundaries[chunk_start:full_chunk_end, 1]
        xr = external_boundaries[chunk_start:full_chunk_end, 2]
        xr1 = external_boundaries[chunk_start:full_chunk_end, 3]


        #assert ((x_center_chunk - half_width_chunk) >= 0).all() and ((x_center_chunk + half_width_chunk) <= imageWidth).all()
        #assert ((y_center_chunk - half_height_chunk) >= 0).all() and ((y_center_chunk + half_height_chunk) <= imageHeight).all()

        # for f in [97, 98, 99, 100]:
        #    print f, weights[f], x_center[f], y_center[f], half_height_opt[f]
        expr = cvx.sum_squares(weights*(x_center_residual_chunk + (0.17*aspectRatio*half_height_opt_residual_chunk*h_vector) - x)) + cvx.sum_squares(weights*(
            y_center_residual_chunk - y)) + cvx.sum_squares(weights*(half_height_opt_residual_chunk - h))
        expr /= n  # normalize by the number of images, get a cost per image
        # end of version 1
        # version 2:
        #dataFrames = np.nonzero(weights)
        # expr = cvx.sum_squares(x_center[dataFrames] - x[dataFrames]) + \
        #       cvx.sum_squares(y_center[dataFrames] - y[dataFrames]) + \
        #       cvx.sum_squares(half_height_opt[dataFrames] - h[dataFrames]) / (zoomSmooth*zoomSmooth)
        # expr /= (imageHeight*imageHeight)*n # normalize by the number of images, get a cost per image
        # end of version 2

        if lambda1 != 0.:
            lambda1Factor = lambda1 * fps / imageHeight
            # print("lambda 1 ",lambda1Factor)
            if n > 1:
                expr += lambda1Factor * \
                    (cvx.tv(x) + cvx.tv(y) + cvx.tv(h) * zoomSmooth)

            # if not the first chunk, add continuity with previous samples
            if chunk_start >= 1:
                expr += lambda1Factor * (cvx.abs(x[0] - optimised_xcenter[chunk_start - 1]) +
                                         cvx.abs(y[0] - optimised_ycenter[chunk_start - 1]) +
                                         cvx.abs(h[0] - optimised_height[chunk_start - 1]) * zoomSmooth)

        if lambda2 != 0.:
            lambda2Factor = lambda2 * fps * fps * fps / imageHeight
            # print("lambda 2 ",lambda2Factor)

            if n > 2:
                expr += lambda2Factor * (cvx.norm(x[3:] - 3*x[2:full_chunk_size-1] + 3*x[1:full_chunk_size-2] - x[0:full_chunk_size-3], 1) +
                                         cvx.norm(y[3:] - 3*y[2:full_chunk_size-1] + 3*y[1:full_chunk_size-2] - y[0:full_chunk_size-3], 1) +
                                         cvx.norm(h[3:] - 3*h[2:full_chunk_size-1] + 3*h[1:full_chunk_size-2] - h[0:full_chunk_size-3], 1) * zoomSmooth)
            # if not the first chunk, add continuity with previous samples
            if chunk_start >= 3 and chunk_size >= 3:
                expr += lambda2Factor * ((cvx.abs(x[0] - 3 * optimised_xcenter[chunk_start - 1] + 3 * optimised_xcenter[chunk_start - 2] - optimised_xcenter[chunk_start - 3]) +
                                          cvx.abs(x[1] - 3 * x[0] + 3 * optimised_xcenter[chunk_start - 1] - optimised_xcenter[chunk_start - 2]) +
                                          cvx.abs(x[2] - 3 * x[1] + 3 * x[0] - optimised_xcenter[chunk_start - 1])) +
                                         (cvx.abs(y[0] - 3 * optimised_ycenter[chunk_start - 1] + 3 * optimised_ycenter[chunk_start - 2] - optimised_ycenter[chunk_start - 3]) +
                                          cvx.abs(y[1] - 3 * y[0] + 3 * optimised_ycenter[chunk_start - 1] - optimised_ycenter[chunk_start - 2]) +
                                          cvx.abs(y[2] - 3 * y[1] + 3 * y[0] - optimised_ycenter[chunk_start - 1])) +
                                         (cvx.abs(h[0] - 3 * optimised_height[chunk_start - 1] + 3 * optimised_height[chunk_start - 2] - optimised_height[chunk_start - 3]) +
                                          cvx.abs(h[1] - 3 * h[0] + 3 * optimised_height[chunk_start - 1] - optimised_height[chunk_start - 2]) +
                                          cvx.abs(h[2] - 3 * h[1] + 3 * h[0] - optimised_height[chunk_start - 1])) * zoomSmooth)

        if lambda3 != 0.:
            lambda3Factor = lambda3 * fps / imageHeight
            lambda2Factor = lambda2 * fps * fps * fps / imageHeight
            lambdaM = 5
            if n > 1:
                m1_term = 0
                for c_x, c_y, c_h in zip(c_x_factor, c_y_factor, c_h_factor):
                    m1_term +=  lambdaM * (cvx.norm(c_x*(x[1:]-x[0:full_chunk_size-1]), 1) +
                                             cvx.norm(c_y*(y[1:]-y[0:full_chunk_size-1]), 1) +
                                             cvx.norm(c_h*(h[1:]-h[0:full_chunk_size-1]), 1) * zoomSmooth)
            if chunk_start >= 1:
                for c in crop_factor:
                    c_x = c[chunk_start-1, 0]
                    c_y = c[chunk_start-1, 1]
                    c_h = c[chunk_start-1, 2]
                    m1_term +=  lambdaM * (cvx.norm(c_x*(x[0]-optimised_xcenter[chunk_start-1]), 1) +
                                             cvx.norm(c_y*(y[0]-optimised_ycenter[chunk_start-1]), 1) +
                                             cvx.norm(c_h*(h[0]-optimised_height[chunk_start-1]), 1) * zoomSmooth)
            if n > 1:
                m2_term = 0
                for b_x, b_y, b_h_g in zip(b_x_factor, b_y_factor, b_h_factor):
                    b_h = gaussian_filter(b_h_g,sigma=5)
                    # print(b_x, b_x.dtype)
                    m2_term +=  lambdaM * (cvx.neg((b_x-(x[1:]-x[0:full_chunk_size-1]))*b_x) +
                                            cvx.neg((b_y-(y[1:]-y[0:full_chunk_size-1]))*b_y) +
                                            cvx.neg((b_h-(h[1:]-h[0:full_chunk_size-1]))*b_h) * zoomSmooth)
            if chunk_start >=1 :
                for b in apparent_motion:
                    b_x = b[chunk_start-1, 0]
                    b_y = b[chunk_start-1, 0]
                    b_h = b[chunk_start-1, 0]
                    m2_term +=  lambdaM * (cvx.neg((b_x-(x[0]-optimised_xcenter[chunk_start-1]))*b_x) +
                                            cvx.neg((b_y-(y[0]-optimised_ycenter[chunk_start-1]))*b_y) +
                                            cvx.neg((b_h-(h[0]-optimised_height[chunk_start-1]))*b_h) * zoomSmooth)
            # expr += m1_term + m2_term

            # if n > 1:
            #     # E out
            #     expr += lambda3Factor * ((inv_tl * cvx.pos(xl1 - x + aspectRatio * h)) +
            #                             (inv_tr * cvx.pos(x + aspectRatio * h - xr1)) )
            #
            #     # E in
            #     expr += lambda3Factor * ((tl * cvx.pos(x - aspectRatio * h - xl)) +
            #                             (tr * cvx.pos(xr - x - aspectRatio * h)) )

        obj = cvx.Minimize(expr)

        #print expr
        # print("H=%d, W=%d lambda1=%f lambda2=%f zoomSmooth=%f fps=%f imageHeight=%f" % (
        #     imageHeight, imageWidth, lambda1, lambda2, zoomSmooth, fps, imageHeight))
        # note that the following constraints are tricked (see above) at noDataFrames, using negative values for half_width and half_height
        constraints = [h >= 0,
                       (x - aspectRatio * h) >= 0,
                       (x - aspectRatio * h) <= (x_center_chunk - half_width_chunk),
                       (x + aspectRatio * h) >= (x_center_chunk + half_width_chunk),
                       (x + aspectRatio * h) <= imageWidth,
                       aspectRatio * h <= len_w,
                       (y - h) >= 0,
                       (y - h) <= (y_center_chunk - half_height_chunk),
                       (y + h) >= (y_center_chunk + half_height_chunk),
                       (y + h) <= imageHeight,
                       h <= len_h]

        prob = cvx.Problem(obj, constraints)
        tryagain = True
        tryreason = ""
        if tryagain or prob.status == cvx.INFEASIBLE or prob.status == cvx.UNBOUNDED:
            tryagain = False
            try:
                # ECOS, the default solver, is much better at solving our problems, especially at handling frames where the actor is not visible
                # all tolerances are multiplied by 10
                result = prob.solve(solver=cvx.ECOS, verbose=False, abstol = 1e-6, reltol = 1e-5, abstol_inacc = 5e-4, reltol_inacc = 5e-4, feastol_inacc = 1e-3)
            except cvx.SolverError as e:
                tryagain = True
                tryreason = str(e)

        if tryagain or prob.status == cvx.INFEASIBLE or prob.status == cvx.UNBOUNDED:
            tryagain = False
            try:
                result = prob.solve(solver=cvx.SCS, verbose=False, max_iters = 2500, eps = 1e-2)
            except cvx.SolverError as e:
                tryagain = True
                tryreason = str(e)

        if tryagain or prob.status == cvx.INFEASIBLE or prob.status == cvx.UNBOUNDED:
            tryagain = False
            try:
                result = prob.solve(solver=cvx.CVXOPT, verbose=False, abstol = 1e-6, reltol = 1e-5, feastol = 1e-6)
            except cvx.SolverError as e:
                tryagain = True
                tryreason = str(e)

        if tryagain or prob.status == cvx.INFEASIBLE or prob.status == cvx.UNBOUNDED:
            tryagain = False
            try:
                result = prob.solve(
                    solver=cvx.CVXOPT, kktsolver=cvx.ROBUST_KKTSOLVER, verbose=False, abstol = 1e-6, reltol = 1e-5, feastol = 1e-6)
            except cvx.SolverError as e:
                tryagain = True
                tryreason = str(e)

        if tryagain:
            raise cvx.solverError(tryreason)
        if prob.status == cvx.INFEASIBLE or prob.status == cvx.UNBOUNDED:
            raise cvx.SolverError('Problem is infeasible or unbounded')

        #raise ValueError('Yeah!')
        # print("result=", result, "\n")
        if full_chunk_end >= n:
            # last chunk - get the full chunk
            optimised_xcenter[chunk_start:full_chunk_end] = x.value.reshape(
                full_chunk_size)
            optimised_ycenter[chunk_start:full_chunk_end] = y.value.reshape(
                full_chunk_size)
            optimised_height[chunk_start:full_chunk_end] = h.value.reshape(
                full_chunk_size)
            chunk_start = full_chunk_end
        else:
            # only get the chunk and advance
            optimised_xcenter[chunk_start:chunk_end] = x.value[:chunk_size].reshape(
                chunk_size)
            optimised_ycenter[chunk_start:chunk_end] = y.value[:chunk_size].reshape(
                chunk_size)
            optimised_height[chunk_start:chunk_end] = h.value[:chunk_size].reshape(
                chunk_size)
            chunk_start = chunk_end
    return np.vstack([optimised_xcenter - aspectRatio * optimised_height, optimised_ycenter - optimised_height, optimised_xcenter + aspectRatio * optimised_height, optimised_ycenter + optimised_height]).transpose()
Example #57
0
    def init_l2(self, mu=0.05, epsilon=1E-2, init_var=1.5, custom_seed=None):
        n = self.nx
        constraints = []
        objective = 0.0

        if custom_seed is not None:
            np.random.seed(custom_seed)

        P = cp.Variable((self.layers, n), 'P')
        E = []
        H = []

        M = []
        for layer in range(self.layers):

            Id1 = np.eye(n)
            Id2 = np.eye(2 * n)

            # Initialize the forward dynamics
            W_star = np.random.normal(0, init_var / np.sqrt(n), (n, n))

            E += [cp.Variable((n, n), 'E{0:d}'.format(layer))]
            H += [cp.Variable((n, n), 'H{0:d}'.format(layer))]

            # Check to see if it is the last layer. If so, loop condition
            if layer == self.layers - 1:
                Pc = cp.diag(P[layer])
                Pn = cp.diag(P[0])
            else:
                Pc = cp.diag(P[layer])
                Pn = cp.diag(P[layer + 1])

            M += [
                cp.bmat([[E[layer] + E[layer].T - Pc - mu * Id1, H[layer].T],
                         [H[layer], Pn]])
            ]

            constraints += [M[layer] >> epsilon * Id2]
            objective += cp.norm(W_star @ E[layer] - H[layer], "fro")**2

        prob = cp.Problem(cp.Minimize(objective), constraints)

        print("Beginning Initialization l2")
        prob.solve(verbose=False, solver=cp.SCS)

        if prob.status in ["infeasible", "unbounded"]:
            print("Unable to solve problem")

        print('Init Complete - status:', prob.status)

        # Reassign the values after projecting

        # Must convert from cvxpy -> numpy -> tensor
        E_np = np.stack(list(map(lambda M: M.value, E)), 0)
        P_np = np.stack(list(map(lambda M: M.value, P)), 0)

        self.E.data = torch.Tensor(E_np)
        self.P.data = torch.Tensor(P_np)

        for layer in range(self.layers):
            self.H[layer].weight.data = torch.Tensor(H[layer].value)
Example #58
0
def parse(nums, word_breaks=False):
    nums = trim(nums)
    # Smoothing the function using total variation
    # tune LAMBDA to a value that works empirically for data
    # increasing LAMBDA smooths the signal more
    # LAMBDA might be a function of the average length of dots and dashes
    # becuase faster message means shorter errors
    x = cvx.Variable(len(nums))
    obj = cvx.Minimize(
        cvx.norm(nums - x) + LAMBDA * sum(cvx.abs(x[:-1] - x[1:])))
    constraints = [x >= -1, x <= 1]
    prob = cvx.Problem(obj, constraints)
    prob.solve()
    smoothed = (np.array(x.value).flatten() >= 0).astype(int) * 2 - 1

    # Comment out to show visualization of smoothing
    # plot_signal(nums, np.array(x.value).flatten(), smoothed)

    # Generating dots and dashes
    loc = np.hstack((0, np.where(smoothed[:-1] != smoothed[1:])[0])) + 1
    loc[0] = 0
    intervals = np.add.reduceat(smoothed, loc)
    on = intervals[intervals > 0].reshape(-1, 1)
    off = intervals[intervals < 0].reshape(-1, 1)
    if on.shape[0] < 2 or off.shape[0] < 2:
        return None, False

    clusterOn = KMeans(n_clusters=2).fit(on).cluster_centers_
    mean_on = np.mean(clusterOn.flatten())
    mean_off_words = -2**63
    mean_off_letters = 0

    if word_breaks:
        clusterOff = KMeans(n_clusters=3).fit(off).cluster_centers_
        a, b, c = np.sort(clusterOff, axis=None)
        mean_off_words = np.mean([a, b])
        mean_off_letters = np.mean([b, c])
    else:
        clusterOff = KMeans(n_clusters=2).fit(off).cluster_centers_
        mean_off_letters = np.mean(clusterOff.flatten())

    # Creating the actual array
    intervals = list(intervals)
    new_intervals = []
    for q in intervals:
        if q < mean_off_words:
            new_intervals.append(-1)
        elif q < mean_off_letters:
            new_intervals.append(0)
        elif q < 0:
            continue
        elif q < mean_on:
            new_intervals.append(1)
        else:
            new_intervals.append(2)

    intervals = np.array(new_intervals)
    """np.place(intervals, intervals < mean_off_letters and intervals > mean_off_words, 0)
    intervals = intervals[intervals >= -1]
    np.place(intervals, np.logical_and(intervals>0, intervals<=mean_on), 1)
    np.place(intervals, intervals>1, 2)"""
    print intervals
    return intervals, True
Example #59
0
def cvxpy_foopsi(fluor, g, sn, b=None, c1=None, bas_nonneg=True, solvers=None):
    """Solves the deconvolution problem using the cvxpy package and the ECOS/SCS library.

    Args:
        fluor: ndarray
            fluorescence trace

        g: list of doubles
            parameters of the autoregressive model, cardinality equivalent to p

        sn: double
            estimated noise level

        b: double
            baseline level. If None it is estimated.

        c1: double
            initial value of calcium. If None it is estimated.

        bas_nonneg: boolean
            should the baseline be estimated

        solvers: tuple of two strings
            primary and secondary solvers to be used. Can be choosen between ECOS, SCS, CVXOPT

    Returns:
        c: estimated calcium trace

        b: estimated baseline

        c1: esimtated initial calcium value

        g: esitmated parameters of the autoregressive model

        sn: estimated noise level

        sp: estimated spikes

    Raises:
        ImportError 'cvxpy solver requires installation of cvxpy. Not working in windows at the moment.'

        ValueError 'Problem solved suboptimally or unfeasible'
    """
    # todo: check the result and gen_vector vars
    try:
        import cvxpy as cvx

    except ImportError: # XXX Is the below still true?
        raise ImportError(
            'cvxpy solver requires installation of cvxpy. Not working in windows at the moment.')

    if solvers is None:
        solvers = ['ECOS', 'SCS']

    T = fluor.size

    # construct deconvolution matrix  (sp = G*c)
    G = scipy.sparse.dia_matrix((np.ones((1, T)), [0]), (T, T))

    for i, gi in enumerate(g):
        G = G + \
            scipy.sparse.dia_matrix((-gi * np.ones((1, T)), [-1 - i]), (T, T))

    gr = np.roots(np.concatenate([np.array([1]), -g.flatten()]))
    gd_vec = np.max(gr)**np.arange(T)  # decay vector for initial fluorescence
    gen_vec = G.dot(scipy.sparse.coo_matrix(np.ones((T, 1))))

    c = cvx.Variable(T)  # calcium at each time step
    constraints = []
    cnt = 0
    if b is None:
        flag_b = True
        cnt += 1
        b = cvx.Variable(1)  # baseline value
        if bas_nonneg:
            b_lb = 0
        else:
            b_lb = np.min(fluor)
        constraints.append(b >= b_lb)
    else:
        flag_b = False

    if c1 is None:
        flag_c1 = True
        cnt += 1
        c1 = cvx.Variable(1)  # baseline value
        constraints.append(c1 >= 0)
    else:
        flag_c1 = False

    thrNoise = sn * np.sqrt(fluor.size)

    try:
        # minimize number of spikes
        objective = cvx.Minimize(cvx.norm(G * c, 1))
        constraints.append(G * c >= 0)
        constraints.append(
            cvx.norm(-c + fluor - b - gd_vec * c1, 2) <= thrNoise)  # constraints
        prob = cvx.Problem(objective, constraints)
        result = prob.solve(solver=solvers[0])

        if not (prob.status == 'optimal' or prob.status == 'optimal_inaccurate'):
            raise ValueError('Problem solved suboptimally or unfeasible')

        print(('PROBLEM STATUS:' + prob.status))
        sys.stdout.flush()
    except (ValueError, cvx.SolverError):     # if solvers fail to solve the problem

        lam = old_div(sn, 500)
        constraints = constraints[:-1]
        objective = cvx.Minimize(cvx.norm(-c + fluor - b - gd_vec *
                                          c1, 2) + lam * cvx.norm(G * c, 1))
        prob = cvx.Problem(objective, constraints)

        try:  # in case scs was not installed properly
            try:
                print('TRYING AGAIN ECOS')
                sys.stdout.flush()
                result = prob.solve(solver=solvers[0])
            except:
                print((solvers[0] + ' DID NOT WORK TRYING ' + solvers[1]))
                result = prob.solve(solver=solvers[1])
        except:
            sys.stderr.write(
                '***** SCS solver failed, try installing and compiling SCS for much faster performance. '
                'Otherwise set the solvers in tempora_params to ["ECOS","CVXOPT"]')
            sys.stderr.flush()
            raise

        if not (prob.status == 'optimal' or prob.status == 'optimal_inaccurate'):
            print(('PROBLEM STATUS:' + prob.status))
            sp = fluor
            c = fluor
            b = 0
            c1 = 0
            return c, b, c1, g, sn, sp

    sp = np.squeeze(np.asarray(G * c.value))
    c = np.squeeze(np.asarray(c.value))
    if flag_b:
        b = np.squeeze(b.value)
    if flag_c1:
        c1 = np.squeeze(c1.value)

    return c, b, c1, g, sn, sp
Example #60
0
for i in range(n):
    if random.random() < DENSITY:
        signal[i] = random.uniform(0, 100)
        nnz += 1

# Gaussian kernel.
m = 1001
kernel = gauss(m, m / 10)

# Noisy signal.
std = 1
noise = np.random.normal(scale=std, size=n + m - 1)
noisy_signal = conv(kernel, signal)  #+ noise

gamma = Parameter(nonneg=True)
fit = norm(conv(kernel, x) - noisy_signal, 2)
regularization = norm(x, 1)
constraints = [x >= 0]
gamma.value = 0.06
prob = Problem(Minimize(fit), constraints)
solver_options = {"NORMALIZE": True, "MAX_ITERS": 2500, "EPS": 1e-3}
result = prob.solve(solver=SCS, verbose=True, NORMALIZE=True, MAX_ITERS=2500)
# Get problem matrix.
data, dims = prob.get_problem_data(solver=SCS)

# Plot result and fit.
import matplotlib.pyplot as plt

plt.plot(range(n), signal, label="true signal")
plt.plot(range(n),
         np.asarray(noisy_signal.value[:n, 0]),