mode(0);
clear;

// UVODNI NACTENI TABULEK

Sheets=readxls("C:\Users\Matej\Documents\data_mpdt\mpdt.xls");
                                                                       // nactení souboru v Excelu
linek=66;                                                              // soucet linek v obou smerech   
zastavek=1343;                                                         // celkem zastavek pro vypocet
                                                                       
sheet = Sheets(1);                                                  
zastavky = sheet (3:zastavek+2,1:2);                        // Cn a KVZ vsech zastavek
kapacity = sheet (2,3:linek+2);                             // KL vsech linek 
poradi = sheet (3:zastavek+2,3:linek+2);                    // poradi jednotlivych zastavek na linkach
sheet = Sheets(2);
T51 = sheet (:,:);                                    // linka 51 T
sheet = Sheets(3);
Z51 = sheet (:,:);                                    // linka 51 Z
sheet = Sheets(4);
T52 = sheet (:,:);                                    // linka 52 T
sheet = Sheets(5);
Z52 = sheet (:,:);                                    // linka 52 Z
sheet = Sheets(6);
T53 = sheet (:,:);                                    // linka 53 T
sheet = Sheets(7);
Z53 = sheet (:,:);                                    // linka 53 Z
sheet = Sheets(8);
T54 = sheet (:,:);                                    // linka 54 T
sheet = Sheets(9);
Z54 = sheet (:,:);                                    // linka 54 Z
sheet = Sheets(10);
T55 = sheet (:,:);                                    // linka 55 T
sheet = Sheets(11);
Z55 = sheet (:,:);                                    // linka 55 Z
sheet = Sheets(12);
T56 = sheet (:,:);                                    // linka 56 T
sheet = Sheets(13);
Z56 = sheet (:,:);                                    // linka 56 Z
sheet = Sheets(14);
T57 = sheet (:,:);                                    // linka 57 T
sheet = Sheets(15);
Z57 = sheet (:,:);                                    // linka 57 Z
sheet = Sheets(16);
T58 = sheet (:,:);                                    // linka 58 T
sheet = Sheets(17);
Z58 = sheet (:,:);                                    // linka 58 Z
sheet = Sheets(18);
T59 = sheet (:,:);                                    // linka 59 T
sheet = Sheets(19);
Z59 = sheet (:,:);                                    // linka 59 Z
sheet = Sheets(20);
T501 = sheet (:,:);                                   // linka 501 T
sheet = Sheets(21);
Z501 = sheet (:,:);                                   // linka 501 Z
sheet = Sheets(22);
T502 = sheet (:,:);                                   // linka 502 T
sheet = Sheets(23);
Z502 = sheet (:,:);                                   // linka 502 Z
sheet = Sheets(24);
T503 = sheet (:,:);                                   // linka 503 T
sheet = Sheets(25);
Z503 = sheet (:,:);                                   // linka 503 Z
sheet = Sheets(26);
T504 = sheet (:,:);                                   // linka 504 T
sheet = Sheets(27);
Z504 = sheet (:,:);                                   // linka 504 Z
sheet = Sheets(28);
T505 = sheet (:,:);                                   // linka 505 T
sheet = Sheets(29);
Z505 = sheet (:,:);                                   // linka 505 Z
sheet = Sheets(30);
T506 = sheet (:,:);                                   // linka 506 T
sheet = Sheets(31);
Z506 = sheet (:,:);                                   // linka 506 Z
sheet = Sheets(32);
T507 = sheet (:,:);                                   // linka 507 T
sheet = Sheets(33);
Z507 = sheet (:,:);                                   // linka 507 Z
sheet = Sheets(34);
T509 = sheet (:,:);                                   // linka 509 T
sheet = Sheets(35);
Z509 = sheet (:,:);                                   // linka 509 Z
sheet = Sheets(36);
T510 = sheet (:,:);                                   // linka 510 T
sheet = Sheets(37);
Z510 = sheet (:,:);                                   // linka 510 Z
sheet = Sheets(38);
T511 = sheet (:,:);                                   // linka 511 T
sheet = Sheets(39);
Z511 = sheet (:,:);                                   // linka 511 Z
sheet = Sheets(40);
T512 = sheet (:,:);                                   // linka 512 T
sheet = Sheets(41);
Z512 = sheet (:,:);                                   // linka 512 Z
sheet = Sheets(42);
T513 = sheet (:,:);                                   // linka 513 T
sheet = Sheets(43);
Z513 = sheet (:,:);                                   // linka 513 Z
sheet = Sheets(44);
T514 = sheet (:,:);                                   // linka 514 T
sheet = Sheets(45);
Z514 = sheet (:,:);                                   // linka 514 Z
sheet = Sheets(46);
T515 = sheet (:,:);                                   // linka 515 T
sheet = Sheets(47);
Z515 = sheet (:,:);                                   // linka 515 Z
sheet = Sheets(48);
T601 = sheet (:,:);                                   // linka 601 T
sheet = Sheets(49);
Z601 = sheet (:,:);                                   // linka 601 Z
sheet = Sheets(50);
T602 = sheet (:,:);                                   // linka 602 T
sheet = Sheets(51);
Z602 = sheet (:,:);                                   // linka 602 Z
sheet = Sheets(52);
T603 = sheet (:,:);                                   // linka 603 T
sheet = Sheets(53);
Z603 = sheet (:,:);                                   // linka 603 Z
sheet = Sheets(54);
T604 = sheet (:,:);                                   // linka 604 T
sheet = Sheets(55);
Z604 = sheet (:,:);                                   // linka 604 Z
sheet = Sheets(56);
T605 = sheet (:,:);                                   // linka 605 T
sheet = Sheets(57);
Z605 = sheet (:,:);                                   // linka 605 Z
sheet = Sheets(58);
T606 = sheet (:,:);                                   // linka 606 T
sheet = Sheets(59);
Z606 = sheet (:,:);                                   // linka 606 Z
sheet = Sheets(60);
T607 = sheet (:,:);                                   // linka 607 T
sheet = Sheets(61);
Z607 = sheet (:,:);                                   // linka 607 Z
sheet = Sheets(62);
T608 = sheet (:,:);                                   // linka 608 T
sheet = Sheets(63);
Z608 = sheet (:,:);                                   // linka 608 Z
sheet = Sheets(64);
T609 = sheet (:,:);                                   // linka 609 T
sheet = Sheets(65);
Z609 = sheet (:,:);                                   // linka 609 Z
sheet = Sheets(66);
T610 = sheet (:,:);                                   // linka 610 T
sheet = Sheets(67);
Z610 = sheet (:,:);                                   // linka 610 Z

