Issue with PopulationSize in Genetic Algorithm toolbox

There is a project I wanted to use the genetic algorithm tools on and attempted to rig a basic script to test it out. However, I could not run the code because it kept giving me the following error.
Error using gaoptimset>checkfield (line 436)
Invalid value for OPTIONS parameter PopulationSize: must be a positive numeric (not a character vector).
I didn't understand where I had specified PopulationSize as a character vector, so I used optimoptions to change PopulationSize to 50 manually. However it kept throwing me the same error.
When stepped through the program, I entered ga() and found that the default value for PopulationSize is the character vector '50 when numberOfVariables <= 5, else 200' which obviously isn't a numeric value. In any case, this value is supposed to be overridden when optimoptions is used to specify a value other than the default, but it didn't.
Am I doing something wrong or is the genetic algorithm solver broken? Code is as follows.
Further details is that I am doing this on my laptop with a copy of MATLAB R2024b downloaded on it. I have the Global Optimization toolbox installed.
% Testing Genetic Algorithm
clc, clear, close all
rng default % For reproducibility
numberOfVariables = 2;
lb = [-3,-3];
ub = [3,3];
a = 0.1;
b = 0.05; % define constant values
FitnessFunction = @(x) test_fitness(x,a,b);
options = optimoptions("ga",PopulationSize=50);
[x,fval] = ga(FitnessFunction,2,[],[],[],[],lb,ub,[],options);

4 Comments

Works for me:
% Testing Genetic Algorithm
clc, clear, close all
rng default % For reproducibility
numberOfVariables = 2;
lb = [-3,-3];
ub = [3,3];
a = 0.1;
b = 0.05; % define constant values
FitnessFunction = @(x) test_fitness(x,a,b);
options = optimoptions("ga",PopulationSize=50);
[x,fval] = ga(FitnessFunction,2,[],[],[],[],lb,ub,[],options);
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
x
x = 1×2
1.0e-04 * 0.0389 -0.1267
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
function y = test_fitness(x,a,b)
y = sum(x.^2);
end
What MATLAB release do you have in use ?
Currently R2024b, but I wouldn't have thought that to cause this level of dysfunction in my code.
According to this page
"PopulationSize" is to be prescribed as a positive integer, not as a character.
But if the above code doesn't work on your computer, I cannot tell you what the problem is.
Maybe you have a second function "ga.m" on your computer ? What do you get if you type
which -all ga.m
/MATLAB/toolbox/globaloptim/globaloptim/ga.m
Maybe there is confusion between MATLAB's "ga" and the Genetic Algorithm Toolbox under
?
Only the one included with MATLAB appears. This issue mystifies me.

Sign in to comment.

 Accepted Answer

The problem is that the arguments to ga are defined by thier positions, and there have to be 10 arguments before the 'options' argument at the end. Adding one more '[],' before hte 'options' argument solves that problem.
All it needs now is the 'test_fitness' function to be defined, and it should work.
Try something like this --
% Testing Genetic Algorithm
clc, clear, close all
rng default % For reproducibility
numberOfVariables = 2;
lb = [-3,-3];
ub = [3,3];
a = 0.1;
b = 0.05; % define constant values
FitnessFunction = @(x) test_fitness(x,a,b);
options = optimoptions("ga",PopulationSize=50);
[x,fval] = ga(FitnessFunction,2,[],[],[],[],lb,ub,[],[],options);
Unrecognized function or variable 'test_fitness'.

Error in solution>@(x)test_fitness(x,a,b) (line 10)
FitnessFunction = @(x) test_fitness(x,a,b);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in makeState (line 58)
firstMemberScore = FitnessFcn(state.Population(initScoreProvided+1,:));
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in galincon (line 24)
state = makeState(GenomeLength,FitnessFcn,Iterate,output.problemtype,options);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error in ga (line 420)
[x,fval,exitFlag,output,population,scores] = galincon(FitnessFcn,nvars, ...
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.
EDIT -- (26 Jan 2026 at 18:01)
Minor text tweaks.
.

5 Comments

I adjusted the code accordingly but I have the exact same error. It still tells me that PopulationSize is a character vector. This issue baffles me. I'm thinking I should just upload my version of Matlab to the lastest just in case that's responsible. It certainly wouldn't hurt.
It works in R2025b with the latest update (although the note says R2025a), however I've been running ga code for a few years across various versions/releases, and your code should run in them as well (although the position problem may remain in some versions).
I would have to see what 'test_fitness' is in order to determine what the problem may be.
In the interim --
% Testing Genetic Algorithm
clc, clear, close all
rng default % For reproducibility
numberOfVariables = 2;
lb = [-3,-3];
ub = [3,3];
a = 0.1;
b = 0.05; % define constant values
FitnessFunction = @(x) test_fitness(x,a,b);
options = optimoptions("ga",PopulationSize=50);
[x,fval] = ga(FitnessFunction,2,[],[],[],[],lb,ub,[],[],options);
ga stopped because the average change in the fitness value is less than options.FunctionTolerance.
x
x = 1×2
-0.5000 -0.5000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
fval
fval = 5.7982e-07
function z = test_fitness(x,a,b)
z = norm(a*x+b);
end
.
I've ran my code in MATLAB online and it works there. I still have not determined the cause of the initial problem but this is the best answer this far. i'll be doing this project in MATLAB Online for the foreseeable future.

Sign in to comment.

More Answers (0)

Products

Release

R2024b

Asked:

on 26 Jan 2026

Commented:

on 27 Jan 2026

Community Treasure Hunt

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

Start Hunting!