コード例 #1
0
ファイル: test_prox_fn.py プロジェクト: PeterZs/ProxImaL
    def test_sum_squares(self):
        """Test sum squares prox fn.
        """
        # No modifiers.
        tmp = Variable(10)
        fn = sum_squares(tmp)
        rho = 1
        v = np.arange(10) * 1.0
        x = fn.prox(rho, v.copy())
        self.assertItemsAlmostEqual(x, v * rho / (2 + rho))

        rho = 2
        x = fn.prox(rho, v.copy())
        self.assertItemsAlmostEqual(x, v * rho / (2 + rho))

        # With modifiers.
        mod_fn = sum_squares(tmp, alpha=2, beta=-1,
                             c=np.ones(10) * 1.0, b=np.ones(10) * 1.0, gamma=1)

        rho = 2
        v = np.arange(10) * 1.0
        x = mod_fn.prox(rho, v.copy())

        # vhat = mod_fn.beta*(v - mod_fn.c/rho)*rho/(rho+2*mod_fn.gamma) - mod_fn.b
        # rho_hat = rho/(mod_fn.alpha*np.sqrt(np.abs(mod_fn.beta)))
        # xhat = fn.prox(rho_hat, vhat)
        x_var = cvx.Variable(10)
        cost = 2 * cvx.sum_squares(-x_var - np.ones(10)) + \
            np.ones(10).T * x_var + cvx.sum_squares(x_var) + \
            (rho / 2) * cvx.sum_squares(x_var - v)
        prob = cvx.Problem(cvx.Minimize(cost))
        prob.solve()

        self.assertItemsAlmostEqual(x, x_var.value, places=3)
コード例 #2
0
ファイル: robust_svm.py プロジェクト: JeroenSoeters/epsilon
def create(m, n):
    mu = 1
    rho = 1
    sigma = 0.1

    A = problem_util.normalized_data_matrix(m, n, mu)
    x0 = sp.rand(n, 1, rho)
    x0.data = np.random.randn(x0.nnz)
    x0 = x0.toarray().ravel()

    b = np.sign(A.dot(x0) + sigma*np.random.randn(m))
    A[b>0,:] += 0.7*np.tile([x0], (np.sum(b>0),1))
    A[b<0,:] -= 0.7*np.tile([x0], (np.sum(b<0),1))

    P = la.block_diag(np.random.randn(n-1,n-1), 0)

    lam = 1
    x = cp.Variable(A.shape[1])

    # Straightforward formulation w/ no constraints
    # TODO(mwytock): Fix compiler so this works
    z0 = 1 - sp.diags([b],[0])*A*x + cp.norm1(P.T*x)
    f_eval = lambda: (lam*cp.sum_squares(x) + cp.sum_entries(cp.max_elemwise(z0, 0))).value

    # Explicit epigraph constraint
    t = cp.Variable(1)
    z = 1 - sp.diags([b],[0])*A*x + t
    f = lam*cp.sum_squares(x) + cp.sum_entries(cp.max_elemwise(z, 0))
    C = [cp.norm1(P.T*x) <= t]
    return cp.Problem(cp.Minimize(f), C), f_eval
コード例 #3
0
ファイル: max_softmax.py プロジェクト: xflash96/epsilon
def create(**kwargs):
    # m>k
    k = kwargs['k']  #class
    m = kwargs['m']  #instance
    n = kwargs['n']  #dim
    p = 5   #p-largest
    q = 10
    X = problem_util.normalized_data_matrix(m,n,1)
    Y = np.random.randint(0, k-1, (q,m))

    Theta = cp.Variable(n,k)
    t = cp.Variable(q)
    texp = cp.Variable(m)
    f = cp.sum_largest(t, p)+cp.sum_entries(texp) + cp.sum_squares(Theta)
    C = []
    C.append(cp.log_sum_exp(X*Theta, axis=1) <= texp)
    for i in range(q):
        Yi = one_hot(Y[i], k)
        C.append(-cp.sum_entries(cp.mul_elemwise(X.T.dot(Yi), Theta)) == t[i])

    t_eval = lambda: np.array([
        -cp.sum_entries(cp.mul_elemwise(X.T.dot(one_hot(Y[i], k)), Theta)).value for i in range(q)])
    f_eval = lambda: cp.sum_largest(t_eval(), p).value \
        + cp.sum_entries(cp.log_sum_exp(X*Theta, axis=1)).value \
        + cp.sum_squares(Theta).value
    
    return cp.Problem(cp.Minimize(f), C), f_val
コード例 #4
0
ファイル: oneclass_svm.py プロジェクト: JeroenSoeters/epsilon
def create(m, n):
    # Generate random points uniform over hypersphere
    A = np.random.randn(m, n)
    A /= np.sqrt(np.sum(A**2, axis=1))[:,np.newaxis]
    A *= (np.random.rand(m)**(1./n))[:,np.newaxis]

    # Shift points and add some outliers
    # NOTE(mwytock): causes problems for operator splitting, should fix
    #x0 = np.random.randn(n)
    x0 = np.zeros(n)
    A += x0

    k = max(m/50, 1)
    idx = np.random.randint(0, m, k)
    A[idx, :] += np.random.randn(k, n)
    lam = 1
    x = cp.Variable(n)
    rho = cp.Variable(1)

    # Straightforward expression
    #z = np.sum(A**2, axis=1) - 2*A*x + cp.sum_squares(x)  # z_i = ||a_i - x||^2
    #f = cp.sum_entries(cp.max_elemwise(z - rho, 0)) + lam*cp.max_elemwise(0, rho)

    # Explicit epigraph form
    t = cp.Variable(1)
    z = np.sum(A**2, axis=1) - 2*A*x + t  # z_i = ||a_i - x||^2
    z0 = np.sum(A**2, axis=1) - 2*A*x + cp.sum_squares(x)
    f_eval = lambda: ((1./n)*cp.sum_entries(cp.max_elemwise(z0 - rho, 0)) + cp.max_elemwise(0, rho)).value
    f = (1./n)*cp.sum_entries(cp.max_elemwise(z-rho, 0)) + lam*cp.sum_entries(cp.max_elemwise(rho, 0))
    C = [cp.sum_squares(x) <= t]

    return cp.Problem(cp.Minimize(f), C), f_eval
コード例 #5
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
コード例 #6
0
ファイル: linear.py プロジェクト: mwytock/cvxflow
def run_spsolve(Ax_expr, x_var):
    b0 = Ax_expr.value
    b0 += sigma*np.random.randn(*b0.shape)
    obj = cvx.sum_squares(Ax_expr - b0) + lam*cvx.sum_squares(x_var)
    prob = cvx.Problem(cvx.Minimize(obj))

    t0 = time.time()
    prob.solve(solver=cvx.LS)
    print "solve_time: %.2f secs" % (time.time() - t0)
    print "objective: %.3e" % obj.value
コード例 #7
0
ファイル: hinge.py プロジェクト: JeroenSoeters/epsilon
def create(**kwargs):
    n = kwargs["n"]

    x = cp.Variable(n)
    u = np.random.rand(n)
    f =  cp.sum_squares(x-u)+cp.sum_entries(cp.max_elemwise(x, 0))
    return cp.Problem(cp.Minimize(f))
コード例 #8
0
ファイル: robust_svm.py プロジェクト: silky/epsilon
def create(m, n):
    # Generate data
    X = np.hstack([np.random.randn(m,n), np.ones((m,1))])
    theta0 = np.random.randn(n+1)
    y = np.sign(X.dot(theta0) + 0.1*np.random.randn(m))
    X[y>0,:] += np.tile([theta0], (np.sum(y>0),1))
    X[y<0,:] -= np.tile([theta0], (np.sum(y<0),1))

    # Generate uncertainty envelope
    P = la.block_diag(np.random.randn(n,n), 0)
    lam = 1e-8
    theta = cp.Variable(n+1)

    # TODO(mwytock): write this as:
    # f = (lam/2*cp.sum_squares(theta) +
    #      problem_util.hinge(1 - y[:,np.newaxis]*X*theta+cp.norm1(P.T*theta)))

    # already in prox form
    t1 = cp.Variable(m)
    t2 = cp.Variable(1)
    z = cp.Variable(n+1)
    f = lam/2*cp.sum_squares(theta) + problem_util.hinge(1-t1)
    C = [t1 == y[:,np.newaxis]*X*theta - t2,
         cp.norm1(z) <= t2,
         P.T*theta == z]
    return cp.Problem(cp.Minimize(f), C)
コード例 #9
0
ファイル: hinge_l2.py プロジェクト: JeroenSoeters/epsilon
def create(**kwargs):
    A, b = problem_util.create_classification(**kwargs)
    lam = 1

    x = cp.Variable(A.shape[1])
    f = ep.hinge_loss(x, A, b) + lam*cp.sum_squares(x)
    return cp.Problem(cp.Minimize(f))
コード例 #10
0
ファイル: diagnostics.py プロジェクト: alnurali/cvxstoc
    def test_least_squares_plot_cost_ests(self, A, b, P, q, r, x, num_replications_list, alpha, fn):
        # Estimate the true objective value
        stoch_objf = cvx.sum_squares(A*x-b)
        fhatmeans, fhatvars, fhatucbs, fhatlcbs = [], [], [], []
        for num_replications in num_replications_list:
            print "Averaging [%d] times the obj. vals. at a candidate point of SAAs with 1 Monte Carlo sample to the stochastic least squares objective." % num_replications
            (fhatmean, fhatvar, fhatucb, fhatlcb) = self.unconstr_diag.est_cost(stoch_objf, num_replications, alpha)
            fhatmeans.append(fhatmean)
            fhatvars.append(fhatvar)
            fhatucbs.append(fhatucb)
            fhatlcbs.append(fhatlcb)

        # Compute the true objective value
        print "Evaluating the true stochastic least squares objective value at a candidate point."
        fstar = (cvx.quad_form(x,P) - 2*x.T*q + r).value
        fstars = np.tile( fstar, len(num_replications_list) )

        # Make plots
        print "Plotting results to [" + fn + "] (on a linear/log scale)."
        fig, ax = plt.subplots()
        plt.rc("text", usetex=True)
        ax.plot( num_replications_list, fhatmeans, "-", label=r"$\hat{f}_{0,N}(x)$", color="blue", lw=self.linewidth )
        ax.fill_between( num_replications_list, fhatucbs, fhatlcbs, facecolor="lightskyblue", alpha=0.5 )
        ax.plot( num_replications_list, fstars, "-", label=r"$f_0(x)$", color="crimson", lw=self.linewidth )

        ax.legend(loc="upper right", fontsize=self.fontsize)
        ax.set_xlabel(r"\# samples $N$", fontsize=self.fontsize)
        ax.set_xscale("log")
        for label in (ax.get_xticklabels() + ax.get_yticklabels()):
            label.set_fontsize(self.fontsize_axis)
        fig.savefig(fn, bbox_inches="tight")
        print "All done."

        # Return
        return (fhatmeans, fhatvars, fstars)
コード例 #11
0
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)
コード例 #12
0
ファイル: prox_test.py プロジェクト: silky/epsilon
def f_least_squares_matrix():
    m = 20
    k = 3
    A = np.random.randn(m, n)
    B = np.random.randn(m, k)
    X = cp.Variable(n, k)
    return cp.sum_squares(A*X  - B)
コード例 #13
0
ファイル: discsolvers.py プロジェクト: brayano/Discretization
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] 
コード例 #14
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
コード例 #15
0
ファイル: mapper.py プロジェクト: ericwayman/SparseIndex
def solveCardinalityObjective(prices,error):
    """
    Solves the problem 
    for i=1:n
    max: x[i]
    subject to: ||Rx - y||_2 <= error, x>=0, sum(x)==1

    which is a lower bound approximation to the problem with objective: 
    card(x)
    R is a number of time periods by number of assets matrix of returns
    for each asset in the index.  
    y is the time series for the index returns
    (The index is assumed to be one share of each asset in the index)
    error is the treshold for the tracking error.
    """
    numAssets = prices.shape[1]
    x = cvx.Variable(numAssets)
    returns = si.pricesToReturns(prices)
    indexReturns = si.pricesToReturnsForIndex(prices)
    TrackingErrorSquared = cvx.sum_squares(returns*x -indexReturns)
    obj = cvx.Maximize(x[0])
    constraints = [x>=0,sum(x)==1,TrackingErrorSquared <=error**2]
    prob = cvx.Problem(obj,constraints)

    optimalVal = 0
    optimalSol = np.array([])
    for i in range(numAssets):
        prob.obj = cvx.Maximize(x[i])
        prob.solve(warm_start=True)
        value = prob.value
        if value > optimalVal:
            optimalVal = value
            optimalSol = x.value
    return optimalVal, np.array(optimalSol).reshape(-1,)
コード例 #16
0
ファイル: test_problem.py プロジェクト: comp-imaging/ProxImaL
    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)
コード例 #17
0
ファイル: lasso.py プロジェクト: JeroenSoeters/epsilon
def create(**kwargs):
    A, B = problem_util.create_regression(**kwargs)
    lambda_max = np.abs(A.T.dot(B)).max()
    lam = 0.5*lambda_max

    X = cp.Variable(A.shape[1], B.shape[1] if len(B.shape) > 1 else 1)
    f = cp.sum_squares(A*X - B) + lam*cp.norm1(X)
    return cp.Problem(cp.Minimize(f))
コード例 #18
0
ファイル: portfolio.py プロジェクト: JeroenSoeters/epsilon
def create(m, n, density=0.1):
    np.random.seed(0)

    mu = np.exp(0.01*np.random.randn(n))-1  # returns
    D = np.random.rand(n)/10;               # idiosyncratic risk
    F = sp.rand(n,m,density)                # factor model
    F.data = np.random.randn(len(F.data))/10
    gamma = 1
    B = 1

    x = cp.Variable(n)
    f = mu.T*x - gamma*(cp.sum_squares(F.T.dot(x)) +
                        cp.sum_squares(cp.mul_elemwise(D, x)))
    C = [cp.sum_entries(x) == B,
         x >= 0]

    return cp.Problem(cp.Maximize(f), C)
コード例 #19
0
ファイル: problem_testutil.py プロジェクト: mwytock/cvxflow
def least_squares():
    np.random.seed(0)
    m = 10
    n = 5
    A = np.random.randn(m,n)
    b = np.random.randn(m)
    x = cvx.Variable(n)
    return cvx.Problem(cvx.Minimize(cvx.sum_squares(A*x - b)))
コード例 #20
0
ファイル: tv_denoise.py プロジェクト: JeroenSoeters/epsilon
def create(n, lam):
    # get data
    A = np.rot90(scipy.misc.imread(IMAGE), -1)[400:1400,600:1600]
    Y = scipy.misc.imresize(A, (n,n))

    # set up problem
    X = [cp.Variable(n,n) for i in range(3)]
    f = sum([cp.sum_squares(X[i] - Y[:,:,i]) for i in range(3)]) + lam * cp.tv(*X)
    return cp.Problem(cp.Minimize(f))
