How do I use ga correctly? fitness function

3 views (last 30 days)
Marius Rudolf
Marius Rudolf on 9 Apr 2019
Answered: Stephan on 9 Apr 2019
Hello together,
I am an student at the University of Ilmenau and i am also working there.
I got a big Matlab file which does not working and my task is to fix it. I know where the problem ist but I have never used the fitness function in matlab before. Another problem is that i do not know what the inputs of the function are.
The Programm does not work for its own it is only a function for another bigger programm but maybe someone solve the problem with the ga function
Here is the hole code and in line 88 (underlined) is the fitness function:
function [rUI, oMdl, vMdl] = guiDNLRX(f)
handles = guihandles(f);
layout_fcn(handles.panel_ident);
setCallbacks(f);
setDefaults(f);
rUI = @readUserInput;
oMdl = @optModel;
vMdl = @validModel;
end
function setCallbacks(f)
end
function setDefaults(f)
handles = guihandles(f);
try
load('defaultsDNLRX.mat');
set(handles.DNLRX_M,'string',defaults(1));
set(handles.DNLRX_n,'string',defaults(2));
set(handles.DNLRX_nx,'string',defaults(3));
set(handles.DNLRX_minD,'string',defaults(4));
set(handles.DNLRX_maxD,'string',defaults(5));
set(handles.DNLRX_alpha,'string',defaults(6));
catch
set(handles.DNLRX_M,'string',5);
set(handles.DNLRX_n,'string',0);
set(handles.DNLRX_nx,'string',7);
set(handles.DNLRX_minD,'string',0);
set(handles.DNLRX_maxD,'string',3e-6);
set(handles.DNLRX_alpha,'string',20);
end
end
function [options, errorflag, errorstr] = readUserInput(f,options,errorflag,errorstr)
handles = guihandles(f);
options.M = str2double(get(handles.DNLRX_M,'String'));
options.n = str2double(get(handles.DNLRX_n,'String'));
options.nx = str2double(get(handles.DNLRX_nx,'String'));
options.bounds(1) = str2double(get(handles.DNLRX_minD,'String'));
options.bounds(2) = str2double(get(handles.DNLRX_maxD,'String'));
options.alpha = str2double(get(handles.DNLRX_alpha,'String'));
defaults = [options.M options.n options.nx options.bounds options.alpha];
save('defaultsDNLRX','defaults');
if ((options.n<0)||(options.nx<0)||(options.M<=0))
errorflag = 1;
errorstr = 'Invalid model order';
end
if ((options.bounds(2)<=0)||(options.bounds(1)<0)||(options.alpha<=0))
errorflag=1;
errorstr='Invalid ident.parameter';
end
end
function [result] = optModel(data, opts)
% run Identification method
addpath models
model = @DNLRX;
linOptFcn = @optLinModel;
%addpath linOptFcns
%linOptFcn = @optLinDnlrx;
tic
warning off Matlab:rankDeficientMatrix
bounds=zeros(opts.M,2);
bounds(:,1) = opts.bounds(1);
bounds(:,2) = opts.bounds(2);
IdentParams = opts;
IdentParams.input = data.x(1:opts.downsampling:end); %position
IdentParams.output = data.u(1:opts.downsampling:end); %force
IdentParams.alpha = ceil(opts.alpha/opts.downsampling);
IdentParams.modelFcn = model;
% call the GA
[d endPop trace]=ga([],bounds(:,1),bounds(:,2),linOptFcn,IdentParams,[70 -1],'maxGenTerm',opts.numGen);
clear endPop
%%Simplex (Nelder-Mead) optimization
simplexOptions = optimset('Display','Iter','MaxIter',opts.iterSimplex,'TolFun',10^(-5));
d = fminsearch(@(d) feval(linOptFcn,d,IdentParams,bounds(:,1),bounds(:,2)),d,simplexOptions);
warning on Matlab:rankDeficientMatrix
% für filterparameter jeden verfügbaren Wert benutzten
IdentParams.input = data.x; %position
IdentParams.output = data.u; %force
IdentParams.alpha = opts.alpha;
result = opts;
%für Berechnung der Filterparameter und Darstellung der Ergebnisse
[~,result.d,Theta,u_model,IdentParams]=feval(linOptFcn,d,IdentParams);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
result.NRMSE = calcNRMSE(IdentParams.output,u_model);
if (opts.offset~=0)
result.offset=Theta(end);
Theta=Theta(1:end-1);
else
result.offset = 0;
end
result.g = Theta(1:opts.nx+1); %coefficiets input FIR filter
result.theta = Theta(opts.nx+2:end); %coefficients spring deformation FIR filter
idx=length(data.u)-length(u_model)+1;
result.u = data.u(idx:end);
result.x = data.x(idx:end);
result.u_model = u_model;
result.identDuration=toc;
%Anzeige result Struktur
result
disp(['Elapsed time is ' num2str(result.identDuration) ' seconds.']);
title = ['DNLRX(' num2str(result.M) ',' num2str(result.n) ',' num2str(result.nx) ') model' ];
plotResult(result,'Trainingsdatensatz',title);
end
function [result] = validModel(data, opts)
addpath models
result = [];
u_model=DNLRX(data.x,opts);
if isfield(data,'u')
%Validierung mit Messdaten
result = opts;
idx=length(data.x)-length(u_model)+1;
result.u_model = u_model;
result.x = data.x(idx:end);
result.u = data.u(idx:end);
result.NRMSE = calcNRMSE(data.u,u_model);
titleName = ['DNLRX(' num2str(result.M) ',' num2str(result.n) ',' num2str(result.nx) ') model' ];
plotResult(result,'Validierungsdatensatz',titleName)
else
%optische Validierung mit Sinusfunktion
figure
plot(u_model)
grid
end
end
function layout_fcn(h25)
h26 = uicontrol(...
'Parent',h25,...
'BackgroundColor',[1 1 1],...
'Position',[52 90 30 20],...
'String','',...
'Style','edit',...
'Tag','DNLRX_M');
h27 = uicontrol(...
'Parent',h25,...
'BackgroundColor',[1 1 1],...
'Position',[52 65 30 20],...
'String','',...
'Style','edit',...
'Tag','DNLRX_n');
h28 = uicontrol(...
'Parent',h25,...
'BackgroundColor',[1 1 1],...
'Position',[52 40 30 20],...
'String','',...
'Style','edit',...
'Tag','DNLRX_nx');
h29 = uicontrol(...
'Parent',h25,...
'FontSize',10,...
'HorizontalAlignment','left',...
'Position',[25 89 20 20],...
'String','M',...
'Style','text',...
'Tag','DNLRX_MText');
h31 = uicontrol(...
'Parent',h25,...
'FontSize',10,...
'HorizontalAlignment','left',...
'Position',[25 64 20 20],...
'String','n',...
'Style','text',...
'Tag','DNLRX_nText');
h30 = uicontrol(...
'Parent',h25,...
'FontSize',10,...
'HorizontalAlignment','left',...
'Position',[25 40 20 20],...
'String','nx',...
'Style','text',...
'Tag','DNLRX_nxText');
%Min Schwellwerte
h38 = uicontrol(...
'Parent',h25,...
'Units','pixels',...
'FontSize',10,...
'HorizontalAlignment','left',...
'Position',[220 89 122 20],...
'String','Min Schwellwerte',...
'Style','text',...
'Tag','DNLRX_minD_text');
h39 = uicontrol(...
'Parent',h25,...
'BackgroundColor',[1 1 1],...
'Position',[350 90 50 20],...
'String','',...
'Style','edit',...
'Tag','DNLRX_minD');
%Max Schwellwerte
h38 = uicontrol(...
'Parent',h25,...
'Units','pixels',...
'FontSize',10,...
'HorizontalAlignment','left',...
'Position',[220 64 122 20],...
'String','Max Schwellwerte',...
'Style','text',...
'Tag','DNLRX_maxD_text');
h39 = uicontrol(...
'Parent',h25,...
'BackgroundColor',[1 1 1],...
'Position',[350 65 50 20],...
'String','',...
'Style','edit',...
'Tag','DNLRX_maxD');
%alpha
h38 = uicontrol(...
'Parent',h25,...
'Units','pixels',...
'FontSize',10,...
'HorizontalAlignment','left',...
'Position',[220 39 122 20],...
'String','alpha',...
'Style','text',...
'Tag','DNLRX_alpha_text');
h39 = uicontrol(...
'Parent',h25,...
'BackgroundColor',[1 1 1],...
'Position',[350 40 50 20],...
'String','',...
'Style','edit',...
'Tag','DNLRX_alpha');
end
This is the error message
>> frictionGUI
Error using ga (line 310)
Fitness function must be a function handle.
Error in guiDNLRX>optModel (line 88)
[d endPop trace]=ga([],bounds(:,1),bounds(:,2),linOptFcn,IdentParams,[70 -1],'maxGenTerm',opts.numGen);
Error in frictionGUI>startButton_Callback (line 218)
result = feval(identData.optModel,data,options);
Error while evaluating UIControl Callback.
Thanks in advance.

Answers (1)

Stephan
Stephan on 9 Apr 2019
Hi,
the line where ga is called
[d endPop trace]=ga([],bounds(:,1),bounds(:,2),linOptFcn,IdentParams,[70 -1],'maxGenTerm',opts.numGen);
is incorrect. The first input argument should be a function handle as the error message tells you. At this position there is an empty array []. The second argument tells ga how many optimization variables are there - there is also no entry. This can not work. It should be something like:
nvars = 1;
lb = 0;
ub = 5;
[d endPop trace] = ga(@(x) sin(x)/x, nvars, lb, ub...)
or
nvars = 2;
lb = [0 -2];
ub = [5 12.6];
[d endPop trace] = ga(@myFitnessFunction, nvars, lb, ub...)
result = function myFitnessFunction(x)
result = x(1).^2 + 3.*x(2)
end
See the documentation of ga.
Best regards
Stephan

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!