8.clear all, close all, clc

global CF ParamOpt
CF = [];
ParamOpt = [];
% MAin script- spustenim toho scriptu dojde k optimalizaci parametru modelu
% pipety navrzene v abaqusu
delete('AbqFiles\\*')
%Working Directories
WorkingDirectory = "workingDirectory"; %nutno doplnit
WorkingDirectoryAbq = WorkingDirectory + "\\AbqFiles"; 
PythonScript = "MKP_model.py"; %nutno změnit

%Vymazani prebytecnych files
% delete('AbqFiles\\*')
% delete('abaqus_acis.log')
% delete('abaqus.rpy')
% n=8;
% for i = 1:n
%           delete("abaqus.rpy."+num2str(i))
%           delete("abaqus"+num2str(i)+".rec")
% end

%Cas vypoctu 'tic-toc'
tic
% Nacteni frekvenci z mereni pipety laserovym vibrometrem
FrequencyMeasured

%Neoptimalizované Parametry pro python script
NumOfSeeds = 6; %pocet seeds na 1/4 obvodu pipety
NumOfFreq = 22; %pocet napocitanych vlastnich frekvenci, nutno zacinat az druhou vlastni
NumUseProcessor = 2; %pocet procesoru pro vypocet
GPGPUAcceleration = 1;  
%Koeficienty pro definovani promeneho tlousky Lb delky dle rovnice!!!
%prepocitano v pythonu
% Koef1+Koef2X+Koef3*X^2+Koef4*X^3
Koef1 = 0.9;
Koef2 = 0.017;
Koef3 = 0;
Koef4 = 0;

konst_parametry = [NumOfSeeds, NumOfFreq , NumUseProcessor, GPGPUAcceleration,...
                    Koef1, Koef2 ,Koef3 ,Koef4]

% pocatecni predpoklad parametru pro optimalizaci
% pocet optimalizacnich paremetru = 13, parametry = 1x13
La_short = 1; %konec pipety v mm, tenka cast s kolenem
Lb_linear = 8.7; % delka pipety s linearni tlouskou
Lc_thick = 48; %dlouha delka tlusteho konce pipety s konst tlouskou
Le_connect = 0.01; % navazující délka pipety mezi kolenem a linearni casti
R_prum = 0.425; %průmerny polomer tluste casti 
rPrumer = 0.003; %prumerny polomer tenke casti pip
ThicknessLongPart = 0.15; % tlouska steny dlouhe casti
ThicknessShortPart = 0.001; %tlouska steny tenke casti
RElbow = 0.3; %polomer kolene pipety !!!!!!!!!! funguje do 0.3
alpha = 35; % uhel ohnuti pipety
%Material parameters Borosilicate glass
Rho = 2.23E-9; %tuna/mm^2
PoissonRatio = 0.2;
EModul = 64000; %Mpa

parametry = [La_short, Lb_linear, Lc_thick, Le_connect, R_prum, rPrumer,...
          ThicknessLongPart, ThicknessShortPart, RElbow, alpha,...
          Rho, PoissonRatio, EModul];
%Spodni hranice hodnot optimalizovanych parametru, 10% nahoru i dolu, 20% u
%Le_connect, 50% u zaoblení kolene
C1 = 0.01; %1%
C2 = 0.03; %3% 
C3 = 0.05; %5% pro uhel zahnutí pipety
C4 = 0.1; %10%
C5 = 0.15; %15%
C6 = 0.2; %20% pro Le_connect 
C7 = 0.5; %pro zaobleni kolene 50%

parametryChange = [C4 C4 C4 C6 C4 C4 C4 C4 C7 C3 C4 C4 C4];
lowerBounds = parametry - parametry.*parametryChange;
upperBounds = parametry + parametry.*parametryChange;


%% 

%%Optimalizacni algoritmus, geneticky algoritmus
%opt = gaoptimset('Display','iter','PlotFcns',{@gaplotbestf,@gaplotstopping}, 'MaxGenerations', 20, 'PopulationSize', 2);

opt = optimoptions('ga','PlotFcn', {@gaplotbestf,@gaplotstopping}, 'MaxGenerations', 50, 'PopulationSize', 60, 'MaxStallGenerations', 5);

[p_opt, Kriterialni_fce] = ga(@(p)Kriterialni_fce_Ga(p, FreqMeasured, PythonScript, WorkingDirectoryAbq, WorkingDirectory, konst_parametry),...
                              numel(parametry), [], [], [], [],...
                              lowerBounds, upperBounds,[],[],opt);

fprintf('The number of generations is: %d\n', Output.generations);
fprintf('The number of function evaluations is: %d\n', Output.funccount);
fprintf('The best function value found is: %g\n', Fval);

Opt3_12 = [Kriterialni_fce, p_opt] 
save Vysledky\\Opt3_12 Opt3_12
save Vysledky\\Frequence1_12Abq FreqModel
savefig 'Genetic Algorithm'
toc