コード例 #21
0
ファイル: test_algs.py プロジェクト: spillai/ProxImaL
    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)
コード例 #22
0
ファイル: main.py プロジェクト: Daiver/jff
def trainLinearRegressor(featuresMat, targetDeltas):
    nSamples, nFeats = featuresMat.shape
    nOutputs = targetDeltas.shape[1]
    b = cvxpy.Variable(nOutputs)
    R = cvxpy.Variable(nOutputs, nFeats)
    residuals = featuresMat * R.T + cvxpy.kron(cvxpy.Constant(np.ones((nSamples))), b) - targetDeltas
    func = cvxpy.sum_squares(residuals)
    prob = cvxpy.Problem(cvxpy.Minimize(func))
    prob.solve(verbose=False)
    return R.value, b.value
コード例 #23
0
ファイル: convex.py プロジェクト: mwytock/cvxflow
def lasso_dense(n):
    m = 2*n
    A = np.random.randn(m, n)
    A /= np.sqrt(np.sum(A**2, 0))
    b = A*sp.rand(n, 1, 0.1) + 1e-2*np.random.randn(m,1)
    lam = 0.2*np.max(np.abs(A.T.dot(b)))

    x = cvx.Variable(n)
    f = cvx.sum_squares(A*x - b) + lam*cvx.norm1(x)
    return cvx.Problem(cvx.Minimize(f))
コード例 #24
0
def create(m, n, d):
    Xp = problem_util.normalized_data_matrix(m, d, 1)
    Xn = problem_util.normalized_data_matrix(n, d, 1)
    lam = 1

    theta = cp.Variable(d)
    f = ep.infinite_push(theta, Xp, Xn) + lam*cp.sum_squares(theta)

    f_eval = lambda: f.value
    return cp.Problem(cp.Minimize(f)), f_eval
コード例 #25
0
ファイル: test_prox_fn.py プロジェクト: PeterZs/ProxImaL
    def test_norm1(self):
        """Test L1 norm prox fn.
        """
        # No modifiers.
        tmp = Variable(10)
        fn = norm1(tmp)
        rho = 1
        v = np.arange(10) * 1.0 - 5.0
        x = fn.prox(rho, v.copy())
        self.assertItemsAlmostEqual(x, np.sign(v) * np.maximum(np.abs(v) - 1.0 / rho, 0))

        rho = 2
        x = fn.prox(rho, v.copy())
        self.assertItemsAlmostEqual(x, np.sign(v) * np.maximum(np.abs(v) - 1.0 / rho, 0))

        # With modifiers.
        mod_fn = norm1(tmp, alpha=0.1, beta=5,
                       c=np.ones(10) * 1.0, b=np.ones(10) * -1.0, gamma=4)

        rho = 2
        v = np.arange(10) * 1.0
        x = mod_fn.prox(rho, v.copy())

        # vhat = mod_fn.beta*(v - mod_fn.c/rho)*rho/(rho+2*mod_fn.gamma) - mod_fn.b
        # rho_hat = rho/(mod_fn.alpha*mod_fn.beta**2)
        # xhat = fn.prox(rho_hat, vhat)
        x_var = cvx.Variable(10)
        cost = 0.1 * cvx.norm1(5 * x_var + np.ones(10)) + np.ones(10).T * x_var + \
            4 * cvx.sum_squares(x_var) + (rho / 2) * cvx.sum_squares(x_var - v)
        prob = cvx.Problem(cvx.Minimize(cost))
        prob.solve()

        self.assertItemsAlmostEqual(x, x_var.value, places=3)

        # With weights.
        tmp = Variable(10)
        v = np.arange(10) * 1.0 - 5.0
        fn = weighted_norm1(tmp, -v + 1)
        rho = 2
        x = fn.prox(rho, v)
        self.assertItemsAlmostEqual(x, np.sign(v) *
                                    np.maximum(np.abs(v) - np.abs(-v + 1) / rho, 0))
コード例 #26
0
ファイル: linear.py プロジェクト: mwytock/cvxflow
def run_tensorflow(Ax_expr, x_var):
    import tensorflow as tf

    from cvxflow.conjugate_gradient import conjugate_gradient_solve
    from cvxflow import cvxpy_expr

    b0 = Ax_expr.value
    b0 += sigma*np.random.randn(*b0.shape)
    obj = cvx.sum_squares(Ax_expr - b0) + lam*cvx.sum_squares(x_var)

    t0 = time.time()
    expr = Ax_expr.canonicalize()[0]
    def A(x):
        return cvxpy_expr.tensor(expr, {x_var.id: x})
    def AT(y):
        return cvxpy_expr.adjoint_tensor(expr, y)[x_var.id]
    def M(x):
        return lam*x + AT(A(x))

    b = tf.constant(b0.reshape(-1,1), dtype=tf.float32)
    x_init = tf.zeros((n,1))
    x, iters, r_norm_sq = conjugate_gradient_solve(M, AT(b), x_init)

    init = tf.initialize_all_variables()
    print "init_time: %.2f secs" % (time.time() - t0)

    t0 = time.time()
    with tf.Session(config=tf.ConfigProto(device_count={"GPU": 0})) as sess:
        sess.run(init)
        x0, iters0, r_norm_sq0 = sess.run([x, iters, r_norm_sq])
        print "cpu_solve_time: %.2f secs" % (time.time() - t0)

    t0 = time.time()
    with tf.Session() as sess:
        sess.run(init)
        x0, iters0, r_norm_sq0 = sess.run([x, iters, r_norm_sq])
        print "gpu_solve_time: %.2f secs" % (time.time() - t0)

    x_var.value = x0
    print "objective: %.3e" % obj.value
    print "cg_iterations:", iters0
コード例 #27
0
 def __init__(self, returns, indexReturns,regularizer,i):
 #initiate a problem with data
     numAssets = returns.shape[1]   
     self.x = cvx.Variable(numAssets)
     self.index = i
     self.t=cvx.Variable()
     baseConstraints = [self.x>=0,self.t>=0,sum(self.x)==1]
     #should have a check to see if i is in the range of num of assets
     constraints = baseConstraints+ [cvx.log(self.x[i])+cvx.log(self.t)>= cvx.log(regularizer)]
     TrackingErrorSquared = cvx.sum_squares(returns*self.x -indexReturns )
     obj = cvx.Minimize( TrackingErrorSquared+self.t)
     cvx.Problem.__init__(self,obj,constraints)
コード例 #28
0
ファイル: convex.py プロジェクト: mwytock/cvxflow
def lasso_conv(n):
    sigma = n/10
    c = np.exp(-np.arange(-n/2., n/2.)**2./(2*sigma**2))/np.sqrt(2*sigma**2*np.pi)
    c[c < 1e-4] = 0

    x0 = np.array(sp.rand(n, 1, 0.1).todense()).ravel()
    b = np.convolve(c, x0) + 1e-2*np.random.randn(2*n-1)
    lam = 0.2*np.max(np.abs(np.convolve(b, c, "valid")))
    print lam

    x = cvx.Variable(n)
    f = cvx.sum_squares(cvx.conv(c, x) - b) + lam*cvx.norm1(x)
    return cvx.Problem(cvx.Minimize(f))
コード例 #29
0
ファイル: market_eq.py プロジェクト: ajfriend/admm
def foo_prox(a, b, x0, phi0, rho):
    n = len(a)
    x = cvx.Variable(n)
    phi = cvx.Variable(n)
    
    logb = np.log(b)
    
    logcash = cvx.log_sum_exp(phi + logb)
    ag_exp = cvx.log(x.T*a) - logcash
    t = cvx.Variable()

    constr = [x >= 0, ag_exp >= t]
    for j in range(n):
        expr = t >= np.log(a[j]) - phi[j]
        constr += [expr]
        
    obj = cvx.sum_squares(x-x0) + cvx.sum_squares(phi-phi0)
    obj = obj*rho/2.0
    prob = cvx.Problem(cvx.Minimize(obj), constr)
    prob.solve(verbose=False, solver='ECOS')
    
    return np.array(x.value).flatten(), np.array(phi.value).flatten()
コード例 #30
0
A = np.array(train_data['A'])
B = np.array(train_data['B'])

_, num_samples = np.shape(A)

print "num_samples:", num_samples

u = cvx.Variable(num_samples)
v = cvx.Variable(num_samples)

constraints = [cvx.sum_entries(u) == 1,
              cvx.sum_entries(v) == 1,
              u >= 0,
              v >= 0]

obj = cvx.Minimize(cvx.sum_squares(A * u - B * v))

prob = cvx.Problem(obj, constraints)

prob.solve()

print "status: ", prob.status
print "optimal value " , prob.value
#print "optimal var", u.value, v.value

w = A * u.value - B * v.value
r = np.transpose(A * u.value + B * v.value) / 2.0 * w

print w
print r
コード例 #31
0
 def _create_obj(self):
     print("Creating optimization objective...")
     self._obj = cp.Minimize(cp.sum_squares(self._W * self._x - self._tau))
コード例 #32
0
        def cvx_solve(row, regions, debug=False):
            if row.isna().sum() > 0:
                raise ValueError("Cannot call this method on data with NaNs")

            n_regions = len(regions)

            D = row[[KEYS["E"]["D"] % r for r in regions]].values
            D_W = [
                el ** 0.5
                for el in row[[KEYS["E"]["D"] % r + "_W" for r in regions]].values
            ]
            NG = row[[KEYS["E"]["NG"] % r for r in regions]].values
            NG_W = [
                el ** 0.5
                for el in row[[KEYS["E"]["NG"] % r + "_W" for r in regions]].values
            ]
            TI = row[[KEYS["E"]["TI"] % r for r in regions]].values
            TI_W = [
                el ** 0.5
                for el in row[[KEYS["E"]["TI"] % r + "_W" for r in regions]].values
            ]

            delta_D = cp.Variable(n_regions, name="delta_D")
            delta_NG = cp.Variable(n_regions, name="delta_NG")
            delta_TI = cp.Variable(n_regions, name="delta_TI")

            obj = (
                cp.sum_squares(cp.multiply(D_W, delta_D))
                + cp.sum_squares(cp.multiply(NG_W, delta_NG))
                + cp.sum_squares(cp.multiply(TI_W, delta_TI))
            )

            ID = {}
            ID_W = {}
            for i, ri in enumerate(regions):
                for j, rj in enumerate(regions):
                    if KEYS["E"]["ID"] % (ri, rj) in row.index:
                        ID[(ri, rj)] = row[KEYS["E"]["ID"] % (ri, rj)]
                        ID_W[(ri, rj)] = row[KEYS["E"]["ID"] % (ri, rj) + "_W"]
            delta_ID = {k: cp.Variable(name=f"{k}") for k in ID}
            constraints = [
                D + delta_D >= 1.0,
                NG + delta_NG >= 1.0,
                D + delta_D + TI + delta_TI - NG - delta_NG == 0.0,
            ]

            if with_ng_src:
                NG_SRC = {}
                NG_SRC_W = {}

                for i, src in enumerate(SRC):
                    for j, r in enumerate(regions):
                        if KEYS["E"][f"SRC_{src}"] % r in row.index:
                            NG_SRC[(src, r)] = row[KEYS["E"][f"SRC_{src}"] % r]
                            NG_SRC_W[(src, r)] = row[KEYS["E"][f"SRC_{src}"] % r + "_W"]
                delta_NG_SRC = {k: cp.Variable(name=f"{k}") for k in NG_SRC}

                for k in NG_SRC:
                    constraints += [NG_SRC[k] + delta_NG_SRC[k] >= 1.0]
                    obj += NG_SRC_W[k] * delta_NG_SRC[k] ** 2

            # Add the antisymmetry constraints twice is less efficient but not a huge deal.
            for ri, rj in ID:  # then (rj, ri) must also be in ID
                constraints += [
                    ID[(ri, rj)]
                    + delta_ID[(ri, rj)]
                    + ID[(rj, ri)]
                    + delta_ID[(rj, ri)]
                    == 0.0
                ]
                obj += ID_W[(ri, rj)] * delta_ID[(ri, rj)] ** 2

            for i, ri in enumerate(regions):
                if with_ng_src:
                    constraints += [
                        NG[i]
                        + delta_NG[i]
                        - cp.sum(
                            [
                                NG_SRC[(src, ri)] + delta_NG_SRC[(src, ri)]
                                for src in SRC
                                if (src, ri) in NG_SRC
                            ]
                        )
                        == 0.0
                    ]
                constraints += [
                    TI[i]
                    + delta_TI[i]
                    - cp.sum(
                        [
                            ID[(ri, rj)] + delta_ID[(ri, rj)]
                            for rj in regions
                            if (ri, rj) in ID
                        ]
                    )
                    == 0.0
                ]
            objective = cp.Minimize(obj)

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

            if with_ng_src:
                r = pd.concat(
                    [
                        pd.Series(
                            NG + delta_NG.value,
                            index=[KEYS["E"]["NG"] % r for r in regions],
                        ),
                        pd.Series(
                            D + delta_D.value,
                            index=[KEYS["E"]["D"] % r for r in regions],
                        ),
                        pd.Series(
                            TI + delta_TI.value,
                            index=[KEYS["E"]["TI"] % r for r in regions],
                        ),
                        pd.Series(
                            {KEYS["E"]["ID"] % k: ID[k] + delta_ID[k].value for k in ID}
                        ),
                        pd.Series(
                            {
                                KEYS["E"][f"SRC_{s}"] % r: NG_SRC[(s, r)]
                                + delta_NG_SRC[(s, r)].value
                                for (s, r) in NG_SRC
                            }
                        ),
                        pd.Series({"CleaningObjective": prob.value})
                    ]
                )
            else:
                r = pd.concat(
                    [
                        pd.Series(
                            NG + delta_NG.value,
                            index=[KEYS["E"]["NG"] % r for r in regions],
                        ),
                        pd.Series(
                            D + delta_D.value,
                            index=[KEYS["E"]["D"] % r for r in regions],
                        ),
                        pd.Series(
                            TI + delta_TI.value,
                            index=[KEYS["E"]["TI"] % r for r in regions],
                        ),
                        pd.Series(
                            {KEYS["E"]["ID"] % k: ID[k] + delta_ID[k].value for k in ID}
                        ),
                        pd.Series({"CleaningObjective": prob.value})
                    ]
                )

            if not debug:
                return r

            if with_ng_src:
                deltas = pd.concat(
                    [
                        pd.Series(
                            delta_NG.value, index=[KEYS["E"]["NG"] % r for r in regions]
                        ),
                        pd.Series(
                            delta_D.value, index=[KEYS["E"]["D"] % r for r in regions]
                        ),
                        pd.Series(
                            delta_TI.value, index=[KEYS["E"]["TI"] % r for r in regions]
                        ),
                        pd.Series({KEYS["E"]["ID"] % k: delta_ID[k].value for k in ID}),
                        pd.Series(
                            {
                                KEYS["E"][f"SRC_{s}"] % r: delta_NG_SRC[(s, r)].value
                                for (s, r) in NG_SRC
                            }
                        ),
                    ]
                )
            else:
                deltas = pd.concat(
                    [
                        pd.Series(
                            delta_NG.value, index=[KEYS["E"]["NG"] % r for r in regions]
                        ),
                        pd.Series(
                            delta_D.value, index=[KEYS["E"]["D"] % r for r in regions]
                        ),
                        pd.Series(
                            delta_TI.value, index=[KEYS["E"]["TI"] % r for r in regions]
                        ),
                        pd.Series({KEYS["E"]["ID"] % k: delta_ID[k].value for k in ID}),
                    ]
                )
            return pd.concat([r, deltas.rename(lambda x: x + "_Delta")])
