How to use ga in matlab as a binary genetic algorithm?

Answers (1)

Stephan
Stephan on 25 Dec 2018
Edited: Stephan on 25 Dec 2018
Hi,
use IntCon and set lower and upper bounds to zero respectively ones.
Best regards
Stephan

5 Comments

Hi
My fitness function is
function y = fitness(x)
y = ((3*x(1) + x(2) - 2*x(3) + 0.8)/(2*x(1) - x(2) + x(3))) + ((4*x(1) - 2*x(2) + x(3))/(7*x(1) + 3*x(2) - x(3)));
and the constraints function is
function [c, ceq] = constraints(x)
c(1)= x(1) + x(2) - x(3) -1;
c(2)= -x(1)+x(2)-x(3) +1;
c(3)= 12*x(1)+5*x(2)+12*x(3) - 34.8;
c(4)= 12*x(1)+12*x(2)+7*x(3) - 29.1;
c(5)= -6*x(1)+x(2)+x(3) + 4.1;
ceq = [];
and the main program is
rng(1,'twister')
IntCon = 1;
%% Start with the default options
options = optimoptions('ga');
%% options setting
options = optimoptions(options,'MaxGenerations', 500);
options = optimoptions(options,'MaxStallGenerations', inf);
options = optimoptions(options,'FunctionTolerance', 0);
options = optimoptions(options,'ConstraintTolerance', 0);
options = optimoptions(options,'Display', 'off');
options = optimoptions(options,'PlotFcn', { @gaplotbestf });
[x,fval,exitflag,output,population,score] = ...
ga(@fitness,3,[],[],[],[],[0 0 0],[0 0 0],@constraints,IntCon,options);
The solution is 2.4 with x values [1,0,0] how to get this values with this code?
They are stored in x.
disp(x)
To have only binary variables use:
Inton = 1:3;
your upper bounds should be:
[1 1 1]
Hi,
I know how to show them but i want the solution like that?
rng(1,'twister')
IntCon = 1:3;
%% Start with the default options
options = optimoptions('ga');
%% options setting
options = optimoptions(options,'MaxGenerations', 500);
options = optimoptions(options,'MaxStallGenerations', inf);
options = optimoptions(options,'FunctionTolerance', 0);
options = optimoptions(options,'ConstraintTolerance', 0);
options = optimoptions(options,'Display', 'off');
options = optimoptions(options,'PlotFcn', { @gaplotbestf });
[x,fval,exitflag,output,population,score] = ...
ga(@fitness,3,[],[],[],[],[0 0 0],[1 1 1],@constraints,IntCon,options);
disp(x);
However, the best is not [1 0 0]: it is [1 0 1]
There are only 8 possible configurations for x, so there is no point in running 500 generations.

Sign in to comment.

Asked:

on 25 Dec 2018

Commented:

on 25 Dec 2018

Community Treasure Hunt

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

Start Hunting!