How to output additional variables from objective function using ga optimization?

11 次查看(过去 30 天)
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

回答(3 个)

Alan Weiss
Alan Weiss 2023-4-10
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 个评论
Moritz Wegner
Moritz Wegner 2023-4-10
Hi,
thank you for your reply.
I am using ga for the optimization of an electric motor. I am using ga to generate random motor configurations. The objectiv function then calls a finite element programm which returns the mean torque and other parameters which are then used to calculate the value of the fitness function to evaluate the randomly generated motor configuration. I am using an output function which saves the fitness function value of the best individual of each generation and the individual. I am trying to store the parameters such as torque as well. And I didnt find a way to return those parameters from the objective function to pass them to the output function. Is there a way?

请先登录,再进行评论。


Star Strider
Star Strider 2023-4-10
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 个评论
Moritz Wegner
Moritz Wegner 2023-4-10
I think i am using a pretty similar approach at the moment. I am saving the data for the best individual od each generation to a file. Then loading the file and adding the data for the next generation. The duration od the optimization isnt the concern, I want to train a neural network with the data later on, so its obly about how to store the data. To my knowledge the approach of saving the data to a file doesnt work with parameters that are only defined in the objective function because the output function cant access those. And that is what I am trying to archieve. I somehow have to pass those parameters to the output function to store them to a file.
Star Strider
Star Strider 2023-4-10
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
Walter Roberson 2023-4-10
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 个评论
Moritz Wegner
Moritz Wegner 2023-4-10
How does the returning work? Could you maybe give me an example code? I havent found any documentation on the syntax.
Walter Roberson
Walter Roberson 2023-4-10
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by