コード例 #33
0
import numpy as np
import cvxpy as cp
import matplotlib.pyplot as plt

from nonlin_meas_data import m, n, sigma, alpha, beta, A, y
np.set_printoptions(precision=3)

EPSILON = 1e-8
#Verifying that the data is sorted
for i in range(m - 1):
    assert y[i] < y[i + 1] + EPSILON

z = cp.Variable(m)
x = cp.Variable(n)
next_diff_z = z[1:] - z[:-1]
next_diff_y = y[1:] - y[:-1]
constraints = [
    next_diff_z <= next_diff_y / alpha,
    next_diff_z >= next_diff_y / beta,
]
obj = cp.Minimize(cp.sum_squares(A @ x - z))
problem = cp.Problem(obj, constraints)
problem.solve()
print(f"problem status: {problem.status}")
if problem.status == 'optimal':
    print(f"x = {x.value}")
    plt.plot(y, z.value)
    plt.show()
コード例 #34
0
    FontPath = '/System/Library/Fonts/ヒラギノ角ゴシック W4.ttc'
elif sys.platform.startswith('linux'):
    FontPath = '/usr/share/fonts/truetype/takao-gothic/TakaoPGothic.ttf'
jpfont = FontProperties(fname=FontPath)
#%% 収益率データの読み込み
R = pd.read_csv('asset_return_data.csv', index_col=0)
T = R.shape[0]
N = R.shape[1]
Mu = R.mean().values
Sigma = R.cov().values * ((T - 1.0) / T)
Return_Dev = (R - Mu).values / np.sqrt(T)
#%% 空売り制約の下での分散最小化問題の設定
Weight = cvx.Variable(N)
Deviation = cvx.Variable(T)
Target_Return = cvx.Parameter(sign='positive')
Risk_Variance = cvx.sum_squares(Deviation)
Opt_Portfolio = cvx.Problem(cvx.Minimize(Risk_Variance), [
    Return_Dev * Weight == Deviation, Weight.T * Mu == Target_Return,
    cvx.sum_entries(Weight) == 1.0, Weight >= 0.0
])
#%% 空売り制約の下での最小分散フロンティアの計算
V_Target = np.linspace(Mu.min(), Mu.max(), num=250)
V_Risk = np.zeros(V_Target.shape)
for idx, Target_Return.value in enumerate(V_Target):
    Opt_Portfolio.solve()
    V_Risk[idx] = np.sqrt(Risk_Variance.value)
#%% 最小分散フロンティアのグラフの作成
fig1 = plt.figure(1, facecolor='w')
plt.plot(V_Risk, V_Target, 'k-')
plt.plot(np.sqrt(np.diagonal(Sigma)), Mu, 'kx')
plt.legend([u'最小分散フロンティア', u'個別資産'], loc='best', frameon=False, prop=jpfont)
コード例 #35
0
def approxBalATE(X, T, Y, tol_vals, objective):
    # weight the control and treated units so that they match the whole population

    n_ctrl = np.sum(1 - T)  # number of controls
    n_trt = np.sum(T)
    n_cov = X.shape[1]  # number of covariates

    tol = cp.Parameter()

    # weight the control units to match the population
    w_c = cp.Variable(n_ctrl)
    if objective == "l1":
        obj_c = cp.Minimize(cp.norm((w_c - 1 / n_ctrl), 1))
    elif objective == "l2":
        obj_c = cp.Minimize(cp.sum_squares(w_c - 1 / n_ctrl))
    elif objective == "entropy":
        obj_c = cp.Minimize(-cp.sum(cp.entr(w_c)))
    else:
        print("invalid objective")
        # return -1

    constraints_c = [cp.sum(w_c) == 1]

    constraints_c += [0 <= w_c]
    for i in range(n_cov):
        constraints_c += [X[T==0][:,i]*w_c - \
                        np.mean(X[:,i]) <= \
                        tol * X[:,i].std()]
        constraints_c += [np.mean(X[:,i]) -\
                        X[T==0][:,i]*w_c <=\
                        tol * X[:,i].std()]
    prob_c = cp.Problem(obj_c, constraints_c)

    w_c_vals = []
    for tol_val in tol_vals:
        tol.value = tol_val
        try:
            result_c = prob_c.solve()
            if w_c.value is None:
                w_c.value = -1. * np.ones(n_ctrl)
            w_c_vals.append(w_c.value)
        except SolverError:
            w_c_vals.append(-1. * np.ones(n_ctrl))

    # weight the treated units to match the population
    w_t = cp.Variable(n_trt)
    if objective == "l1":
        obj_t = cp.Minimize(cp.norm((w_t - 1 / n_trt), 1))
    elif objective == "l2":
        obj_t = cp.Minimize(cp.sum_squares(w_t - 1 / n_trt))
    elif objective == "entropy":
        obj_t = cp.Minimize(-cp.sum(cp.entr(w_t)))
    else:
        print("invalid objective")
        # return -1

    constraints_t = [cp.sum(w_t) == 1]

    constraints_t += [0 <= w_t]
    for i in range(n_cov):
        constraints_t += [X[T==1][:,i]*w_t - \
                        np.mean(X[:,i]) <= \
                        tol * X[:,i].std()]
        constraints_t += [np.mean(X[:,i]) -\
                        X[T==1][:,i]*w_t <= \
                        tol * X[:,i].std()]
    prob_t = cp.Problem(obj_t, constraints_t)

    w_t_vals = []
    for tol_val in tol_vals:
        tol.value = tol_val
        try:
            result_t = prob_t.solve()
            if w_t.value is None:
                w_t.value = -1. * np.ones(n_trt)
            w_t_vals.append(w_t.value)
        except SolverError:
            w_t_vals.append(-1. * np.ones(n_trt))

    return w_c_vals, w_t_vals
        split_plane = ck.prompt('Which Plane to split weight', type=int)
        while split_plane < 1 or split_plane > N:
            print('please enter a number between 1-', N)
            split_plane = ck.prompt('Which Plane to split weight', type=int)
            continue
        Split(W[split_plane - 1].value)


# =============================================================================
# =============================================================================
# # #USING CVXPY LEAST SQUARES TO SOLVE THE PROBLEM
# =============================================================================
# =============================================================================

W = cp.Variable((N, 1), complex=True)
objective = cp.Minimize(cp.sum_squares(ALPHA * W + A))
prob = cp.Problem(objective)
prob.solve()
print("Residule Squares")
print("-----------------")
print("Maximum Residual Error =", "%.2f" % error(W))
prnt(W)
print("Expected residual Array")
print(np.round(abs(np.add(np.matmul(ALPHA, W.value), A)), 2))

Call_Split(W)

# =============================================================================
# =============================================================================
# # # SOLVING USING MIN-MAX
# =============================================================================
コード例 #37
0
 def to_cvxpy(self, current_weights):
     self._make_variable(current_weights)
     self._target_weights = self._target_weights.reindex(
         self._new_index).fillna(0)
     err = cvx.sum_squares(self._new_weights - self._target_weights.values)
     return cvx.Minimize(err)
コード例 #38
0
 def deviation_risk_parity(w, cov_matrix):
     n = cov_matrix.shape[0]
     rp = (w * (cov_matrix @ w)) / cp.quad_form(w, cov_matrix)
     return cp.sum_squares(rp - 1 / n).value
コード例 #39
0
import cvxpy as cvx
import qcqp

n = 10
m = 8
l = 2

tau = 10
eta = 1

np.random.seed(1)
H = np.random.randn(m, n)
G = np.random.randn(l, n)

w = cvx.Variable(n)
obj = cvx.Minimize(cvx.sum_squares(w))
cons = [cvx.square(H * w) >= tau, cvx.square(G * w) <= eta]
prob = cvx.Problem(obj, cons)

# SDP-based lower bound
lb = prob.solve(method='sdp-relax', solver=cvx.MOSEK)
print('Lower bound: %.3f' % lb)

# Upper bounds
ub_cd = prob.solve(method='coord-descent', solver=cvx.MOSEK, num_samples=10)
ub_admm = prob.solve(method='qcqp-admm', solver=cvx.MOSEK, num_samples=10)
ub_dccp = prob.solve(method='qcqp-dccp',
                     solver=cvx.MOSEK,
                     num_samples=10,
                     tau=1)
