- Genetic Algorithm, like many optimization algorithm, works with floating point arithmetic which has inherent limitations.
- The way Genetic Algorithm converges to a solution.
genetic algorithm with integer constraint returns non-integer solution
3 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
Prateekshya
on 10 Jul 2024
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:
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.
0 Comments
See Also
Categories
Find more on Genetic Algorithm 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!