// KONEC NACITANI
// VYPOCET KPN

KPNs = zeros(zastavek,linek);
KPN = KPNs;                                           // iniciace zatím prázdných matic

for j = 1:zastavek
    for i = 1:linek
        if poradi(j,i)>0 then
            KPNs(j,i) = kapacity(1,i);                // prepis poradi zastavek kapacitami linek
        end
    end
end

for j = 1:zastavek
    sumKPN = KPNs(j,:);
    sumKPN = sum(sumKPN);                             // suma kapacit
    for i = 1:linek
        if poradi(j,i)>0 then
            KPN(j,i) = KPNs(j,i) ./ sumKPN;           // vypocet KPN
        end
    end
end
clear KPNs;
clear sumKPN;

//VYPOCET DPN
DPNs = zeros(zastavek,linek);
DPN = DPNs;                                           // iniciace zatím prázdných matic

for i = 1:linek                   
    select i                                          // vyber matice prislusne linky
        case 1 then A = T51; 
        case 2 then A = Z51;
        case 3 then A = T52; 
        case 4 then A = Z52;
        case 5 then A = T53; 
        case 6 then A = Z53;
        case 7 then A = T54; 
        case 8 then A = Z54;
        case 9 then A = T55; 
        case 10 then A = Z55;
        case 11 then A = T56; 
        case 12 then A = Z56;
        case 13 then A = T57; 
        case 14 then A = Z57;
        case 15 then A = T58; 
        case 16 then A = Z58;
        case 17 then A = T59; 
        case 18 then A = Z59;
        case 19 then A = T501; 
        case 20 then A = Z501;
        case 21 then A = T502; 
        case 22 then A = Z502;
        case 23 then A = T503; 
        case 24 then A = Z503;
        case 25 then A = T504; 
        case 26 then A = Z504;
        case 27 then A = T505; 
        case 28 then A = Z505;
        case 29 then A = T506; 
        case 30 then A = Z506;
        case 31 then A = T507; 
        case 32 then A = Z507;
        case 33 then A = T509; 
        case 34 then A = Z509;
        case 35 then A = T510; 
        case 36 then A = Z510;
        case 37 then A = T511; 
        case 38 then A = Z511;
        case 39 then A = T512; 
        case 40 then A = Z512;
        case 41 then A = T513; 
        case 42 then A = Z513;
        case 43 then A = T514; 
        case 44 then A = Z514;
        case 45 then A = T515; 
        case 46 then A = Z515;
        case 47 then A = T601; 
        case 48 then A = Z601;
        case 49 then A = T602; 
        case 50 then A = Z602;
        case 51 then A = T603; 
        case 52 then A = Z603;
        case 53 then A = T604; 
        case 54 then A = Z604;
        case 55 then A = T605; 
        case 56 then A = Z605;
        case 57 then A = T606; 
        case 58 then A = Z606;
        case 59 then A = T607; 
        case 60 then A = Z607;
        case 61 then A = T608; 
        case 62 then A = Z608;
        case 63 then A = T609; 
        case 64 then A = Z609;
        case 65 then A = T610; 
        case 66 then A = Z610;
        else mprintf("Error");    
    end
    pom=A(:,1);
    celzas=length(pom);                                   // zjisteni poctu zastavek na lince
        for j = 1:zastavek
            if poradi(j,i)>0 then
                for k = 1:zastavek
                    if poradi(k,i)>poradi(j,i) then       // kontrola, ze scitame KVZ nasledujicich zast.
                        if A( poradi(k,i) , poradi (j,i) ) > -1;   // kontrola, ze je splnena promost tr.
                            DPNs(j,i)=DPNs(j,i)+zastavky(k,2);
                        end
                    end
                end 
            end            
        end
    clear A;
    clear pom;
    clear celzas;
