clear all; close all; clc;
backup=true;
if (backup)
    clear all; close all; clc;
    try
    run files\backup.m;
    catch
        disp('Nebylo mozno nacist skript backup.m...');
        disp('Bud neexistuje nebo je zadana spatna cesta...');
        return;
    end
else
pocet_iteraci = 10;
pocet_castic = 2;%30
pocet_vstupu = 4;
%---------------------------------------------------------------------
tloustka = 1;       %tloustka zarezu
delka = 6;         %delka zarezu
frekvence = 2;      %vypocitana v poradi frekvence
sirka = 37.7;       %sirka anteny
vyska = 28.4;       %vyska anteny
ep = 4.4;           %epsilon_r
solnum = 2;
%-------------------funkce---------------------------------------------

delta_t = 1;
c1 = 1.49;
c2 = 1.49;
K=0.729;
fi_1 = 2.8;
fi_2 = 1.3;
%---------------zadane frekvence---------------------------------------
fr1 = 1.8e+009;
fr2 = 1.8e+009;
%----------------------------------------------------------------------

evalute = ones(pocet_castic,1); %vektor kriterialnich funkci
DL = [1 4 6 8 9 10 11 12];
TL = [0.1 0.2 0.5 1 1.5 1.6 2 2.5];
bound = [30 70; 25 55 ];	% hranice
m = 1;
g = 1;
w = linspace(0.9,0.4,pocet_iteraci); %pouziti k treti variante

walls = 'reflect';
weight = 'original';

maximum = 0;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 1. Nahodne rozmisteni castic a jejich rychlosti
P(:,1) = bound(1,1) + 10.* rand( pocet_castic,1);                      % Sirka
P(:,2) = bound(2,1) + 5.* rand( pocet_castic,1);                       % Vyska
P(:,3) = DL(:,1+bi2de((round( rand( pocet_castic, 3))),'left-msb'))';  % Delka_zarezu
P(:,4) = TL(:,1+bi2de((round(rand(pocet_castic,3))),'left-msb'))';     % Tloustka_zarezu


v(:,1) = -0.2 + 1.* rand( pocet_castic,1);
v(:,2) = -0.1 + 1.* rand( pocet_castic,1);
%  ve vektorovem prostoru
start = 1;
end

while  (maximum ~= pocet_iteraci) | (best == 0)

    % 2. Fitness evalution

for i = 1 : pocet_castic
    Frekv = antenaCST(P( i, 1), P( i, 2), P( i, 3), P( i, 4));
    F(i,1) = sqrt(real((Frekv-fr1)^2) + (Frekv-fr2)^2);
