- 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.
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Chris
le 30 Avr 2022
Modifié(e) : Chris
le 30 Avr 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:
Réponse acceptée
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Genetic Algorithm dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!