GA Function non linear Constraints decrease Initial Population to 1 ?

Dear Matlab users,
I am currently having a problem to define the nonlinear constraints.
The point of problem is with nonlinear constraints, it doesnt' keep the number of initial population to fitnessfunction and left even only one individual.
But without nonlinear constraints option it keeps 45 individuals.
Please take a look this codes and may very greatful, when you write me any small tips.
size(InitPop) = 45
Vectorized= 'on'
1. Starting InitialPopulation [45,9]
options = optimoptions(@ga, 'PopulationSize', 45, 'InitialPopulation', InitPop, ...
'PopulationType', 'doubleVector', ''MaxStallGenerations', 10, ...
'FunctionTolerance', 1e-03, 'MaxGenerations', 100, 'Vectorized', 'on');
2.
[Value, fval, ~, output] = ga(@fitnessfunction, nvars, [], [], [], [], lb,ub, @nonlincon, [], options);
3. Definition of nonlinear constraints
function [c, ceq] = nonlincon(x)
c = 3 - (x(:, 1) + 3 * tan(x(:, 2)));% 3 - (x1 + 3tan(x2)) <= 0
ceq = [];
end
Thank you so much for time
Best regards.

14 Comments

@Sunghyun Woo Since the code you've posted throws an error, there is a chance that we are not seeing what you are actually running,
options = optimoptions(@ga, 'PopulationSize', 45, 'InitialPopulation', InitPop, ...
'PopulationType', 'doubleVector', ''MaxStallGenerations', 10, ...
Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters, or other syntax error. To construct matrices, use brackets instead of parentheses.
'FunctionTolerance', 1e-03, 'MaxGenerations', 100, 'Vectorized', 'on');
This is the lb and ub.
% Define lower and upper bounds
lb = [7.5, 2.5, -2, -0.92, 5.5, 225.7, -4, -1.5, 5.5]; % Lower bounds
ub = [25, 2.9, 1.5, 2, 8, 228, 1.5, 0.22, 8]; % Upper bounds
And InitPop was randomly generated by this code.
InitPop = rand(45, 9) .* (ub - lb) + lb;
We don't know your fitness function.
% Define lower and upper bounds
lb = [7.5, 2.5, -2, -0.92, 5.5, 225.7, -4, -1.5, 5.5]; % Lower bounds
ub = [25, 2.9, 1.5, 2, 8, 228, 1.5, 0.22, 8]; % Upper bounds
InitPop = rand(45, 9) .* (ub - lb) + lb;
nvars = numel(lb);
options = optimoptions(@ga, 'PopulationSize', 45, 'InitialPopulation', InitPop, ...
'PopulationType', 'doubleVector', 'MaxStallGenerations', 10, ...
'FunctionTolerance', 1e-03, 'MaxGenerations', 100, 'Vectorized', 'on');
[Value, fval, ~, output] = ga(@fitnessfunction, nvars, [], [], [], [], lb,ub, @nonlincon, [], options);
Unrecognized function or variable 'fitnessfunction'.

Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});

