forked from drwuHUST/MTGA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEMTEA.m
153 lines (141 loc) · 6.76 KB
/
EMTEA.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
function dataEMTEA = EMTEA(tasks,popSize,nGen,selPocess,pIL,nRepeat,idxTask,dq,initPop)
% EMTEA, Dongrui WU (drwu@hust.edu.cn), 8/20/2018
tic;
dataDisp=cell(1,3);
dataDisp{1}=idxTask; dataDisp{2}='EMTEA';
mu = 2; % Index of Simulated Binary Crossover (tunable)
mum = 5; % Index of polynomial mutation
nTasks=length(tasks);
while mod(popSize,nTasks)
popSize = popSize + 1;
end
dimTasks=zeros(1,nTasks); population=cell(1,nTasks);
for i=1:nTasks
dimTasks(i) = tasks(i).dims;
end
callsPerIndividual=zeros(1,popSize);
bestFitness = zeros(nRepeat,nGen,nTasks); % best fitness found
totalEvals=zeros(nRepeat,nGen); % total number of task evaluations so far
options = optimoptions(@fminunc,'Display','off','Algorithm','quasi-newton','MaxIter',2); % settings for individual learning
nTransfer=10;
fCosts=cell(1,2);
M=cell(nTasks,nTasks);
rnvecs=cell(1,2);
for r = 1:nRepeat
dataDisp{3}=r;
dq.send(dataDisp);
% Initialize the first generation randomly
gen=1;
for idxTask=1:nTasks
for i = 1 : popSize
population{idxTask}(i) = Chromosome();
population{idxTask}(i) = initialize(population{idxTask}(i),dimTasks(idxTask));
if nargin>=9
population{idxTask}(i).rnvec(1:dimTasks(idxTask))=initPop{idxTask,r}(i,1:dimTasks(idxTask));
else
initPop{idxTask,r}(i,1:dimTasks(idxTask))=population{idxTask}(i).rnvec;
end
[population{idxTask}(i),callsPerIndividual(i)] = evaluate_SOO(population{idxTask}(i),tasks(idxTask),pIL,options);
end
totalEvals(r,gen)=totalEvals(r,gen)+sum(callsPerIndividual);
fCosts{idxTask}=[population{idxTask}.factorial_costs];
[fCosts{idxTask},idsCost]=sort(fCosts{idxTask});
population{idxTask}=population{idxTask}(idsCost); % sort the chromosomes according to their costs
bestFitness(r,gen,idxTask)=fCosts{idxTask}(1);
rnvecs{idxTask}=reshape([population{idxTask}.rnvec],dimTasks(idxTask),popSize)';
rnvecs{idxTask}=cat(2,rnvecs{idxTask},ones(popSize,1));
end
% Compute M
for i=1:nTasks-1
for j=i+1:nTasks
if dimTasks(i)>dimTasks(j)
rnvecs{j}=cat(2,rnvecs{j},zeros(popSize,dimTasks(i)-dimTasks(j)));
elseif dimTasks(i)<dimTasks(j)
rnvecs{i}=cat(2,rnvecs{i},zeros(popSize,dimTasks(j)-dimTasks(i)));
end
M{i,j}=rnvecs{i}\rnvecs{j};
M{j,i}=rnvecs{j}\rnvecs{i};
end
end
for gen=2:nGen
totalEvals(r,gen)=totalEvals(r,gen-1);
for idxTask=1:nTasks
prevTask=idxTask-1;
if idxTask==1; prevTask=nTasks; end
tempPopulation=population{idxTask}(end:-1:1);
% Transfer some chromosomes from the previous task for reproduction
if ~rem(gen,10)
% replace bad chromosomes in the current population by good
% chromosomes from the previous population
if dimTasks(idxTask)>dimTasks(prevTask) % the previous task has a smaller dimensionality
for i=1:nTransfer
temp=[population{prevTask}(i).rnvec 1 zeros(1,dimTasks(idxTask)-dimTasks(prevTask))]...
*M{prevTask,idxTask};
tempPopulation(i).rnvec=temp(1:dimTasks(idxTask));
end
else
for i=1:nTransfer
temp=[population{prevTask}(i).rnvec 1]*M{prevTask,idxTask};
tempPopulation(i).rnvec=temp(1:dimTasks(idxTask));
end
end
end
idsOrder = randperm(popSize);
for i = 1 : popSize/2
p1 = idsOrder(i);
p2 = idsOrder(i+popSize/2);
u = rand(1,dimTasks(idxTask));
cf = zeros(1,dimTasks(idxTask));
cf(u<=0.5)=(2*u(u<=0.5)).^(1/(mu+1));
cf(u>0.5)=(2*(1-u(u>0.5))).^(-1/(mu+1));
child(2*i-1)=Chromosome();
child(2*i)=Chromosome();
child(2*i-1) = crossover(child(2*i-1),tempPopulation(p1),tempPopulation(p2),cf);
child(2*i) = crossover(child(2*i),tempPopulation(p2),tempPopulation(p1),cf);
child(2*i-1)=mutate(child(2*i-1),child(2*i-1),dimTasks(idxTask),mum);
child(2*i)=mutate(child(2*i),child(2*i),dimTasks(idxTask),mum);
% % variable swap
% swap_indicator = (rand(1,dimTasks(idxTask)) >= 0.5);
% temp = child(2*i-1).rnvec(swap_indicator);
% child(2*i-1).rnvec(swap_indicator) = child(2*i).rnvec(swap_indicator);
% child(2*i).rnvec(swap_indicator) = temp;
end
for i = 1 : popSize
[child(i),callsPerIndividual(i)] = evaluate_SOO(child(i),tasks(idxTask),pIL,options);
end
totalEvals(r,gen)=totalEvals(r,gen)+sum(callsPerIndividual);
intpopulation(1:popSize)=child;
intpopulation(popSize+1:2*popSize)=population{idxTask};
[~,idsCost]=sort([intpopulation.factorial_costs]);
intpopulation=intpopulation(idsCost);
bestFitness(r,gen,idxTask)=bestFitness(r,gen-1,idxTask);
if intpopulation(1).factorial_costs<=bestFitness(r,gen,idxTask)
bestFitness(r,gen,idxTask)=intpopulation(1).factorial_costs;
bestChromosome(r,idxTask)=intpopulation(1);
end
if strcmp(selPocess,'elitist')
if length(intpopulation)>=popSize
population{idxTask}=intpopulation(1:popSize);
else
population{idxTask}(1:length(intpopulation))=intpopulation;
for i=length(intpopulation)+1:popSize
population{idxTask}(i) = Chromosome();
population{idxTask}(i) = initialize(population{idxTask}(i),dimTasks(idxTask));
end
end
elseif strcmp(selPocess,'roulette wheel')
for i = 1:length(intpopulation)
intpopulation(i).scalar_fitness=1/i;
end
for i=1:popSize
population{idxTask}(i)=intpopulation(RouletteWheelSelection([intpopulation.scalar_fitness]));
end
end
end
end
end
dataEMTEA.wallClockTime=toc;
dataEMTEA.bestFitness=bestFitness;
dataEMTEA.bestIndData=bestChromosome;
dataEMTEA.totalEvals=totalEvals;
dataEMTEA.initPop=initPop;