Nonlinear constraints in genetic algorithm are not fulfilled

7 views (last 30 days)
Hey everybody,
I'm trying to use a genetic algorithm for finding the global minimise of a non-analytic function. The ga() calls a function, which starts a extern simulation, which returns the value to minimise. In my simple example I use 2 variables. Both variables must be unsigned integers. The first one has predefined lower and upper limits. The second one has also lower and upper limits which are dependent on the actual value of the first variable:
x(1)_min <= x(1) <= x(1)_max (1)
x(1)^2 <= x(2) <= 2*x(1)^2 (2)
The first condition is defined in lower and upper values. The second one is defined as nonlinear constraint. Because x(1)_min and x(1)_max are positive values, x(2) should also be positive.
My problem is, that the defined nonlinear constraints are partially not fulfilled, when I take a look at the population after calculating. Some of the values of x(2) are negative, some are higher than 2*x(1)^2
Does anyone know, why this happens? Why are the cnstraints not fulfilled?
Best regards; J
% limits for x(1)
l = [30]; % lower limit for x(1)
u = [50]; % upper limit for x(1)
% Nonlinear constraint for x(2)
c = @(x)[x(1)^2-x(2); %x(1)^2 <= x(2)
x(2)-2*x(1)^2]; %x(2) <= 2*x(1)^2
ceq = @(x) [];
nonlinfcn = @(x)deal(c(x),ceq(x));
% Function to call extern simulation; actually for test cases a test-function which
% returns the value of f(x) = 2x(1)^2+0.5*x(2)
Fitness_function=@(x)run_simulation(x);
n=2; % number of variables
rng default;
options=optimoptions('ga','PlotFcn',@gaplotbestf,'PopulationSize',100,'MaxGenerations',50,'MaxStallGenerations',50);
[x,fval,exitflag,output,population,scores] = ga(Fitness_function,n,[],[],[],[],...
[l],[u],[nonlinfcn],[1:n],options)

Answers (2)

Matt J
Matt J on 28 Jan 2022
There is nothing ensuring that arbitrary population members in an arbitrary generation will satisfy nonlinear constraints. The final output x should satisfy constraints within the tolerances set by optimoptions if the solver was successful. The exitflag will indicate whether ga() felt that it was successful or not.

Alan Weiss
Alan Weiss on 28 Jan 2022
Edited: Alan Weiss on 28 Jan 2022
You could help the solver by setting a lower bound of 900 on x(2), and an upper bound of 2*50^2. You will still get infeasible population members, but not as many.
Alan Weiss
MATLAB mathematical toolbox documentation
  1 Comment
Matt J
Matt J on 28 Jan 2022
JohannesR's comment moved here
Hi, thank you for your tips. I will try to define the boundary conditions more narrowly.
But my problem is, that the real function would be an extern FEM simulation. If the boundary conditions are not met, the FEM-model would get "destroyed" such that it wouldn't be solvable.
I thought about checking the constraints once again inside of run_simulation() and if they are not met, the function could return any high value, which is more than expected.
Best regards
J

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by