Error in GWO optimization algorithm

Question: 1) writing (Best_pos(1:4)) is right? because it gives me error that the dimensions of the array being concatenated [ParsListMain is 4 elements].
2) Why the Best_pos gives me the ub of the code which is [3.7,0.05,2.8,3.5]?
Thanks in advance.
[Best_score,Best_pos,GWO_cg_curve]= GWO(20,100,[3.6,0.045,2.7,3.4],[3.7,0.05,2.8,3.5],4,@ee_battery_lse);
% Update Battery block with optimized parameters
Pars = reshape([ParsListMain; cellstr(num2str('Best_pos(1:4)'))'],1,[]);
for k=1:2:length(Pars)
evalin('base',[Pars{k} '=' Pars{k+1} ';'])
end
% Display optimized parameters
fprintf(['Optimized parameters for the battery main ' ...
'dialog tab are:\n']);
fprintf('\t%5s = %s\n', Pars{:});
clear i_data v_data t_data T_data Ts

 Accepted Answer

cellstr(num2str('Best_pos(1:4)'))'
ans = 1×1 cell array
{'Best_pos(1:4)'}
'Best_pos(1:4)' is a character vector. num2str() applied to a character vector gives the character vector. cellstr() applied to the character vector wraps it in a cell array, giving you a 1 x 1 cell array.
You mention that ParsListMain is 1 x 4, but you do not indicate what data type it is. If it were numeric then in context of the [] with the 1 x 1 cell array, then the numeric content would be wrapped in a cell and you would get a 2 x 1 cell array. If, though, ParsListMain is a 1 x 4 cell array then you would be doing vertcat between a 1 x 4 cell and a 1 x 1 cell, and that would be an error.
If Best_pos happens to be a numeric row vector then in
Pars = reshape([ParsListMain; cellstr(num2str(Best_pos(1:4)))'],1,[]);
the indexing Best_pos(1:4) of the (assumed) row vector would give you a 1 x 4 numeric vector, and num2str() of a numeric vector gives you a 1 x whatever character vector, and you would be back to the same problem as before.
If Best_post happens to be any other shape other than row vector (in particular if it happens to be a column vector) then Best_pos(1:4) would give you a 4 x 1 numeric vector, and num2str() of that would give you a 4 x whatever character array, cellstr() of which would give you a 4 x 1 cell array. Which you then transpose to 1 x 4. vertical concatenation of that with a 1 x 4 cell array could work fine.

5 Comments

We would need your function to test why you are getting the upper bound.
Hend Mostafa
Hend Mostafa on 24 Apr 2022
Edited: Hend Mostafa on 24 Apr 2022
ParsListMain = {'Vnom', 'R1', 'AH', 'V1', 'AH1'}; this is ParsList Main
and Best-pos gives me the optimized values of ParsListMain first 4 parameters
ParsListMain and Best_pos are row vectors
This is my code using GWO algorithm
% Load Battery data
load ee_battery_data.mat
assignin('base','T1',battery_data(find([battery_data(1:2).T]==25)).T);
assignin('base','T2',battery_data(find([battery_data(1:2).T]~=25)).T);
% Display the Battery model
Model = 'ee_battery';
open_system(Model)
%%
close_system(Model, 0);
ParsListMain = {'Vnom', 'R1', 'AH', 'V1', 'AH1'};
ParsListDyn = {'Rp1', 'tau1'};
ParsListTemp = {'Vnom_T2', 'R1_T2', 'V1_T2','Rp1_T2','tau1_T2'};
Pars0 = reshape([[ParsListMain ParsListDyn ParsListTemp]; cellstr(num2str([InitGuessMain InitGuessDyn InitGuessTemp]'))'],1,[]);
fprintf('\t%5s = %s\n', Pars0{:});
clear Pars0
% Load single cell Battery model and set parameters
load_system(Model);
% Enable Fast Restart to speedup the simulation
set_param(Model,'FastRestart','on')
%% Optimize Main Tab Dialog Parameters Without Charge Dynamics (Step 1)
% Find ambient temperature data index
idx_data = find([battery_data(1:num_lines).T]==25);
assignin('base','t_data',battery_data(idx_data).t);
assignin('base','i_data',battery_data(idx_data).i);
assignin('base','T_data',battery_data(idx_data).T*ones(length(t_data),1));
assignin('base','T0',battery_data(idx_data).T);
assignin('base','Ts',t_data(2)-t_data(1));
assignin('base','v_data',battery_data(idx_data).v);
% Optimize parameters in main dialog tab of Battery
assignin('base','ParsList',ParsListMain(1:4));
% InitGuess = InitGuessMain(1:4);
% OptPars = fminsearch(@ee_battery_lse_r, InitGuess, ...
[Best_score,Best_pos,GWO_cg_curve]= GWO(50,50,[3.6,0.045,2.7,3.4],[3.7,0.05,2.8,3.5],4,@ee_battery_lse);
% Update Battery block with optimized parameters
Pars = reshape([ParsListMain(1:4) ; cellstr(num2str(Best_pos'))'],1,[]);
for k=1:2:length(Pars)
evalin('base',[Pars{k} '=' Pars{k+1} ';'])
end
Display optimized parameters
fprintf(['Optimized parameters for the battery main ' ...
'dialog tab are:\n']);
fprintf('\t%5s = %s\n', Pars{:});
clear i_data v_data t_data T_data Ts
clear k InitGuess
If ParsListMain is a cell array of character vectors, then ParsListMain(1:4) would be {'Vnom', 'R1', 'AH', 'V1'} which would be a cell array of character vector. It is not clear what the intended output of num2str({'Vnom', 'R1', 'AH', 'V1'}) would be.
Pars0 = reshape([[ParsListMain ParsListDyn ParsListTemp]; cellstr(num2str([InitGuessMain InitGuessDyn InitGuessTemp]'))'],1,[]);
where the Init* variables are each numeric vectors.

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2019b

Community Treasure Hunt

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

Start Hunting!