Genetic Algorithm Custom Output

17 ビュー (過去 30 日間)
Burak Bayca
Burak Bayca 2025 年 1 月 27 日 8:11
回答済み: Alan Weiss 2025 年 1 月 29 日 0:40
Hello, I'm trying to optimize a simple Simulink model output by using Genetic Algorithm. Three different output values are implemented in this model. The first one is used as cost value. Hovewer, the other two values are not assigned to anything. I want to get these outputs as timeseries after the Genetic Algorithm calculations, like "The second output of Generation XX Individual YY is ...". When the calculations are done with single core, I can get all the results I want, but when the parallel toolbox is used, only the last generation and last individual is visible after the calculations. How can I fix that parallel calculation? Here is my simple code that gives all individuals and cost values for me, but I need the other two outputs of simulation. Thanks.
clear all;clc;,
%%
tic
saveFlag = 0;
rng('shuffle');
objectiveFunction = @(params) nlaSimResults(params);
lowerBound = [1000 0.2];
upperBound = [25000 0.8];
popSize = 10;
genSize = 20;
options = optimoptions('ga','UseParallel',true,'UseVectorized',false,'PopulationSize',popSize,'MaxGenerations',genSize,'OutputFcn',@gaOutputFunction,'Display','iter');
[bestParams, minCost, ~, optResults, optPop, optScores] = ga(objectiveFunction,2,[],[],[],[],lowerBound,upperBound,[],options);
optimTime = toc;
if saveFlag == 1; save('OptimResulst','bestParams','minCost','optResults','optScores','optimTime','popSize','genSize'); end
function cost = nlaSimResults(params)
global otherVars
Altitude = params(1);
Mach = params(2);
assignin('base','Altitude',Altitude);
assignin('base','Mach',Mach);
sim('debugModel',10); % Simulation Model with Altitude and Mach inputs
cost = logsout{1}.Values.Data(1); % The first output of the model is assigned as cost
if isempty(otherVars)
otherVars = {};
end
otherVars = [otherVars; logsout{2}.Values.Data logsout{3}.Values.Data];
assignin('base','otherVars',otherVars);
end
function [state, options, flag] = gaOutputFunction(options, state, flag)
persistent popHistory bestValues bestIndividuals otherVarsHistory
global otherVars
if isempty(popHistory)
popHistory = {};
bestValues = [];
bestIndividuals = [];
otherVarsHistory = {};
end
[bestScore, bestIdx] = min(state.Score);
bestValues = [bestValues; bestScore];
bestIndividuals = [bestIndividuals; state.Population(bestIdx, :)];
popHistory(state.Generation + 1) = {state.Population};
otherVarsHistory{state.Generation + 1} = otherVars;
otherVars = [];
assignin('base','popHistory',popHistory);
assignin('base','bestValues',bestValues);
assignin('base','bestIndividuals',bestIndividuals);
assignin('base','otherVarsHistory',otherVarsHistory);
end
My model is so simple, just returning the sum, product and division of the altitude and mach values.

回答 (1 件)

Alan Weiss
Alan Weiss 2025 年 1 月 29 日 0:40
I am not sure, but perhaps using the workflow in this example will help:
Alan Weiss
MATLAB mathematical toolbox documentation

カテゴリ

Help Center および File ExchangeGenetic Algorithm についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by