print('Lower bound: %.3f' % lb)
コード例 #40
0
    def __init__(self,
                 d,
                 dataset_name=None,
                 X=None,
                 use_slab=False,
                 constrain_max_loss=False,
                 use_l2=False,
                 x_pos_tuple=None,
                 x_neg_tuple=None,
                 model_type='svm'):

        self.use_slab = use_slab
        self.constrain_max_loss = constrain_max_loss
        self.use_l2 = use_l2
        self.dataset = dataset_name
        # creating variables with shape of "d"
        # need to deal with some binary feature attibutes
        if dataset_name == "adult":
            # ---- adult -----
            # attributes 0-3 are continuous and represent: "age", "capital-gain", "capital-loss", "hours-per-week"
            # attributes 4-11 are binary representing the work class.
            # attributes 12-26: education background
            # attributes 27-32: martial status
            # attributes 33-46: occupation
            # attributes 47-51: relationship
            # 52-56: race

            # bool_inds = np.array([0]*d)
            # bool_inds[4:] = bool_inds[4:] + 1
            # print("the bolean indices are:",tuple(bool_inds))
            # self.cvx_x_pos = cvx.Variable(d, boolean = tuple([bool_inds]))
            self.cvx_x_pos_real = cvx.Variable(4)
            self.cvx_x_neg_real = cvx.Variable(4)
            self.cvx_x_pos_binary = cvx.Bool(d - 4)
            self.cvx_x_neg_binary = cvx.Bool(d - 4)
            # used for the binary constraints
            arr = np.array([0] * (d - 4))
            arr[0:8] = 1
            self.cvx_work_class = cvx.Parameter(d - 4, value=arr)
            arr = np.array([0] * (d - 4))
            arr[8:23] = 1
            self.cvx_education = cvx.Parameter(d - 4, value=arr)
            arr = np.array([0] * (d - 4))
            arr[23:29] = 1
            self.cvx_martial = cvx.Parameter(d - 4, value=arr)
            arr = np.array([0] * (d - 4))
            arr[29:43] = 1
            self.cvx_occupation = cvx.Parameter(d - 4, value=arr)
            arr = np.array([0] * (d - 4))
            arr[43:48] = 1
            self.cvx_relationship = cvx.Parameter(d - 4, value=arr)
            arr = np.array([0] * (d - 4))
            arr[48:53] = 1
            self.cvx_race = cvx.Parameter(d - 4, value=arr)
        else:
            self.cvx_x_pos = cvx.Variable(d)
            self.cvx_x_neg = cvx.Variable(d)

        self.cvx_g = cvx.Parameter(d)
        self.cvx_theta = cvx.Parameter(d)
        self.cvx_bias = cvx.Parameter(1)
        self.cvx_epsilon_pos = cvx.Parameter(1)
        self.cvx_epsilon_neg = cvx.Parameter(1)

        self.cvx_centroid_pos = cvx.Parameter(d)
        self.cvx_centroid_neg = cvx.Parameter(d)

        if use_l2:
            self.cvx_sphere_radius_pos = cvx.Parameter(1)
            self.cvx_sphere_radius_neg = cvx.Parameter(1)

        if use_slab:
            self.cvx_centroid_vec = cvx.Parameter(d)
            self.cvx_slab_radius_pos = cvx.Parameter(1)
            self.cvx_slab_radius_neg = cvx.Parameter(1)

        if constrain_max_loss:
            self.cvx_max_loss_pos = cvx.Parameter(1)
            self.cvx_max_loss_neg = cvx.Parameter(1)

        self.cvx_err = cvx.Variable(d)
        self.objective = cvx.Minimize(cvx.pnorm(self.cvx_err, 2))
        # version 0.4 is below
        if dataset_name == 'adult':
            self.constraints = [
                self.cvx_g - self.cvx_epsilon_pos * cvx.vstack(self.cvx_x_pos_real,self.cvx_x_pos_binary) +\
                     self.cvx_epsilon_neg * cvx.vstack(self.cvx_x_neg_real,self.cvx_x_neg_binary) == self.cvx_err,
                cvx_dot(self.cvx_theta, cvx.vstack(self.cvx_x_pos_real,self.cvx_x_pos_binary)) + self.cvx_bias < 1, # margin constraint, ideally should be 1
                -(cvx_dot(self.cvx_theta, cvx.vstack(self.cvx_x_neg_real,self.cvx_x_neg_binary)) + self.cvx_bias) < 1 , # ideally should be 1
            ]
        else:
            if model_type == 'svm':
                self.constraints = [
                    self.cvx_g - self.cvx_epsilon_pos * self.cvx_x_pos +
                    self.cvx_epsilon_neg * self.cvx_x_neg == self.cvx_err,
                    cvx_dot(self.cvx_theta, self.cvx_x_pos) + self.cvx_bias <
                    1,  # margin constraint, ideally should be 1
                    -(cvx_dot(self.cvx_theta, self.cvx_x_neg) + self.cvx_bias)
                    < 1,  # ideally should be 1
                ]
            # cvx 1.0 version
            # if model_type == 'svm':
            #     self.constraints = [
            #         self.cvx_g - self.cvx_epsilon_pos * self.cvx_x_pos + self.cvx_epsilon_neg * self.cvx_x_neg == self.cvx_err,
            #         cvx_dot(self.cvx_theta, self.cvx_x_pos) + self.cvx_bias <= 1, # margin constraint, ideally should be 1
            #         -(cvx_dot(self.cvx_theta, self.cvx_x_neg) + self.cvx_bias) <= 1 , # ideally should be 1
            #     ]
            elif model_type == 'lr':  # lr is not convex, cannot support it now
                pos_margin = cvx_dot(self.cvx_x_pos,
                                     self.cvx_theta) + self.cvx_bias
                neg_margin = -cvx_dot(self.cvx_x_neg,
                                      self.cvx_theta) + self.cvx_bias
                pos_grad = -sigmoid(-pos_margin) * self.cvx_x_pos
                neg_grad = sigmoid(-neg_margin) * self.cvx_x_neg
                self.constraints = [
                    self.cvx_g - self.cvx_epsilon_pos * pos_grad +
                    self.cvx_epsilon_neg * neg_grad == self.cvx_err
                ]
            else:
                print("Please use common linear classifier!")
                raise NotImplementedError

        if use_slab:
            self.constraints.append(
                cvx_dot(self.cvx_centroid_vec, self.cvx_x_pos -
                        self.cvx_centroid_pos) < self.cvx_slab_radius_pos)
            self.constraints.append(
                -cvx_dot(self.cvx_centroid_vec, self.cvx_x_pos -
                         self.cvx_centroid_pos) < self.cvx_slab_radius_pos)

            self.constraints.append(
                cvx_dot(self.cvx_centroid_vec, self.cvx_x_neg -
                        self.cvx_centroid_neg) < self.cvx_slab_radius_neg)
            self.constraints.append(
                -cvx_dot(self.cvx_centroid_vec, self.cvx_x_neg -
                         self.cvx_centroid_neg) < self.cvx_slab_radius_neg)

        if dataset_name in ['mnist_17', 'enron', 'imdb']:
            self.constraints.append(self.cvx_x_pos >= 0)
            self.constraints.append(self.cvx_x_neg >= 0)

        # additional constraints on synthetic dataset
        if x_pos_tuple:
            assert x_neg_tuple != None
            self.x_pos_min, self.x_pos_max = x_pos_tuple
            self.x_neg_min, self.x_neg_max = x_neg_tuple

            if dataset_name == 'adult':
                self.constraints.append(self.cvx_x_pos_real >= self.x_pos_min)
                self.constraints.append(self.cvx_x_pos_real <= self.x_pos_max)
                self.constraints.append(self.cvx_x_neg_real >= self.x_neg_min)
                self.constraints.append(self.cvx_x_neg_real <= self.x_neg_max)
            else:
                self.constraints.append(self.cvx_x_pos >= self.x_pos_min)
                self.constraints.append(self.cvx_x_pos <= self.x_pos_max)
                self.constraints.append(self.cvx_x_neg >= self.x_neg_min)
                self.constraints.append(self.cvx_x_neg <= self.x_neg_max)

        if constrain_max_loss:
            self.constraints.append(1 -
                                    (cvx_dot(self.cvx_theta, self.cvx_x_pos) +
                                     self.cvx_bias) < self.cvx_max_loss_pos)
            self.constraints.append(1 +
                                    (cvx_dot(self.cvx_theta, self.cvx_x_neg) +
                                     self.cvx_bias) < self.cvx_max_loss_neg)

        if dataset_name in ['mnist_17']:
            self.constraints.append(self.cvx_x_pos <= 1)
            self.constraints.append(self.cvx_x_neg <= 1)
        if dataset_name == 'adult':
            # binary featutre constraints: beacuse of one-hot encoding
            self.constraints.append(
                cvx_dot(self.cvx_work_class, self.cvx_x_pos_binary) == 1)
            self.constraints.append(
                cvx_dot(self.cvx_work_class, self.cvx_x_neg_binary) == 1)
            self.constraints.append(
                cvx_dot(self.cvx_education, self.cvx_x_pos_binary) == 1)
            self.constraints.append(
                cvx_dot(self.cvx_education, self.cvx_x_neg_binary) == 1)
            self.constraints.append(
                cvx_dot(self.cvx_martial, self.cvx_x_pos_binary) == 1)
            self.constraints.append(
                cvx_dot(self.cvx_martial, self.cvx_x_neg_binary) == 1)
            self.constraints.append(
                cvx_dot(self.cvx_occupation, self.cvx_x_pos_binary) == 1)
            self.constraints.append(
                cvx_dot(self.cvx_occupation, self.cvx_x_neg_binary) == 1)
            self.constraints.append(
                cvx_dot(self.cvx_relationship, self.cvx_x_pos_binary) == 1)
            self.constraints.append(
                cvx_dot(self.cvx_relationship, self.cvx_x_neg_binary) == 1)
            self.constraints.append(
                cvx_dot(self.cvx_race, self.cvx_x_pos_binary) == 1)
            self.constraints.append(
                cvx_dot(self.cvx_race, self.cvx_x_neg_binary) == 1)

        # If we pass in X, do the LP/integer constraint
        if (X is not None) and (dataset_name in ['enron', 'imdb']):
            if sparse.issparse(X):
                X_max = np.max(X, axis=0).toarray().reshape(-1)
            else:
                X_max = np.max(X, axis=0).reshape(-1)
            X_max[X_max < 1] = 1
            X_max[X_max > 50] = 50

            self.constraints.append(self.cvx_x_pos <= X_max)
            self.constraints.append(self.cvx_x_neg <= X_max)
            kmax = int(np.ceil(np.max(X_max)))

            self.cvx_env_pos = cvx.Variable(d)
            self.cvx_env_neg = cvx.Variable(d)
            for k in range(1, kmax + 1):
                active = k <= (X_max)
                self.constraints.append(
                    self.cvx_env_pos[active] >= self.cvx_x_pos[active] *
                    (2 * k - 1) - k * (k - 1))
                self.constraints.append(
                    self.cvx_env_neg[active] >= self.cvx_x_neg[active] *
                    (2 * k - 1) - k * (k - 1))

            if use_l2:
                self.constraints.append(
                    (cvx.sum_entries(self.cvx_env_pos) -
                     2 * cvx_dot(self.cvx_centroid_pos, self.cvx_x_pos) +
                     cvx.sum_squares(self.cvx_centroid_pos)) < (
                         self.cvx_sphere_radius_pos**2))
                self.constraints.append(
                    (cvx.sum_entries(self.cvx_env_neg) -
                     2 * cvx_dot(self.cvx_centroid_neg, self.cvx_x_neg) +
                     cvx.sum_squares(self.cvx_centroid_neg)) < (
                         self.cvx_sphere_radius_neg**2))
        else:
            if use_l2:
                self.constraints.append(
                    cvx.pnorm(self.cvx_x_pos - self.cvx_centroid_pos, 2)**2 <
                    self.cvx_sphere_radius_pos**2)
                self.constraints.append(
                    cvx.pnorm(self.cvx_x_neg - self.cvx_centroid_neg, 2)**2 <
                    self.cvx_sphere_radius_neg**2)
コード例 #41
0
#!/usr/bin/python
import numpy as np
import cvxpy as cvx
from qcqp import *

n, m = 10, 15
np.random.seed(1)

A = np.random.randn(m, n)
b = np.random.randn(m, 1)

x = cvx.Variable(n)
obj = cvx.sum_squares(A * x - b)
cons = [cvx.square(x) == 1]
prob = cvx.Problem(cvx.Minimize(obj), cons)
qcqp = QCQP(prob)

# sample from the semidefinite relaxation
qcqp.suggest(SDR)
print("SDR lower bound: %.3f" % qcqp.sdr_bound)

f_cd, v_cd = qcqp.improve(COORD_DESCENT)
x_cd = x.value
print("Coordinate descent: objective %.3f, violation %.3f" % (f_cd, v_cd))

# SDR solution is cached and not solved again
qcqp.suggest(SDR)
f_dccp, v_dccp = qcqp.improve(DCCP)
print("Penalty CCP: objective %.3f, violation %.3f" % (f_dccp, v_dccp))
f_dccp, v_dccp = qcqp.improve(COORD_DESCENT, phase1=False)
x_dccp = x.value
コード例 #42
0
        (len(lines), 1))

t = np.linspace(0, 5, num=5001)
f = np.linspace(1, 100, num=100)
phi = np.array([[np.sin(2 * np.pi * fk * ti) for fk in f] for ti in t])

alphas = [0.00001, 0.0001, 0.001, 0.01, 0.1, 1, 10, 100, 1000, 10000]
residuals = []
xs = []
probvals = []

for alpha in alphas:
    print(alpha)
    x = cp.Variable((100, 1))
    obj = cp.Minimize((alpha * cp.norm(x, 1)) +
                      cp.sum_squares(y_n - cp.matmul(phi, x)))
    prob = cp.Problem(obj)
    prob.solve()
    residuals.append(cp.norm(y_n - (phi @ x)).value)
    xs.append(x.value)
    probvals.append(prob.value)

print(residuals)
print(probvals)

plt.plot(alphas, probvals)
plt.xscale('log')
plt.show()

for i in range(len(xs)):
    plt.plot(range(1, 101), xs[i])
コード例 #43
0
    def test_intro(self) -> None:
        """Test examples from cvxpy.org introduction.
        """
        import numpy

        # cvx.Problem data.
        m = 30
        n = 20
        numpy.random.seed(1)
        A = numpy.random.randn(m, n)
        b = numpy.random.randn(m)

        # Construct the problem.
        x = cvx.Variable(n)
        objective = cvx.Minimize(cvx.sum_squares(A @ x - b))
        constraints = [0 <= x, x <= 1]
        prob = cvx.Problem(objective, constraints)

        # The optimal objective is returned by p.solve().
        prob.solve()
        # The optimal value for x is stored in x.value.
        print(x.value)
        # The optimal Lagrange multiplier for a constraint
        # is stored in constraint.dual_value.
        print(constraints[0].dual_value)

        ########################################

        # Create two scalar variables.
        x = cvx.Variable()
        y = cvx.Variable()

        # Create two constraints.
        constraints = [x + y == 1, x - y >= 1]

        # Form objective.
        obj = cvx.Minimize(cvx.square(x - y))

        # Form and solve problem.
        prob = cvx.Problem(obj, constraints)
        prob.solve()  # Returns the optimal value.
        print("status:", prob.status)
        print("optimal value", prob.value)
        print("optimal var", x.value, y.value)

        ########################################

        # Create two scalar variables.
        x = cvx.Variable()
        y = cvx.Variable()

        # Create two constraints.
        constraints = [x + y == 1, x - y >= 1]

        # Form objective.
        obj = cvx.Minimize(cvx.square(x - y))

        # Form and solve problem.
        prob = cvx.Problem(obj, constraints)
        prob.solve()  # Returns the optimal value.
        print("status:", prob.status)
        print("optimal value", prob.value)
        print("optimal var", x.value, y.value)

        self.assertEqual(prob.status, cvx.OPTIMAL)
        self.assertAlmostEqual(prob.value, 1.0)
        self.assertAlmostEqual(x.value, 1.0)
        self.assertAlmostEqual(y.value, 0)

        ########################################

        # Replace the objective.
        prob = cvx.Problem(cvx.Maximize(x + y), prob.constraints)
        print("optimal value", prob.solve())

        self.assertAlmostEqual(prob.value, 1.0, places=3)

        # Replace the constraint (x + y == 1).
        constraints = prob.constraints
        constraints[0] = (x + y <= 3)
        prob = cvx.Problem(prob.objective, constraints)
        print("optimal value", prob.solve())

        self.assertAlmostEqual(prob.value, 3.0, places=2)

        ########################################

        x = cvx.Variable()

        # An infeasible problem.
        prob = cvx.Problem(cvx.Minimize(x), [x >= 1, x <= 0])
        prob.solve()
        print("status:", prob.status)
        print("optimal value", prob.value)

        self.assertEqual(prob.status, cvx.INFEASIBLE)
        self.assertAlmostEqual(prob.value, np.inf)

        # An unbounded problem.
        prob = cvx.Problem(cvx.Minimize(x))
        prob.solve()
        print("status:", prob.status)
        print("optimal value", prob.value)

        self.assertEqual(prob.status, cvx.UNBOUNDED)
        self.assertAlmostEqual(prob.value, -np.inf)

        ########################################

        # A scalar variable.
        cvx.Variable()

        # Column vector variable of length 5.
        x = cvx.Variable(5)

        # Matrix variable with 4 rows and 7 columns.
        A = cvx.Variable((4, 7))

        ########################################
        import numpy

        # cvx.Problem data.
        m = 10
        n = 5
        numpy.random.seed(1)
        A = numpy.random.randn(m, n)
        b = numpy.random.randn(m)

        # Construct the problem.
        x = cvx.Variable(n)
        objective = cvx.Minimize(cvx.sum_squares(A @ x - b))
        constraints = [0 <= x, x <= 1]
        prob = cvx.Problem(objective, constraints)

        print("Optimal value", prob.solve())
        print("Optimal var")
        print(x.value)  # A numpy matrix.

        self.assertAlmostEqual(prob.value, 4.14133859146)

        ########################################
        # Positive scalar parameter.
        m = cvx.Parameter(nonneg=True)

        # Column vector parameter with unknown sign (by default).
        cvx.Parameter(5)

        # Matrix parameter with negative entries.
        G = cvx.Parameter((4, 7), nonpos=True)

        # Assigns a constant value to G.
        G.value = -numpy.ones((4, 7))
        ########################################

        # Create parameter, then assign value.
        rho = cvx.Parameter(nonneg=True)
        rho.value = 2

        # Initialize parameter with a value.
        rho = cvx.Parameter(nonneg=True, value=2)

        ########################################

        import numpy

        # cvx.Problem data.
        n = 15
        m = 10
        numpy.random.seed(1)
        A = numpy.random.randn(n, m)
        b = numpy.random.randn(n)
        # gamma must be positive due to DCP rules.
        gamma = cvx.Parameter(nonneg=True)

        # Construct the problem.
        x = cvx.Variable(m)
        error = cvx.sum_squares(A @ x - b)
        obj = cvx.Minimize(error + gamma * cvx.norm(x, 1))
        prob = cvx.Problem(obj)

        # Construct a trade-off curve of ||Ax-b||^2 vs. ||x||_1
        sq_penalty = []
        l1_penalty = []
        x_values = []
        gamma_vals = numpy.logspace(-4, 6)
        for val in gamma_vals:
            gamma.value = val
            prob.solve()
            # Use expr.value to get the numerical value of
            # an expression in the problem.
            sq_penalty.append(error.value)
            l1_penalty.append(cvx.norm(x, 1).value)
            x_values.append(x.value)

        ########################################
        import numpy

        X = cvx.Variable((5, 4))
        A = numpy.ones((3, 5))

        # Use expr.size to get the dimensions.
        print("dimensions of X:", X.size)
        print("dimensions of sum(X):", cvx.sum(X).size)
        print("dimensions of A @ X:", (A @ X).size)

        # ValueError raised for invalid dimensions.
        try:
            A + X
        except ValueError as e:
            print(e)
