Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_l1(self):
setseed(100)
m,n = 500,250
P = normal(m,n)
q = normal(m,1)
u1,st1 = l1(P,q)
u2,st2 = l1blas(P,q)
self.assertTrue(st1 == 'optimal')
self.assertTrue(st2 == 'optimal')
self.assertAlmostEqualLists(list(u1),list(u2),places=3)
def test_case3(self):
m, n = 500, 100
setseed(100)
A = normal(m,n)
b = normal(m)
x1 = variable(n)
lp1 = op(max(abs(A*x1-b)))
lp1.solve()
self.assertTrue(lp1.status == 'optimal')
x2 = variable(n)
lp2 = op(sum(abs(A*x2-b)))
lp2.solve()
self.assertTrue(lp2.status == 'optimal')
x3 = variable(n)
lp3 = op(sum(max(0, abs(A*x3-b)-0.75, 2*abs(A*x3-b)-2.25)))
lp3.solve()
self.assertTrue(lp3.status == 'optimal')
# The norm and penalty approximation problems of section 10.5 (Examples).
from cvxopt import normal, setseed
from cvxopt.modeling import variable, op, max, sum
setseed(0)
m, n = 500, 100
A = normal(m,n)
b = normal(m)
x1 = variable(n)
prob1=op(max(abs(A*x1+b)))
prob1.solve()
x2 = variable(n)
prob2=op(sum(abs(A*x2+b)))
prob2.solve()
x3 = variable(n)
prob3=op(sum(max(0, abs(A*x3+b)-0.75, 2*abs(A*x3+b)-2.25)))
prob3.solve()
try: import pylab
except ImportError: pass
Q = o.matrix(cholesky(Q))
R = o.matrix(cholesky(R))
#Q = o.spdiag(o.matrix(npy.ones(n))) #
#R = o.spdiag(o.matrix(npy.ones(m))) #
A = o.normal(n,n)
s = max(abs(eigvals(A)))
#A = o.spdiag(o.matrix(npy.ones(n))) #A/s
A = A/s
B = o.normal(n,m)
q = max(abs(svdvals(B)))
#B = o.matrix(npy.ones((n,m)))#1.1*B/q
B = 1.1*B/q
#xinit = o.matrix(npy.ones(n)) #5*o.normal(n,1)
xinit = 5*o.normal(n,1)
# TODO: add "/" for constants or something...
p = Scoop()
problem = ["parameter Q matrix",
"parameter R matrix",
"parameter A matrix",
"parameter B matrix",
"parameter xinit vector"]
data = {'Q': Q, 'R':R, 'A':A, 'B':B, 'xinit':xinit}
for i in range(T):
problem += ["variable x%i vector" % i,
"variable u%i vector" % i]
data['x%i' % i] = n
data['u%i' % i] = m
m = 2 # inputs
T = 20 # horizon
o.setseed(2)
Q = o.normal(n,n)
Q = Q.trans()*Q
R = o.normal(m,m)
R = R.trans()*R + o.spdiag(o.matrix(npy.ones(m)))
Q = o.matrix(cholesky(Q))
R = o.matrix(cholesky(R))
#Q = o.spdiag(o.matrix(npy.ones(n))) #
#R = o.spdiag(o.matrix(npy.ones(m))) #
A = o.normal(n,n)
s = max(abs(eigvals(A)))
#A = o.spdiag(o.matrix(npy.ones(n))) #A/s
A = A/s
B = o.normal(n,m)
q = max(abs(svdvals(B)))
#B = o.matrix(npy.ones((n,m)))#1.1*B/q
B = 1.1*B/q
#xinit = o.matrix(npy.ones(n)) #5*o.normal(n,1)
xinit = 5*o.normal(n,1)
# TODO: add "/" for constants or something...
p = Scoop()
problem = ["parameter Q matrix",
"parameter R matrix",
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
from cvxpy import Variable, Problem, Minimize, log
import cvxopt
cvxopt.solvers.options['show_progress'] = False
# create problem data
m, n = 5, 10
A = cvxopt.normal(m,n)
tmp = cvxopt.uniform(n,1)
b = A*tmp
x = Variable(n)
p = Problem(
Minimize(-sum(log(x))),
[A*x == b]
)
status = p.solve()
cvxpy_x = x.value
def acent(A, b):
m, n = A.size
def F(x=None, z=None):
if x is None: return 0, cvxopt.matrix(1.0, (n,1))
lapack.syevx(+w, lmbda, range='I', il=1, iu=1)
x0 = matrix(-lmbda[0]+1.0, (n,1))
s0 = +w
s0[::n+1] += x0
# Initial feasible z is identity.
z0 = matrix(0.0, (n,n))
z0[::n+1] = 1.0
dims = {'l': 0, 'q': [], 's': [n]}
sol = solvers.conelp(c, Fs, w[:], dims, kktsolver = Fkkt,
primalstart = {'x': x0, 's': s0[:]}, dualstart = {'z': z0[:]})
return sol['x'], matrix(sol['z'], (n,n))
n = 100
w = normal(n,n)
x, z = mcsdp(w)
# Solved a QCQP with 3 inequalities:
# minimize 1/2 x'*P0*x + q0'*r + r0
# s.t. 1/2 x'*Pi*x + qi'*r + ri <= 0 for i=1,2,3
# and verifies that strong duality holds.
# Input data
n = 6
eps = sys.float_info.epsilon
P0 = cvxopt.normal(n, n)
eye = cvxopt.spmatrix(1.0, range(n), range(n))
P0 = P0.T * P0 + eps * eye
print(P0)
P1 = cvxopt.normal(n, n)
P1 = P1.T*P1
P2 = cvxopt.normal(n, n)
P2 = P2.T*P2
P3 = cvxopt.normal(n, n)
P3 = P3.T*P3
q0 = cvxopt.normal(n, 1)
q1 = cvxopt.normal(n, 1)
q2 = cvxopt.normal(n, 1)
q3 = cvxopt.normal(n, 1)
r0 = cvxopt.normal(1, 1)
r1 = cvxopt.normal(1, 1)
r2 = cvxopt.normal(1, 1)
r3 = cvxopt.normal(1, 1)
# Section 6.1.2: Residual minimization with deadzone penalty
# Ported from cvx matlab to cvxpy by Misrab Faizullah-Khan
# Original comments below
# Boyd & Vandenberghe "Convex Optimization"
# Joelle Skaf - 08/17/05
#
# The penalty function approximation problem has the form:
# minimize sum(deadzone(Ax - b))
# where 'deadzone' is the deadzone penalty function
# deadzone(y) = max(abs(y)-1,0)
# Input data
m = 16
n = 8
A = cvxopt.normal(m,n)
b = cvxopt.normal(m,1)
# Formulate the problem
x = Variable(n)
objective = Minimize( sum(maximum( abs(A*x -b) - 1 , 0 )) )
p = Problem(objective, [])
# Solve it
print ('Computing the optimal solution of the deadzone approximation problem:')
p.solve()
print ('Optimal vector:')
print (x.value)
print ('Residual vector:')
print (A*x.value - b)
# Figures 6.11-14, pages 315-317.
# Total variation reconstruction.
from math import pi
from cvxopt import blas, lapack, solvers
from cvxopt import matrix, spmatrix, sin, mul, div, normal
#solvers.options['show_progress'] = 0
try: import pylab
except ImportError: pylab_installed = False
else: pylab_installed = True
n = 2000
t = matrix( list(range(n)), tc='d' )
ex = matrix( n//4*[1.0] + n//4*[-1.0] + n//4*[1.0] + n//4*[-1.0] ) + \
0.5 * sin( 2.0*pi/n * t )
corr = ex + 0.1 * normal(n,1)
if pylab_installed:
pylab.figure(1, facecolor='w', figsize=(8,5))
pylab.subplot(211)
pylab.plot(t, ex)
pylab.ylabel('x[i]')
pylab.xlabel('i')
pylab.axis([0, 2000, -2, 2])
pylab.title('Original and corrupted signal (fig. 6.11)')
pylab.subplot(212)
pylab.plot(t, corr)
pylab.ylabel('xcor[i]')
pylab.xlabel('i')
pylab.axis([0, 2000, -2, 2])