forked from guoliu/optimizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evolve.m
64 lines (56 loc) · 1.8 KB
/
evolve.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
function pop = evolve(pop)
rng('shuffle');
grePcn = 90; % percentile of paerent in greedy method
lambda = 0.4; % strength of greedy jump
%% Define gamma
if mod(pop.gen,10)==0
gamma = 1;
else
gamma = 2.4/sqrt(2*pop.dms);
end
%% Generate parent matrix p, without repeating
s = (1:pop.size)';
p1 = mod(randi([0 pop.size-2],pop.size,1)+s,pop.size)+1;
p2 = randi([1,pop.size-2],pop.size,1);
p2 = (p2>=s|p2>=p1)+p2; p2 = (p2>=s&p2>=p1)+p2;
%% Greedy or gentle
if isfield(pop,'greedy') && (mod(pop.gen,10)~=0)
starPool = find(pop.fitness>=prctile(pop.fitness,grePcn));
ps = starPool(randi(numel(starPool),pop.size,1));
pad = lambda*(pop.inds(ps,:)-pop.inds);
else
pad = normrnd(0,1e-6,[pop.size,pop.dms]);
end
%% Create offspring embryo
embryo = pop.inds + gamma*(pop.inds(p1,:) - pop.inds(p2,:)) + pad;
%% Add crossover to offspring
if isfield(pop,'CR')
trandition = rand(pop.size,pop.dms) > pop.CR;
embryo(trandition) = pop.inds(trandition);
end
%% Add random (yet within range) mutations to offspring
if isfield(pop,'mutation')
weirdos = rand(pop.size,pop.dms) < pop.mutation;
pool = rand(pop.size, pop.dms).*(pop.range(:,:,2)...
- pop.range(:,:,1))+pop.range(:,:,1);
embryo(weirdos) = pool(weirdos);
end
%% Reflect back outliers and meet simplex requirement
shell = pop; shell.inds = embryo;
shell = cage(shell); embryo = shell.inds;
%% Evaluate and substitue offspring
fitness = multiEval(pop.func, embryo);
switch pop.type
case 'DEMC'
if pop.logFlag
snob = exp(fitness-pop.fitness)>rand(pop.size,1);
else
snob = fitness./pop.fitness>rand(pop.size,1);
end
case 'DE'
snob = fitness<pop.fitness;
end
pop.inds(snob,:) = embryo(snob,:);
pop.fitness(snob,:) = fitness(snob,:);
%% Change generation number
pop.gen = pop.gen + 1;