GA Optimization not working because fitness function must be a function handle.

5 visualizaciones (últimos 30 días)
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
Chris el 30 de Abr. de 2022
Editada: Chris el 30 de Abr. de 2022
  1. If u is a vector, you overwrite it when you assign it using optimvar. A possible fix:
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:
Josee Rizkallah
Josee Rizkallah el 3 de Mayo de 2022
For some reason, the ga function only returns one value for x and fval when it should return a vector of 1801x1, but with your help, this is what I got:
prob = @(u) fuelconsumption(u);
options = optimoptions('ga','UseVectorized',true,'PopulationSize',TimeCycle,'MaxStallGenerations',3,'FunctionTolerance',1e-04,'MaxGenerations',75)
[x fval]= ga(prob,1,[],[],[],[],-21,25,[],options);

Iniciar sesión para comentar.

Respuesta aceptada

Matt J
Matt J el 30 de Abr. de 2022
Editada: Matt J el 30 de Abr. de 2022
There is no real reason to use optimproblem for a simple 1D bounded problem. Just do,
[x fval] = ga(@fuelconsumption,1,[],[],[],[],-21,25,[],options);
or, for that matter
[x fval] = fminbnd(@fuelconsumption,-21,25);
  2 comentarios
Josee Rizkallah
Josee Rizkallah el 3 de Mayo de 2022
When I try to use
[x fval] = fminbnd(@fuelconsumption,-21,25);
I get the following error:
Error using fminbnd
User supplied objective function must return a scalar value.
Which makes sense since my function returns a vector. How can I fix this?
Matt J
Matt J el 3 de Mayo de 2022
Editada: Matt J el 3 de Mayo de 2022
Your function should not be returning a vector. It should be returning the scalar quantity that you are trying to minimize.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Genetic Algorithm en Help Center y File Exchange.

Productos


Versión

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by