コード例 #44
0
    def __init__(self,
                 d,
                 use_sphere=True,
                 use_slab=True,
                 non_negative=False,
                 less_than_one=False,
                 constrain_max_loss=False,
                 goal=None,
                 X=None):

        assert goal in ['find_nearest_point', 'maximize_test_loss']

        self.use_slab = use_slab
        self.constrain_max_loss = constrain_max_loss
        self.goal = goal

        self.use_projection = not (non_negative or less_than_one or X)
        if self.use_projection:
            eff_d = 3 + (constrain_max_loss == True)
        else:
            eff_d = d

        self.cvx_x = cvx.Variable(eff_d)
        self.cvx_w = cvx.Parameter(eff_d)
        self.cvx_centroid = cvx.Parameter(eff_d)
        self.cvx_sphere_radius = cvx.Parameter(1)

        self.cvx_x_c = self.cvx_x - self.cvx_centroid

        if goal == 'find_nearest_point':
            self.objective = cvx.Minimize(
                cvx.pnorm(self.cvx_x - self.cvx_w, 2)**2)
        elif goal == 'maximize_test_loss':
            self.cvx_y = cvx.Parameter(1)
            # No need for bias term
            self.objective = cvx.Maximize(1 - self.cvx_y *
                                          cvx_dot(self.cvx_w, self.cvx_x))

        self.constraints = []
        if use_sphere:
            if X is not None:
                if sparse.issparse(X):
                    X_max = X.max(axis=0).toarray().reshape(-1)
                else:
                    X_max = np.max(X, axis=0).reshape(-1)
                X_max[X_max < 1] = 1
                X_max[X_max > 50] = 50
                self.constraints.append(self.cvx_x <= X_max)
                kmax = int(np.ceil(np.max(X_max)))

                self.cvx_env = cvx.Variable(eff_d)
                for k in range(1, kmax + 1):
                    active = k <= (X_max)
                    self.constraints.append(
                        self.cvx_env[active] >= self.cvx_x[active] *
                        (2 * k - 1) - k * (k - 1))

                self.constraints.append(
                    (cvx.sum_entries(self.cvx_env) -
                     2 * cvx_dot(self.cvx_centroid, self.cvx_x) +
                     cvx.sum_squares(self.cvx_centroid)) < (
                         self.cvx_sphere_radius**2))
            else:
                self.constraints.append(
                    cvx.pnorm(self.cvx_x_c, 2)**2 < self.cvx_sphere_radius**2)

        if use_slab:
            self.cvx_centroid_vec = cvx.Parameter(eff_d)
            self.cvx_slab_radius = cvx.Parameter(1)
            self.constraints.append(
                cvx_dot(self.cvx_centroid_vec, self.cvx_x_c) <
                self.cvx_slab_radius)
            self.constraints.append(
                -cvx_dot(self.cvx_centroid_vec, self.cvx_x_c) <
                self.cvx_slab_radius)

        if non_negative:
            self.constraints.append(self.cvx_x >= 0)

        if less_than_one:
            self.constraints.append(self.cvx_x <= 1)

        if constrain_max_loss:
            self.cvx_max_loss = cvx.Parameter(1)
            self.cvx_constraint_w = cvx.Parameter(eff_d)
            self.cvx_constraint_b = cvx.Parameter(1)
            self.constraints.append(
                1 - self.cvx_y * (cvx_dot(self.cvx_constraint_w, self.cvx_x) +
                                  self.cvx_constraint_b) < self.cvx_max_loss)

        self.prob = cvx.Problem(self.objective, self.constraints)
コード例 #45
0
ファイル: test.py プロジェクト: Yamp/home_lab
import cvxpy as cp
import numpy as np

m, n = 30, 20  # создаем матрицу и вектор (уравнений больше чем переменых)
A, b = np.random.randn(m, n), np.random.randn(m)

# Создаем вектор переменных
x = cp.Variable(n)
constraints = [0 <= x, x <= 1]
prob = cp.Problem(
    objective=cp.Minimize(cp.sum_squares(A * x - b)),
    constraints=constraints,
)

# The optimal objective value is returned by `prob.solve()`.
result = prob.solve_tsp()
# The optimal value for x is stored in `x.value`.
print(x.value)
# The optimal Lagrange multiplier for a constraint is stored in
# `constraint.dual_value`.
print(constraints[0].dual_value)
コード例 #46
0
    def test_absorb_lin_op(self):
        """Test absorb lin op operator.
        """
        # norm1.
        tmp = Variable(10)
        v = np.arange(10) * 1.0 - 5.0

        fn = norm1(mul_elemwise(-v, tmp), alpha=5.)
        rho = 2
        new_prox = absorb_lin_op(fn)[0]
        x = new_prox.prox(rho, v.copy())
        self.assertItemsAlmostEqual(
            x,
            np.sign(v) * np.maximum(np.abs(v) - 5. * np.abs(v) / rho, 0))

        fn = norm1(mul_elemwise(-v, mul_elemwise(2 * v, tmp)), alpha=5.)
        rho = 2
        new_prox = absorb_lin_op(fn)[0]
        x = new_prox.prox(rho, v.copy())
        self.assertItemsAlmostEqual(
            x,
            np.sign(v) * np.maximum(np.abs(v) - 5. * np.abs(v) / rho, 0))
        new_prox = absorb_lin_op(new_prox)[0]
        x = new_prox.prox(rho, v.copy())
        new_v = 2 * v * v
        self.assertItemsAlmostEqual(
            x,
            np.sign(new_v) *
            np.maximum(np.abs(new_v) - 5. * np.abs(new_v) / rho, 0))

        # nonneg.
        tmp = Variable(10)
        v = np.arange(10) * 1.0 - 5.0

        fn = nonneg(mul_elemwise(-v, tmp), alpha=5.)
        rho = 2
        new_prox = absorb_lin_op(fn)[0]
        x = new_prox.prox(rho, v.copy())
        self.assertItemsAlmostEqual(x, fn.prox(rho, -np.abs(v)))

        # sum_squares.
        tmp = Variable(10)
        v = np.arange(10) * 1.0 - 5.0

        alpha = 5.
        val = np.arange(10)
        fn = sum_squares(mul_elemwise(-v, tmp), alpha=alpha, c=val)
        rho = 2
        new_prox = absorb_lin_op(fn)[0]
        x = new_prox.prox(rho, v.copy())

        cvx_x = cvx.Variable(10)
        prob = cvx.Problem(
            cvx.Minimize(
                cvx.sum_squares(cvx_x - v) * (rho / 2) +
                5 * cvx.sum_squares(cvx.multiply(-v, cvx_x)) +
                (val * -v).T @ cvx_x))
        prob.solve()
        self.assertItemsAlmostEqual(x, cvx_x.value, places=3)

        # Test scale.
        tmp = Variable(10)
        v = np.arange(10) * 1.0 - 5.0

        fn = norm1(10 * tmp)
        rho = 2
        new_prox = absorb_lin_op(fn)[0]
        x = new_prox.prox(rho, v.copy())
        cvx_x = cvx.Variable(10)
        prob = cvx.Problem(
            cvx.Minimize(cvx.sum_squares(cvx_x - v) + cvx.norm(10 * cvx_x, 1)))
        prob.solve()
        self.assertItemsAlmostEqual(x, cvx_x.value, places=3)

        val = np.arange(10)
        fn = norm1(10 * tmp, c=val, b=val, gamma=0.01)
        rho = 2
        new_prox = absorb_lin_op(fn)[0]
        x = new_prox.prox(rho, v.copy())
        cvx_x = cvx.Variable(10)
        prob = cvx.Problem(cvx.Minimize(cvx.sum_squares(cvx_x - v) +
                                        cvx.norm(10 * cvx_x - val, 1) + 10 * val.T * \
                                                 cvx_x + cvx.sum_squares(cvx_x)
                                        ))
        prob.solve()
        self.assertItemsAlmostEqual(x, cvx_x.value, places=2)

        # sum_entries
        tmp = Variable(10)
        v = np.arange(10) * 1.0 - 5.0

        fn = sum_entries(sum([10 * tmp, mul_elemwise(v, tmp)]))

        funcs = absorb.absorb_all_lin_ops([fn])
        c = __builtins__['sum']([func.c for func in funcs])
        self.assertItemsAlmostEqual(c, v + 10, places=3)
コード例 #47
0
            constr += [
                etahat[:,
                       t - 1] == tau[:, t] - M @ ntddot[:, t] - W @ ntdot[:, t]
            ]

        # Trust region constraints.
        constr += [cp.abs(nt[:] - ot[:]) <= rhos[i]]

        # Torque limit.
        constr += [cp.abs(tau[:]) <= taumax]

        # Second arm can't fold back on first.
        constr += [nt[1, :] <= math.pi, nt[1, :] >= -1 * math.pi]

        # Establish objective function
        objective = cp.Minimize(h * cp.sum_squares(tau[:]) +
                                lambdalambda * cp.sum(cp.abs(etahat[:])))

        problem = cp.Problem(objective, constr)
        optval = problem.solve(solver=cp.ECOS)  # Solve...That...Problem!

        # Calculate actual torque violations.
        eta = np.zeros((2, N))
        for t in range(1, N + 1):
            # Dynamics equations. CONVERT TO PYTHON
            t1 = nt.value[0, t]
            t2 = nt.value[1, t]
            t1dot = ntdot.value[0, t]
            t2dot = ntdot.value[1, t]

            thetalist = np.array([t1, t2])
コード例 #48
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)
コード例 #49
0
elif sys.platform.startswith('darwin'):
    FontPath = '/System/Library/Fonts/ヒラギノ角ゴシック W4.ttc'
elif sys.platform.startswith('linux'):
    FontPath = '/usr/share/fonts/truetype/takao-gothic/TakaoPGothic.ttf'
jpfont = FontProperties(fname=FontPath)
#%% 収益率データの読み込み
R = pd.read_csv('asset_return_data.csv', index_col=0)
T = R.shape[0]
N = R.shape[1]
Mu = R.mean().values
Return_Dev = (R - Mu).values / np.sqrt(T)
#%% 下方半分散最小化問題の設定
Weight = cvx.Variable(N)
Deviation = cvx.Variable(T)
Target_Return = cvx.Parameter(nonneg=True)
Risk_Semivariance = cvx.sum_squares(Deviation)
Opt_Portfolio = cvx.Problem(cvx.Minimize(Risk_Semivariance),
                            [Weight.T*Mu == Target_Return,
                             cvx.sum(Weight) == 1.0,
                             Weight >= 0.0,
                             Deviation >= 0.0,
                             Return_Dev*Weight + Deviation >= 0.0])
#%% 最小下方半分散フロンティアの計算
V_Target = np.linspace(Mu.min(), Mu.max(), num=250)
V_Risk = np.zeros(V_Target.shape)
for idx, Target_Return.value in enumerate(V_Target):
    Opt_Portfolio.solve(solver=cvx.ECOS)
    V_Risk[idx] = np.sqrt(Risk_Semivariance.value)
