-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsdp.py
More file actions
67 lines (55 loc) · 1.9 KB
/
sdp.py
File metadata and controls
67 lines (55 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
Copyright 2013 Steven Diamond
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
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.
"""
"""
This script finds a PSD matrix that is closest to a given symmetric,
real matrix, as measured by the Frobenius norm. That is, for
a given matrix P, it solves:
minimize || Z - P ||_F
subject to Z >= 0
Adapted from an example provided in the SeDuMi documentation and CVX examples.
Unlike those examples, the data is real (not complex) and the result is only
required to be PSD (instead of also Toeplitz)
"""
# import cvxpy as cvx
# import numpy as np
# import cvxopt
#
# # create data P
# P = cvxopt.matrix(np.matrix('4 1 3; 1 3.5 0.8; 3 0.8 1'))
# Z = cvx.Variable(3,3)
# objective = cvx.Minimize( sum(cvx.square(P - Z)) )
# constr = [cvx.constraints.semi_definite.SDP(P)]
# prob = cvx.Problem(objective, constr)
# prob.solve()
import numpy as np
import cvxpy as cp
# create data P
P = cp.Parameter((3,3))
Z = cp.Variable((3,3), PSD=True)
objective = cp.Minimize( cp.lambda_max(P) - cp.lambda_min(P - Z) )
prob = cp.Problem(objective, [Z >= 0])
P.value = np.matrix('4 1 3; 1 3.5 0.8; 3 0.8 1')
prob.solve()
print("optimal value =", prob.value)
# [ 4, 1+2*j, 3-j ; ...
# 1-2*j, 3.5, 0.8+2.3*j ; ...
# 3+j, 0.8-2.3*j, 4 ];
#
# % Construct and solve the model
# n = size( P, 1 );
# cvx_begin sdp
# variable Z(n,n) hermitian toeplitz
# dual variable Q
# minimize( norm( Z - P, 'fro' ) )
# Z >= 0 : Q;
# cvx_end