GA Function non linear Constraints decrease Initial Population to 1 ?
Show older comments
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
Walter Roberson
on 6 Jan 2025
I have to wonder what lb and ub are ?
@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, ...
'FunctionTolerance', 1e-03, 'MaxGenerations', 100, 'Vectorized', 'on');
Sunghyun
on 7 Jan 2025
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);
function [c, ceq] = nonlincon(x)
c = 3 - (x(:, 1) + 3 * tan(x(:, 2)));% 3 - (x1 + 3tan(x2)) <= 0
ceq = [];
end
Torsten
on 7 Jan 2025
I don't understand how your 9 parameters should change the constant f of your fitness function.
Sunghyun
on 7 Jan 2025
Walter Roberson
on 7 Jan 2025
data = readmatrix('result.csv');
Don't do that ! Instead read the file once at the beginning and pass the data into the function !
I only find the options "InitialPopulationMatrix" and "UseVectorized", not the options "InitialPopulation" and "Vectorized" (at least in R2024b):
optimoptions(@ga)
Sunghyun
on 8 Jan 2025
Torsten
on 8 Jan 2025
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.
Sunghyun
on 9 Jan 2025
Answers (0)
Categories
Find more on Introduction to Installation and Licensing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!