ex = exist('XData', 'var');
if ex == 0
    load('essentials.mat');
end

pFL = repelem(EXPORT.pTyre_FL(2,:),101);
pFL = pFL(1:50128);

lap = repelem([1,2,3,4,5],10100);
lap = lap(1:50128);

aXRT = EXPORT.aXRT(2,:);
aYRT = EXPORT.aYRT(2,:);
crv = EXPORT.curvRT(2,:);
vXRT = EXPORT.vXRT(2,:);

%pri zmenseni poctu vstupu, smazat inicializovane promenne s vetsim poctem
%vstupu (XData zavisle)
XData = [steering; pBrakeF; AccelSlip; sa1; pFL; rpmFL; lap];
%XData = [rpmFL;];
% XData = [aXRT; aYRT; crv];
YData = mean([tI;tO;tCI;tCO]);
% YData = vXRT;

%normalizace dat
XData_n = zeros(size(XData,1),size (XData,2));
mux = zeros(1,size(XData,1));
sigx = zeros(1,size(XData,1));
for i = 1:size(XData,1)
    mux(i) = mean(XData(i,:));
    sigx(i) = std(XData(i,:));
    for j = 1:size (XData,2)
        XData_n(i,j) = (XData(i,j) - mux(i)) / sigx(i);
    end
end

YData_n = zeros(size(YData,1),size (YData,2));
muy = zeros(1,size(YData,1));
sigy = zeros(1,size(YData,1));
for i = 1:size(YData,1)
    muy(i) = mean(YData(i,:));
    sigy(i) = std(YData(i,:));
    for j = 1:size (YData,2)
        YData_n(i,j) = (YData(i,j) - muy(i)) / sigy(i);
    end
end

idxTrainStart = 1;
idxTrainStop = 10000;
idxValStart = 25001;
idxValStop = 30000;
idxTestStart = 1;
idxTestStop = 50000;

XTrain = XData_n(:,idxTrainStart:idxTrainStop);
YTrain = YData_n(:,idxTrainStart:idxTrainStop);

XVal = XData_n(:,idxValStart:idxValStop);
YVal = YData_n(:,idxValStart:idxValStop);

XTest = XData_n(:,idxTestStart:idxTestStop);
YTest = YData_n(:,idxTestStart:idxTestStop);

numResponses = size(YData_n,1);
featureDimension = size(XData_n,1);
numHiddenUnits = 7;
maxEpochs = 100;
InitialLearnRate = 0.002;
LearnRateDropPeriod = 40;
LearnRateDropFactor = 0.9;

layers = [ ...
    sequenceInputLayer(featureDimension)
    lstmLayer(numHiddenUnits)
    fullyConnectedLayer(50) % overfitting
    dropoutLayer(0.5) % overfitting
    fullyConnectedLayer(numResponses)
    regressionLayer];

options = trainingOptions('adam', ...
    'MaxEpochs',maxEpochs, ...
    'GradientThreshold',1, ...
    'InitialLearnRate',InitialLearnRate, ...
    'LearnRateSchedule','piecewise', ...
    'LearnRateDropPeriod',LearnRateDropPeriod, ...
    'LearnRateDropFactor',LearnRateDropFactor, ...
    ...'ValidationData',{XVal,YVal},...
    ...'ValidationFrequency',5,...
    ...'ValidationPatience',10,...
    'Verbose',0,...
    'Plots','training-progress'...
    ...'ExecutionEnvironment','cpu' ... %never again
    );

[net,netinfo] = trainNetwork(XTrain,YTrain,layers,options);

YPred_n = predict(net,XTest);

%denormalizace
YPred = zeros(size(YPred_n,1),size(YPred_n,2));
YTest_re = zeros(size(YPred_n,1),size(YPred_n,2));
for i = 1:size(YPred_n,1)
    for j = 1:size(YPred_n,2)
        YPred(i,j) = YPred_n(i,j) * sigy(i) + muy(i);
        YTest_re(i,j) = YTest(i,j) * sigy(i) + muy(i);
    end
end

f1 = figure;
plot(1:length(YTest_re),YTest_re(1,:),1:length(YPred),YPred(1,:))
hA = gca;
hA.LooseInset = hA.TightInset;
legend("Ytest","Ypred");
grid on;
set(f1, 'Position', [100, 100, 900, 500])

a1_rmse_train = netinfo.TrainingRMSE(end);
%a0_rmse_train_manual = sqrt(sum(((YTest(idxTrainStart:idxTrainStop) - YPred_n(idxTrainStart:idxTrainStop)).^2)/(idxTrainStop - (idxTrainStart-1))));
a2_rmse_test = sqrt(sum(((YTest(idxTrainStop:idxTestStop) - YPred_n(idxTrainStop:idxTestStop)).^2)/(idxTestStop - idxTrainStop)));
a3_rmse_whole = sqrt(sum(((YTest - YPred_n).^2)/(idxTestStop - (idxTestStart-1))));




















