python - CVX to cvxpy and cvxopt -
i've been trying pass code matlab python. have same convex optimization problem working on matlab i'm having problems passing either cvxpy
or cvxopt
.
n = 1000; = 20; y = rand(n,1); = rand(n,i); cvx_begin variable x(n); variable lambda(i); minimize(sum_square(x-y)); subject x == a*lambda; lambda >= zeros(i,1); lambda'*ones(i,1) == 1; cvx_end
this tried python , cvxpy.
import numpy np cvxpy import * # problem data. n = 100 = 20 np.random.seed(1) y = np.random.randn(n) = np.random.randn(n, i) # construct problem. x = variable(n) lmbd = variable(i) objective = minimize(sum_squares(x - y)) constraints = [x == np.dot(a, lmbd), lmbd <= np.zeros(itr), np.sum(lmbd) == 1] prob = problem(objective, constraints) print("status:", prob.status) print("optimal value", prob.value)
nonetheless, it's not working. of have idea how make work? i'm pretty sure problem in constraints. , nice have cvxopt
.
i think got it, had 1 of constraints wrong =), added random seed number in order compare results , check in fact same in both languages. leave data here maybe useful someday ;)
matlab
rand('twister', 0); n = 100; = 20; y = rand(n,1); = rand(n,i); cvx_begin variable x(n); variable lmbd(i); minimize(sum_square(x-y)); subject x == a*lmbd; lmbd >= zeros(i,1); lmbd'*ones(i,1) == 1; cvx_end
cvxpy
import numpy np cvxpy import * # random seed np.random.seed(0) # problem data. n = 100 = 20 y = np.random.rand(n) # = np.random.rand(n, i) # normal = np.random.rand(i, n).t # in order test random numbers # construct problem. x = variable(n) lmbd = variable(i) objective = minimize(sum_squares(x - y)) constraints = [x == a*lmbd, lmbd >= np.zeros(i), sum_entries(lmbd) == 1] prob = problem(objective, constraints) result = prob.solve(verbose=true)
cvxopt pending.....
Comments
Post a Comment