├── Maxcut_SDR_CD.m ├── README.md ├── SNL.m └── pic ├── 10.jpg ├── 15.jpg ├── 20.jpg ├── 25.jpg ├── 30.jpg ├── 5.jpg ├── distance_error.jpg ├── noise_free.mat ├── noise_free_new.mat ├── noisy.mat └── result_15%.jpg /Maxcut_SDR_CD.m: -------------------------------------------------------------------------------- 1 | %% Initialize 2 | clear all; 3 | num_iter = 20; 4 | Nlist = [4, 8, 16, 20] 5 | result_truths = zeros([size(Nlist,2),num_iter]); 6 | result_SDRs = zeros([size(Nlist,2),num_iter]); 7 | result_CDs = zeros([size(Nlist,2),num_iter]); 8 | p=0.50 9 | %% SDR QCQP 10 | for idx_N = 1:size(Nlist,2) 11 | N = Nlist(idx_N) 12 | for idx_iter = 1: num_iter 13 | idx_iter 14 | % Adjacency Matrix 15 | A = randi(2,N,N) - 1; 16 | Mask = rand(N,N)>=1-p; 17 | A = A.*Mask; 18 | A = A - tril(A,-1) + triu(A,1)'; 19 | % Laplacian Matrix 20 | D = diag(A * ones(N,1)); 21 | L = D - A; 22 | % find the ground truth 23 | maxCut = 0; 24 | for i = 1:2^N 25 | x = de2bi([i-1], N); 26 | x = x *2 -1; 27 | currCut = x*L*x'/4; 28 | if currCut > maxCut 29 | maxCut = currCut; 30 | result = x; 31 | end 32 | end 33 | maxCut_truth = maxCut; 34 | % CVX 35 | cvx_begin 36 | % cvx_precision high 37 | variable X(N,N) symmetric; 38 | maximize( trace(L*X)/4 ) 39 | subject to: 40 | for i = 1:N 41 | e = zeros([1,N]); 42 | e(i) = 1; 43 | E = e'*e; 44 | trace(E*X) == 1; 45 | end 46 | X == semidefinite(N); 47 | cvx_end 48 | 49 | % Coordinate descent 50 | x = randsample([-1,1], N,true); 51 | change =true; 52 | maxCut = x*L*x'/4; 53 | while change == true 54 | change = false; 55 | for i = 1:N 56 | x(i) = -1 * x(i); 57 | currCut = x*L*x'/4; 58 | if currCut > maxCut 59 | maxCut = currCut; 60 | change =true; 61 | else 62 | x(i) = -1 * x(i); 63 | end 64 | end 65 | end 66 | maxCut_CD = maxCut; 67 | 68 | result_truth = maxCut_truth 69 | result_SDR = trace(L*X)/4 70 | result_CD = maxCut_CD 71 | result_truths(idx_N, idx_iter) = result_truth; 72 | result_SDRs(idx_N, idx_iter) = result_SDR; 73 | result_CDs(idx_N, idx_iter) = result_CD; 74 | end 75 | end 76 | 77 | %% 78 | result_truths; 79 | result_SDRs; 80 | result_SDRs./result_truth; 81 | bound_ratio = mean(result_SDRs./result_truths, 2) 82 | CD_ration = mean(result_CDs./result_truths,2) 83 | figure 84 | plot(log(Nlist),bound_ratio,'r-o',log(Nlist),CD_ration,'b-o' ) 85 | title('Max cut : log N vs (SDR relax bound/GT, CD /GT)','FontSize',20) 86 | xlabel('log N','FontSize',20) 87 | ylabel('ratio','FontSize',20) 88 | legend({'SDR relaxation bound ratio','CD solver result ration'},'Fontsize',15); 89 | 90 | %% 91 | 92 | figure 93 | plot(log(Nlist),mean(result_SDRs,2),'r-o'... 94 | ,log(Nlist),mean(result_truths,2),'g-o',... 95 | log(Nlist),mean(result_CDs,2),'b-o') 96 | title('Max cut : log N vs Maxcut','FontSize',20) 97 | xlabel('log N','FontSize',15) 98 | ylabel('Maxcut','FontSize',15) 99 | legend({'SDR relaxation bound','Grouth Truth','CD solver'},'Fontsize',15); 100 | 101 | 102 | 103 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Convex-Optimization 2 | SDP using cvx toolbox for Sensor Network Localization 3 | 4 | Equation M_ik matrix in (34) may not be correct in paper [2]. The e_k term should be negative. 5 | The cost function is randomly selected, may not be appropriate 6 | -------------------------------------------------------------------------------- /SNL.m: -------------------------------------------------------------------------------- 1 | %% basic SNL problem simulation 2 | clear all; 3 | anchor_number = 5; 4 | sensor_number = 45; 5 | global_record = []; 6 | anchor = rand(anchor_number,2)*100; 7 | sensor = rand(sensor_number,2)*100; 8 | average_time = 1; 9 | 10 | p_start = 30; 11 | p_end = 30; 12 | p_step = 5; 13 | 14 | for threshold = p_start:p_step:p_end 15 | record = []; 16 | for times = 1:1:average_time 17 | %% 18 | % figure 19 | % plot(anchor(:,1), anchor(:,2),'bo', 'LineWidth',3); 20 | % hold on; 21 | % plot(sensor(:,1), sensor(:,2),'ro', 'LineWidth',3); 22 | % hold on; 23 | % xlim([0,100]); 24 | % ylim([0,100]); 25 | % title('position of anchor and sensor'); 26 | %% 27 | kernal = sensor*sensor'; 28 | Z = [eye(2) sensor'; 29 | sensor kernal]; 30 | indicator = eye(sensor_number); 31 | %% 32 | M_ss = zeros(sensor_number+2, sensor_number+2, sensor_number, sensor_number); 33 | for i = 1:1:sensor_number 34 | for j = 1:1:sensor_number 35 | M_ss(3:end, 3:end, i, j) = (indicator(i,:)-indicator(j,:))'*(indicator(i,:)-indicator(j,:)); 36 | end 37 | end 38 | M_sa = zeros(sensor_number+2, sensor_number+2, anchor_number, sensor_number); 39 | for i = 1:1:anchor_number 40 | for j = 1:1:sensor_number 41 | M_sa(:,:,i,j) = [anchor(i,:)'; indicator(j,:)']*[anchor(i,:) indicator(j,:)]; 42 | end 43 | end 44 | %% Ground Truth 45 | Distance_ss = zeros(sensor_number, sensor_number); 46 | for i = 1:1:sensor_number 47 | for j = 1:1:i-1 48 | Distance_ss(j,i) = trace(M_ss(:,:,i,j)*Z); 49 | Distance_ss(i,j) = Distance_ss(j,i); 50 | end 51 | end 52 | 53 | 54 | Distance_sa = zeros(anchor_number, sensor_number); 55 | for i = 1:1:anchor_number 56 | for j = 1:1:sensor_number 57 | Distance_sa(i,j) = trace(M_sa(:,:,i,j)*Z); 58 | end 59 | end 60 | %% hist 61 | Distance_sa_vec = reshape(Distance_sa, size(Distance_sa,1)*size(Distance_sa, 2), 1); 62 | Distance_ss_vec = reshape(Distance_ss, size(Distance_ss,1)*size(Distance_ss, 2), 1); 63 | 64 | % figure 65 | % max_sa = max(max(hist(Distance_sa))); 66 | % hist(Distance_sa) 67 | % hold on 68 | sa_threshold = prctile(Distance_sa_vec, threshold); 69 | % plot([sa_threshold sa_threshold], [0, max_sa], 'r.-'); 70 | % title('hist of anchor-sensor distance'); 71 | % 72 | % figure 73 | % max_ss = max(max(hist(Distance_ss))); 74 | % hist(Distance_ss); 75 | % hold on 76 | ss_threshold = prctile(Distance_ss_vec, threshold); 77 | % plot([ss_threshold ss_threshold], [0, max_ss], 'r.-'); 78 | % title('hist of sensor-sensor distance'); 79 | %% CVX 80 | n = sensor_number; 81 | m = anchor_number; 82 | cvx_begin 83 | cvx_precision high 84 | variable X(n+2,n+2) symmetric; 85 | minimize( 0 ) 86 | subject to: 87 | for i = 1:2 88 | X(i,i) == 1; 89 | end 90 | for i = 1:n 91 | for j = 1:n 92 | if Distance_ss(i,j)<= ss_threshold 93 | abs(trace(M_ss(:,:,i,j)*X) - Distance_ss(i,j)) <= 10; 94 | end 95 | end 96 | end 97 | for i = 1:m 98 | for j = 1:n 99 | if Distance_sa(i,j)<= sa_threshold 100 | abs(trace(M_sa(:,:,i,j)*X) - Distance_sa(i,j)) <= 10; 101 | end 102 | end 103 | end 104 | X == semidefinite(n+2); 105 | cvx_end 106 | %% measurement 107 | diff = trace((X-Z)*(X-Z)'); 108 | disp('The difference of reconstruction matrix is: ') 109 | disp(diff) 110 | 111 | %% reconstruction without rank-2 approx 112 | % figure 113 | % plot(sensor(3:end,1), sensor(3:end,2),'r*', 'LineWidth',5); 114 | % hold on; 115 | % plot(X(3:end,1), X(3:end,2),'Bo', 'LineWidth',2); 116 | % hold on 117 | % xlim([0,100]); 118 | % ylim([0,100]); 119 | % title('position of anchor and sensor'); 120 | 121 | %% Rank 2 approximation 122 | if isnan(max(max(X))) 123 | diff_2 = diff; 124 | else 125 | [U,S,V] = svds(X); 126 | Z_2 = U(:,1:2) * S(1:2,1:2) * V(:,1:2)'; 127 | pos = Z_2(3:end,1:2); 128 | diff_2 = trace((pos-sensor)*(pos-sensor)'); 129 | end 130 | disp('The difference of reconstruction matrix(rank2 approx) is: ') 131 | disp(diff_2) 132 | record = [record diff_2]; 133 | end 134 | figure 135 | plot(sensor(:,1), sensor(:,2),'r*', 'LineWidth',5); 136 | hold on 137 | plot(Z_2(3:end,1), Z_2(3:end,2),'bo', 'LineWidth',2); 138 | hold on 139 | plot(anchor(:,1), anchor(:,1),'c*', 'LineWidth',5); 140 | xlim([0,100]); 141 | ylim([0,100]); 142 | legend('position', 'reconstruction','anchor'); 143 | title('Position of anchor and sensor'); 144 | 145 | global_record = [global_record; record]; 146 | end 147 | 148 | 149 | -------------------------------------------------------------------------------- /pic/10.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jarvislkm/Convex-Optimization/bb3b33f7d644b9dcd964c450881fae64e447c9ec/pic/10.jpg -------------------------------------------------------------------------------- /pic/15.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jarvislkm/Convex-Optimization/bb3b33f7d644b9dcd964c450881fae64e447c9ec/pic/15.jpg -------------------------------------------------------------------------------- /pic/20.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jarvislkm/Convex-Optimization/bb3b33f7d644b9dcd964c450881fae64e447c9ec/pic/20.jpg -------------------------------------------------------------------------------- /pic/25.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jarvislkm/Convex-Optimization/bb3b33f7d644b9dcd964c450881fae64e447c9ec/pic/25.jpg -------------------------------------------------------------------------------- /pic/30.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jarvislkm/Convex-Optimization/bb3b33f7d644b9dcd964c450881fae64e447c9ec/pic/30.jpg -------------------------------------------------------------------------------- /pic/5.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jarvislkm/Convex-Optimization/bb3b33f7d644b9dcd964c450881fae64e447c9ec/pic/5.jpg -------------------------------------------------------------------------------- /pic/distance_error.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jarvislkm/Convex-Optimization/bb3b33f7d644b9dcd964c450881fae64e447c9ec/pic/distance_error.jpg -------------------------------------------------------------------------------- /pic/noise_free.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jarvislkm/Convex-Optimization/bb3b33f7d644b9dcd964c450881fae64e447c9ec/pic/noise_free.mat -------------------------------------------------------------------------------- /pic/noise_free_new.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jarvislkm/Convex-Optimization/bb3b33f7d644b9dcd964c450881fae64e447c9ec/pic/noise_free_new.mat -------------------------------------------------------------------------------- /pic/noisy.mat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jarvislkm/Convex-Optimization/bb3b33f7d644b9dcd964c450881fae64e447c9ec/pic/noisy.mat -------------------------------------------------------------------------------- /pic/result_15%.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jarvislkm/Convex-Optimization/bb3b33f7d644b9dcd964c450881fae64e447c9ec/pic/result_15%.jpg --------------------------------------------------------------------------------