end

for j = 1:zastavek
    sumDPN = DPNs(j,:);
    sumDPN = sum(sumDPN);                             // suma kapacit
    for i = 1:linek
        if poradi(j,i)>0 then
            if sumDPN > 0 then                        // kontrola deleni nulou
                DPN(j,i) = DPNs(j,i) ./ sumDPN;       // vypocet DPN
            end
        end
    end
end
clear sumDPN;

// VYPOCET CPN a jednotlivych nastupu Cn

CPN = KPN + DPN;
CPN = CPN ./ 2;

Cn = zeros(zastavek,linek);
for i = 1:linek
    Cn(:,i) = zastavky (:,1) .* CPN (:,i);
end

// VYPOCET VYSTUPUJICICH

for i = 1:linek
    select i                                          // vyber matice prislusne linky
        case 1 then A = T51; 
        case 2 then A = Z51;
        case 3 then A = T52; 
        case 4 then A = Z52;
        case 5 then A = T53; 
        case 6 then A = Z53;
        case 7 then A = T54; 
        case 8 then A = Z54;
        case 9 then A = T55; 
        case 10 then A = Z55;
        case 11 then A = T56; 
        case 12 then A = Z56;
        case 13 then A = T57; 
        case 14 then A = Z57;
        case 15 then A = T58; 
        case 16 then A = Z58;
        case 17 then A = T59; 
        case 18 then A = Z59;
        case 19 then A = T501; 
        case 20 then A = Z501;
        case 21 then A = T502; 
        case 22 then A = Z502;
        case 23 then A = T503; 
        case 24 then A = Z503;
        case 25 then A = T504; 
        case 26 then A = Z504;
        case 27 then A = T505; 
        case 28 then A = Z505;
        case 29 then A = T506; 
        case 30 then A = Z506;
        case 31 then A = T507; 
        case 32 then A = Z507;
        case 33 then A = T509; 
        case 34 then A = Z509;
        case 35 then A = T510; 
        case 36 then A = Z510;
        case 37 then A = T511; 
        case 38 then A = Z511;
        case 39 then A = T512; 
        case 40 then A = Z512;
        case 41 then A = T513; 
        case 42 then A = Z513;
        case 43 then A = T514; 
        case 44 then A = Z514;
        case 45 then A = T515; 
        case 46 then A = Z515;
        case 47 then A = T601; 
        case 48 then A = Z601;
        case 49 then A = T602; 
        case 50 then A = Z602;
        case 51 then A = T603; 
        case 52 then A = Z603;
        case 53 then A = T604; 
        case 54 then A = Z604;
        case 55 then A = T605; 
        case 56 then A = Z605;
        case 57 then A = T606; 
        case 58 then A = Z606;
        case 59 then A = T607; 
        case 60 then A = Z607;
        case 61 then A = T608; 
        case 62 then A = Z608;
        case 63 then A = T609; 
        case 64 then A = Z609;
        case 65 then A = T610; 
        case 66 then A = Z610;
        else mprintf("Error");    
    end    
    pom=A(:,1);
    celzas=length(pom);                                   // zjisteni poctu zastavek na lince
        for m = 1:celzas-1                                // nastupni zastavka
            for n = m+1:celzas                            // vystupni zastavka
                if A(n,m) > -1 then                       // zjisteni splneni kriteria primosti trasy
                    for j = 1:zastavek
                        if poradi(j,i) == m then           // nalezeni nastupni zastavky
                            for k = 1:zastavek
                                if poradi (k,i) == n then  // nalezeni vystupni zastavky
                                    if DPNs(j,i) > 0 then
                                        A(n,m) = zastavky(k,2) ./ DPNs(j,i) .* Cn(j,i);
                                    end                    // vlastni vypocet vystupujicich cest.
                                end
                            end
                        end
                    end
                else
                    A(n,m)=0;                         // prepsani relaci nesplnujici KPT nulou
                end
            end
        end
    select i                                          // vyber prislusne linky a ulozeni matice
        case 1 then T51 = A; 
        case 2 then Z51 = A;
        case 3 then T52 = A; 
        case 4 then Z52 = A;
        case 5 then T53 = A; 
        case 6 then Z53 = A;
        case 7 then T54 = A; 
        case 8 then Z54 = A;
        case 9 then T55 = A; 
        case 10 then Z55 = A;
        case 11 then T56 = A; 
        case 12 then Z56 = A;
        case 13 then T57 = A; 
        case 14 then Z57 = A;
        case 15 then T58 = A; 
        case 16 then Z58 = A;
        case 17 then T59 = A; 
        case 18 then Z59 = A;
        case 19 then T501 = A; 
        case 20 then Z501 = A;
        case 21 then T502 = A; 
        case 22 then Z502 = A;
        case 23 then T503 = A; 
        case 24 then Z503 = A;
        case 25 then T504 = A; 
        case 26 then Z504 = A;
        case 27 then T505 = A; 
        case 28 then Z505 = A;
        case 29 then T506 = A; 
        case 30 then Z506 = A;
        case 31 then T507 = A; 
        case 32 then Z507 = A;
        case 33 then T509 = A; 
        case 34 then Z509 = A;
        case 35 then T510 = A; 
        case 36 then Z510 = A;
        case 37 then T511 = A; 
        case 38 then Z511 = A;
        case 39 then T512 = A; 
        case 40 then Z512 = A;
        case 41 then T513 = A; 
        case 42 then Z513 = A;
        case 43 then T514 = A; 
        case 44 then Z514 = A;
        case 45 then T515 = A; 
        case 46 then Z515 = A;
        case 47 then T601 = A; 
        case 48 then Z601 = A;
        case 49 then T602 = A; 
        case 50 then Z602 = A;
        case 51 then T603 = A; 
        case 52 then Z603 = A;
        case 53 then T604 = A; 
        case 54 then Z604 = A;
        case 55 then T605 = A; 
        case 56 then Z605 = A;
        case 57 then T606 = A; 
        case 58 then Z606 = A;
        case 59 then T607 = A; 
        case 60 then Z607 = A;
        case 61 then T608 = A; 
        case 62 then Z608 = A;
        case 63 then T609 = A; 
        case 64 then Z609 = A;
        case 65 then T610 = A; 
        case 66 then Z610 = A;
        else mprintf("Error");    
    end    
    clear A;
    clear pom;
    clear celzas;
end