Error in gacon (line 23)
Iterate.f = FitnessFcn(Iterate.x');

Error in ga (line 424)
[x,fval,exitFlag,output,population,scores] = gacon(FitnessFcn,nvars, ...
function [c, ceq] = nonlincon(x)
c = 3 - (x(:, 1) + 3 * tan(x(:, 2)));% 3 - (x1 + 3tan(x2)) <= 0
ceq = [];
end
Thank you for answer Torsten!
I will upload the code that can help figure out the current issue.
The key point is that after the ga function, the [x] of fitnessfunction(x) has the value of one individual.
Ga Function Input : size(InitPop) = [45x9]
Fitnessfunction input : size(x) = [1x9]
% Setup
nvars = 9;
popsize = 45;
mutationrate = 0.1;
lb = [7.5, 2.5, -2, -0.92, 5.5, 225.7, -4, -1.5, 5.5]; % Lower bounds
ub = [25, 2.9, 1.5, 2, 8, 228, 1.5, 0.22, 8]; % Upper bounds
InitPop = rand(45, 9) .* (ub - lb) + lb;
disp(size(InitPop)) % = 45 9
45 9
% Optimization options
options = optimoptions(@ga, 'PopulationSize', 45, 'InitialPopulation', InitPop, ...
'PopulationType', 'doubleVector', 'MaxStallGenerations', 10, ...
'FunctionTolerance', 1e-03, 'MaxGenerations', 100, 'Vectorized', 'on');
[Value, fval, ~, output] = ga(@fitnessfunction, nvars, [], [], [], [], lb,ub, @nonlincon, [], options);
1 9
Error using readmatrix (line 171)
Unable to find or open 'result.csv'. Check the path and filename or file permissions.

Error in solution>fitnessfunction (line 27)
data = readmatrix('result.csv');

Error in createAnonymousFcn>@(x)fcn(x,FcnArgs{:}) (line 11)
fcn_handle = @(x) fcn(x,FcnArgs{:});

Error in gacon (line 23)
Iterate.f = FitnessFcn(Iterate.x');

Error in ga (line 424)
[x,fval,exitFlag,output,population,scores] = gacon(FitnessFcn,nvars, ...
% Define Nonlinear Constraints
function [c, ceq] = nonlincon(x)
c = 3 - (x(:, 1) + 3 * tan(x(:, 2))); % 3 - (x1 + 3tan(x2)) <= 0
ceq = [];
end
% Define Fitnessfunction
function f = fitnessfunction(x)
disp(size(x)) % = 1 9
% run simulation and get results
data = readmatrix('result.csv');
f = data(:, 1);
end
I don't understand how your 9 parameters should change the constant f of your fitness function.
Hi Torsten,
sorry for this confusing. I could not upload the whole Fitnessfunction because of security.
But the point was not the codes in fitnessfunction.
The first input of x in firnessfunction started with one individual.
I thought that it should start with 45 individuals.
Thank you so much
At the start, shouldn't the fitnessfunction be called 45 times with 45 different x-vectors of size 1x9 instead of only once with an x-matrix of size 45x9 ? I guess, yes.
data = readmatrix('result.csv');
Don't do that ! Instead read the file once at the beginning and pass the data into the function !
Thanks for answers Torsten and Walter!
Dear Torsten,
That's the reason I set the Options of GA ( 'Vectorized' , 'on'). I think this option can make the fitnessfunction calculates parallel each x-vector at once.
Thanks a lot!
Dear Walter,
I don't understand what you mean, Would you please explain me with more details? :)
Thanks a lot!
I only find the options "InitialPopulationMatrix" and "UseVectorized", not the options "InitialPopulation" and "Vectorized" (at least in R2024b):
optimoptions(@ga)
ans =
ga options: Set properties: No options set. Default properties: ConstraintTolerance: 1.0000e-03 CreationFcn: [] CrossoverFcn: [] CrossoverFraction: 0.8000 Display: 'final' EliteCount: '0.05*PopulationSize' FitnessLimit: -Inf FitnessScalingFcn: @fitscalingrank FunctionTolerance: 1.0000e-06 HybridFcn: [] InitialPopulationMatrix: [] InitialPopulationRange: [] InitialScoresMatrix: [] MaxGenerations: '100*numberOfVariables' MaxStallGenerations: 50 MaxStallTime: Inf MaxTime: Inf MutationFcn: [] NonlinearConstraintAlgorithm: 'auglag' OutputFcn: [] PlotFcn: [] PopulationSize: '50 when numberOfVariables <= 5, else 200' PopulationType: 'doubleVector' SelectionFcn: [] UseParallel: 0 UseVectorized: 0
Thanks a lot Torsten,
I changed the options as you told. You were right.
But my question was not solved yet, it has still only one vector x. Nevertheless, the good news is, that after one round in fitness function with one vector, the fitnessfunction runs again with the 45 vectors.
I think this is the logic in ga function.
What @Walter Roberson wanted to tell you:
Don't read data files in functions of your code. It can happen that functions are called several thousand times during one computation - and at each call, MATLAB wastes time opening your data file, reading the data it already read a hundred times before and closing the file.
Read your data once before you call "ga", save them in an array and pass this array to the functions where the data are needed.
Dear Torsten,
thank you so much for kind answers. I finally found the reason as I gold you. After initial check of the fitnessfunction start main loop.
Thank you so much!

Sign in to comment.

Answers (0)

Categories

Products

Release

R2022a

Asked:

on 6 Jan 2025

Commented:

on 9 Jan 2025

Community Treasure Hunt

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

Start Hunting!