genetic algorithm with integer constraint returns non-integer solution

7 次查看(过去 30 天)
I was solving the Four Peaks Problem[1] using ga function in MATLAB.
This problem has an integer constraint and the solution is expected to be in integers, but MATLAB's ga function returns non-integer solution, although they are almost integers.
Following is the code. I expect to see
oz = [21.0000 79.0000], and fval = -179.0000
but I see
oz = [21.0000 79.0010], and fval = -179.0010
Well, they are, like I said, almost integers, so I surely can interpret them, but I was just curious. Is this expected output? Or is the function missing something during the postprocessing?
%% Setup
close
clear
clc
% for reproducibility
rng(1,'twister')
% length of the bit string
N = 100;
% threshold
T = 20;
% Pass in fixed parameters to objfun.
objfun = @(oz) -four_peaks(oz,T,N);
% constraints
INTCON = [1, 1]; % integer constraints: Both o and z must be integers.
lb = [0, 0]; % Both o and z cannot be negative.
ub = [N,N]; % Both o and z cannot be greater than the length of the bit string.
A = [1,1]; b = N; % inequality constraint: o+z < length of the bit string.
%% Solve
% Solve
[oz,fval,exitflag,output,population,scores] = ga(objfun,2,A,b,[],[],lb,ub,[],INTCON);
%% Report
disp(oz)
disp(-fval)
%% Object Function
function y = four_peaks(x, T, N)
if x(1) > T && x(2) > T
y = max(x) + N;
else
y = max(x);
end
end

回答(1 个)

Prateekshya
Prateekshya 2024-7-10,3:16
Hello Seungbum,
I can see that the results are not exactly integers but close to integers.
The above mentioned behavior is an expected behavior due to the following factors:
  1. Genetic Algorithm, like many optimization algorithm, works with floating point arithmetic which has inherent limitations.
  2. The way Genetic Algorithm converges to a solution.
Using "INTCON" can increase the integer strictness which is already used in the given code.
However, the important thing to note here is, if the constraints are not integers, the results will turn out to be pure floating point values which are not very close to integers. Hence, these are the expected values.
I hope this helps!
Thank you.

类别

Help CenterFile Exchange 中查找有关 Genetic Algorithm 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by