-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from tinrabuzin/setup_example
Running SecondOrder example during setup
- Loading branch information
Showing
10 changed files
with
408 additions
and
185 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
%% <Rapid Parameter Identification is a toolbox for automated parameter identification> | ||
% | ||
% 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 <http://www.gnu.org/licenses/>. | ||
|
||
classdef RaPIdClass <handle | ||
%RAPIDCLASS defines a handle object to store and send settings and data in RaPId | ||
% without too much overhead | ||
|
||
properties | ||
psoSettings | ||
gaSettings | ||
combiSettings | ||
naiveSettings | ||
knitroSettings | ||
nmSettings | ||
cgSettings | ||
psoExtSettings | ||
gaExtSettings | ||
fminconSettings | ||
pfSettings | ||
experimentSettings | ||
experimentData | ||
resultData | ||
fmuOutputNames | ||
parameterNames | ||
fmuInputNames | ||
version; %decimals of sqrt(2) | ||
end | ||
|
||
methods (Static) | ||
function obj = loadobj(s)% Implements self-updating loading features to ensure backwards | ||
% compatibility with old objects | ||
try | ||
if ~isprop(s, 'version') || isempty(s.version) % if no version -> 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
Oops, something went wrong.