-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.m
105 lines (93 loc) · 3.27 KB
/
main.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
global nStates;
global nData;
global nRules_Output;
global rbarOutput;
global rbarState;
global currentIndividual;
global targetOutput;
rngrnd = 1;
rng(rngrnd);
%% Load Air Quality Index NO2 Dataset
fprintf("Loading Air Quality Index NO2 Dataset...\n");
load('Benchmarks\\NO2_10step.mat');
tempdata = data;
TargetDimension = 5;
data = (data - min(data)) ./ (max(data)-min(data)) ;
%data = data * 2 - 1;
trainInput = data(1:24000,1:4);
targetOutput = data(1:24000,5);
testInput = data(24001:end,1:4);
testOutput = data(24001:end,5);
%%
% ****************************************************
% * Parameters *
% ****************************************************
coverageRules = false; % Generating Fuzzy Rules Using Coverage ?
ruleSigma = 0.3;
coverageThreshold = 0.2;
nFuzzySetsOutput = 2;
nFuzzySetsState = 2;
nStates = 2;
nRules_Output = nFuzzySetsOutput ^ 4;
nRules_State = nFuzzySetsState ^ 4;
mfType = "trimf";
PSO_SwarmSize = 20;
PSO_MaxIteration = 10;
nData = size(trainInput,1);
nDimensions = size(trainInput,2);
finalResults = [];
%% Create Fuzzy Rules
if ~coverageRules
if mfType == "gaussmf"
mfOutput = createGaussianMembershipFunction(nFuzzySetsOutput);
mfState = createGaussianMembershipFunction(nFuzzySetsState);
elseif mfType == "trimf"
mfOutput = createMembershipFunction(nFuzzySetsOutput);
mfState = createMembershipFunction(nFuzzySetsState);
elseif mfType == "gauss2mf"
mfOutput = createGaussian2MembershipFunction(nFuzzySetsOutput);
mfState = createGaussian2MembershipFunction(nFuzzySetsState);
elseif mfType == "gbellmf"
mfOutput = createGeneralizedBellshapeMembershipfunction(nFuzzySetsOutput);
mfState = createGeneralizedBellshapeMembershipfunction(nFuzzySetsState);
end
mfOutput = repmat(mfOutput,1,nDimensions);
mfState = repmat(mfState,1,nDimensions);
else
if ~exist('mf','var')
if mfType == "gaussmf"
generateGaussianFuzzyRulesUsingCoverage;
elseif mfType == "trimf"
generateTriangleFuzzyRulesUsingCoverage;
end
end
mfOutput = mf;
mfState = mf;
nFuzzySetsOutput = length(mf);
nFuzzySetsState = length(mf);
nRules_Output = nFuzzySetsOutput;
nRules_State = nFuzzySetsState;
end
fprintf("Calculating Membership Values...\n");
calculateMembershipValues;
%% Initialization
% W -> Weight of Output Network
% V -> Weight of State Network
currentIndividual.V = rand(nStates,nRules_State);
currentIndividual.W = rand(nStates,nRules_Output);
costCalculation02;
%% Training Network
fprintf("Training Network Using PSO...");
fun = @(x) costCalculation03(x);
lb = zeros(1,nRules_State*nStates);
ub = ones(1,nRules_State*nStates);
options = optimoptions('particleswarm','SwarmSize',PSO_SwarmSize,'Display','iter','MaxIterations',PSO_MaxIteration);
[x, fval] = particleswarm(fun,nRules_State*nStates,lb,ub,options);
currentIndividual.V = x;
costCalculation02
calculatingTestError;
finalResults = [scaledTestRMSE scaledTestSMAP testRMSE];
fprintf("Ten Step Ahead Prediction Results:\n");
fprintf("RMSE = %e\n",testRMSE);
fprintf("MAE = %e\n",testMAE);
fprintf("SMAPE = %e\n",testSMAP);