clc;
%bdclose all;
slCharacterEncoding('Shift_JIS');
clear all;
close all;
set_MILS_const
InitBreach;
mdl = 'FC_MILS_LN3';
BrAPC = BreachSimulinkSystem(mdl);
 
%BrAPC.PrintParams()
BrAPC.PrintSignals()
APC_falsify= BrAPC.copy(); % Creates a copy of the interface object

% setting input profiles
Temp_gen = constant_signal_gen({'Temp'}); %,1 ,{'previous'});
Temp1_gen = constant_signal_gen({'Temp1'}); %,1 ,{'previous'});
FCI_gen = step_signal_gen({'FCI'});
FCI1_gen = step_signal_gen({'FCI1'});
input_gen = BreachSignalGen({Temp_gen, Temp1_gen, FCI_gen, FCI1_gen});
APC_falsify.SetInputGen(input_gen);
%APC_falsify.PlotSignals({'Temp', 'Temp1', 'FCI', 'FCI1'})
APC_falsify.SetParam({'FCI_step_base','FCI1_step_base', 'FCI_step_time', 'FCI1_step_time'}, [0 0 5 5]);
APC_falsify.PrintParams()


%FTF_falsify.SetParamRanges({'speed_att_u0'},[0 600]);
APC_falsify.SetParamRanges({'Temp_u0','Temp1_u0','FCI_step_amp','FCI1_step_amp'},[-30 90; -30 90; 10 500; 10 500]);
STL_ReadFile('APC_2.stl')
tic
%falsify_pb = FalsificationProblem(APC_falsify, ptemp);
falsify_pb = FalsificationProblem(APC_falsify, good_APC);
falsify_pb.setup_solver('cmaes');
falsify_pb.max_time = inf;
falsify_pb.max_obj_eval = 500;
%falsify_pb.setup_solver('fminsearch');
res = falsify_pb.solve();
toc
%falsify_pb.max_time = 100; % Run Simulink simulation until time=30s
AFC_False = falsify_pb.GetBrSet_False(); % AFC_False contains the falsifying trace
AFC_False.PlotSignals({'Temp', 'Temp1', 'FCI', 'FCI1', 'AFR_actual','AFR_actual1'}, [], {'LineWidth', 1.3});

% subplot(3,1,1); set(gca, 'YLim', [0 600], 'FontSize',12, 'LineWidth', 1.3);
% ylabel('Speed', 'FontSize', 12)
% grid off
% box on
% subplot(3,1,2); set(gca, 'YLim', [0 30], 'FontSize',12, 'LineWidth', 1.3);
% ylabel('$$w_{\lambda_1}$$','Interpreter','latex', 'FontSize', 14)
% plot([0 50], [11.68 11.68],'r', 'LineWidth', 1);
% plot([0 50], [17.52 17.52],'r', 'LineWidth', 1);
% grid off
% box on
% subplot(3,1,3); set(gca, 'YLim', [0 30], 'FontSize',12, 'LineWidth', 1.3);
% ylabel('$$w_{\lambda_2}$$','Interpreter','latex', 'FontSize', 14)
% xlabel('Time (seconds)', 'FontSize', 12)
% plot([0 50], [11.68 11.68],'r', 'LineWidth', 1);
% plot([0 50], [17.52 17.52],'r', 'LineWidth', 1);
% grid off
% box on
%set(gca, 'xticks',[10 20 30 40 50]);

