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

4 views (last 30 days)
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 Comments
Chris
Chris on 30 Apr 2022
Edited: Chris on 30 Apr 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 on 3 May 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);

Sign in to comment.

Accepted Answer

Matt J
Matt J on 30 Apr 2022
Edited: Matt J on 30 Apr 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 Comments
Josee Rizkallah
Josee Rizkallah on 3 May 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 on 3 May 2022
Edited: Matt J on 3 May 2022
Your function should not be returning a vector. It should be returning the scalar quantity that you are trying to minimize.

Sign in to comment.

More Answers (0)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!