# Ws anakyklwseis

Jan 21st, 2022
997
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. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
126. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
127. % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
128.
129. %% MATRIX 545 x 545 (about sockets)
130. sockets = num_check_edges;
131. checks = num_checks;
132. vars = num_variables;
133. comb1 = 1:sockets;
134.
135. %% Create a vector with 82 2's and 91 3's and 27 4's (n=200)
136. var_temp = zeros(1, vars);
137. counter = 1;
138. for i = 1:length(LAMDA_LIST)
139.     var_temp(counter : counter + LAMDA_LIST(i) - 1) = ...
140.     (i+1) * ones(1, LAMDA_LIST(i));
141.     counter = LAMDA_LIST(i) + counter;
142. end
143. var_temp;
144.
145.
146. check_temp = zeros(1, checks);
147. counter = 1;
148. for i = 1:length(RHO_LIST)
149.     check_temp(counter : counter + RHO_LIST(i) - 1) = ...
150.     (i+1) * ones(1, RHO_LIST(i));
151.     counter = RHO_LIST(i) + counter;
152. end
153. check_temp;
154.
155.
156. %% ACCUMULATIVE RANGES
157. var_ranges = zeros(1, length(var_temp));
158. var_ranges = [0 var_ranges];
159.
160. for i = 1:length(var_temp)
161.     S = var_ranges(i) + var_temp(i);
162.     var_ranges(i+1) = S;
163. end
164. var_ranges;
165.
166.
167. check_ranges = zeros(1, length(check_temp));
168. check_ranges = [0 check_ranges];
169.
170. for i = 1:length(check_temp)
171.     S = check_ranges(i) + check_temp(i);
172.     check_ranges(i+1) = S;
173. end
174. check_ranges;
175.
176.
177.
178.
179.
180.
181.
182. anakyklwseis = 1000;
183. H = zeros(checks, vars);
184. counter_tries = 0;
185.
186. %% WHILE LOOP IN ORDER TO AVOID ANAKYKLWSEIS
187. while anakyklwseis ~= 0
188.
189.     counter_tries = counter_tries + 1;
190.     socket_matrix = zeros(sockets);
191.     % Socket Matrix is 545 x 545
192.     comb2 = comb1(randperm(length(comb1)));
193.     for i = 1:sockets
194.         ch = comb1(i);
195.         var = comb2(i);
196.         socket_matrix(ch, var) = 1;
197.     end
198.
199.
200.     %% Decomposition onto submatrices - Creation of matrix
201.     % var_ranges and check-ranges have 1 more element (0 in the beginning)
202.     % socket_matrix = randi([0 1], 6, 9)
203.     % check_ranges = [0, 1, 3, 6]
204.     % var_ranges = [0, 2, 4, 6, 9]
205.     % H = zeros(3, 4);
206.
207.     for i = 1:length(check_ranges) - 1
208.         for j = 1:length(var_ranges) - 1
209.             i;
210.             j;
211.             rows_range = check_ranges(i)+1 : + check_ranges(i+1);
212.             cols_range = var_ranges(j)+1 : var_ranges(j+1);
213.             submatrix = socket_matrix(rows_range, cols_range);
214.             SUM = sum(sum(submatrix));
215.             H(i, j) = mod(SUM, 2);
216.         end
217.     end
218.     H;
219.
220.     anakyklwseis = num_variable_edges - sum(sum(H));
221.     pretty_display("Anakyklwseis: ", anakyklwseis / 2);
222.
223. end
224.
225.
226. H;
227. display(' ');
228. pretty_display("Anakyklwseis: ", anakyklwseis / 2);
229. pretty_display("Tries needed: ", counter_tries);
230.