optimization toolbox, GA, size of parameters array change after the first generation

1 visualizzazione (ultimi 30 giorni)
The script aims to optimize a set of 6 parameters using GA, the required steps are the following:
in the first generation and first population, a first set of 6 parameters is generated, implement these values in an input file for a simulation software, run the simulation, get the energy from the logfile (c_inter), then calculate the error = c_inter - desired_ouptut ( the desired output value is a value defined in the script)
The objective function here is to minimize this value of error, to be less than 0.1.
During the generation 0, the script generate different set of values of the 6 parameters at each population and run the simulation and calculate the error. The issue here is that the GA should generate another set of parameters for the next generation, however, when it comes to move to the next generation, the size of the parameters changes to 1 (instead of 6) and thus I got the error : Index exceeds the number of array elements. Index must not exceed 1.
  3 Commenti
Asma
Asma il 8 Apr 2024
Modificato: Sam Chak il 8 Apr 2024
Hello Sam ,
options = optimoptions('ga', 'PopulationSize', 20, 'MaxGenerations', 20, 'OutputFcn', @(options, state, flag) gaOutput(options, state, flag), 'PlotFcn', 'gaplotbestf');
[x, fval, exitflag, output] = ga(fitnessFunction, nvars, [], [], [], [], lb, ub, [], options);
I figured out that this error comes from the OutputFcn, because when I remove it from the optimoptions line, the code works correctly. However, I need to have this OutputFcn.
How can I edit it to avoid this error.
The gaOutput function is :
function [state, options, flag] = gaOutput(options, state, flag)
persistent errors;
if isempty(errors)
errors = [];
end
% Calculate the error for the current generation
current_error = calculateError(state.Best);
errors = [errors; current_error];
avg_error = mean(errors);
disp(['Generation: ', num2str(state.Generation), ', Current Error: ', num2str(current_error), ', Average Error: ', num2str(avg_error)]);
% Save the optimal parameters and error for each generation to the result file
fid = fopen(resultFile, 'a');
fprintf(fid, ['Generation: ', num2str(state.Generation), '\n']);
fprintf(fid, ['Optimal Parameters: ', num2str(state.Population), '\n']);
fprintf(fid, ['Error: ', num2str(current_error), '\n']);
fprintf(fid, '----------------------------------------\n');
fclose(fid);
end
Thank you for the support.
Best
Asma

Accedi per commentare.

Risposte (1)

Nipun
Nipun il 11 Giu 2024
Hi Asma,
I understand that you are using Genetic Algorithm (GA) in MATLAB to optimize a set of 6 parameters, and you are encountering an issue where, upon moving to the next generation in your GA process, the size of the parameter array unexpectedly changes to 1, causing an index out-of-bounds error. This issue typically arises from how the objective function handles the input parameters.
To ensure your objective function correctly processes the parameters as a vector of size 6 for each individual in the population across generations, make sure your objective function is defined to accept a single input argument (a vector of parameters) and that it correctly interprets this vector within the function. Here is a concise example of how you might structure your objective function:
function error = objectiveFunction(params)
% Assuming params is a 1x6 vector of parameters
% Implement these values in an input file for simulation software
% Run the simulation with the current set of parameters
c_inter = runSimulation(params); % Placeholder for actual simulation call
% Calculate the error
desired_output = 100; % Example desired output
error = abs(c_inter - desired_output); % Objective: Minimize this error
% Ensure the error is a scalar value
end
And then, you would use GA in MATLAB like this:
% Define GA options
options = optimoptions('ga', 'PopulationSize', 50, 'MaxGenerations', 100, ...
'Display', 'iter', 'PlotFcn', @gaplotbestf);
% Define the number of variables
nvars = 6; % 6 parameters to optimize
% Call GA
[x,fval] = ga(@objectiveFunction, nvars, [], [], [], [], [], [], [], options);
Make sure that the objectiveFunction correctly processes the input params as a 1x6 vector.
For more details on using the Genetic Algorithm function in MATLAB, refer to the follwoing MathWorks documentation: https://www.mathworks.com/help/gads/ga.html
Hope this helps.
Regards,
Nipun

Prodotti


Release

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by