#%% 最小下方半分散フロンティアのグラフの作成
fig1 = plt.figure(1, facecolor='w')
plt.plot(V_Risk, V_Target, 'k-')
コード例 #50
0
	def __update_ecc(self, nb_cost_mat, dis_k_vec, rw_constraints='inequality'):
	#	if self.__ds_name == 'Letter-high':
		if self.__ged_options['edit_cost'] == 'LETTER':
			raise Exception('Cannot compute for cost "LETTER".')
			pass
	#		# method 1: set alpha automatically, just tune c_vir and c_eir by 
	#		# LMS using cvxpy.
	#		alpha = 0.5
	#		coeff = 100 # np.max(alpha * nb_cost_mat[:,4] / dis_k_vec)
	##		if np.count_nonzero(nb_cost_mat[:,4]) == 0:
	##			alpha = 0.75
	##		else:
	##			alpha = np.min([dis_k_vec / c_vs for c_vs in nb_cost_mat[:,4] if c_vs != 0])
	##		alpha = alpha * 0.99
	#		param_vir = alpha * (nb_cost_mat[:,0] + nb_cost_mat[:,1])
	#		param_eir = (1 - alpha) * (nb_cost_mat[:,4] + nb_cost_mat[:,5])
	#		nb_cost_mat_new = np.column_stack((param_vir, param_eir))
	#		dis_new = coeff * dis_k_vec - alpha * nb_cost_mat[:,3]
	#		
	#		x = cp.Variable(nb_cost_mat_new.shape[1])
	#		cost = cp.sum_squares(nb_cost_mat_new * x - dis_new)
	#		constraints = [x >= [0.0 for i in range(nb_cost_mat_new.shape[1])]]
	#		prob = cp.Problem(cp.Minimize(cost), constraints)
	#		prob.solve()
	#		edit_costs_new = x.value
	#		edit_costs_new = np.array([edit_costs_new[0], edit_costs_new[1], alpha])
	#		residual = np.sqrt(prob.value)
		
	#		# method 2: tune c_vir, c_eir and alpha by nonlinear programming by 
	#		# scipy.optimize.minimize.
	#		w0 = nb_cost_mat[:,0] + nb_cost_mat[:,1]
	#		w1 = nb_cost_mat[:,4] + nb_cost_mat[:,5]
	#		w2 = nb_cost_mat[:,3]
	#		w3 = dis_k_vec
	#		func_min = lambda x: np.sum((w0 * x[0] * x[3] + w1 * x[1] * (1 - x[2]) \
	#							 + w2 * x[2] - w3 * x[3]) ** 2)
	#		bounds = ((0, None), (0., None), (0.5, 0.5), (0, None))
	#		res = minimize(func_min, [0.9, 1.7, 0.75, 10], bounds=bounds)
	#		edit_costs_new = res.x[0:3]
	#		residual = res.fun
		
		# method 3: tune c_vir, c_eir and alpha by nonlinear programming using cvxpy.
		
		
	#		# method 4: tune c_vir, c_eir and alpha by QP function
	#		# scipy.optimize.least_squares. An initial guess is required.
	#		w0 = nb_cost_mat[:,0] + nb_cost_mat[:,1]
	#		w1 = nb_cost_mat[:,4] + nb_cost_mat[:,5]
	#		w2 = nb_cost_mat[:,3]
	#		w3 = dis_k_vec
	#		func = lambda x: (w0 * x[0] * x[3] + w1 * x[1] * (1 - x[2]) \
	#							 + w2 * x[2] - w3 * x[3]) ** 2
	#		res = optimize.root(func, [0.9, 1.7, 0.75, 100])
	#		edit_costs_new = res.x
	#		residual = None
		elif self.__ged_options['edit_cost'] == 'LETTER2':
	#			# 1. if c_vi != c_vr, c_ei != c_er.
	#			nb_cost_mat_new = nb_cost_mat[:,[0,1,3,4,5]]
	#			x = cp.Variable(nb_cost_mat_new.shape[1])
	#			cost_fun = cp.sum_squares(nb_cost_mat_new @ x - dis_k_vec)
	##			# 1.1 no constraints.
	##			constraints = [x >= [0.0 for i in range(nb_cost_mat_new.shape[1])]]
	#			# 1.2 c_vs <= c_vi + c_vr.
	#			constraints = [x >= [0.0 for i in range(nb_cost_mat_new.shape[1])],
	#						   np.array([1.0, 1.0, -1.0, 0.0, 0.0]).T@x >= 0.0]			
	##			# 2. if c_vi == c_vr, c_ei == c_er.
	##			nb_cost_mat_new = nb_cost_mat[:,[0,3,4]]
	##			nb_cost_mat_new[:,0] += nb_cost_mat[:,1]
	##			nb_cost_mat_new[:,2] += nb_cost_mat[:,5]
	##			x = cp.Variable(nb_cost_mat_new.shape[1])
	##			cost_fun = cp.sum_squares(nb_cost_mat_new @ x - dis_k_vec)
	##			# 2.1 no constraints.
	##			constraints = [x >= [0.0 for i in range(nb_cost_mat_new.shape[1])]]
	###			# 2.2 c_vs <= c_vi + c_vr.
	###			constraints = [x >= [0.0 for i in range(nb_cost_mat_new.shape[1])],
	###						   np.array([2.0, -1.0, 0.0]).T@x >= 0.0]	 
	#			
	#			prob = cp.Problem(cp.Minimize(cost_fun), constraints)
	#			prob.solve()
	#			edit_costs_new = [x.value[0], x.value[0], x.value[1], x.value[2], x.value[2]]
	#			edit_costs_new = np.array(edit_costs_new)
	#			residual = np.sqrt(prob.value)
			if not self.__triangle_rule and self.__allow_zeros:
				nb_cost_mat_new = nb_cost_mat[:,[0,1,3,4,5]]
				x = cp.Variable(nb_cost_mat_new.shape[1])
				cost_fun = cp.sum_squares(nb_cost_mat_new @ x - dis_k_vec)
				constraints = [x >= [0.0 for i in range(nb_cost_mat_new.shape[1])],
							   np.array([1.0, 0.0, 0.0, 0.0, 0.0]).T@x >= 0.01,
							   np.array([0.0, 1.0, 0.0, 0.0, 0.0]).T@x >= 0.01,
							   np.array([0.0, 0.0, 0.0, 1.0, 0.0]).T@x >= 0.01,
							   np.array([0.0, 0.0, 0.0, 0.0, 1.0]).T@x >= 0.01]
				prob = cp.Problem(cp.Minimize(cost_fun), constraints)
				self.__execute_cvx(prob)
				edit_costs_new = x.value
				residual = np.sqrt(prob.value)
			elif self.__triangle_rule and self.__allow_zeros:
				nb_cost_mat_new = nb_cost_mat[:,[0,1,3,4,5]]
				x = cp.Variable(nb_cost_mat_new.shape[1])
				cost_fun = cp.sum_squares(nb_cost_mat_new @ x - dis_k_vec)
				constraints = [x >= [0.0 for i in range(nb_cost_mat_new.shape[1])],
							   np.array([1.0, 0.0, 0.0, 0.0, 0.0]).T@x >= 0.01,
							   np.array([0.0, 1.0, 0.0, 0.0, 0.0]).T@x >= 0.01,
							   np.array([0.0, 0.0, 0.0, 1.0, 0.0]).T@x >= 0.01,
							   np.array([0.0, 0.0, 0.0, 0.0, 1.0]).T@x >= 0.01,
							   np.array([1.0, 1.0, -1.0, 0.0, 0.0]).T@x >= 0.0]
				prob = cp.Problem(cp.Minimize(cost_fun), constraints)
				self.__execute_cvx(prob)
				edit_costs_new = x.value
				residual = np.sqrt(prob.value)
			elif not self.__triangle_rule and not self.__allow_zeros:
				nb_cost_mat_new = nb_cost_mat[:,[0,1,3,4,5]]
				x = cp.Variable(nb_cost_mat_new.shape[1])
				cost_fun = cp.sum_squares(nb_cost_mat_new @ x - dis_k_vec)
				constraints = [x >= [0.01 for i in range(nb_cost_mat_new.shape[1])]]
				prob = cp.Problem(cp.Minimize(cost_fun), constraints)
				prob.solve()
				edit_costs_new = x.value
				residual = np.sqrt(prob.value)
	#			elif method == 'inequality_modified':
	#				# c_vs <= c_vi + c_vr.
	#				nb_cost_mat_new = nb_cost_mat[:,[0,1,3,4,5]]
	#				x = cp.Variable(nb_cost_mat_new.shape[1])
	#				cost_fun = cp.sum_squares(nb_cost_mat_new @ x - dis_k_vec)
	#				constraints = [x >= [0.0 for i in range(nb_cost_mat_new.shape[1])],
	#							   np.array([1.0, 1.0, -1.0, 0.0, 0.0]).T@x >= 0.0]
	#				prob = cp.Problem(cp.Minimize(cost_fun), constraints)
	#				prob.solve()
	#				# use same costs for insertion and removal rather than the fitted costs.
	#				edit_costs_new = [x.value[0], x.value[0], x.value[1], x.value[2], x.value[2]]
	#				edit_costs_new = np.array(edit_costs_new)
	#				residual = np.sqrt(prob.value)
			elif self.__triangle_rule and not self.__allow_zeros:
				# c_vs <= c_vi + c_vr.
				nb_cost_mat_new = nb_cost_mat[:,[0,1,3,4,5]]
				x = cp.Variable(nb_cost_mat_new.shape[1])
				cost_fun = cp.sum_squares(nb_cost_mat_new @ x - dis_k_vec)
				constraints = [x >= [0.01 for i in range(nb_cost_mat_new.shape[1])],
							   np.array([1.0, 1.0, -1.0, 0.0, 0.0]).T@x >= 0.0]
				prob = cp.Problem(cp.Minimize(cost_fun), constraints)
				self.__execute_cvx(prob)
				edit_costs_new = x.value
				residual = np.sqrt(prob.value)				
			elif rw_constraints == '2constraints': # @todo: rearrange it later.
				# c_vs <= c_vi + c_vr and c_vi == c_vr, c_ei == c_er.
				nb_cost_mat_new = nb_cost_mat[:,[0,1,3,4,5]]
				x = cp.Variable(nb_cost_mat_new.shape[1])
				cost_fun = cp.sum_squares(nb_cost_mat_new @ x - dis_k_vec)
				constraints = [x >= [0.01 for i in range(nb_cost_mat_new.shape[1])],
							   np.array([1.0, 1.0, -1.0, 0.0, 0.0]).T@x >= 0.0,
							   np.array([1.0, -1.0, 0.0, 0.0, 0.0]).T@x == 0.0,
							   np.array([0.0, 0.0, 0.0, 1.0, -1.0]).T@x == 0.0]
				prob = cp.Problem(cp.Minimize(cost_fun), constraints)
				prob.solve()
				edit_costs_new = x.value
				residual = np.sqrt(prob.value)

		elif self.__ged_options['edit_cost'] == 'NON_SYMBOLIC':
			is_n_attr = np.count_nonzero(nb_cost_mat[:,2])
			is_e_attr = np.count_nonzero(nb_cost_mat[:,5])
			
			if self.__ds_name == 'SYNTHETICnew': # @todo: rearrenge this later.
	#			nb_cost_mat_new = nb_cost_mat[:,[0,1,2,3,4]]
				nb_cost_mat_new = nb_cost_mat[:,[2,3,4]]
				x = cp.Variable(nb_cost_mat_new.shape[1])
				cost_fun = cp.sum_squares(nb_cost_mat_new @ x - dis_k_vec)
	#			constraints = [x >= [0.0 for i in range(nb_cost_mat_new.shape[1])],
	#						   np.array([0.0, 0.0, 0.0, 1.0, -1.0]).T@x == 0.0]
	#			constraints = [x >= [0.0001 for i in range(nb_cost_mat_new.shape[1])]]
				constraints = [x >= [0.0001 for i in range(nb_cost_mat_new.shape[1])],
					   np.array([0.0, 1.0, -1.0]).T@x == 0.0]
				prob = cp.Problem(cp.Minimize(cost_fun), constraints)
				prob.solve()
	#			print(x.value)
				edit_costs_new = np.concatenate((np.array([0.0, 0.0]), x.value, 
												 np.array([0.0])))
				residual = np.sqrt(prob.value)
				
			elif not self.__triangle_rule and self.__allow_zeros:
				if is_n_attr and is_e_attr:
					nb_cost_mat_new = nb_cost_mat[:,[0,1,2,3,4,5]]
					x = cp.Variable(nb_cost_mat_new.shape[1])
					cost_fun = cp.sum_squares(nb_cost_mat_new @ x - dis_k_vec)
					constraints = [x >= [0.0 for i in range(nb_cost_mat_new.shape[1])],
								   np.array([1.0, 0.0, 0.0, 0.0, 0.0, 0.0]).T@x >= 0.01,
								   np.array([0.0, 1.0, 0.0, 0.0, 0.0, 0.0]).T@x >= 0.01,
								   np.array([0.0, 0.0, 0.0, 1.0, 0.0, 0.0]).T@x >= 0.01,
								   np.array([0.0, 0.0, 0.0, 0.0, 1.0, 0.0]).T@x >= 0.01]
					prob = cp.Problem(cp.Minimize(cost_fun), constraints)
					self.__execute_cvx(prob)
					edit_costs_new = x.value
					residual = np.sqrt(prob.value)
				elif is_n_attr and not is_e_attr:
					nb_cost_mat_new = nb_cost_mat[:,[0,1,2,3,4]]
					x = cp.Variable(nb_cost_mat_new.shape[1])
					cost_fun = cp.sum_squares(nb_cost_mat_new @ x - dis_k_vec)
					constraints = [x >= [0.0 for i in range(nb_cost_mat_new.shape[1])],
								   np.array([1.0, 0.0, 0.0, 0.0, 0.0]).T@x >= 0.01,
								   np.array([0.0, 1.0, 0.0, 0.0, 0.0]).T@x >= 0.01,
								   np.array([0.0, 0.0, 0.0, 1.0, 0.0]).T@x >= 0.01,
								   np.array([0.0, 0.0, 0.0, 0.0, 1.0]).T@x >= 0.01]
					prob = cp.Problem(cp.Minimize(cost_fun), constraints)
					self.__execute_cvx(prob)
					edit_costs_new = np.concatenate((x.value, np.array([0.0])))
					residual = np.sqrt(prob.value)
				elif not is_n_attr and is_e_attr:
					nb_cost_mat_new = nb_cost_mat[:,[0,1,3,4,5]]
					x = cp.Variable(nb_cost_mat_new.shape[1])
					cost_fun = cp.sum_squares(nb_cost_mat_new @ x - dis_k_vec)
					constraints = [x >= [0.0 for i in range(nb_cost_mat_new.shape[1])],
								   np.array([1.0, 0.0, 0.0, 0.0, 0.0]).T@x >= 0.01,
								   np.array([0.0, 1.0, 0.0, 0.0, 0.0]).T@x >= 0.01,
								   np.array([0.0, 0.0, 1.0, 0.0, 0.0]).T@x >= 0.01,
								   np.array([0.0, 0.0, 0.0, 1.0, 0.0]).T@x >= 0.01]
					prob = cp.Problem(cp.Minimize(cost_fun), constraints)
					self.__execute_cvx(prob)
					edit_costs_new = np.concatenate((x.value[0:2], np.array([0.0]), x.value[2:]))
					residual = np.sqrt(prob.value)
				else:
					nb_cost_mat_new = nb_cost_mat[:,[0,1,3,4]]
					x = cp.Variable(nb_cost_mat_new.shape[1])
					cost_fun = cp.sum_squares(nb_cost_mat_new @ x - dis_k_vec)
					constraints = [x >= [0.01 for i in range(nb_cost_mat_new.shape[1])]]
					prob = cp.Problem(cp.Minimize(cost_fun), constraints)
					self.__execute_cvx(prob)
					edit_costs_new = np.concatenate((x.value[0:2], np.array([0.0]), 
													 x.value[2:], np.array([0.0])))
					residual = np.sqrt(prob.value)
			elif self.__triangle_rule and self.__allow_zeros:
				if is_n_attr and is_e_attr:
					nb_cost_mat_new = nb_cost_mat[:,[0,1,2,3,4,5]]
					x = cp.Variable(nb_cost_mat_new.shape[1])
					cost_fun = cp.sum_squares(nb_cost_mat_new @ x - dis_k_vec)
					constraints = [x >= [0.0 for i in range(nb_cost_mat_new.shape[1])],
								   np.array([1.0, 0.0, 0.0, 0.0, 0.0, 0.0]).T@x >= 0.01,
								   np.array([0.0, 1.0, 0.0, 0.0, 0.0, 0.0]).T@x >= 0.01,
								   np.array([0.0, 0.0, 0.0, 1.0, 0.0, 0.0]).T@x >= 0.01,
								   np.array([0.0, 0.0, 0.0, 0.0, 1.0, 0.0]).T@x >= 0.01,
								   np.array([1.0, 1.0, -1.0, 0.0, 0.0, 0.0]).T@x >= 0.0,
								   np.array([0.0, 0.0, 0.0, 1.0, 1.0, -1.0]).T@x >= 0.0]
					prob = cp.Problem(cp.Minimize(cost_fun), constraints)
					self.__execute_cvx(prob)
					edit_costs_new = x.value
					residual = np.sqrt(prob.value)
				elif is_n_attr and not is_e_attr:
					nb_cost_mat_new = nb_cost_mat[:,[0,1,2,3,4]]
					x = cp.Variable(nb_cost_mat_new.shape[1])
					cost_fun = cp.sum_squares(nb_cost_mat_new @ x - dis_k_vec)
					constraints = [x >= [0.0 for i in range(nb_cost_mat_new.shape[1])],
								   np.array([1.0, 0.0, 0.0, 0.0, 0.0]).T@x >= 0.01,
								   np.array([0.0, 1.0, 0.0, 0.0, 0.0]).T@x >= 0.01,
								   np.array([0.0, 0.0, 0.0, 1.0, 0.0]).T@x >= 0.01,
								   np.array([0.0, 0.0, 0.0, 0.0, 1.0]).T@x >= 0.01,
								   np.array([1.0, 1.0, -1.0, 0.0, 0.0]).T@x >= 0.0]
					prob = cp.Problem(cp.Minimize(cost_fun), constraints)
					self.__execute_cvx(prob)
					edit_costs_new = np.concatenate((x.value, np.array([0.0])))
					residual = np.sqrt(prob.value)
				elif not is_n_attr and is_e_attr:
					nb_cost_mat_new = nb_cost_mat[:,[0,1,3,4,5]]
					x = cp.Variable(nb_cost_mat_new.shape[1])
					cost_fun = cp.sum_squares(nb_cost_mat_new @ x - dis_k_vec)
					constraints = [x >= [0.0 for i in range(nb_cost_mat_new.shape[1])],
								   np.array([1.0, 0.0, 0.0, 0.0, 0.0]).T@x >= 0.01,
								   np.array([0.0, 1.0, 0.0, 0.0, 0.0]).T@x >= 0.01,
								   np.array([0.0, 0.0, 1.0, 0.0, 0.0]).T@x >= 0.01,
								   np.array([0.0, 0.0, 0.0, 1.0, 0.0]).T@x >= 0.01,
								   np.array([0.0, 0.0, 1.0, 1.0, -1.0]).T@x >= 0.0]
					prob = cp.Problem(cp.Minimize(cost_fun), constraints)
					self.__execute_cvx(prob)
					edit_costs_new = np.concatenate((x.value[0:2], np.array([0.0]), x.value[2:]))
					residual = np.sqrt(prob.value)
				else:
					nb_cost_mat_new = nb_cost_mat[:,[0,1,3,4]]
					x = cp.Variable(nb_cost_mat_new.shape[1])
					cost_fun = cp.sum_squares(nb_cost_mat_new @ x - dis_k_vec)
					constraints = [x >= [0.01 for i in range(nb_cost_mat_new.shape[1])]]
					prob = cp.Problem(cp.Minimize(cost_fun), constraints)
					self.__execute_cvx(prob)
					edit_costs_new = np.concatenate((x.value[0:2], np.array([0.0]), 
													 x.value[2:], np.array([0.0])))
					residual = np.sqrt(prob.value)
			elif not self.__triangle_rule and not self.__allow_zeros:
				if is_n_attr and is_e_attr:
					nb_cost_mat_new = nb_cost_mat[:,[0,1,2,3,4,5]]
					x = cp.Variable(nb_cost_mat_new.shape[1])
					cost_fun = cp.sum_squares(nb_cost_mat_new @ x - dis_k_vec)
					constraints = [x >= [0.01 for i in range(nb_cost_mat_new.shape[1])]]
					prob = cp.Problem(cp.Minimize(cost_fun), constraints)
					self.__execute_cvx(prob)
					edit_costs_new = x.value
					residual = np.sqrt(prob.value)
				elif is_n_attr and not is_e_attr:
					nb_cost_mat_new = nb_cost_mat[:,[0,1,2,3,4]]
					x = cp.Variable(nb_cost_mat_new.shape[1])
					cost_fun = cp.sum_squares(nb_cost_mat_new @ x - dis_k_vec)
					constraints = [x >= [0.01 for i in range(nb_cost_mat_new.shape[1])]]
					prob = cp.Problem(cp.Minimize(cost_fun), constraints)
					self.__execute_cvx(prob)
					edit_costs_new = np.concatenate((x.value, np.array([0.0])))
					residual = np.sqrt(prob.value)
				elif not is_n_attr and is_e_attr:
					nb_cost_mat_new = nb_cost_mat[:,[0,1,3,4,5]]
					x = cp.Variable(nb_cost_mat_new.shape[1])
					cost_fun = cp.sum_squares(nb_cost_mat_new @ x - dis_k_vec)
					constraints = [x >= [0.01 for i in range(nb_cost_mat_new.shape[1])]]
					prob = cp.Problem(cp.Minimize(cost_fun), constraints)
					self.__execute_cvx(prob)
					edit_costs_new = np.concatenate((x.value[0:2], np.array([0.0]), x.value[2:]))
					residual = np.sqrt(prob.value)
				else:
					nb_cost_mat_new = nb_cost_mat[:,[0,1,3,4]]
					x = cp.Variable(nb_cost_mat_new.shape[1])
					cost_fun = cp.sum_squares(nb_cost_mat_new @ x - dis_k_vec)
					constraints = [x >= [0.01 for i in range(nb_cost_mat_new.shape[1])]]
					prob = cp.Problem(cp.Minimize(cost_fun), constraints)
					self.__execute_cvx(prob)
					edit_costs_new = np.concatenate((x.value[0:2], np.array([0.0]), 
													 x.value[2:], np.array([0.0])))
					residual = np.sqrt(prob.value)
			elif self.__triangle_rule and not self.__allow_zeros:
				# c_vs <= c_vi + c_vr.
				if is_n_attr and is_e_attr:
					nb_cost_mat_new = nb_cost_mat[:,[0,1,2,3,4,5]]
					x = cp.Variable(nb_cost_mat_new.shape[1])
					cost_fun = cp.sum_squares(nb_cost_mat_new @ x - dis_k_vec)
					constraints = [x >= [0.01 for i in range(nb_cost_mat_new.shape[1])],
								   np.array([1.0, 1.0, -1.0, 0.0, 0.0, 0.0]).T@x >= 0.0,
								   np.array([0.0, 0.0, 0.0, 1.0, 1.0, -1.0]).T@x >= 0.0]
					prob = cp.Problem(cp.Minimize(cost_fun), constraints)
					self.__execute_cvx(prob)
					edit_costs_new = x.value
					residual = np.sqrt(prob.value)
				elif is_n_attr and not is_e_attr:
					nb_cost_mat_new = nb_cost_mat[:,[0,1,2,3,4]]
					x = cp.Variable(nb_cost_mat_new.shape[1])
					cost_fun = cp.sum_squares(nb_cost_mat_new @ x - dis_k_vec)
					constraints = [x >= [0.01 for i in range(nb_cost_mat_new.shape[1])],
								   np.array([1.0, 1.0, -1.0, 0.0, 0.0]).T@x >= 0.0]
					prob = cp.Problem(cp.Minimize(cost_fun), constraints)
					self.__execute_cvx(prob)
					edit_costs_new = np.concatenate((x.value, np.array([0.0])))
					residual = np.sqrt(prob.value)
				elif not is_n_attr and is_e_attr:
					nb_cost_mat_new = nb_cost_mat[:,[0,1,3,4,5]]
					x = cp.Variable(nb_cost_mat_new.shape[1])
					cost_fun = cp.sum_squares(nb_cost_mat_new @ x - dis_k_vec)
					constraints = [x >= [0.01 for i in range(nb_cost_mat_new.shape[1])],
								   np.array([0.0, 0.0, 1.0, 1.0, -1.0]).T@x >= 0.0]
					prob = cp.Problem(cp.Minimize(cost_fun), constraints)
					self.__execute_cvx(prob)
					edit_costs_new = np.concatenate((x.value[0:2], np.array([0.0]), x.value[2:]))
					residual = np.sqrt(prob.value)
				else:
					nb_cost_mat_new = nb_cost_mat[:,[0,1,3,4]]
					x = cp.Variable(nb_cost_mat_new.shape[1])
					cost_fun = cp.sum_squares(nb_cost_mat_new @ x - dis_k_vec)
					constraints = [x >= [0.01 for i in range(nb_cost_mat_new.shape[1])]]
					prob = cp.Problem(cp.Minimize(cost_fun), constraints)
					self.__execute_cvx(prob)
					edit_costs_new = np.concatenate((x.value[0:2], np.array([0.0]), 
													 x.value[2:], np.array([0.0])))
					residual = np.sqrt(prob.value)

		elif self.__ged_options['edit_cost'] == 'CONSTANT': # @todo: node/edge may not labeled.
			if not self.__triangle_rule and self.__allow_zeros:
				x = cp.Variable(nb_cost_mat.shape[1])
				cost_fun = cp.sum_squares(nb_cost_mat @ x - dis_k_vec)
				constraints = [x >= [0.0 for i in range(nb_cost_mat.shape[1])],
							   np.array([1.0, 0.0, 0.0, 0.0, 0.0, 0.0]).T@x >= 0.01,
							   np.array([0.0, 1.0, 0.0, 0.0, 0.0, 0.0]).T@x >= 0.01,
							   np.array([0.0, 0.0, 0.0, 1.0, 0.0, 0.0]).T@x >= 0.01,
							   np.array([0.0, 0.0, 0.0, 0.0, 1.0, 0.0]).T@x >= 0.01]
				prob = cp.Problem(cp.Minimize(cost_fun), constraints)
				self.__execute_cvx(prob)
				edit_costs_new = x.value
				residual = np.sqrt(prob.value)
			elif self.__triangle_rule and self.__allow_zeros:
				x = cp.Variable(nb_cost_mat.shape[1])
				cost_fun = cp.sum_squares(nb_cost_mat @ x - dis_k_vec)
				constraints = [x >= [0.0 for i in range(nb_cost_mat.shape[1])],
							   np.array([1.0, 0.0, 0.0, 0.0, 0.0, 0.0]).T@x >= 0.01,
							   np.array([0.0, 1.0, 0.0, 0.0, 0.0, 0.0]).T@x >= 0.01,
							   np.array([0.0, 0.0, 0.0, 1.0, 0.0, 0.0]).T@x >= 0.01,
							   np.array([0.0, 0.0, 0.0, 0.0, 1.0, 0.0]).T@x >= 0.01,
							   np.array([1.0, 1.0, -1.0, 0.0, 0.0, 0.0]).T@x >= 0.0,
							   np.array([0.0, 0.0, 0.0, 1.0, 1.0, -1.0]).T@x >= 0.0]
				prob = cp.Problem(cp.Minimize(cost_fun), constraints)
				self.__execute_cvx(prob)
				edit_costs_new = x.value
				residual = np.sqrt(prob.value)
			elif not self.__triangle_rule and not self.__allow_zeros:
				x = cp.Variable(nb_cost_mat.shape[1])
				cost_fun = cp.sum_squares(nb_cost_mat @ x - dis_k_vec)
				constraints = [x >= [0.01 for i in range(nb_cost_mat.shape[1])]]
				prob = cp.Problem(cp.Minimize(cost_fun), constraints)
				self.__execute_cvx(prob)
				edit_costs_new = x.value
				residual = np.sqrt(prob.value)
			elif self.__triangle_rule and not self.__allow_zeros:
				x = cp.Variable(nb_cost_mat.shape[1])
				cost_fun = cp.sum_squares(nb_cost_mat @ x - dis_k_vec)
				constraints = [x >= [0.01 for i in range(nb_cost_mat.shape[1])],
							   np.array([1.0, 1.0, -1.0, 0.0, 0.0, 0.0]).T@x >= 0.0,
							   np.array([0.0, 0.0, 0.0, 1.0, 1.0, -1.0]).T@x >= 0.0]
				prob = cp.Problem(cp.Minimize(cost_fun), constraints)
				self.__execute_cvx(prob)
				edit_costs_new = x.value
				residual = np.sqrt(prob.value)
		else:
			raise Exception('The edit cost "', self.__ged_options['edit_cost'], '" is not supported for update progress.')
	#	# method 1: simple least square method.
	#	edit_costs_new, residual, _, _ = np.linalg.lstsq(nb_cost_mat, dis_k_vec,
	#													 rcond=None)
		
	#	# method 2: least square method with x_i >= 0.
	#	edit_costs_new, residual = optimize.nnls(nb_cost_mat, dis_k_vec)
		
		# method 3: solve as a quadratic program with constraints.
	#	P = np.dot(nb_cost_mat.T, nb_cost_mat)
	#	q_T = -2 * np.dot(dis_k_vec.T, nb_cost_mat)
	#	G = -1 * np.identity(nb_cost_mat.shape[1])
	#	h = np.array([0 for i in range(nb_cost_mat.shape[1])])
	#	A = np.array([1 for i in range(nb_cost_mat.shape[1])])
	#	b = 1
	#	x = cp.Variable(nb_cost_mat.shape[1])
	#	prob = cp.Problem(cp.Minimize(cp.quad_form(x, P) + q_T@x),
	#					  [G@x <= h])
	#	prob.solve()
	#	edit_costs_new = x.value
	#	residual = prob.value - np.dot(dis_k_vec.T, dis_k_vec)
		
	#	G = -1 * np.identity(nb_cost_mat.shape[1])
	#	h = np.array([0 for i in range(nb_cost_mat.shape[1])])
			x = cp.Variable(nb_cost_mat.shape[1])
			cost_fun = cp.sum_squares(nb_cost_mat @ x - dis_k_vec)
			constraints = [x >= [0.0 for i in range(nb_cost_mat.shape[1])],
		#				   np.array([1.0, 1.0, -1.0, 0.0, 0.0]).T@x >= 0.0]
						   np.array([1.0, 1.0, -1.0, 0.0, 0.0, 0.0]).T@x >= 0.0,
						   np.array([0.0, 0.0, 0.0, 1.0, 1.0, -1.0]).T@x >= 0.0]
			prob = cp.Problem(cp.Minimize(cost_fun), constraints)
			self.__execute_cvx(prob)
			edit_costs_new = x.value
			residual = np.sqrt(prob.value)
		
		# method 4: 
		
		return edit_costs_new, residual
