# Decomposition

Jan 21st, 2022
1,015
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. clc
2. clear all
3.
4. %% Estw oti brhkame ta PANTA SWSTA
5. l_max = 3;
6. r_max = 6;
7. l_optimum = [0.3, 0.5, 0.2];
8. r_optimum = [0.2, 0.2 0.1, 0.1, 0.3, 0.1];
9. % Codewords length
10. n = 200;
11.
12.
13. %% Create Pi dia i and Lamda i dia i
14. r_i_dia_i_list = zeros(1, r_max);
15. l_i_dia_i_list = zeros(1, l_max);
16. for index = 1:r_max
17.     r_i_dia_i_list(index) = r_optimum(index) / (index + 1);     % Epeidi arxizei apo to 2 enw emeis apo to 1
18. end
19.
20. for index = 1:l_max
21.     l_i_dia_i_list(index) = l_optimum(index) / (index + 1);
22. end
23. SUM = sum(l_i_dia_i_list);
24.
25. r_i_dia_i_list;
26. l_i_dia_i_list;
27.
28. %% Create LAMDA AND RHO LISTS
29. RHO = r_i_dia_i_list / SUM * n;
30. LAMDA = l_i_dia_i_list / SUM * n;
31. RHO_HAT = floor(RHO);
32. LAMDA_HAT = floor(LAMDA);
33. % Calculate A
34. A_condition = sum(RHO - RHO_HAT);
35.
36.
37. %% GENERAL CONSTRAINTS
38. % Declare 2 sum constant variables
39. constant_19 = n - sum(LAMDA_HAT);
40. i_r = 2:r_max+1;
41. i_l = 2:l_max+1;
42. constant_20 = sum(i_r .* RHO_HAT) - sum(i_l .* LAMDA_HAT);
43.
44. % Prwta stoixeia ta "l", kai meta ta "r"
45. Aeq = zeros(2, l_max + r_max);
46. Beq = zeros(2, 1);
47.
48. % Constraint 19
49. Aeq(1, 1:l_max) = 1;
50. Beq(1, 1) = constant_19;
51. % Constraint 20
52. Aeq(2, 1:l_max) = i_l;
53. Aeq(2, l_max+1 : l_max+r_max) = - i_r;
54. Beq(2, 1) = constant_20;
55. % Constraint 21
56.
57.
58. %% CASE CONSTRAINTS
59. % CASE 1 -  ANISOTIKOS - MATRIX A1
60. A1 = zeros(1, l_max + r_max);
61. A1(1, l_max+1 : l_max+r_max) = -1;
62. B1 = -ceil(A_condition);
63.
64. % CASE 2
65. A2 = zeros(1, l_max + r_max);
66. A2(1, l_max+1 : l_max+r_max) = 1;
67. B2 = floor(A_condition);
68.
69. % OBJECTIVE FUNCTIONS
70. % CASE 1
71. f1 = zeros(1, l_max + r_max);
72. f1(1, l_max+1 : l_max+r_max) = 1;
73.
74. % CASE 2
75. f2 = -f1;
76.
77.
78. %% SIMULATIONS
79. lb = zeros(1, l_max + r_max);
80. ub = ones(1, l_max + r_max);
81. % CASE 1
82. result1 = linprog(f1, A1, B1 , Aeq, Beq, lb, ub);
83. % CASE 2
84. result2 = linprog(f2, A2, B2 , Aeq, Beq, lb, ub);
85.
86. %% FIND THE BEST
87. error1 = (sum(result1(l_max+1 : l_max+r_max)) - A_condition) ^ 2;
88. error2 = (sum(result2(l_max+1 : l_max+r_max)) - A_condition) ^ 2;
89. if error1 >= error2
90.     result = result2;
91. else
92.     result = result1;
93. end
94. % result = [xl1, xl2, xl3, xr1, ..., xr6] ---> length = 9
95.
96.
97. %% FIND THE NEW LAMDA AND RHO - UPDATE
98. n;
99. result = result';
100. LAMDA_HAT;
101. RHO_HAT;
102. LAMDA_LIST = LAMDA_HAT + result(1 : l_max);
103. RHO_LIST = RHO_HAT + result(l_max + 1 : l_max + r_max);
104.
105.
106. %% DISPLAY AKMES
107. % !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
108. % MPOREI KAPOIA NOUMERA NA MIN EINAI INTEGERS, ENW ANAPARISTOUN # AKMWN !
109. num_variable_edges = sum(LAMDA_LIST .* i_l);
110. num_check_edges = sum(RHO_LIST .* i_r);
111. num_variables = sum(LAMDA_LIST);
112. num_checks = sum(RHO_LIST);
113. %   Above: L'(1) = P'(1)  (sxesh 2 kai 3)
114.
115. pretty_display("n = ", n);
116. pretty_display("Variable degrees = ", LAMDA_LIST);
117. pretty_display("Check degrees = ", RHO_LIST);
118. pretty_display("Num of variable edges = ", num_variable_edges);
119. pretty_display("Num of check edges = ", num_check_edges);
120. pretty_display("Num of VARIABLES = ", num_variables);
121. pretty_display("Num of CHECKS = ", num_checks);
122.
123.
124.
125. %% MATRIX 545 x 545 (about sockets)
126. sockets = num_check_edges;
127. checks = num_checks;
128. vars = num_variables;
129.
130. % Socket Matrix is 545 x 545
131. socket_matrix = zeros(sockets);
132. comb1 = 1:sockets;
133. comb2 = comb1(randperm(length(comb1)));
134.
135. for i = 1:sockets
136.     ch = comb1(i);
137.     var = comb2(i);
138.     socket_matrix(ch, var) = 1;
139. end
140.
141.
142. %% Create a vector with 82 2's and 91 3's and 27 4's (n=200)
143. var_temp = zeros(1, vars);
144. counter = 1;
145. for i = 1:length(LAMDA_LIST)
146.     var_temp(counter : counter + LAMDA_LIST(i) - 1) = ...
147.     (i+1) * ones(1, LAMDA_LIST(i));
148.     counter = LAMDA_LIST(i) + counter;
149. end
150. var_temp;
151.
152.
153. check_temp = zeros(1, checks);
154. counter = 1;
155. for i = 1:length(RHO_LIST)
156.     check_temp(counter : counter + RHO_LIST(i) - 1) = ...
157.     (i+1) * ones(1, RHO_LIST(i));
158.     counter = RHO_LIST(i) + counter;
159. end
160. check_temp;
161.
162.
163. %% H is a decomposition of socket matrix
164. H = zeros(checks, vars);
165. % for i = 1:vars
166. %     for j = 1:checks
167. %         MAT = sockets( : , :);
168. %         H(i, j) = sum(MAT);
169. %     end
170. % end
171.
172.
173.
174.
175. %% ACCUMULATIVE RANGES
176. var_ranges = zeros(1, length(var_temp));
177. var_ranges = [0 var_ranges];
178.
179. for i = 1:length(var_temp)
180.     S = var_ranges(i) + var_temp(i);
181.     var_ranges(i+1) = S;
182. end
183. var_ranges;
184.
185.
186. check_ranges = zeros(1, length(check_temp));
187. check_ranges = [0 check_ranges];
188.
189. for i = 1:length(check_temp)
190.     S = check_ranges(i) + check_temp(i);
191.     check_ranges(i+1) = S;
192. end
193. check_ranges;
194.
195.
196. %% Decomposition onto submatrices
197. % var_ranges and check-ranges have 1 more element (0 in the beginning)
198. socket_matrix = randi([0 1], 6, 9)
199. check_ranges = [0, 1, 3, 6]
200. var_ranges = [0, 2, 4, 6, 9]
201. H = zeros(3, 4);
202.
203. for i = 1:length(check_ranges) - 1
204.     for j = 1:length(var_ranges) - 1
205.         i
206.         j
207.         rows_range = check_ranges(i)+1 : + check_ranges(i+1)
208.         cols_range = var_ranges(j)+1 : + var_ranges(j+1)
209.         submatrix = socket_matrix(rows_range, cols_range)
210.         SUM = sum(sum(submatrix))
211.         H(i, j) = mod(SUM, 2)
212.     end
213. end
214.
215. H
216.
217.
218.