- 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 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Josee Rizkallah
el 30 de Abr. de 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 comentarios
Chris
el 30 de Abr. de 2022
Editada: Chris
el 30 de Abr. de 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:
Respuesta aceptada
Más respuestas (0)
Ver también
Categorías
Más información sobre Genetic Algorithm en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!