Connect the optimizer function to data extracted from another software

3 views (last 30 days)
Khaled Mohamed
Khaled Mohamed on 22 Oct 2021
Edited: Matt J on 22 Oct 2021
Hi
I'm trying to optimize 3 parameters using genetic algorithim (or simulated annealing, the fit is not complicated both algorithims can reach the min.). The problem can be summarized as follows:
  1. run an external software to get the data (this data is dependent on the parameters i want to optimize)
  2. extract the data from the run in part 1 and comapre it with the exp. data by calculating a cost function
  3. do an iteration to optimize the parameters and use the new parameters to run the software as in part 1
  4. compare the data again as in part 2 and repeat until the cost function reaches a minimum
I've done most of the code, to extract the data and run the software from matlab. I also programmed the function containing the 'cost function' and the file containing the optimization scheme. However, I cannot connect all the 3 files. For clarification i show here the optimizer and the cost function codes.
The code below takes md_e and E_best_vals_from_md_r and these are sets of data from step 2 to calculate the cost function
function cost_func=e_and_r_vals(md_e,E_best_vals_from_md_r)
E_diff_sq = zeros(23,1);
E_down_sq = zeros(23,1);
for m = 1:23
E_diff_sq(m,1) = (md_e(m,1)-E_best_vals_from_md_r(m,1))^2;
E_down_sq(m,1) = (E_best_vals_from_md_r(m,1))^2;
end
E_down = sum(E_down_sq);
E_up = sum(E_diff_sq);
cost_func = E_up/E_down;
end
The code below is the optimizer code, and it is incomplete as this is my problem.
%% Fit options
opt_opts.FunctionTolerance = 1E-9;
opt_opts.MaxGenerations = 50000000;
opt_opts.MaxStallGenerations = 50000000;
opt_opts.PlotFcn = "@gaplotbestf";
opt_opts.HybridFcn = "patternsearch";
opt_opts.MigrationDirection = 'both';
opt_opts.MigrationInterval = 100000;
opt_opts.PopulationSize = 1000;
opt_opts.FitnessLimit = -inf;
%% Optimizer
% Do a ro
lb = [ 0 ; 0 ; 0 ];
ub = [ Inf ; Inf ; Inf ];
get_data;
f=@(~) e_and_r_vals(md_e,E_best_vals_from_md_r)
[best_fit_param,fval] = ga(f,3,[],[],[],[],lb,ub,[],opt_opts)
The issue is that I'm not able to connect the optimizer code to the cost function and consquently to the parameters that I want to fit. The parameters that I want to fit are in another long code which i did not attach. In short, my question is how to connect these two codes with the code that gets the data to optimize the parameters which I send to the external software to get the data which i use to calculate the cost function.
Sorry for the lengthy question and I hope you will assist me.

Accepted Answer

Matt J
Matt J on 22 Oct 2021
Edited: Matt J on 22 Oct 2021
You've written a fitness function
f=@(~) e_and_r_vals(md_e,E_best_vals_from_md_r)
which takes no input arguments. The fitness function must accept a 1x3 vector of parameter guesses and invoke your external software with those parameters. Presumably, it would look something like,
f=@(x) e_and_r_vals(x, md_e,E_best_vals_from_md_r)
  4 Comments
Matt J
Matt J on 22 Oct 2021
We don't know anything about your external software. Only you know how to pass parameters to it, and to receive output from it. Basically, the fitness function that you give to Ga has to be a wrapper for the external software. The code should look like this,
x=ga(@fitnessFunc,___)
function value=fitnessFunc(x)
value=ExternalSoftware(x);
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!