-
Notifications
You must be signed in to change notification settings - Fork 0
/
TrainUsingDE.m
143 lines (91 loc) · 3.03 KB
/
TrainUsingDE.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
function bestfis=TrainUsingDE(fis,data)
%% Problem Definition
p0=GetFISParams(fis);
Problem.CostFunction=@(x) TrainFISCost(x,fis,data);
Problem.nVar=numel(p0);
alpha=1;
Problem.VarMin=-(10^alpha);
Problem.VarMax=10^alpha;
%% DE Params
Params.MaxIt=100;
Params.nPop=20;
%% Run DE
results=RunDE(Problem,Params);
%% Get Results
p=results.BestSol.Position.*p0;
bestfis=SetFISParams(fis,p);
end
function results=RunDE(Problem,Params)
disp('Starting DE ...');
%% Problem Definition
CostFunction=Problem.CostFunction; % Cost Function
nVar=Problem.nVar; % Number of Decision Variables
VarSize=[1 nVar]; % Size of Decision Variables Matrix
VarMin=Problem.VarMin; % Lower Bound of Variables
VarMax=Problem.VarMax; % Upper Bound of Variables
%% DE Parameters
MaxIt=Params.MaxIt; % Maximum Number of Iterations
nPop=Params.nPop; % Population Size
beta_min=0.2; % Lower Bound of Scaling Factor
beta_max=0.8; % Upper Bound of Scaling Factor
pCR=0.1; % Crossover Probability
%% Initialization
empty_individual.Position=[];
empty_individual.Cost=[];
BestSol.Cost=inf;
pop=repmat(empty_individual,nPop,1);
for i=1:nPop
if i>1
pop(i).Position=unifrnd(VarMin,VarMax,VarSize);
else
pop(i).Position=ones(VarSize);
end
pop(i).Cost=CostFunction(pop(i).Position);
if pop(i).Cost<BestSol.Cost
BestSol=pop(i);
end
end
BestCost=zeros(MaxIt,1);
%% DE Main Loop
for it=1:MaxIt
for i=1:nPop
x=pop(i).Position;
A=randperm(nPop);
A(A==i)=[];
a=A(1);
b=A(2);
c=A(3);
% Mutation
%beta=unifrnd(beta_min,beta_max);
beta=unifrnd(beta_min,beta_max,VarSize);
y=pop(a).Position+beta.*(pop(b).Position-pop(c).Position);
% Crossover
z=zeros(size(x));
j0=randi([1 numel(x)]);
for j=1:numel(x)
if j==j0 || rand<=pCR
z(j)=y(j);
else
z(j)=x(j);
end
end
NewSol.Position=z;
NewSol.Cost=CostFunction(NewSol.Position);
if NewSol.Cost<pop(i).Cost
pop(i)=NewSol;
if pop(i).Cost<BestSol.Cost
BestSol=pop(i);
end
end
end
% Update Best Cost
BestCost(it)=BestSol.Cost;
% Show Iteration Information
disp(['Iteration ' num2str(it) ': Best Cost = ' num2str(BestCost(it))]);
end
disp('End of DE.');
disp(' ');
%% Results
results.BestSol=BestSol;
results.BestCost=BestCost;
end