-
Notifications
You must be signed in to change notification settings - Fork 1
/
HarmonySearchFCN.m
147 lines (143 loc) · 4.29 KB
/
HarmonySearchFCN.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
function bestfis=HarmonySearchFCN(fis,data)
% Variables
p0=GettingFuzzyParameters(fis);
Problem.CostFunction=@(x) FuzzyCost(x,fis,data);
Problem.nVar=numel(p0);
alpha=1;
VarMin = -10; % Decision Variables Lower Bound
VarMax = 10; % Decision Variables Upper Bound
Problem.VarMin=-(10^alpha);
Problem.VarMax=10^alpha;
%
% Harmony Search Parameters
Params.MaxIt = 30; % Maximum Number of Iterations
Params.HMS = 3; % Harmony Memory Size
Params.nNew = 3; % Number of New Harmonies
Params.HMCR = 0.9; % Harmony Memory Consideration Rate
Params.PAR = 0.1; % Pitch Adjustment Rate
Params.FW = 0.02*(VarMax-VarMin); % Fret Width (Bandwidth)
Params.FW_damp = 0.995; % Fret Width Damp Ratio
%
% Starting Harmony Search Algorithm
results=Runhars(Problem,Params);
% Getting the Results
p=results.BestSol.Position.*p0;
bestfis=FuzzyParameters(fis,p);
end
%%----------------------------------------------
function results=Runhars(Problem,Params)
disp('Starting Harmony Search Training');
%------------------------------------------------
% Cost Function
CostFunction=Problem.CostFunction;
% Number of Decision Variables
nVar=Problem.nVar;
% Size of Decision Variables Matrixv
VarSize=[1 nVar];
% Lower Bound of Variables
VarMin=Problem.VarMin;
% Upper Bound of Variables
VarMax=Problem.VarMax;
% Some Change
if isscalar(VarMin) && isscalar(VarMax)
dmax = (VarMax-VarMin)*sqrt(nVar);
else
dmax = norm(VarMax-VarMin);
end
%--------------------------------------------
% Harmony Search Algorithm Parameters
% Maximum Number of Iterations
MaxIt=Params.MaxIt;
% Harmony Memory Size
% nPop=Params.HMS;
HMS=Params.HMS;
% Number of New Harmonies
nNew=Params.nNew;
HMCR = 0.9; % Harmony Memory Consideration Rate
PAR = 0.1; % Pitch Adjustment Rate
FW = 0.02*(VarMax-VarMin); % Fret Width (Bandwidth)
FW_damp = 0.995; % Fret Width Damp Ratio
%------------------------------------------------------
% Second Stage
% Empty Harmony Structure
empty_harmony.Position = [];
empty_harmony.Cost = [];
% Initialize Harmony Memory
HM = repmat(empty_harmony, HMS, 1);
% Create Initial Harmonies
for i = 1:HMS
HM(i).Position = unifrnd(VarMin, VarMax, VarSize);
HM(i).Cost = CostFunction(HM(i).Position);
end
% Sort Harmony Memory
[~, SortOrder] = sort([HM.Cost]);
HM = HM(SortOrder);
% Update Best Solution Ever Found
BestSol = HM(1);
% Array to Hold Best Cost Values
BestCost = zeros(MaxIt, 1);
%
%% Harmony Search Algorithm Main Body
%
for it = 1:MaxIt
% Initialize Array for New Harmonies
NEW = repmat(empty_harmony, nNew, 1);
% Create New Harmonies
for k = 1:nNew
% Create New Harmony Position
NEW(k).Position = unifrnd(VarMin, VarMax, VarSize);
for j = 1:nVar
if rand <= HMCR
% Use Harmony Memory
i = randi([1 HMS]);
NEW(k).Position(j) = HM(i).Position(j);
end
% Pitch Adjustment
if rand <= PAR
%DELTA = FW*unifrnd(-1, +1); % Uniform
DELTA = FW*randn(); % Gaussian (Normal)
NEW(k).Position(j) = NEW(k).Position(j)+DELTA;
end
end
% Apply Variable Limits
NEW(k).Position = max(NEW(k).Position, VarMin);
NEW(k).Position = min(NEW(k).Position, VarMax);
% Evaluation
NEW(k).Cost = CostFunction(NEW(k).Position);
end
% Merge Harmony Memory and New Harmonies
HM = [HM
NEW]; %#ok
% Sort Harmony Memory
[~, SortOrder] = sort([HM.Cost]);
HM = HM(SortOrder);
% Truncate Extra Harmonies
HM = HM(1:HMS);
% Update Best Solution Ever Found
BestSol = HM(1);
% Store Best Cost Ever Found
BestCost(it) = BestSol.Cost;
% Show Iteration Information
disp(['In Iteration ' num2str(it) ' Out Of ' num2str(MaxIt) ': Fittest Cost = ' num2str(BestCost(it))]);
% Damp Fret Width
FW = FW*FW_damp;
end
%------------------------------------------------
disp('Harmony Search Algorithm Came To End');
% Store Result
results.BestSol=BestSol;
results.BestCost=BestCost;
% Plot Harmony Search Training Stages
set(gcf, 'Position', [600, 300, 800, 350])
plot(BestCost,'-',...
'LineWidth',2,...
'MarkerSize',3,...
'MarkerEdgeColor','g',...
'Color',[0.2,0.9,0.4]);
title('Harmony Search Algorithm Training')
xlabel('HS Iteration Number','FontSize',10,...
'FontWeight','bold','Color','k');
ylabel('HS Best Cost Result','FontSize',10,...
'FontWeight','bold','Color','k');
legend({'Harmony Search Train'});
end