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 个评论
Walter Roberson
2025-1-6
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
2025-1-7
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
2025-1-7
I don't understand how your 9 parameters should change the constant f of your fitness function.
Sunghyun
2025-1-7
Walter Roberson
2025-1-7
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
2025-1-8
Torsten
2025-1-8
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
2025-1-9
回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!