How to output additional variables from objective function using ga optimization?
Show older comments
Hi,
I am having trouble to output variables from the objective function using ga other than the value of the objective funtion.
Does anyone have an idea on how to do that?
Greeting,
Moritz
Answers (3)
Alan Weiss
on 10 Apr 2023
0 votes
I am not sure what you are trying to do. You might be trying to pass intermediate values calculated within ga to reuse, for example in a constraint function. I am sorry, but that generally does not work for ga, as the order of operations that ga internally uses does not lend itself to that purpose. In other words, the technique in Objective and Nonlinear Constraints in the Same Function does not work for ga.
If you are trying to do something else, ask again with more detail.
Alan Weiss
MATLAB mathematical toolbox documentation
1 Comment
Moritz Wegner
on 10 Apr 2023
Star Strider
on 10 Apr 2023
0 votes
‘Does anyone have an idea on how to do that?’
After the optimisation terminates, run the objective function with the optimised parameter values and return all the desired outputs.
If the fitness function returns several results, for example:
function [a,b,c] = ftnsfcn(a,x)
... CODE ...
end
and the ga call is something like this:
[A,fval] = ga(@(a)ftnsfcn(a,x), NrParms);
after the optimiization terminates, run this:
[a,b,c] = ftnsfcn(A,x)
This would be easier to illustrate with a more specific example if you have one to share.
.
4 Comments
Moritz Wegner
on 10 Apr 2023
Star Strider
on 10 Apr 2023
It would return the parameters of the best individual, yes.
The only way to save all the values for every iteration of every individual would be to write those to some sort of file every time. The ga optimisation under those circumstnaces could take days, if not longer, to converge, and then you would have the problem of going through all those saved results, only some of which would be meaningful. (I wrote a funciton that would save the results of the best individual in every generation in How to save data from Genetic Algorithm in case MATLAB crashes? - MATLAB Answers - MATLAB Central.) Perhaps getting the parameters from only those saved individuals would be enough. You could do that offline after ga converges.
Moritz Wegner
on 10 Apr 2023
Star Strider
on 10 Apr 2023
I’m slightly lost at this point. You can save the best individual in each generation, and then afterwards use that information as input to the fitness function and get all the outputs from it for each saved individual.
That’s what I would do, anyway.
Walter Roberson
on 10 Apr 2023
0 votes
Have your objective function return a data structure of additional values. memoize() your objective function with a fair size buffer. Have the output function (which runs once per generation) fetch the best value of the model parameters and call the memorized function to retrieve the associated data structure, and store the values somewhere.
2 Comments
Moritz Wegner
on 10 Apr 2023
Walter Roberson
on 10 Apr 2023
memoized_objective = memoize(@objective);
memoized_objective.Cachesize = 1000;
options = optimoptions('ga','outputfcn', @(options,state,flag)save_best_data(options,state,flag,memoized_objective));
initialize_saved_data();
[bestx, fval, exitflag] = ga(memoized_objective, nvars, A, b, Aeq, beq, lb, ub, nonlcon, options);
best_data = recall_best_data();
function [cost, data_to_save] = objective(x)
stuff
data_to_save = {torque, other_parameters};
cost = whatever;
end
function initialize_saved_data()
do whatever is needed to initialize the store of saved data
end
function [state,options,optchanged] = save_best_data(options,state,flag,memoized_objective)
if state.LastImprovement == state.Generation
scores = state.Score;
[~, minidx] = min(scores);
best_params = state.Population(minidx,:);
[best_cost, data_to_save] = memoized_objective(best_params);
now save data_to_save however is appropriate
end
end
function all_saved_data = recall_best_data()
now recall the saved data and assign it to all_saved_data
end
Categories
Find more on Get Started with Optimization Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!