## genetic algorithm with integer constraint returns non-integer solution

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

endI 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 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 genetic algorithm, four peaks problem MATLAB Answers — New Questions