필터 지우기
필터 지우기

genetic algorithm with integer constraint returns non-integer solution

조회 수: 7 (최근 30일)
Seungbum Koo
Seungbum Koo 2020년 12월 21일
답변: Prateekshya 2024년 7월 10일 3:16
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