Вы находитесь на странице: 1из 2

Evolutionary Algorithms Problem Set - Genetic Algorithms in MATLAB

1. Below you find the sceleton of a canonical GA implementation in MATLAB:

function

[opt,

fopt,

stat]

%

lambda

Strategy

=

parameters

=

ga(l,

pc

=

pm

=

%

Statistics

administration

evalcount

=

0;

stat.histf

=

zeros(1,

stopeval);

%

for

Initialize

i

Generate

%

population

=

1:lambda

random

chromosome,

fitnessfct,

decodefct,

decode

to

phenotype,

and

P(:,i)

=

%

random

bitstring

g(:,i)

=

feval(decodefct,

P(:,i));

f(i)

=

feval(fitnessfct,

g(:,i));

%

=

optindex]

P(:,optindex);

stat.histf(evalcount)

aopt

evalcount

evalcount

Statistics

[fopt,

=

administration

=

1;

max(f);

+

=

fopt;

end

%

while

Evolution

loop

evalcount

<

stopeval

selectfct,

evaluate

stopeval)

%

Generate

new

population

(recombination,

mutation)

for

i

=

1:lambda

 
 

p1

=

feval(selectfct,

P,

f);

if

(rand()

<

pc)

p2

Pnew(:,i)

=

feval(selectfct,

=

%

P,

f);

crossover

else

Pnew(:,i)

end

Pnew(:,i)

end

=

=

%

%

copy

mutation

%

Replace

old

population

by

new

population

P

=

Pnew;

%

Decode

and

evaluate

for

i

=

g(:,i)

f(i)

=

1:lambda

=

feval(fitnessfct,

feval(decodefct,

P(:,i));

g(:,i));

1

end

% Statistics

administration

[fopt,

opt

for

=

i

optindex]

=

max(f);

P(:,optindex);

=

1:lambda

evalcount

=

evalcount

+

1;

stat.histf(evalcount)

=

fopt;

end

%

clf

subplot(2,1,1)

plot(stat.histf(1:evalcount))

subplot(2,1,2)

bar([1:l],opt)

xlim([1

drawnow()

Plot

statistics

l])

end

end

(a)

(b)

Complete the code.

Run the GA a couple of times on the ONEMAX problem (= counting ones problem) provided below. Use bitstrings of length 100 and an evaluation budget of 100000 eval- uations. Use the phenotype decoding function (which is a dummy function in this case as we don’t need any phenotype decoding) and selection function provided below:

function

f

=

ONEMAX(a)

f

=

sum(a);

end

function

g

=

no_decoding(a)

g

= a;

end

function

a

=

select_proportional(P,

f)

cumsum_f

=

cumsum(f);

sum(f)

r *

=

rand();

i 1;

=

while

(r

>=

cumsum_f(i))

i

=

i

+

1;

end

a

=

P(:,i);

end

How does it perform? Compare also using different bitstring lengths.

(c)

In the lecture, also a ’fix’ for proportional selection was discussed in which the fitness values are rescaled. Implement a function select one scaled proportional which implements this fixed proportional selection method. Does it make a difference on the counting ones?

(d)

Also construct a tournament selection method and compare it against the other two selection methods.

2. Implement a genotype decoding function such that the GA can be used to optimize real- valued optimization problems. Test it on a 10-dimensional version of the sphere function

(f( x) =

N

i=1 x i

2

with x i R), use the search interval x i [10, 10].

2