Advertisement
makispaiktis

LDPC_simulation.m

Jan 23rd, 2022 (edited)
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 7.32 KB | None | 0 0
  1. function [H, num_variables, num_checks]= LDPC_simulation(n, l_optimum, r_optimum)
  2.  
  3.     %% Estw oti brhkame ta PANTA SWSTA
  4.     l_max = length(l_optimum);
  5.     r_max = length(r_optimum);
  6.  
  7.     % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8.     % MPOREI NA MIN BGOUN AKERAIOI!!!
  9.  
  10.     % l_optimum = [0.3, 0.5, 0.2];        
  11.     % r_optimum = [0.2, 0.2, 0.1, 0.1, 0.3, 0.1];
  12.     % n = 200;
  13.  
  14.  
  15.     %% Create Pi dia i and Lamda i dia i
  16.     r_i_dia_i_list = zeros(1, r_max);
  17.     l_i_dia_i_list = zeros(1, l_max);
  18.     for index = 1:r_max
  19.         r_i_dia_i_list(index) = r_optimum(index) / (index + 1);     % Epeidi arxizei apo to 2 enw emeis apo to 1
  20.     end
  21.  
  22.     for index = 1:l_max
  23.         l_i_dia_i_list(index) = l_optimum(index) / (index + 1);
  24.     end
  25.     SUM = sum(l_i_dia_i_list);
  26.  
  27.     r_i_dia_i_list;
  28.     l_i_dia_i_list;
  29.  
  30.     %% Create LAMDA AND RHO LISTS
  31.     RHO = r_i_dia_i_list / SUM * n;
  32.     LAMDA = l_i_dia_i_list / SUM * n;
  33.     RHO_HAT = floor(RHO);
  34.     LAMDA_HAT = floor(LAMDA);
  35.     % Calculate A
  36.     A_condition = sum(RHO - RHO_HAT);
  37.  
  38.  
  39.     %% GENERAL CONSTRAINTS
  40.     % Declare 2 sum constant variables
  41.     constant_19 = n - sum(LAMDA_HAT);
  42.     i_r = 2:r_max+1;
  43.     i_l = 2:l_max+1;
  44.     constant_20 = sum(i_r .* RHO_HAT) - sum(i_l .* LAMDA_HAT);
  45.  
  46.     % Prwta stoixeia ta "l", kai meta ta "r"
  47.     Aeq = zeros(2, l_max + r_max);
  48.     Beq = zeros(2, 1);
  49.  
  50.     % Constraint 19
  51.     Aeq(1, 1:l_max) = 1;
  52.     Beq(1, 1) = constant_19;
  53.     % Constraint 20
  54.     Aeq(2, 1:l_max) = i_l;
  55.     Aeq(2, l_max+1 : l_max+r_max) = - i_r;
  56.     Beq(2, 1) = constant_20;
  57.     % Constraint 21
  58.  
  59.  
  60.     %% CASE CONSTRAINTS
  61.     % CASE 1 -  ANISOTIKOS - MATRIX A1
  62.     A1 = zeros(1, l_max + r_max);
  63.     A1(1, l_max+1 : l_max+r_max) = -1;
  64.     B1 = -ceil(A_condition);
  65.  
  66.     % CASE 2
  67.     A2 = zeros(1, l_max + r_max);
  68.     A2(1, l_max+1 : l_max+r_max) = 1;
  69.     B2 = floor(A_condition);
  70.  
  71.     % OBJECTIVE FUNCTIONS
  72.     % CASE 1
  73.     f1 = zeros(1, l_max + r_max);
  74.     f1(1, l_max+1 : l_max+r_max) = 1;
  75.     f1 = f1';
  76.  
  77.     % CASE 2
  78.     f2 = -f1;
  79.  
  80.  
  81.     %% SIMULATIONS
  82.     lb = zeros(1, l_max + r_max)';
  83.     ub = ones(1, l_max + r_max)';
  84.    
  85.     intcon = 1:l_max + r_max;
  86.     % CASE 1
  87.     result1 = intlinprog(f1, intcon, A1, B1 , Aeq, Beq, lb, ub);
  88.     % CASE 2
  89.     result2 = intlinprog(f2, intcon, A2, B2 , Aeq, Beq, lb, ub);
  90.    
  91.     if isempty(result1) && isempty(result2)
  92.         H = NaN;
  93.         num_variables = NaN;
  94.         num_checks = NaN;
  95.         disp("Not possible solution found - exit code 1")
  96.         return
  97.     end
  98. %     % CASE 1
  99. %     result1 = linprog(f1, A1, B1 , Aeq, Beq, lb, ub);
  100. %     % CASE 2
  101. %     result2 = linprog(f2, A2, B2 , Aeq, Beq, lb, ub);
  102.  
  103.     %% FIND THE BEST
  104.    
  105.     if isempty(result1)
  106.        
  107.         result = result2;
  108.     elseif isempty(result2)
  109.        
  110.         result = result1;
  111.     else
  112.          error1 = (sum(result1(l_max+1 : l_max+r_max)) - A_condition) ^ 2;
  113.          error2 = (sum(result2(l_max+1 : l_max+r_max)) - A_condition) ^ 2;
  114.          if error1 >= error2
  115.             result = result2;
  116.          else
  117.             result = result1;
  118.          end
  119.     end
  120.    
  121.    
  122.     % result = [xl1, xl2, xl3, xr1, ..., xr6] ---> length = 9
  123.  
  124.  
  125.     %% FIND THE NEW LAMDA AND RHO - UPDATE
  126.     result = floor(result');
  127.     LAMDA_LIST = LAMDA_HAT + result(1 : l_max);
  128.     RHO_LIST = RHO_HAT + result(l_max + 1 : l_max + r_max);
  129.    
  130.  
  131.     %% DISPLAY AKMES
  132.     % !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  133.     % MPOREI KAPOIA NOUMERA NA MIN EINAI INTEGERS, ENW ANAPARISTOUN # AKMWN !
  134.     num_variable_edges = sum(LAMDA_LIST .* i_l);
  135.     num_check_edges = sum(RHO_LIST .* i_r);
  136.     num_variables = sum(LAMDA_LIST);
  137.     num_checks = sum(RHO_LIST);
  138.     %   Above: L'(1) = P'(1)  (sxesh 2 kai 3)
  139.  
  140.     if num_check_edges ~= num_variable_edges
  141.         H = NaN;
  142.         num_variables = NaN;
  143.         num_checks = NaN;
  144.         disp("Not possible solution found - exit code 2")
  145.         return
  146.     end
  147.        
  148.     pretty_display("n = ", n);
  149.     pretty_display("Variable degrees = ", LAMDA_LIST);
  150.     pretty_display("Check degrees = ", RHO_LIST);
  151.     pretty_display("Num of variable edges = ", num_variable_edges);
  152.     pretty_display("Num of check edges = ", num_check_edges);
  153.     pretty_display("Num of VARIABLES = ", num_variables);
  154.     pretty_display("Num of CHECKS = ", num_checks);
  155.  
  156.  
  157.  
  158.     % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  159.     % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  160.     % ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  161.  
  162.     %% MATRIX 545 x 545 (about sockets)
  163.     sockets = num_check_edges;
  164.     checks = num_checks;
  165.     vars = num_variables;
  166.     comb1 = 1:sockets;
  167.  
  168.     %% Create a vector with 82 2's and 91 3's and 27 4's (n=200)
  169.     var_temp = zeros(1, vars);
  170.     counter = 1;
  171.     for i = 1:length(LAMDA_LIST)
  172.         var_temp(counter : counter + LAMDA_LIST(i) - 1) = ...
  173.         (i+1) * ones(1, LAMDA_LIST(i));
  174.         counter = LAMDA_LIST(i) + counter;
  175.     end
  176.    
  177.     check_temp = zeros(1, checks);
  178.     counter = 1;
  179.     for i = 1:length(RHO_LIST)
  180.         check_temp(counter : counter + RHO_LIST(i) - 1) = ...
  181.         (i+1) * ones(1, RHO_LIST(i));
  182.         counter = RHO_LIST(i) + counter;
  183.     end
  184.  
  185.     %% ACCUMULATIVE RANGES
  186.     var_ranges = zeros(1, length(var_temp));
  187.     var_ranges = [0 var_ranges];
  188.  
  189.     for i = 1:length(var_temp)
  190.         S = var_ranges(i) + var_temp(i);
  191.         var_ranges(i+1) = S;
  192.     end
  193.  
  194.     check_ranges = zeros(1, length(check_temp));
  195.     check_ranges = [0 check_ranges];
  196.  
  197.     for i = 1:length(check_temp)
  198.         S = check_ranges(i) + check_temp(i);
  199.         check_ranges(i+1) = S;
  200.     end
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.     anakyklwseis = 1000;
  208.     H = zeros(checks, vars);
  209.     counter_tries = 0;
  210.    
  211.    
  212.    
  213.    
  214.  
  215.     %% WHILE LOOP IN ORDER TO AVOID ANAKYKLWSEIS
  216.     while anakyklwseis ~= 0
  217.  
  218.         counter_tries = counter_tries + 1;
  219.         socket_matrix = zeros(sockets);
  220.         % Socket Matrix is 545 x 545  
  221.         comb2 = comb1(randperm(length(comb1)));
  222.         for i = 1:sockets
  223.             ch = comb1(i);
  224.             var = comb2(i);
  225.             socket_matrix(ch, var) = 1;
  226.         end
  227.  
  228.  
  229.         %% Decomposition onto submatrices - Creation of matrix
  230.         % var_ranges and check-ranges have 1 more element (0 in the beginning)
  231.         % socket_matrix = randi([0 1], 6, 9)
  232.         % check_ranges = [0, 1, 3, 6]
  233.         % var_ranges = [0, 2, 4, 6, 9]
  234.         % H = zeros(3, 4);
  235.  
  236.         for i = 1:length(check_ranges) - 1
  237.             for j = 1:length(var_ranges) - 1
  238.                 i;
  239.                 j;
  240.                 rows_range = check_ranges(i)+1 : + check_ranges(i+1);
  241.                 cols_range = var_ranges(j)+1 : var_ranges(j+1);
  242.                 submatrix = socket_matrix(rows_range, cols_range);
  243.                 SUM = sum(sum(submatrix));
  244.                 H(i, j) = mod(SUM, 2);
  245.             end
  246.         end
  247.  
  248.  
  249.         anakyklwseis = num_variable_edges - sum(sum(H));
  250.         % pretty_display("Anakyklwseis: ", anakyklwseis / 2);
  251.  
  252.     end
  253.  
  254.     disp(' ');
  255.     pretty_display("Anakyklwseis: ", anakyklwseis / 2);
  256.     pretty_display("Tries needed: ", counter_tries);
  257.     n = num_variables;
  258.     k = num_variables - num_checks;
  259.     rate_expected = k / n;
  260.     pretty_display("Expected rate: ", rate_expected);
  261.  
  262.    
  263. end
  264.  
  265.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement