from cvxpy import * # Create two scalar variables. x = Variable() y = Variable() # Create two constraints. constraints = [x + y == 1, x - y >= 1] # Form objective. obj = Minimize(square(x - y)) # Form and solve problem. prob = 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 import cvxpy as cvx # 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 # Replace the objective. prob.objective = Maximize(x + y) print "optimal value", prob.solve() # Replace the constraint (x + y == 1). prob.constraints[0] = (x + y <= 3) print "optimal value", prob.solve() from cvxpy import * x = Variable() # An infeasible problem. prob = Problem(Minimize(x), [x >= 1, x <= 0]) prob.solve() print "status:", prob.status print "optimal value", prob.value # An unbounded problem. prob = Problem(Minimize(x)) prob.solve() print "status:", prob.status print "optimal value", prob.value # A scalar variable. a = Variable() # Column vector variable of length 5. x = Variable(5) # Matrix variable with 4 rows and 7 columns. A = Variable(4, 7) # Solves a bounded least-squares problem. from cvxpy import * import numpy # 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 = Variable(n) objective = Minimize(sum(square(A*x - b))) constraints = [0 <= x, x <= 1] prob = Problem(objective, constraints) print "Optimal value", prob.solve() print "Optimal var" print x.value # A numpy matrix. # Positive scalar parameter. m = Parameter(nonneg=True) # Column vector parameter with unknown sign (by default). c = Parameter(5) # Matrix parameter with negative entries. G = Parameter(4, 7, nonpos=True) # Assigns a constant value to G. G.value = -numpy.ones((4, 7)) from cvxpy import * import numpy import matplotlib.pyplot as plt # 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 = Parameter(nonneg=True) # Construct the problem. x = Variable(m) sum_of_squares = sum(square(A*x - b)) obj = Minimize(sum_of_squares + gamma*norm(x, 1)) prob = 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(sum_of_squares.value) l1_penalty.append(norm(x, 1).value) x_values.append(x.value) %matplotlib inline plt.rc('text', usetex=True) plt.rc('font', family='serif') plt.figure(figsize=(6,10)) # Plot trade-off curve. plt.subplot(211) plt.plot(l1_penalty, sq_penalty) plt.xlabel(r'\|x\|_1', fontsize=16) plt.ylabel(r'\|Ax-b\|^2', fontsize=16) plt.title('Trade-Off Curve for LASSO', fontsize=16) # Plot entries of x vs. gamma. plt.subplot(212) for i in range(m): plt.plot(gamma_vals, [xi[i,0] for xi in x_values]) plt.xlabel(r'\gamma', fontsize=16) plt.ylabel(r'x_{i}', fontsize=16) plt.xscale('log') plt.title(r'\text{Entries of x vs. }\gamma', fontsize=16) plt.tight_layout() plt.show() from multiprocessing import Pool # Assign a value to gamma and find the optimal x. def get_x(gamma_value): gamma.value = gamma_value result = prob.solve() return x.value # Parallel computation (set to 1 process here). pool = Pool(processes = 1) x_values = pool.map(get_x, gamma_vals)