diff --git a/Examples/SecondOrder/container.mat b/Examples/SecondOrder/container.mat index 32ab565..6ae1591 100644 Binary files a/Examples/SecondOrder/container.mat and b/Examples/SecondOrder/container.mat differ diff --git a/Examples/SecondOrder/run_example.m b/Examples/SecondOrder/run_example.m new file mode 100644 index 0000000..4730692 --- /dev/null +++ b/Examples/SecondOrder/run_example.m @@ -0,0 +1,85 @@ +%% ==========Moving to the example folder========== + + +[str,~,~] = fileparts(mfilename('fullpath')); +oldFolder=cd(str); + +%% ==========Reference data settings========== + +% Create a RaPIdObject (optional but recommended - will work with just a structure) +RaPIdObject=RaPIdClass(); + +%Output data +RaPIdObject.experimentData.pathToReferenceData = 'measuredDataO.mat'; %Data file name +RaPIdObject.experimentData.expressionReferenceTime = 'time'; %Time variable name +RaPIdObject.experimentData.expressionReferenceData = 'signal'; %Data variable name + +%Input data +RaPIdObject.experimentData.pathToInData = ''; + +%% ==========Experiment settings========== + +%General settings +RaPIdObject.experimentSettings.tf = 50; %Simulation length +RaPIdObject.experimentSettings.ts = 0.01; %Sampling time +RaPIdObject.experimentSettings.t_fitness_start = 4; %Start calculating fintess function after t_fintess_start +RaPIdObject.experimentSettings.timeOut = 2; %Seconds before simulation timeout +RaPIdObject.experimentSettings.integrationMethod = 'ode45'; %Solver selection +RaPIdObject.experimentSettings.solverMode = 'Simulink'; +RaPIdObject.experimentSettings.optimizationAlgorithm = 'pso'; %Selection of optimization algorithm +RaPIdObject.experimentSettings.maxIterations = 100; %Maximum number of estimation iterations +RaPIdObject.experimentSettings.verbose = 1; %Can trigger more data for debugging +RaPIdObject.experimentSettings.saveHist = 0; %Don't save history + +%Model related settings +RaPIdObject.experimentSettings.pathToSimulinkModel = 'variable_rafael.mdl'; %Simulink model file name +RaPIdObject.experimentSettings.pathToFMUModel = 'Rafael_0original_0estimated.fmu'; %FMU file name +RaPIdObject.experimentSettings.modelName = 'variable_rafael'; %Simulink model name +RaPIdObject.experimentSettings.blockName = 'variable_rafael/Rafael_original_estimated'; %FMU name +RaPIdObject.experimentSettings.scopeName = 'simout'; %Result sink name +RaPIdObject.experimentSettings.displayMode = 'Show'; + +%Estimation parameter settings +RaPIdObject.experimentSettings.p_0 = [0.3, 4.1, 1.7, 1.1]; %Initial parameter guess +RaPIdObject.experimentSettings.p_min = [0.1, 3.9, 1.5, 1.1]; %Minimum values of parameters +RaPIdObject.experimentSettings.p_max = [1, 4.2, 1.7, 1.5]; %Maximum values of parameters + +%Fitness function settings +RaPIdObject.experimentSettings.cost_type = 1; %Fitness function selection +RaPIdObject.experimentSettings.objective_weights = 1; %Weights of the output signals for fitness function + +%% ==========Optimization Algorithm settings========== + +switch RaPIdObject.experimentSettings.optimizationAlgorithm + case 'pso' + RaPIdObject.psoSettings.w = 0.25; %Particle inertia weight + RaPIdObject.psoSettings.self_coeff = 0.25; %Self recognition coefficient + RaPIdObject.psoSettings.social_coeff = 0.25; %Social coefficient + RaPIdObject.psoSettings.limit = 0.25; %Iteration limit + RaPIdObject.psoSettings.nRandMin = 8; %Minimum number of random particles + RaPIdObject.psoSettings.nb_particles = 8; %Number of particles + RaPIdObject.psoSettings.fitnessStopRatio = 1e-5; %Fitness stop ratio + RaPIdObject.psoSettings.kick_multiplier = 0.002; %Kick multiplier + RaPIdObject.psoSettings.method = 'PSO'; +end + +%% ==========FMU parameters, inputs and outputs========== + +RaPIdObject.parameterNames = {'transferFunction.b[1]','transferFunction.a[1]','transferFunction.a[2]','transferFunction.a[3]'}; +RaPIdObject.fmuInputNames = {}; +RaPIdObject.fmuOutputNames = {'y1'}; %Output variable names + +%% ==========Running the computation========== + +%Opening simulink model +open_system(RaPIdObject.experimentSettings.pathToSimulinkModel); %Opening the simulink model +open_system(strcat(RaPIdObject.experimentSettings.modelName,'/Scope')); %Opening the scope in the model to observe estimation process +pause(1); %Waiting one second for scope to initialize +%% + +%Starting the estimation process +[sol, hist] = rapid(RaPIdObject); +sprintf('Vector of estimated parameters is: %s',mat2str(sol,3)) +%Restoring workspace +cd(oldFolder); + diff --git a/Examples/SecondOrder/variable_rafael.mdl b/Examples/SecondOrder/variable_rafael.mdl index a6371ae..4ee5961 100644 --- a/Examples/SecondOrder/variable_rafael.mdl +++ b/Examples/SecondOrder/variable_rafael.mdl @@ -6,7 +6,7 @@ Model { NumRootInports 0 NumRootOutports 0 ParameterArgumentNames "" - ComputedModelVersion "1.22" + ComputedModelVersion "1.24" NumModelReferences 0 NumTestPointedSignals 0 } @@ -39,7 +39,7 @@ Model { DockPosition "Left" Width [50] Height [50] - Filter [8] + Filter [9] } Object { $PropName "ExplorerBarInfo" @@ -56,7 +56,7 @@ Model { LoadSaveID "0" Extents [1512.0, 869.0] ZoomFactor [3.5635593220338988] - Offset [-2.1474435196194435, 50.071343638525562] + Offset [-2.1474435196194577, 50.071343638525548] } } } @@ -66,9 +66,9 @@ Model { ModifiedByFormat "%" LastModifiedBy "Tin Rabuzin" ModifiedDateFormat "%" - LastModifiedDate "Fri Feb 19 15:13:42 2016" - RTWModifiedTimeStamp 377795587 - ModelVersionFormat "1.%" + LastModifiedDate "Tue Feb 23 09:54:14 2016" + RTWModifiedTimeStamp 378122050 + ModelVersionFormat "1.%" ConfigurationManager "None" SampleTimeColors off SampleTimeAnnotations off @@ -850,8 +850,8 @@ Model { Position [310, 207, 350, 263] ZOrder -3 Floating off - Location [207, 743, 907, 1016] - Open on + Location [389, 469, 1296, 987] + Open off NumInputPorts "1" List { ListType AxesTitles @@ -859,12 +859,12 @@ Model { } List { ListType ScopeGraphics - FigureColor "[0.501960784313725 0.501960784313725 0.501960784313725]" - AxesColor "[0 0 0]" - AxesTickColor "[1 1 1]" - LineColors "[1 1 0;1 0 1;0 1 1;1 0 0;0 1 0;0 0 1]" + FigureColor "[1 1 1]" + AxesColor "[1 1 1]" + AxesTickColor "[0 0 0]" + LineColors "[0 0 1;1 0 0;0 1 1;1 0 0;0 1 0;0 0 1]" LineStyles "-|-|-|-|-|-" - LineWidths "[0.5 0.5 0.5 0.5 0.5 0.5]" + LineWidths "[2 2 0.5 0.5 0.5 0.5]" MarkerStyles "none|none|none|none|none|none" } ShowLegends off @@ -942,7 +942,7 @@ MatData { "6UE '-T;W!4:6UE !T;VQE0 . , 8 ( !@ % \" 0 ) X P !" "@ @ ) @ 4 ( 0 $ ! ( 0 ! #@ #@ & \" 8 !0 @ ! 0" - " $ \"0 @ 400 X P !@ @ ) @ 4 ( 0 $ ! ( 0 ! #@ " + " $ \"0 @ @ D(0 X P !@ @ ) @ 4 ( 0 $ ! ( 0 ! #@ " " % & \" 0 !0 @ ! &0 $ $ !D !2869A96Q?;W)I9VEN86Q?97-T:6UA=&5D " " X P !@ @ ) @ 4 ( 0 $ ! ( 0 ! #@ (@ & \" ( !0 " " @ ! 0 $ !0 $ T ! #0 &9M:6QL;V=L979E; . . 8 ( ! % \" " @@ -1122,17 +1122,17 @@ MatData { "871H &9O;&1EFEP961P871H ')E;&%T:7" "9E9FUU9F]L9&5R &1L;'!A=&@ 'AM;'!A=&@ %)E;&]A9 %)E;&]A9$-P" "='( $YE=F5R3&]A9&5D $YO1&QL4&QA=&9O +% +% Copyright 2015 Luigi Vanfretti, Achour Amazouz, Maxime Baudette, +% Tetiana Bogodorova, Jan Lavenius, Tin Rabuzin, Giuseppe Laera, +% Francisco Gomez-Lopez +% +% The authors can be contacted by email: luigiv at kth dot se +% +% This file is part of Rapid Parameter Identification ("RaPId") . +% +% RaPId is free software: you can redistribute it and/or modify +% it under the terms of the GNU Lesser General Public License as published by +% the Free Software Foundation, either version 3 of the License, or +% (at your option) any later version. +% +% RaPId is distributed in the hope that it will be useful, +% but WITHOUT ANY WARRANTY; without even the implied warranty of +% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +% GNU Lesser General Public License for more details. +% +% You should have received a copy of the GNU Lesser General Public License +% along with RaPId. If not, see . + +classdef RaPIdClass assign version=1 + s.version=double(~isempty(isprop(s, 'version'))); % 0 if really old, else 1 - old + obj = RaPIdClass(s); % update object + elseif s.version <1.41 % check if latest ver + obj = RaPIdClass(s); % update object + else + obj=s; % everything is up to date + end + catch err + disp(err) + disp(struct2table(err.stack)) + end + end + + end + + methods + function obj = RaPIdClass(varargin) %Constructor + if nargin == 0 %Create a new object of the latest version + try + obj=initRapidObj(obj); + catch + %NOP, will still give a an object + end + elseif isa(varargin{1},'RaPIdClass') && varargin{1}.version==1.41 + %NOP, up to date + else % Old version, try to update + obj=RaPIdClass(); % New object + obj=obj.updateRapidObj(varargin{1}); % update old object + end + end + function obj = saveobj(obj) + + end + end +end \ No newline at end of file diff --git a/Sources/core/@RaPIdClass/initRapidObj.m b/Sources/core/@RaPIdClass/initRapidObj.m new file mode 100644 index 0000000..ab60d26 --- /dev/null +++ b/Sources/core/@RaPIdClass/initRapidObj.m @@ -0,0 +1,37 @@ +function obj = initRapidObj(obj) +% INITRAPIDOBJ this function is meant to create a default rapidobject +% Detailed explanation goes here +obj.experimentSettings=struct('tf',1,'ts',0.1,'p_min',[],'p_max',[],'p_0',[],... + 'cost_type',1,'objective_weights',1,'t_fitness_start',0,... + 'integrationMethod','ode45','maxIterations',100,'solverMode','Simulink',... + 'pathToSimulinkModel','','modelName','','blockName','','scopeName','simout',... + 'verbose',1,'displayMode','hide','saveHist',0,'timeOut',2,... + 'pathToFmuModel','','outputPostProcessing','','optimizationAlgorithm','pso',... + 'pathToFMUModel',''); +obj.experimentData = struct('pathToInData','','expressionInData','',... + 'expressionInDataTime','','referenceOutdata',[],'referenceTime',[],... + 'pathToReferenceData','','expressionReferenceTime','','expressionReferenceData',''); +obj.psoSettings=struct('w',0.25,'self_coeff',0.25,'social_coeff',0.25,... + 'limit',0.2500,'nRandMin',8,'nb_particles',8,'fitnessStopRatio',1e-5,... + 'kick_multiplier',0.002,'w_min', 0.01, 'w_max',1,'method','PSO'); +obj.gaSettings=struct('nbCromosomes',40,'nbCroossOver1',10,'nbCroossOver2',10,... + 'nbMutations',10,'nbReproduction',10,'limit',10,'fitnessStopRatio',1e-5,... + 'headSize1',5,'headSize2',5,'headSize3',3,'nbReinjection',5,'nRandMin',10, ... + 'p0s', [],'storeData',0); +obj.combiSettings=struct('firstMethod','pso','psoExtOptions','nm','secondMetod','nm'); +obj.naiveSettings=struct('tolerance1',1e-4,'tolerance2',1e-7,'iterations',1,'iterations2',1,'iterations3',1); +obj.knitroSettings=struct('path2Knitro','','knOptionsFile','', ... + 'knOptions','optimset(''Algorithm'',''active-set'',''TolFun'',1e-6,''TolX'',1e-6);'); +obj.nmSettings='optimset(''fminsearch'')'; +obj.cgSettings='optimset(optimset(''fminunc''),''FinDiffType'',''central'',''TolX'',1e-6,''LargeScale'',''off'')'; +obj.psoExtSettings='psoptimset'; +obj.gaExtSettings='gaoptimset'; +obj.fminconSettings='optimset(''FinDiffRelStep'',0.1)'; +obj.pfSettings=struct('nb_particles', 100,'prune_threshold', 0.1,'kernel_sigma',1); +obj.resultData=[]; +obj.fmuOutputNames={''}; +obj.parameterNames={''}; +obj.fmuInputNames={''}; +obj.version=1.41; +end + diff --git a/Sources/core/@RaPIdClass/updateRapidObj.m b/Sources/core/@RaPIdClass/updateRapidObj.m new file mode 100644 index 0000000..e2aaac2 --- /dev/null +++ b/Sources/core/@RaPIdClass/updateRapidObj.m @@ -0,0 +1,80 @@ +function obj = updateRapidObj(obj,oldThing) +% UPDATERAPIDOBJ this function will update old rapid-objects +% Detailed explanation goes here +objversion=oldThing.version; %old version number +%% This part takes care of updating old structs to rapidobjects +if isstruct(oldThing) && objversion==0 %to convert some very old mySettings-structs + obj.psoSettings = oldThing.pso_options; + obj.naiveSettings = oldThing.naive_options; + obj.gaSettings = oldThing.ga_options; + obj.combiSettings = oldThing.combiOptions; + obj.knitroSettings = oldThing.kn_options; + obj.nmSettings = oldThing.nmOptions; + obj.cgSettings = oldThing.cgOptions; + obj.psoExtSettings = oldThing.psoExtOptions; + obj.gaExtSettings = oldThing.gaExtOptions; + obj.fminconSettings = oldThing.fminconOptions; + obj.fmuOutputNames = oldThing.fmuOutData; + obj.parameterNames = oldThing.parameterNames; + obj.experimentSettings = struct('tf',oldThing.tf,'ts',oldThing.Ts,... + 'p_min',oldThing.p_min,'p_max',oldThing.p_max,'p_0',oldThing.p0,'cost_type',oldThing.cost,'objective_weights',oldThing.objective.vect,... + 't_fitness_start',oldThing.t0_fitness,'integrationMethod',oldThing.intMethod, ... + 'pathToSimulinkModel',oldThing.path2simulinkModel,'modelName',oldThing.modelName,'blockName',oldThing.blockName,... + 'scopeName',oldThing.scopeName,'verbose',oldThing.verbose,'saveHist', false,'optimizationAlgorithm',oldThing.methodName); + obj.experimentData = struct('referenceOutdata',oldThing.realData,'referenceTime',oldThing.realTime,'pathToReferenceData',oldThing.path2data,... + 'expressionReferenceTime',oldThing.dataT,'expressionReferenceData',oldThing.dataY); + obj.experimentSettings.outputPostProcessing=@(x)x; %function handle, do nothing as standard + if isfield(oldThing,'displayMode') + obj.experimentSettings.displayMode = oldThing.displayMode; + end + if isfield(oldThing,'nbMaxIterations') + obj.experimentSettings.maxIterations = oldThing.nbMaxIterations; + end + if isfield(oldThing, 'mode') + obj.experimentSettings.solverMode = oldThing.mode; + end + if isfield(oldThing,'fmuInputNames') + obj.fmuInputNames = oldThing.fmuInputNames; + end + if isfield(oldThing,'path2fmuModel') + obj.experimentSettings.pathToFmuModel = oldThing.path2fmuModel; + end + if isfield(oldThing,'pf_options') && ~isempty(oldThing.pf_options) + obj.pfSettings = oldThing.pf_options; + end + if isfield(oldThing,'inDat') + oldThing.inDat = struct('path',[],'signal',[],'time',[]); + end + obj.experimentData.pathToInData = oldThing.inDat.path; + obj.experimentData.expressionInData = oldThing.inDat.signal; + obj.experimentData.expressionInDataTime = oldThing.inDat.time; + objversion=1; % ver should now be according to 1 +end +%% Copy settings to new object +names=properties(obj); +ii=find(~strcmp(names,'version')); +for k=1:length(ii) + obj.(names{ii(k)})=oldThing.(names{ii(k)}); +end +%% We can now be sure that the object has the right properties copied, time to update the last changes to make objects up to date to 1.41 +if objversion<1.4 %Make sure everythings is up to date to 1.4 + obj.experimentSettings.timeOut=2; + objversion=1.4; % increment version number +end + + if objversion <1.41 % Make sure everythings is up to date to 1.41 + oldnames={'alpha1','alpha2','alpha3'}; + newnames={'w','self_coeff','social_coeff'}; + for k=1:3 %renaming fields in psoSettings + obj.psoSettings.(newnames{k})=obj.psoSettings.(oldnames{k}); + + end + obj.psoSettings=rmfield(obj.psoSettings,oldnames); + obj.psoSettings.method='PSO'; + obj.psoSettings.w_min=0.01; + obj.psoSettings.w_max=1; + objversion=1.41; % will be used when incrementing + end + % this will contain more things as changed to attributes + % are introduced. i.e if ==1.4 and so on. +end \ No newline at end of file diff --git a/Sources/core/RaPIdClass.m b/Sources/core/RaPIdClass.m deleted file mode 100644 index 217f48b..0000000 --- a/Sources/core/RaPIdClass.m +++ /dev/null @@ -1,162 +0,0 @@ -%% -% -% Copyright 2015 Luigi Vanfretti, Achour Amazouz, Maxime Baudette, -% Tetiana Bogodorova, Jan Lavenius, Tin Rabuzin, Giuseppe Laera, -% Francisco Gomez-Lopez -% -% The authors can be contacted by email: luigiv at kth dot se -% -% This file is part of Rapid Parameter Identification ("RaPId") . -% -% RaPId is free software: you can redistribute it and/or modify -% it under the terms of the GNU Lesser General Public License as published by -% the Free Software Foundation, either version 3 of the License, or -% (at your option) any later version. -% -% RaPId is distributed in the hope that it will be useful, -% but WITHOUT ANY WARRANTY; without even the implied warranty of -% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -% GNU Lesser General Public License for more details. -% -% You should have received a copy of the GNU Lesser General Public License -% along with RaPId. If not, see . - -classdef RaPIdClass assign version=1 - s.version=double(~isempty(isprop(s, 'version'))); % 0 if really old, else 1 - old - obj = RaPIdClass(s); % update object - elseif s.version <1.41 % check if latest ver - obj = RaPIdClass(s); % update object - else - obj=s; % everything is up to date - end - catch err - disp(err) - end - end - end - methods - function obj = RaPIdClass(varargin) - if nargin > 0 && isstruct(varargin{1}) && varargin{1}.version==0 %to convert some old mySettings-structs, will be removed in future. - tmp=varargin{1}; - obj.psoSettings = tmp.pso_options; - obj.naiveSettings = tmp.naive_options; - obj.gaSettings = tmp.ga_options; - obj.combiSettings = tmp.combiOptions; - obj.knitroSettings = tmp.kn_options; - obj.nmSettings = tmp.nmOptions; - obj.cgSettings = tmp.cgOptions; - obj.psoExtSettings = tmp.psoExtOptions; - obj.gaExtSettings = tmp.gaExtOptions; - obj.fminconSettings = tmp.fminconOptions; - obj.fmuOutputNames = tmp.fmuOutData; - obj.parameterNames = tmp.parameterNames; - obj.experimentSettings = struct('tf',tmp.tf,'ts',tmp.Ts,... - 'p_min',tmp.p_min,'p_max',tmp.p_max,'p_0',tmp.p0,'cost_type',tmp.cost,'objective_weights',tmp.objective.vect,... - 't_fitness_start',tmp.t0_fitness,'integrationMethod',tmp.intMethod, ... - 'pathToSimulinkModel',tmp.path2simulinkModel,'modelName',tmp.modelName,'blockName',tmp.blockName,... - 'scopeName',tmp.scopeName,'verbose',tmp.verbose,'saveHist', false,'optimizationAlgorithm',tmp.methodName); - obj.experimentData = struct('referenceOutdata',tmp.realData,'referenceTime',tmp.realTime,'pathToReferenceData',tmp.path2data,... - 'expressionReferenceTime',tmp.dataT,'expressionReferenceData',tmp.dataY); - obj.experimentSettings.outputPostProcessing=@(x)x; %function handle, do nothing as standard - if isfield(tmp,'displayMode') - obj.experimentSettings.displayMode = tmp.displayMode; - else - obj.experimentSettings.displayMode = 'hide'; - end - if isfield(tmp,'nbMaxIterations') - obj.experimentSettings.maxIterations = tmp.nbMaxIterations; - else - obj.experimentSettings.maxIterations = 100; - end - if isfield(tmp, 'mode') - obj.experimentSettings.solverMode = tmp.mode; - else - obj.experimentSettings.solverMode = 'Simulink'; - end - if isfield(tmp,'fmuInputNames') - obj.fmuInputNames = tmp.fmuInputNames; - end - if isfield(tmp,'path2fmuModel') - obj.experimentSettings.pathToFmuModel = tmp.path2fmuModel; - end - if isfield(tmp,'pf_options') && ~isempty(tmp.pf_options) - obj.pfSettings = tmp.pf_options; - else - obj.pfSettings = struct('nb_particles', 100,'prune_threshold', 0.1,'kernel_sigma',1); - end - if ~isfield(tmp,'inDat') - tmp.inDat = struct('path',[],'signal',[],'time',[]); - end - obj.experimentData.pathToInData = tmp.inDat.path; - obj.experimentData.expressionInData = tmp.inDat.signal; - obj.experimentData.expressionInDataTime = tmp.inDat.time; - obj.version=1; % ver should be according to 1 - obj=RaPIdClass(obj); % update to latest version - end - if nargin > 0 && isstruct(varargin{1}) && varargin{1}.version==1 - tmp=varargin{1}; - names=properties(obj); - for k=1:length(names) - obj.(names{k})=tmp.(names{k}); - end - obj=RaPIdClass(obj); % update rapidobject to latest version - elseif nargin>0 && isa(varargin{1},'RaPIdClass') - obj=varargin{1}; - if obj.version<1.4 - obj.experimentSettings.timeOut=2; %default - obj.version=1.4; - end - if obj.version <1.41 - oldnames={'alpha1','alpha2','alpha3'}; - newnames={'w','self_coeff','social_coeff'}; - for k=1:3 %renaming fields in psoSettings - obj.psoSettings.(newnames{k})=obj.psoSettings.(oldnames{k}); - end - obj.psoSettings=rmfield(obj.psoSettings,oldnames); % remove old names - obj.psoSettings.method='PSO'; - obj.psoSettings.w_min=0.01; - obj.psoSettings.w_max=1; - obj.version=1.41; - end - % this should contain more things as changed to attributes - % are introduced. i.e if ==1.4 and so on. - end - - end - function obj = saveobj(obj) - - end - end -end - diff --git a/Sources/core/functions/rapid.m b/Sources/core/functions/rapid.m index 194d4d2..9b7c883 100644 --- a/Sources/core/functions/rapid.m +++ b/Sources/core/functions/rapid.m @@ -247,8 +247,8 @@ %% Cleaning up after the optimization switch RaPIdObject.experimentSettings.solverMode case 'Simulink' - switch RaPIdObject.experimentSettings.displayMode - case 'show' + switch lower(RaPIdObject.experimentSettings.displayMode) + case {'show'} % NOP otherwise close_system(RaPIdObject.experimentSettings.modelName,0) diff --git a/Sources/install/run_example.m b/Sources/install/run_example.m new file mode 100644 index 0000000..05e9934 --- /dev/null +++ b/Sources/install/run_example.m @@ -0,0 +1,88 @@ +%Script to run SecondOrder example during initialization + + +%% ==========Moving to the example folder========== + +str = '../../Examples/SecondOrder'; +cd(str); + +%% ==========Reference data settings========== + +%Output data +RaPIdObject.experimentData.pathToReferenceData = 'measuredDataO.mat'; %Data file name +RaPIdObject.experimentData.expressionReferenceTime = 'time'; %Time variable name +RaPIdObject.experimentData.expressionReferenceData = 'signal'; %Data variable name + +%Input data +RaPIdObject.experimentData.pathToInData = ''; + +%% ==========Experiment settings========== + +%General settings +RaPIdObject.experimentSettings.tf = 50; %Simulation length +RaPIdObject.experimentSettings.ts = 0.01; %Sampling time +RaPIdObject.experimentSettings.t_fitness_start = 4; %Start calculating fintess function after t_fintess_start +RaPIdObject.experimentSettings.timeOut = 2; %Seconds before simulation timeout +RaPIdObject.experimentSettings.integrationMethod = 'ode45'; %Solver selection +RaPIdObject.experimentSettings.solverMode = 'Simulink'; +RaPIdObject.experimentSettings.optimizationAlgorithm = 'pso'; %Selection of optimization algorithm +RaPIdObject.experimentSettings.maxIterations = 1; %Maximum number of estimation iterations +RaPIdObject.experimentSettings.verbose = 1; %Can trigger more data for debugging +RaPIdObject.experimentSettings.saveHist = 0; %Don't save history + +%Model related settings +RaPIdObject.experimentSettings.pathToSimulinkModel = 'variable_rafael.mdl'; %Simulink model file name +RaPIdObject.experimentSettings.pathToFMUModel = 'Rafael_0original_0estimated.fmu'; %FMU file name +RaPIdObject.experimentSettings.modelName = 'variable_rafael'; %Simulink model name +RaPIdObject.experimentSettings.blockName = 'variable_rafael/Rafael_original_estimated'; %FMU name +RaPIdObject.experimentSettings.scopeName = 'simout'; %Result sink name +RaPIdObject.experimentSettings.displayMode = 'Show'; + +%Estimation parameter settings +RaPIdObject.experimentSettings.p_0 = [0.3, 4.1, 1.7, 1.1]; %Initial parameter guess +RaPIdObject.experimentSettings.p_min = [0.1, 3.9, 1.5, 1.1]; %Minimum values of parameters +RaPIdObject.experimentSettings.p_max = [1, 4.2, 1.7, 1.5]; %Maximum values of parameters + +%Fitness function settings +RaPIdObject.experimentSettings.cost_type = 1; %Fitness function selection +RaPIdObject.experimentSettings.objective_weights = 1; %Weights of the output signals for fitness function + +%% ==========Optimization Algorithm settings========== + +switch RaPIdObject.experimentSettings.optimizationAlgorithm + case 'pso' + RaPIdObject.psoSettings.w = 0.25; %Particle inertia weight + RaPIdObject.psoSettings.self_coeff = 0.25; %Self recognition coefficient + RaPIdObject.psoSettings.social_coeff = 0.25; %Social coefficient + RaPIdObject.psoSettings.limit = 0.25; %Iteration limit + RaPIdObject.psoSettings.nRandMin = 8; %Minimum number of random particles + RaPIdObject.psoSettings.nb_particles = 8; %Number of particles + RaPIdObject.psoSettings.fitnessStopRatio = 1e-5; %Fitness stop ratio + RaPIdObject.psoSettings.kick_multiplier = 0.002; %Kick multiplier + RaPIdObject.psoSettings.method = 'PSO'; +end + +%% ==========FMU parameters, inputs and outputs========== + +RaPIdObject.parameterNames = {'transferFunction.b[1]','transferFunction.a[1]','transferFunction.a[2]','transferFunction.a[3]'}; +RaPIdObject.fmuInputNames = {}; +RaPIdObject.fmuOutputNames = {'y1'}; %Output variable names + +%% ==========Running the computation========== + +%Opening simulink model +open_system(RaPIdObject.experimentSettings.pathToSimulinkModel); %Opening the simulink model +open_system(strcat(RaPIdObject.experimentSettings.modelName,'/Scope')); %Opening the scope in the model to observe estimation process +pause(1); %Waiting one second for scope to initialize +%% + +%Starting the estimation process +[sol, hist] = rapid(RaPIdObject); +cd('../../Sources'); +if isempty(sol) + warning('Test example failed!'); +else + disp(strcat('Vector of estimated parameters is: ', mat2str(sol,3))); + disp('======= Test example succeeded! ======='); +end +clear RaPIdObject sol hist str dataMeasuredS diff --git a/Sources/setup_rapid.m b/Sources/setup_rapid.m index cd23b10..9b50cdc 100644 --- a/Sources/setup_rapid.m +++ b/Sources/setup_rapid.m @@ -31,6 +31,13 @@ % Check of RaPId dependencies run('.\install\check_installed') - +disp('======= Running the test example ======='); +try + run('.\install\run_example') +catch msg_error + cd('../../Sources'); + disp(' /!\ Something went wrong in the example /!\'); + throwAsCaller(msg_error); +end % En of setup, run the GUI run_rapid_gui \ No newline at end of file