Solving linear program with matrices in CVXPY - python

I'm using CVXPY in Python 3 to try to model the following linear program in X (N by T matrix). Let
R be an N by 1 matrix where each row is the sum of the entire row of values in X.
P be a 1 by N matrix defined in terms of X such as P_t = 1/(G-d-x_t).
I want to solve for an ideal such that:
minimize (X x P)
subject to:
The sum of reach row i in X has to be at least the value in R_i
Each value in X has to be at least 0
Any thoughts? I have the following code and not getting any luck:
from cvxpy import *
X = Variable(N,T)
P = np.random.randn(T, 1)
R = cumsum(X,axis=0) # using cumsum because
http://www.cvxpy.org/en/latest/tutorial/functions/index.html#vector-matrix-functions
objective = Minimize(sum_entries(square(X*P))) #think this is good
constraints = [0 <= X, cumsum(X,axis=0) >= R]
prob = Problem(objective, constraints)

Related

cvxpy - cannot solve a weighted SDP problem

I want to test a a weighted SDP problem in cvxpy, namely min_{X > 0} | P * X - B |^2, where * is the Hadamard product. I started with the simple case with P being the identity matrix. This is the code I wrote for the problem.
import cvxpy as cp
import numpy as np
import random
##parameters
random.seed(1)
n = 50
CN = 10
#generate real A
D = np.diag(1000 * np.logspace(-np.log(CN), 0, n))
Q, R = np.linalg.qr(np.random.normal(size = (n,n)))
A = Q # D # Q
P = np.ones((n,n))
PA = np.multiply(P, A)
X = cp.Variable((n,n), symmetric=True)
X.value = np.identity(n)
constraints = [X >> 0]
prob = cp.Problem(cp.Minimize(0.5 * cp.norm(P # A - PA, 'fro')),
constraints)
prob.solve()
X.value
I used # in place of * because matrix multiplication with identity is equal to that with Hardamard product ( I also can't find the atom function for element-wise multiplication in cvxpy; if someone knows it, it would be really helpful if you can inform me).
After testing out, I consistently get the optimal value for X as the zero matrix. I think there must be some error in my implementation but I cannot tell which is wrong. Any help will be much appreciated.

Does anyone know how to do convex optimization in Python with CVXOPT module?

I want to maximize Ax = b where A is an m-by-n matrix and x is an n-vector. The constraints on x are that its entries sum to 1 and that A x >= 0.
Using CVXPY instead:
from cvxpy import *
import numpy as np
m = 30
n = 10
# generate random data
np.random.seed(1)
A = np.random.randn(m,n)
b = np.random.randn(m)
# optimization variable
x = Variable(n)
# build optimization problem
prob = Problem( Maximize(sum(A*x)), [ sum(x) == 1, A*x >= 0 ])
# solve optimization problem and prints results
result = prob.solve()
print x.value
This optimization problem is unbounded and, thus, there is no optimal solution.

L1 convex optimization with equality constraints in python

I need to minimize L_1(x) subject to Mx = y.
x is a vector with dimension b, y is a vector with dimension a, and M is a matrix with dimensions (a,b).
After some reading I determined to use scipy.optimize.minimize:
import numpy as np
from scipy.optimize import minimize
def objective(x): #L_1 norm objective function
return np.linalg.norm(x,ord=1)
constraints = [] #list of all constraint functions
for i in range(a):
def con(x,y=y,i=i):
return np.matmul(M[i],x)-y[i]
constraints.append(con)
#make constraints into tuple of dicts as required by scipy
cons = tuple({'type':'eq','fun':c} for c in constraints)
#perform the minimization with sequential least squares programming
opt = minimize(objective,x0 = x0,
constraints=cons,method='SLSQP',options={'disp': True})
First,
what can I use for x0? x is unknown, and I need an x0 which satisfies the constraint M*x0 = y: How can I find an initial guess which satisfies the constraint? M is a matrix of independent Gaussian variables (~N(0,1)) if that helps.
Second,
Is there a problem with the way I've set this up? When I use the true x (which I happen to know in the development phase) for x0, I expect it to return x = x0 quickly. Instead, it returns a zero vector x = [0,0,0...,0]. This behavior is unexpected.
Edit:
Here is a solution using cvxpy** solving min(L_1(x)) subject to Mx=y:
import cvxpy as cvx
x = cvx.Variable(b) #b is dim x
objective = cvx.Minimize(cvx.norm(x,1)) #L_1 norm objective function
constraints = [M*x == y] #y is dim a and M is dim a by b
prob = cvx.Problem(objective,constraints)
result = prob.solve(verbose=False)
#then clean up and chop the 1e-12 vals out of the solution
x = np.array(x.value) #extract array from variable
x = np.array([a for b in x for a in b]) #unpack the extra brackets
x[np.abs(x)<1e-9]=0 #chop small numbers to 0

Solving CVXPY Matrix Optimization Linear Programming

I'm trying to solve for the ideal matrix X in the following linear program setup:
X = N by T matrix which is our variable. For simplicity, let's set N to 4 and T to 3.
X_column_sum = 1 by T matrix where each column value is the sum of all values of the corresponding column in X
R = N by 1 matrix with randomly determined values
G = constant (let's set to 100 for simplicity)
d = 1 by T matrix whose values take in the range [0, G-1]
P = 1 by T matrix equal to X_column_sum + d
C = X dotted with P
I want to minimize the sum of the entries of C, while preserving the following constraints:
all values in X have to be >= 0
the sum of all values in each corresponding row of X have to be at least equal to the corresponding value in R
I tried the following code using cvxpy in python, but to no avail:
from cvxpy import *
X = Variable(N,T)
G = 100
d = np.random.randn(1, T)
d *= G-1
X_column_sum = cumsum(X,axis=0)
P = cost_matrix_cars + d
R = matrix([[10]]*N) # all set to 10 for testing
objective = Minimize(sum_entries(X#P)) #think this is good
constraints = [0 <= X, cumsum(X,axis=0) >= R]
prob = Problem(objective, constraints)
print("Optimal value", prob.solve())
print("Optimal X is",x.value ) # A numpy matrix.

How to create an array that can be accessed according to its indices in Numpy?

I am trying to solve the following problem via a Finite Difference Approximation in Python using NumPy:
$u_t = k \, u_{xx}$, on $0 < x < L$ and $t > 0$;
$u(0,t) = u(L,t) = 0$;
$u(x,0) = f(x)$.
I take $u(x,0) = f(x) = x^2$ for my problem.
Programming is not my forte so I need help with the implementation of my code. Here is my code (I'm sorry it is a bit messy, but not too bad I hope):
## This program is to implement a Finite Difference method approximation
## to solve the Heat Equation, u_t = k * u_xx,
## in 1D w/out sources & on a finite interval 0 < x < L. The PDE
## is subject to B.C: u(0,t) = u(L,t) = 0,
## and the I.C: u(x,0) = f(x).
import numpy as np
import matplotlib.pyplot as plt
# definition of initial condition function
def f(x):
return x^2
# parameters
L = 1
T = 10
N = 10
M = 100
s = 0.25
# uniform mesh
x_init = 0
x_end = L
dx = float(x_end - x_init) / N
#x = np.zeros(N+1)
x = np.arange(x_init, x_end, dx)
x[0] = x_init
# time discretization
t_init = 0
t_end = T
dt = float(t_end - t_init) / M
#t = np.zeros(M+1)
t = np.arange(t_init, t_end, dt)
t[0] = t_init
# Boundary Conditions
for m in xrange(0, M):
t[m] = m * dt
# Initial Conditions
for j in xrange(0, N):
x[j] = j * dx
# definition of solution to u_t = k * u_xx
u = np.zeros((N+1, M+1)) # NxM array to store values of the solution
# finite difference scheme
for j in xrange(0, N-1):
u[j][0] = x**2 #initial condition
for m in xrange(0, M):
for j in xrange(1, N-1):
if j == 1:
u[j-1][m] = 0 # Boundary condition
else:
u[j][m+1] = u[j][m] + s * ( u[j+1][m] - #FDM scheme
2 * u[j][m] + u[j-1][m] )
else:
if j == N-1:
u[j+1][m] = 0 # Boundary Condition
print u, t, x
#plt.plot(t, u)
#plt.show()
So the first issue I am having is I am trying to create an array/matrix to store values for the solution. I wanted it to be an NxM matrix, but in my code I made the matrix (N+1)x(M+1) because I kept getting an error that the index was going out of bounds. Anyways how can I make such a matrix using numpy.array so as not to needlessly take up memory by creating a (N+1)x(M+1) matrix filled with zeros?
Second, how can I "access" such an array? The real solution u(x,t) is approximated by u(x[j], t[m]) were j is the jth spatial value, and m is the mth time value. The finite difference scheme is given by:
u(x[j],t[m+1]) = u(x[j],t[m]) + s * ( u(x[j+1],t[m]) - 2 * u(x[j],t[m]) + u(x[j-1],t[m]) )
(See here for the formulation)
I want to be able to implement the Initial Condition u(x[j],t[0]) = x**2 for all values of j = 0,...,N-1. I also need to implement Boundary Conditions u(x[0],t[m]) = 0 = u(x[N],t[m]) for all values of t = 0,...,M. Is the nested loop I created the best way to do this? Originally I tried implementing the I.C. and B.C. under two different for loops which I used to calculate values of the matrices x and t (in my code I still have comments placed where I tried to do this)
I think I am just not using the right notation but I cannot find anywhere in the documentation for NumPy how to "call" such an array so at to iterate through each value in the proposed scheme. Can anyone shed some light on what I am doing wrong?
Any help is very greatly appreciated. This is not homework but rather to understand how to program FDM for Heat Equation because later I will use similar methods to solve the Black-Scholes PDE.
EDIT: So when I run my code on line 60 (the last "else" that I use) I get an error that says invalid syntax, and on line 51 (u[j][0] = x**2 #initial condition) I get an error that reads "setting an array element with a sequence." What does that mean?

Categories