- If u is a vector, you overwrite it when you assign it using optimvar. A possible fix:
GA Optimization not working because fitness function must be a function handle.
5 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Josee Rizkallah
am 30 Apr. 2022
Bearbeitet: Matt J
am 3 Mai 2022
Hi,
I am trying to optimize u for a customized function called fuelconsumption. This function takes as an input u. Note that u is a vector of size 1801x1.
When trying to optimize u using GA, I get the following error:
Error using ga
Fitness function must be a function handle.
Error in ga_optimization (line 26)
[x fval] = ga(prob,1,[],[],[],[],[],[],[],options);
Here is my code for reference:
%% Problem Definition
tic
u = optimvar("u","LowerBound",-21,"UpperBound",25);
f=@(u) fuelconsumption(u);
prob = optimproblem("Objective",fuelconsumption(u));
options= optimoptions(@ga, 'PopulationSize',TimeCycle)
options = optimoptions('ga','UseVectorized',true);
[x fval] = ga(prob,1,[],[],[],[],[],[],[],options);
toc
Any help would be really appreciated!
2 Kommentare
Chris
am 30 Apr. 2022
Bearbeitet: Chris
am 30 Apr. 2022
u = optimvar("u",1801,1,"LowerBound",-21,"UpperBound",25);
2. You overwrite your options struct the second time you call optimoptions. Name-Value pairs can be entered all at once:
options = optimoptions('ga','UseVectorized',true,'PopulationSize',TimeCycle)
or you can use dot indexing:
options = optimoptions('ga');
options.UseVectorized = true;
options.PopulationSize = TimeCycle; % Assuming TimeCycle is a variable you've defined
(or some combination of the two methods)
This should help a little bit, but it's still not a full answer. Here's some more useful help:
Akzeptierte Antwort
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Genetic Algorithm finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!