Connect the optimizer function to data extracted from another software

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

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

Hi,
Thank you for your answer
I have one version of the code that is the same as your suggestion but the optimizer uses the same data from the external software everytime (stored in a vector). How can I make the optimizer do an interation and then returns the parameters to my code which will use them to run the external software and get new values to the optimizer for comparing.
Here is the modified code. I know unfortunately that there is something wrong in the order of exceution.
%% Optimizer
% Do a ro
lb = [ 0 ; 0 ; 0 ];
ub = [ Inf ; Inf ; Inf ];
% initial values
best_fit_param(1) = 0.0080;
best_fit_param(2) = 0.9673;
best_fit_param(3) = 3.1262;
% run software and get data
get_data;
f=@(param_vec) e_and_r_vals(param_vec,md_e,E_best_vals_from_md_r)
[best_fit_param,fval,exitflag,output,population,scores] = ga(f,3,[],[],[],[],lb,ub,[],opt_opts)
How can I make the optimizer do an interation and then returns the parameters to my code
You don't have to "make" it do that. ga() will automatically send updated parameters to the fitness function. Your job is to have the fitness function forward the parameters to your external software routine and evaluate them.
I've been trying but I think I still miss something. Can you please tell me what should the order of exceution be ? and how to pass the updated parameters everytime to the variables used in the external function
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)

Asked:

on 22 Oct 2021

Edited:

on 22 Oct 2021

Community Treasure Hunt

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

Start Hunting!