end
%    plot([maximum, F(:,1)])
%    hold on
    % 2a. Fitness evalution - 'INVISIBLE' walls

    switch walls;
        case 'invisible';

            for i = 1:pocet_castic
                if evalute(i) == 1
                    P(i, pocet_vstupu+1) = F(i,1);
                else
                    P(i, pocet_vstupu+1) = F_old(i,1);
                end
            end
            % 2b.,2c. - pro parametry 'ABSORB' a 'REFLECT' zde neni treba zmeny, vycisleni kriterialni funkce probehne normalne
        case 'absorb';
            P(:, pocet_vstupu+1) = F(:,1);
        case 'reflect';
            P(:, pocet_vstupu+1) = F(:,1);
    end


    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    if start == 1
        P_best=P;                                             % Inicializace matice lokalniho minima F, pro kazdou castici
        [val,ind] = min(P_best( :,pocet_vstupu+1));            % Hledani globalniho minima F v prvni iteraci
        best = val;
        G_vect = P_best(ind,:);
    end

    % 3. Hledani lokalniho minima F
    if start == 0
        for i=1:length(P(:,1))
            if P(i,(pocet_vstupu+1)) <= P_best(i,pocet_vstupu+1)
                for j=1:pocet_vstupu+1
                    P_best(i,j)=P(i,j);
                end
            end
        end
    end

    % 4.Hledani globalniho minima F ve vsech iteracich

    if start == 0
        [val,ind] = min(P_best( :,pocet_vstupu+1));
        if val    < best
            best   = val;
            G_vect = P_best(ind,:);
        end
    end
    start = 0;

    for i = 1:pocet_castic;
        G_best(i,:) = G_vect(1,:);                       % Globalni minimum je pro vsechny castice stejne
    end

    G(g) = G_best(1,5);
    g = g + 1;


    %5. Modifikace rychlosti castic v zavislosti na predchozich global a local
    %   best

    switch  weight;

        %5a. Konstantni inercialni vaha (Clerc a Kennedy)
        case 'constant'

            for i = 1:pocet_castic
                for j=1:2                             % j=1:N (pocet rozmeru problemu)
                    v(i,j) = K.*(v(i,j)+fi_1.*rand(1).*(P_best(i,j)-P(i,j))+fi_2.*rand(1).*(G_best(i,j)-P(i,j)));
                end
            end

            %5b. Linearne snizovana inercialni vaha (Eberhart a Shi)
        case 'variable'

            for i = 1:pocet_castic
                for j=1:2
                    v(i,j) = w(m).*v(i,j)  + c1*rand(1).*(P_best(i,j)-P(i,j)) + c2.*rand(1).*(G_best(i,j)-P(i,j));
                end
            end
            m = m + 1;


            %5c. Max. rychlost nastavena na 10-20% dynamickeho rozsahu v kazde dimenzi
            % prohledavaneho prostoru reseni (Eberhart a Shi)
        case 'original'
            n = 1
            for i = 1:pocet_castic
                for j=1:2
                    v(i,j) = v(i,j)  + 2.*rand(1).*(P_best(i,j)-P(i,j)) + 2.*rand(1).*(G_best(i,j)-P(i,j));
                    if v(i,j) > n.*(bound(j,2) - bound(j,1))
                        v(i,j) = n.*(bound(j,2) - bound(j,1));
                    end
                end
            end
    end





    switch  walls;
        %6a. Update pozic castic 'INVISIBLE' walls
        case 'invisible';

            for i = 1:pocet_castic
                for j = 1:2
                    P(i,j) = P(i,j) + v(i,j).*delta_t;
                    if (P(i,j) < bound(j,1) ) | (P(i,j) > bound(j,2))
                        evalute(i) = 0;
                        break
                    else
                        evalute(i) = 1;
                    end
                end
            end

            %6b. Update pozic castic 'ABSORB'ing walls
        case  'absorb';

            for i = 1:pocet_castic
                for j = 1:2
                    P(i,j) = P(i,j) + v(i,j).*delta_t;
                    if P(i,j) < bound(j,1)
                        P(i,j) = bound(j,1);
                        v(i,j) = 0;
                    elseif P(i,j) > bound(j,2)
                        P(i,j) = bound(j,2);
                        v(i,j) = 0;
                    end
                end
            end

            %6c. Update pozic castic 'REFLECT'ing walls
        case  'reflect';

            for i = 1:pocet_castic
                for j = 1:2
                    P(i,j) = P(i,j) + v(i,j).*delta_t;
                    if P(i,j) < bound(j,1)
                        P(i,j) = 2*bound(j,1) - P(i,j);
                    elseif P(i,j) > bound(j,2)
                        P(i,j) = 2*bound(j,2) - P(i,j);
                    end
                end
            end
    end

    F_old = (P(:,pocet_vstupu+1));
    P = P(:,1:pocet_vstupu); %odstraneni funkce F, kvuli vstupu do neuronove site

    maximum = maximum + 1;
    PSOBACKUPFILE(pocet_castic,pocet_iteraci,pocet_vstupu,maximum,g,best,G,G_best,G_vect,P,P_best,F,v,[fr1,fr2],walls,weight,delta_t,[c1,c2],K,[fi_1,fi_2],evalute,bound,w,start);
end
best;
G_vect(1,1:pocet_vstupu)
% plot([maximum, F]);
% hold on;