コード例 #51
0
 def init_objective(self):
     """ Initialize the objective function """
     objective = [500*cvxpy.sum_squares(self.s0)]
     return objective
コード例 #52
0
ファイル: prox_test.py プロジェクト: nishi951/cvxbenchmarks
def f_least_squares(m):
    A = np.random.randn(m, n)
    b = np.random.randn(m)
    return cp.sum_squares(A*x - b)
コード例 #53
0
def solve_opt_problem(A, b, t, n):
    x = cp.Variable(n)
    objective = cp.Minimize(cp.sum_squares(A @ x - b))
    prob = cp.Problem(objective)
    result = prob.solve(warm_start=True)
    return x.value
コード例 #54
0
    def compute_compensation(self):
        """
        Compute CoP and normalized leg stiffness compensation.
        """
        # Force limits
        lambda_max = max_force / (mass * height)
        lambda_min = min_force / (mass * height)
        omega_max = sqrt(lambda_max)
        omega_min = sqrt(lambda_min)

        # Measurements
        Delta_com = self.pendulum.com.p - self.ref_com
        Delta_comd = self.pendulum.com.pd - self.ref_comd
        height = dot(self.contact.normal, self.pendulum.com.p - self.contact.p)
        lambda_d = self.ref_lambda
        measured_comd = self.pendulum.com.pd
        nu_d = self.ref_vrp
        omega_d = self.ref_omega
        r_d_contact = self.ref_cop_contact
        xi_d = self.ref_dcm

        # Optimization variables
        Delta_lambda = cvxpy.Variable(1)
        Delta_nu = cvxpy.Variable(3)
        Delta_omega = cvxpy.Variable(1)
        Delta_r = cvxpy.Variable(2)
        u = cvxpy.Variable(3)

        # Linearized variation dynamics
        Delta_xi = Delta_com + Delta_comd / omega_d \
            - measured_comd / (omega_d ** 2) * Delta_omega
        Delta_omegad = 2 * omega_d * Delta_omega - Delta_lambda
        Delta_r_world = contact.R[:3, :2] * Delta_r
        r_contact = r_d_contact + Delta_r
        lambda_ = lambda_d + Delta_lambda
        omega = omega_d + Delta_omega

        # Pole placement
        Delta_xid = (Delta_lambda * (xi_d - nu_d) + lambda_d *
                     (Delta_xi - Delta_nu) + -Delta_omega * lambda_d *
                     (xi_d - nu_d) / omega_d) / omega_d

        # Kinematic DCM height constraint
        xi_z = self.ref_dcm[2] + Delta_xi[2] + 1.5 * sim.dt * Delta_xid[2]

        # Cost function
        costs = []
        sq_costs = [(1., u[0]), (1., u[1]), (1e-3, u[2])]
        for weight, expr in sq_costs:
            costs.append((weight, cvxpy.sum_squares(expr)))
        cost = sum(weight * expr for (weight, expr) in costs)

        # Quadratic program
        prob = cvxpy.Problem(
            objective=cvxpy.Minimize(cost),
            constraints=[
                Delta_xid == lambda_d / omega_d * ((1 - k_p) * Delta_xi + u),
                Delta_omegad == omega_d * (1 - k_p) * Delta_omega,
                Delta_nu == Delta_r_world +
                gravity * Delta_lambda / lambda_d**2,
                cvxpy.abs(r_contact) <= self.r_contact_max,
                lambda_ <= lambda_max, lambda_ >= lambda_min,
                xi_z <= max_dcm_height, xi_z >= min_dcm_height,
                omega <= omega_max, omega >= omega_min
            ])
        prob.solve()

        # Read outputs from solution
        Delta_lambda_opt = Delta_lambda.value
        Delta_r_opt = array(Delta_r.value).reshape((2, ))
        self.omega = omega_d + Delta_omega.value
        self.dcm = self.pendulum.com.p \
            + self.pendulum.com.pd / self.omega
        return (Delta_r_opt, Delta_lambda_opt)
