-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfdt.m
213 lines (181 loc) · 6.68 KB
/
fdt.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
function fdt(inFile, mf, op, decTree, w)
% Fuzzy decision trees
in = csvread(inFile);
% Input data has 7 variables
% Car ID, Risk, Value Loss, Horsepower, City MPG, Highway MPG, and Price
% Plot the input data for visualization
% Input data is zscore normalized to reduce the range of inputs
figure; plot(zscore(in(:,2:end), 1, 1));
title('Input data plot');
legend('Risk', 'Value Loss', 'Horse power', 'City MPG', 'Highway MPG', 'Price')
% Membership funtions for the input characteristics
if mf==1
% Risk
risk.Low = [-3 -3 -2 0];
risk.Average = [-2 0 2];
risk.High = [0 2 3 3];
% Value Loss
valueLoss.Low = [0 0 100 200];
valueLoss.Average = [100 120 200];
valueLoss.High = [120 200 300 300]; %#ok<*STRNU>
% Horsepower
horsepower.Low = [0 0 60 100];
horsepower.Average = [60 100 140];
horsepower.Low = [100 140 250 250];
% City MPG
cityMPG.Poor = [0 0 20 30];
cityMPG.Average = [20 30 40];
cityMPG.Good = [30 40 60 60];
% Highway MPG
highwayMPG.Poor = [0 0 20 30];
highwayMPG.Average = [20 30 40];
highwayMPG.Good = [30 40 60 60];
% Price
price.Cheap = [0 0 7000 10000];
price.Average = [7000 10000 20000];
price.Expensive = [10000 20000 40000 40000];
elseif mf==2 % 2nd membership function for 2nd decision tree
% Risk
risk.Low = [-3 -3 -2 0];
risk.Average = [-2 0 2];
risk.High = [-3 1 3 3]; % Modified
% Value Loss
valueLoss.Low = [0 60 100 140]; % Modified
valueLoss.Average = [100 120 200];
valueLoss.High = [120 200 300 300]; %#ok<*STRNU>
% Horsepower
horsepower.Low = [0 0 60 100];
horsepower.Average = [50 85 125]; % Modified
horsepower.Low = [100 140 250 250];
% City MPG
cityMPG.Poor = [0 0 20 30];
cityMPG.Average = [25 35 40]; % Modified
cityMPG.Good = [30 40 60 60];
% Highway MPG
highwayMPG.Poor = [0 0 20 30];
highwayMPG.Average = [20 30 40];
highwayMPG.Good = [30 45 60 60]; % Modified
% Price
price.Cheap = [0 0 7000 10000];
price.Average = [7000 10000 40000 40000]; % Modified, membership is trapmf
price.Expensive = [10000 20000 40000 40000];
end
% Decision Tree for evaluting the car for the membership function,
% operation and assumed decision tree
assesment = decisionTree(in, decTree, op, w, risk, valueLoss, horsepower, cityMPG, highwayMPG, price);
[bestVal, bestIdx] = max(assesment, [], 1); % Finding the best Car
disp(['Best Car ID is ', num2str(in(bestIdx, 1)), ' and its value is ', num2str(bestVal)]);
close all;
% Plot output car ratings for each car
figure(1);
plot(in(:,1), assesment)
xlabel('Car ID')
ylabel('Car Rating')
title('Car assessment rating based on Fuzzy Decision Tree')
% Histogram plot all the car ratings
[counts, centers]=hist(assesment, 10); % Histogram of output car ratings
figure(2);
bar(centers, counts)
title('Histogram for car assessment')
end
% Decision Tree for the given input
function assesment = decisionTree(in, decisionTree, opModel, w, risk, valueLoss, horsepower, cityMPG, highwayMPG, price)
% Number of inputs
N = size(in, 1);
assesment = zeros(N,1);
for i=1:N % Loop for all the input data for the decision tree
if decisionTree==1 % Default decision tree
assesment(i) = fuzzyOp([...
fuzzyOp([...
fuzzyOp([eval_mf({highwayMPG.Good, in(i,6)}, 'trapmf'), eval_mf({horsepower.Average, in(i,4)}, 'trimf')], 'AND_F', opModel, w),...
fuzzyOp(eval_mf({cityMPG.Poor, in(i,5)}, 'trapmf'), 'NOT_F', opModel, w)],...
'AND_F', opModel, w),...
fuzzyOp([...
fuzzyOp([eval_mf({risk.Low, in(i,2)}, 'trapmf'), eval_mf({valueLoss.Low, in(i,3)}, 'trapmf')],'AND_F', opModel, w),...
eval_mf({price.Cheap, in(i,7)}, 'trapmf')],...
'OR_F', opModel, w)],...
'AND_F', opModel, w);
elseif decisionTree==2
assesment(i) = fuzzyOp([...
fuzzyOp([...
fuzzyOp([eval_mf({cityMPG.Average, in(i,5)}, 'trimf'), eval_mf({highwayMPG.Good, in(i,6)}, 'trapmf')], 'OR_F', opModel, w),...
fuzzyOp(eval_mf({horsepower.Average, in(i,4)}, 'trimf'), 'NOT_F', opModel, w)],...
'AND_F', opModel, w),...
fuzzyOp([...
fuzzyOp([eval_mf({valueLoss.Low, in(i,3)}, 'trapmf'),...
fuzzyOp(eval_mf({risk.High, in(i,2)}, 'trapmf'), 'NOT_F', opModel, w)],...
'AND_F', opModel, w),...
fuzzyOp(eval_mf({price.Average, in(i,7)}, 'trapmf'), 'NOT_F', opModel, w)],...
'AND_F', opModel, w)],...
'AND_F', opModel, w);
end
end
end
% Fuzzy operators
function out = fuzzyOp(in, op, model, w)
% Models
% zadeh, Bounded, and Yager
if strcmp(op, 'AND_F') % Fuzzy intersection or conjuction
if strcmp(model, 'zadeh')
out = min(in, [], 2);
elseif strcmp(model, 'bounded')
out = max(0, sum(in, 2)-1);
elseif strcmp(model, 'yager')
out = 1 - min(1, sum((1-in).^w, 2).^1/w);
end
elseif strcmp(op, 'OR_F') % Fuzzy union
if strcmp(model, 'zadeh') %#ok<*STCMP>
out = max(in,[], 2);
elseif strcmp(model, 'bounded')
out = min(1, sum(in, 2));
elseif strcmp(model, 'yager')
out = min(1, sum(in.^w, 2).^1/w);
end
elseif strcmp(op, 'NOT_F') % Fuzzy complement
if strcmp(model, 'zadeh')
out = 1-in;
elseif strcmp(model, 'bounded')
out = 1-in;
elseif strcmp(model, 'yager')
out = (1-in.^w).^1/w;
end
end
end
% Membership function
%--------------------------------------
% mf is assumed to be of the form:
% {[a b c d], val}
%--------------------------------------
function mv = eval_mf(mf, fn)
% 11 membership functions
% lines: traingular, trapezoidal
% TODO
% Smooth: gaussmf, gauss2mf, gbellmf
% Asymmetric functions: sigmf, dsigmf, psigmf
% Polynomial: zmf, pimf, smf
if strcmp(fn, 'trimf')
% Triangular
if mf{2} > mf{1}(3)
mv = 0;
elseif mf{2} > mf{1}(2)
mv = (mf{1}(3) - mf{2}) / (mf{1}(3) - mf{1}(2));
elseif mf{2} > mf{1}(1)
mv = (mf{2} - mf{1}(1)) / (mf{1}(2) - mf{1}(1));
else
mv = 0;
end
elseif strcmp(fn, 'trapmf')
% Trapezoid
if mf{2} > mf{1}(4)
mv = 0;
elseif mf{2} > mf{1}(3)
mv = (mf{1}(4) - mf{2}) / (mf{1}(4) - mf{1}(3));
elseif mf{2} > mf{1}(2)
mv = 1;
elseif mf{2} > mf{1}(1)
mv = (mf{2} - mf{1}(1)) / (mf{1}(2) - mf{1}(1));
else
mv = 0;
end
end
end