I am a student only starting to use MATLAB last year so I do not know if the resolution to this is simple or not but If anyone can give me any help I would appreciate it. Thank you in advance!
I want to automate a 2 input-1 output Hammerstein-Wiener model estimation through genetic algorithms
3 次查看(过去 30 天)
显示 更早的评论
I tried to automate the process of estimation of the parameters for an hammerstein-wiener model using the existing matlab funtions. However I was not able to put it to work. If I run the functions in the command window without the genetic algorithms it all works properly but when I introduce the genetic algorithm estimations, specially in the cost function it starts to give back errors, maybe related with the code in the cost function I think is related with not initializing the parameters for the model but I do not know how to solve this. The code I implemented is as follows:
% Define GA options
options = optimoptions('ga','PopulationSize',5,'MaxGenerations',5);
% Define GA optimization function
fun = @(x)modelTuning(x);
% Data for training 2 inputs, 1 output
timedata = iddata (Ensaio2t2,[Ensaio2in(:,1), Ensaio2in(:,2)]);
% initmodel = idnlhw ([[2 2],[3 3],[6 4]]);
% Define bounds
lb = [1 1 1 1];
%
ub = [10 10 10 10];
% Run GA optimization
[x,fval] = ga(fun,4,[],[],[],[],lb,ub,[],options);
testmodel = idnlhw ([[x(1) x(2)],[x(3) x(4)],[6 4]]);
model20234t2 = nlhw(timedata,testmodel);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function error = modelTuning(x)
% tmodel = idnlhw ([[2 2],[3 3],[6 4]]);
tmodel = idnlhw ([[x(1) x(2)],[x(3) x(4)],[6 4]]);
timedata = iddata (Ensaio2t2,[Ensaio2in(:,1), Ensaio2in(:,2)]);
modelt2 = nlhw(timedata, tmodel);
[~,q,~]=compare (timedata, modelt2);
error = 100-q;
end
The error:
Error using idnlhw
The "idnlhw" command should be called with the syntax:
model = idnlhw(Orders, InputNonlinearity, OutputNonlinearity, PV-pairs), or
model = idnlhw(PV-pairs).
Type "help idnlhw" for more information.
Error in modelestimation>modelTuning (line 24)
tmodel = idnlhw ([[x(1) x(2)],[x(3) x(4)],[6 4]]);
Error in modelestimation>@(x)modelTuning(x) (line 6)
fun = @(x)modelTuning(x);
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
Error in makeState (line 58)
firstMemberScore = FitnessFcn(state.Population(initScoreProvided+1,:));
Error in galincon (line 24)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
Error in ga (line 416)
[x,fval,exitFlag,output,population,scores] = galincon(FitnessFcn,nvars, ...
Error in modelestimation (line 17)
[x,fval] = ga(fun,4,[],[],[],[],lb,ub,[],options);
Caused by:
Failure in initial user-supplied fitness function evaluation. GA cannot continue.
回答(1 个)
Tianyu
2023-8-8
Hi Ivo,
Here are a few suggestions you can try to modify your code
I do not have the data, so allow me to use iddata1 for example
>> load iddata1.mat z1
% 1, you need to pass your data to your function, using the following syntax
>> fun = @(x,z1)modelTuning(x,z1);
...
>> [x,fval] = ga(@(x)fun(x,z1),4,[],[],[],[],lb,ub,[],options);
% In function definition, use
>> function error = modelTuning(x,z1)
% 2, From the error information, you can see that the "idnlhw" is not called with correct syntax, i.e. "SYS = IDNLHW(ORDERS,InputNL,OutputNL)". ORDERS should be integers. Something like this should work
>> idnlhw ([[1 1],[1 1],[1 1]]);
% If you want to treat x as integers, add integer constraint in GA algorithm. See https://www.mathworks.com/help/gads/ga.html?searchHighlight=ga&s_tid=srchtitle_support_results_1_ga#d124e52414
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!