コード例 #55
0
    def minimize(self):

        if self.verbose:
            print('iter\t cost\t\t dnorm', end='')
            if self.f.f_star() < np.inf:
                print('\t\t gap', end='')

        # compute first function and subgradient
        self.f_x, self.g_x = self.f.function(self.x), self.f.jacobian(self.x)

        ng = np.linalg.norm(self.g_x)
        if self.eps < 0:
            ng0 = -ng  # norm of first subgradient
        else:
            ng0 = 1  # un-scaled stopping criterion

        G = self.g_x  # matrix of subgradients

        F = self.f_x - self.g_x.dot(
            self.x)  # vector of translated function values

        while True:

            # construct the master problem
            d = Variable(self.x.size)
            v = Variable(1)

            # each (fxi , gi , xi) gives the constraint:
            #
            #  v >= fxi + gi^T * (x + d - xi) = gi^T * (x + d) + (fi - gi^T * xi)
            #
            # so we just keep the single constant fi - gi^T * xi instead of xi
            M = [v >= F + G @ (self.x + d)]

            if self.f.f_star() < np.inf:
                # cheating: use information about f_star in the model
                M += [v >= self.f.f_star()]

            # objective function
            c = v + self.mu * sum_squares(d) / 2

            if self.is_verbose() and self.master_verbose:
                print('\n')

            # solve the master problem
            Problem(Minimize(c), M).solve(solver=self.master_solver.upper(),
                                          verbose=self.is_verbose()
                                          and self.master_verbose)

            try:
                d = -d.value.ravel()
            except AttributeError:
                self.status = 'error'
                break

            v = v.value.item()

            self.nd = np.linalg.norm(d)

            # output statistics
            if self.is_verbose():
                print('\n{:4d}\t{: 1.4e}\t{: 1.4e}'.format(
                    self.iter, self.f_x, self.nd),
                      end='')
                if self.f.f_star() < np.inf:
                    print('\t{: 1.4e}'.format((self.f_x - self.f.f_star()) /
                                              max(abs(self.f.f_star()), 1)),
                          end='')

            # stopping criteria
            if self.mu * self.nd <= self.eps * ng0:
                self.status = 'optimal'
                break

            if self.iter >= self.max_iter:
                self.status = 'stopped'
                break

            last_x = self.x - d

            # compute function and subgradient
            fd, self.g_x = self.f.function(last_x), self.f.jacobian(last_x)

            try:
                self.callback()
            except StopIteration:
                break

            if fd <= self.m_inf:
                self.status = 'unbounded'
                break

            G = np.vstack((G, self.g_x))

            F = np.hstack((F, fd - self.g_x.dot(last_x)))

            if fd <= self.f_x + self.m1 * (v - self.f_x):  # SS: serious step

                self.x = last_x
                self.f_x = fd

                try:
                    self.check_lagrangian_dual_optimality()
                except StopIteration:
                    break

            else:  # NS: null step

                if self.f.ndim <= 3:
                    self.x0_history_ns.append(self.x[0])
                    self.x1_history_ns.append(self.x[1])
                    self.f_x_history_ns.append(self.f_x)

            self.iter += 1

        self.check_lagrangian_dual_conditions()

        if self.verbose:
            print('\n')

        return self
コード例 #56
0
ファイル: prox_test.py プロジェクト: nishi951/cvxbenchmarks
    epigraph("NORM_1", None, lambda: [cp.norm1(x) <= t]),
    epigraph("NORM_NUCLEAR", None, lambda: [cp.norm(X, "nuc") <= t]),
    epigraph("SUM_DEADZONE", None, lambda: [f_dead_zone() <= t]),
    epigraph("SUM_EXP", None, lambda: [cp.sum_entries(cp.exp(x)) <= t]),
    epigraph("SUM_HINGE", None, lambda: [f_hinge() <= t]),
    epigraph("SUM_HINGE", None, lambda: [f_hinge_axis0() <= t_rvec]),
    epigraph("SUM_HINGE", None, lambda: [f_hinge_axis1() <= t_vec]),
    epigraph("SUM_INV_POS", None, lambda: [cp.sum_entries(cp.inv_pos(x)) <= t]),
    epigraph("SUM_KL_DIV", None, lambda: [cp.sum_entries(cp.kl_div(p1,q1)) <= t]),
    epigraph("SUM_LARGEST", None, lambda: [cp.sum_largest(x, 4) <= t]),
    epigraph("SUM_LOGISTIC", None, lambda: [cp.sum_entries(cp.logistic(x)) <= t]),
    epigraph("SUM_NEG_ENTR", None, lambda: [cp.sum_entries(-cp.entr(x)) <= t]),
    epigraph("SUM_NEG_LOG", None, lambda: [cp.sum_entries(-cp.log(x)) <= t]),
    epigraph("SUM_QUANTILE", None, lambda: [f_quantile() <= t]),
    #epigraph("SUM_SQUARE", None, lambda: [f_quad_form() <= t]),
    epigraph("SUM_SQUARE", None, lambda: [cp.sum_squares(x) <= t]),
]

def run_prox(prox_function_type, prob, v_map, lam=1, epigraph=False):
    eval_prox_impl(prob, v_map, lam, prox_function_type, epigraph)
    actual = {x: x.value for x in prob.variables()}

    # Compare to solution with cvxpy
    prob.objective.args[0] *= lam
    prob.objective.args[0] += sum(
        0.5*cp.sum_squares(x - v_map[x]) for x, v in v_map.items())
    try:
        prob.solve()
    except cp.SolverError as e:
        # If CVXPY fails with default, try again with SCS
        prob.solve(solver=cp.SCS)
コード例 #57
0
#B0_comb_m = np.matrix.transpose(np.matrix(B0_comb))

b_mul = np.matrix(A_comb) * np.matrix.transpose(np.matrix(x_comb))
b_mul = np.sum(b_mul, axis = 1)

b_dot = np.dot(A_comb, x_comb)




gamma = 0.0001
theta = 1
    
Ax = np.matrix(A_comb) * x

cost = cp.sum_squares(Ax - b_comb) + gamma*cp.norm(x, 1)

obj = cp.Minimize(cost)

constr = [x >= -0.7,x <= 0.01]

prob = cp.Problem(obj, constr)

opt_val = prob.solve()

solution = x.value
    
    


plt.figure()
コード例 #58
0
    def test_readme_examples(self) -> None:
        import numpy
        numpy.random.seed(1)
        # cvx.Problem data.
        m = 30
        n = 20
        A = numpy.random.randn(m, n)
        b = numpy.random.randn(m)

        # Construct the problem.
        x = cvx.Variable(n)
        objective = cvx.Minimize(cvx.sum_squares(A @ x - b))
        constraints = [0 <= x, x <= 1]
        p = cvx.Problem(objective, constraints)

        # The optimal objective is returned by p.solve().
        p.solve()
        # The optimal value for x is stored in x.value.
        print(x.value)
        # The optimal Lagrange multiplier for a constraint
        # is stored in constraint.dual_value.
        print(constraints[0].dual_value)

        ####################################################

        # Scalar variable.
        a = cvx.Variable()

        # Column vector variable of length 5.
        x = cvx.Variable(5)

        # Matrix variable with 4 rows and 7 columns.
        A = cvx.Variable((4, 7))

        ####################################################

        # Positive scalar parameter.
        m = cvx.Parameter(nonneg=True)

        # Column vector parameter with unknown sign (by default).
        cvx.Parameter(5)

        # Matrix parameter with negative entries.
        G = cvx.Parameter((4, 7), nonpos=True)

        # Assigns a constant value to G.
        G.value = -numpy.ones((4, 7))

        # Raises an error for assigning a value with invalid sign.
        with self.assertRaises(Exception) as cm:
            G.value = numpy.ones((4, 7))
        self.assertEqual(str(cm.exception),
                         "Parameter value must be nonpositive.")

        ####################################################
        a = cvx.Variable()
        x = cvx.Variable(5)

        # expr is an Expression object after each assignment.
        expr = 2 * x
        expr = expr - a
        expr = cvx.sum(expr) + cvx.norm(x, 2)

        ####################################################

        import numpy as np

        # cvx.Problem data.
        n = 10
        m = 5
        A = np.random.randn(n, m)
        b = np.random.randn(n)
        gamma = cvx.Parameter(nonneg=True)

        # Construct the problem.
        x = cvx.Variable(m)
        objective = cvx.Minimize(
            cvx.sum_squares(A @ x - b) + gamma * cvx.norm(x, 1))
        p = cvx.Problem(objective)

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

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

        ####################################################
        n = 10

        mu = np.random.randn(1, n)
        sigma = np.random.randn(n, n)
        sigma = sigma.T.dot(sigma)
        gamma = cvx.Parameter(nonneg=True)
        gamma.value = 1
        x = cvx.Variable(n)

        # Constants:
        # mu is the vector of expected returns.
        # sigma is the covariance matrix.
        # gamma is a cvx.Parameter that trades off risk and return.

        # cvx.Variables:
        # x is a vector of stock holdings as fractions of total assets.

        expected_return = mu @ x
        risk = cvx.quad_form(x, sigma)

        objective = cvx.Maximize(expected_return - gamma * risk)
        p = cvx.Problem(objective, [cvx.sum(x) == 1])
        p.solve()

        # The optimal expected return.
        print(expected_return.value)

        # The optimal risk.
        print(risk.value)

        ###########################################

        N = 50
        M = 40
        n = 10
        data = []
        for i in range(N):
            data += [(1, np.random.normal(loc=1.0, scale=2.0, size=n))]
        for i in range(M):
            data += [(-1, np.random.normal(loc=-1.0, scale=2.0, size=n))]

        # Construct problem.
        gamma = cvx.Parameter(nonneg=True)
        gamma.value = 0.1
        # 'a' is a variable constrained to have at most 6 non-zero entries.
        a = cvx.Variable(n)  # mi.SparseVar(n, nonzeros=6)
        b = cvx.Variable()

        slack = [
            cvx.pos(1 - label * (sample.T @ a - b)) for (label, sample) in data
        ]
        objective = cvx.Minimize(cvx.norm(a, 2) + gamma * sum(slack))
        p = cvx.Problem(objective)
        # Extensions can attach new solve methods to the CVXPY cvx.Problem class.
        # p.solve(method="admm")
        p.solve()

        # Count misclassifications.
        errors = 0
        for label, sample in data:
            if label * (sample.T @ a - b).value < 0:
                errors += 1

        print("%s misclassifications" % errors)
        print(a.value)
        print(b.value)
コード例 #59
0
noise = np.loadtxt("noise.csv", delimiter=",")
y = x @ c_true + 0.1 * (math.sqrt(n)) * noise

# # Reorder measurements, then censor
sort_ind = np.argsort(y)
y = y[sort_ind]
x = x[sort_ind]
D = (y[M] + y[M + 1]) / 2
y = y[:M]
print(y.shape)
print(x.shape)

c_hat = cp.Variable(n)
y_censored = cp.Variable(K - M)
Y = cp.hstack((y, y_censored))
objective = cp.Minimize(cp.sum_squares(x @ c_hat - Y))
constraints = [y_censored >= D]
prob = cp.Problem(objective, constraints)
prob.solve()

print(c_hat.value)
# print(y_censored.value)

residual = np.linalg.norm(c_true - c_hat.value) / np.linalg.norm(c_true)
print('residual for c_hat', residual)

print(
    '#########################################################################'
)

c_hat = cp.Variable(n)
コード例 #60
-1
ファイル: tps.py プロジェクト: ttblue/tps_normals
def tps_fit3_cvx(x_na, y_ng, bend_coef, rot_coef, wt_n):
    """
    Use cvx instead of just matrix multiply.
    Working with null space of matrices.
    """

    if wt_n is None: wt_n = co.matrix(np.ones(len(x_na)))
    n,d = x_na.shape
    K_nn = tps_kernel_matrix(x_na)
    _,_,VT = nlg.svd(np.c_[x_na,np.ones((x_na.shape[0],1))].T)
    Nmat = VT.T[:,d+1:]
    rot_coefs = np.diag(np.ones(d) * rot_coef if np.isscalar(rot_coef) else rot_coef)
    
    
    A = cp.Variable(Nmat.shape[1],d)
    B = cp.Variable(d,d)
    c = cp.Variable(d,1)
    
    Y = co.matrix(y_ng)
    K = co.matrix(K_nn)
    N = co.matrix(Nmat)
    X = co.matrix(x_na)
    W = co.matrix(np.diag(wt_n).copy())
    R = co.matrix(rot_coefs)
    ones = co.matrix(np.ones((n,1)))
    
    constraints = []
    
    # For correspondences
    V1 = cp.Variable(n,d)
    constraints.append(V1 == Y-K*N*A-X*B - ones*c.T)
    V2 = cp.Variable(n,d)
    constraints.append(V2 == cp.sqrt(W)*V1)
    # For bending cost
    Q = [] # for quadratic forms
    for i in range(d):
        Q.append(cp.quad_form(A[:,i], N.T*K*N))
    # For rotation cost
    # Element wise square root actually works here as R is diagonal and positive
    V3 = cp.Variable(d,d)
    constraints.append(V3 == cp.sqrt(R)*B)
    
    # Orthogonality constraints for bending are taken care of already because working with the null space
    #constraints.extend([X.T*A == 0, ones.T*A == 0])
    
    # TPS objective
    objective = cp.Minimize(cp.sum_squares(V2) + bend_coef*sum(Q) + cp.sum_squares(V3))
    p = cp.Problem(objective, constraints)
    p.solve(verbose=True)
    
    return np.array(B.value), np.squeeze(np.array(c.value)) , Nmat.dot(np.array(A.value))