-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFuzzyLogic.lua
388 lines (352 loc) · 13.2 KB
/
FuzzyLogic.lua
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
-- by Ahmad Arsyel (1301164193)
local QUEST_PATH = "DataTugas2.csv"
--local ANSWER_PATH = "/home/reydvires/Development/Lua Project/FuzzyLogic/TebakanTugas2.csv"
local ANSWER_PATH = "D:/TelkomUniversity/AI/Tugas_FuzzyLogic/fuzzy-logic/TebakanTugas2.csv"
-- @param table Print traverse of table
local function print_famtable(table)
for _, v in ipairs(table) do -- show parsing from CSV file
print(v.family_id,v.income,v.charge)
end
end
-- @param path Use your path for .csv file
local function parse_CSV(path)
local family_list = {}
-- output will saved in family_list table
for line in io.lines(path) do
local col1, col2, col3 = line:match("%i*(.-),%s*(.-),%s*(.*)") -- converting
family_list[#family_list + 1] = {
family_id = col1,
income = tonumber(col2),
charge = tonumber(col3)
}
end
table.remove(family_list, 1) -- remove the title/header
--[[print("Parsing result:")
print_famtable(family_list)]]
return family_list
end
-- @param path Use your own path for .csv raw file targeted
-- @param data_table Saving file to .csv from table
-- @param sep Separator of file
local function table_to_CSV(path, data_table, sep)
--relative path:/home/reydvires/Development/Lua Project/FuzzyLogic/TebakanTugas2.csv
--relative path:D:/TelkomUniversity/AI/Tugas_FuzzyLogic/fuzzy-logic/TebakanTugas2.csv
sep = sep or ','
local file = assert(io.open(path, "w")) -- w mean write
for _, v in ipairs(data_table) do
file:write(v)
file:write('\n')
end
file:close()
print("file saved")
end
----------
local function insertion_sort(tab)
for i=2, #tab do
local x = tab[i]
local j = i-1
while (j > 0) and (tab[j].score < x.score) do
tab[j+1] = tab[j]
j = j - 1
end
tab[j+1] = x
end
end
local function minim(a, b)
if a > b then
return b
else
return a
end
end
local function member_on_up(x, on_low, to_top)
return (x-on_low)/(to_top-on_low)
end
local function member_on_down(x, on_top, to_low)
return (to_low-x)/(to_low-on_top)
end
-- @param x Calculate number from crisp value of income
local function linguistic_var_income(x, fp) -- membership of income
local member = {0.4, 0.8, 1.2, 1.6} -- lakukan analisis
local curr_linguistic
if x <= member[1] then
curr_linguistic = {
f_state = "poor", f_score = 1,
s_state = nil, s_score = nil
}
elseif x > member[1] and x < member[2] then
curr_linguistic = {
f_state = "poor", f_score = member_on_down(x, member[1], member[2]),
s_state = "medium", s_score = member_on_up(x, member[1], member[2])
}
elseif x >= member[2] and x <= member[3] then
curr_linguistic = {
f_state = "medium", f_score = 1,
s_state = nil, s_score = nil
}
elseif x > member[3] and x < member[4] then
curr_linguistic = {
f_state = "medium", f_score = member_on_down(x, member[3], member[4]),
s_state = "rich", s_score = member_on_up(x, member[3], member[4])
}
else -- if x >= 1.75 and x <= 2 then
curr_linguistic = {
f_state = "rich", f_score = 1,
s_state = nil, s_score = nil
}
end
table.insert(fp.income, curr_linguistic)
end
-- @param x Calculate number from crisp value of chare
local function linguistic_var_charge(y, fp) -- membership of charge
local member = {20, 30, 50, 60, 70, 80, 85, 95} -- lakukan analisis
local curr_linguistic
if y <= member[1] then
curr_linguistic = {
f_state = "very low", f_score = 1,
s_state = nil, s_score = nil
}
elseif y > member[1] and y < member[2] then
curr_linguistic = {
f_state = "very low", f_score = member_on_down(y, member[1], member[2]),
s_state = "low", s_score = member_on_up(y, member[1], member[2])
}
elseif y >= member[2] and y <= member[3] then
curr_linguistic = {
f_state = "low", f_score = 1,
s_state = nil, s_score = nil
}
elseif y > member[3] and y < member[4] then
curr_linguistic = {
f_state = "low", f_score = member_on_down(y, member[3], member[4]),
s_state = "average", s_score = member_on_up(y, member[3], member[4])
}
elseif y >= member[4] and y <= member[5] then
curr_linguistic = {
f_state = "average", f_score = 1,
s_state = nil, s_score = nil
}
elseif y > member[5] and y < member[6] then
curr_linguistic = {
f_state = "average", f_score = member_on_down(y, member[5], member[6]),
s_state = "high", s_score = member_on_up(y, member[5], member[6])
}
elseif y >= member[6] and y <= member[7] then
curr_linguistic = {
f_state = "high", f_score = 1,
s_state = nil, s_score = nil
}
elseif y > member[7] and y < member[8] then
curr_linguistic = {
f_state = "high", f_score = member_on_down(y, member[7], member[8]),
s_state = "very high", s_score = member_on_up(y, member[7], member[8])
}
else
curr_linguistic = {
f_state = "very high", f_score = 1,
s_state = nil, s_score = nil
}
end
table.insert(fp.charge, curr_linguistic)
end
local function rules_analyzing(income_state, income_score, charge_state, charge_score) -- lakukan analisis
local curr_linscore = nil
if (income_state == "poor" and charge_state == "very low") then
curr_linscore = {"consider", minim(income_score, charge_score)}
elseif (income_state == "poor" and charge_state == "low") then
curr_linscore = {"accepted", minim(income_score, charge_score)}
elseif (income_state == "poor" and charge_state == "average") then
curr_linscore = {"accepted", minim(income_score, charge_score)}
elseif (income_state == "poor" and charge_state == "high") then
curr_linscore = {"accepted", minim(income_score, charge_score)}
elseif (income_state == "poor" and charge_state == "very high") then
curr_linscore = {"accepted", minim(income_score, charge_score)}
elseif (income_state == "medium" and charge_state == "very low") then
curr_linscore = {"rejected", minim(income_score, charge_score)}
elseif (income_state == "medium" and charge_state == "low") then
curr_linscore = {"consider", minim(income_score, charge_score)}
elseif (income_state == "medium" and charge_state == "average") then
curr_linscore = {"accepted", minim(income_score, charge_score)}
elseif (income_state == "medium" and charge_state == "high") then
curr_linscore = {"accepted", minim(income_score, charge_score)}
elseif (income_state == "medium" and charge_state == "very high") then
curr_linscore = {"accepted", minim(income_score, charge_score)}
elseif (income_state == "rich" and charge_state == "very low") then
curr_linscore = {"rejected", minim(income_score, charge_score)}
elseif (income_state == "rich" and charge_state == "low") then
curr_linscore = {"rejected", minim(income_score, charge_score)}
elseif (income_state == "rich" and charge_state == "average") then
curr_linscore = {"rejected", minim(income_score, charge_score)}
elseif (income_state == "rich" and charge_state == "high") then
curr_linscore = {"consider", minim(income_score, charge_score)}
elseif (income_state == "rich" and charge_state == "very high") then
curr_linscore = {"accepted", minim(income_score, charge_score)}
end
return curr_linscore
end
local function eliminating(label_j, label_k, score_j, score_k, tab)
if (label_j == label_k) and (score_j > score_k) then
if tab == nil then
tab = score_j
elseif score_j > tab then
tab = score_j
end
elseif (label_j == label_k) then
if tab == nil then
tab = score_k
elseif score_k > tab then
tab = score_k
end
end
return tab
end
local function find_val(value, an_array, in_number)
for i=1, in_number do
if an_array[i][1] == value then
return an_array[i][2]
else
return nil
end
end
end
local function convert_nil_to_zero(data, tab)
if data == nil then
data = find_val(str, tab, 4)
if data == nil then
data = 0
end
end
return data
end
local function inference(tab_income, tab_charge)
-- output will saved in family_result table
local inference_tab = {}
local result = {}
local fin_result = {
accepted = nil,
consider = nil,
rejected = nil
}
local tab_result = {}
for i=1, #tab_income do
result[1] = rules_analyzing(tab_income[i].f_state, tab_income[i].f_score, tab_charge[i].f_state, tab_charge[i].f_score)
if (tab_income[i].s_state == nil and tab_charge[i].s_state ~= nil) then
result[2] = rules_analyzing(tab_income[i].f_state, tab_income[i].f_score, tab_charge[i].s_state, tab_charge[i].s_score)
elseif (tab_income[i].s_state ~= nil and tab_charge[i].s_state ~= nil) then
result[2] = rules_analyzing(tab_income[i].f_state, tab_income[i].f_score, tab_charge[i].s_state, tab_charge[i].s_score)
else
result[2] = result[1]
end
if (tab_income[i].s_state ~= nil and tab_charge[i].s_state == nil) then
result[3] = rules_analyzing(tab_income[i].s_state, tab_income[i].s_score, tab_charge[i].f_state, tab_charge[i].f_score)
elseif (tab_income[i].s_state ~= nil and tab_charge[i].s_state ~= nil) then
result[3] = rules_analyzing(tab_income[i].s_state, tab_income[i].s_score, tab_charge[i].f_state, tab_charge[i].f_score)
else
result[3] = result[1]
end
if (tab_income[i].s_state ~= nil and tab_charge[i].s_state ~= nil) then
result[4] = rules_analyzing(tab_income[i].s_state, tab_income[i].s_score, tab_charge[i].s_state, tab_charge[i].s_score)
else
result[4] = result[1]
end
--[[print(i .. ")\nStates1", tab_income[i].f_state, tab_income[i].f_score, tab_charge[i].f_state, tab_charge[i].f_score)
print("States2", tab_income[i].s_state, tab_income[i].s_score, tab_charge[i].s_state, tab_charge[i].s_score)
print(("Result --> %s: %s"):format(result[1][1], result[1][2]))
print(("Result --> %s: %s"):format(result[2][1], result[2][2]))
print(("Result --> %s: %s"):format(result[3][1], result[3][2]))
print(("Result --> %s: %s\n"):format(result[4][1], result[4][2]))]]
table.insert(inference_tab, {
label = {result[1][1], result[2][1], result[3][1], result[4][1]},
value = {result[1][2], result[2][2], result[3][2], result[4][2]}
})
end
-- eliminate duplicate with the highest score, cross product/combination
for i, v in ipairs(inference_tab) do -- traversing 100 data
for j=1, 4 do
for k=1, 4 do
if (v.label[j] == "accepted") then
fin_result.accepted = eliminating(
v.label[j], v.label[k], v.value[j], v.value[k], fin_result[v.label[j]]
)
elseif (v.label[j] == "consider") then
fin_result.consider = eliminating(
v.label[j], v.label[k], v.value[j], v.value[k], fin_result[v.label[j]]
)
elseif (v.label[j] == "rejected") then
fin_result.rejected = eliminating(
v.label[j], v.label[k], v.value[j], v.value[k], fin_result[v.label[j]]
)
end
end
end
-- converting nil value into 0 (zero)
fin_result.accepted = convert_nil_to_zero(fin_result.accepted, inference_tab)
fin_result.consider = convert_nil_to_zero(fin_result.consider, inference_tab)
fin_result.rejected = convert_nil_to_zero(fin_result.rejected, inference_tab)
--[[print((i ..") accepted: %s, consider: %s, rejected: %s"):
format(fin_result.accepted, fin_result.consider, fin_result.rejected))]]
table.insert(tab_result, {fin_result.accepted, fin_result.consider, fin_result.rejected})
-- reset state
fin_result.accepted = nil
fin_result.consider = nil
fin_result.rejected = nil
end
return tab_result
end
local function sugeno_func(acc, con, rej)
return (acc*100 + con*65 + rej*10)/(acc+con+rej) -- lakukan analisis
end
local function label_defuzzification(score)
if score == 100 then
return "ACCEPTED"
elseif score > 10 and score < 100 then
return "CONSIDER"
else
return "REJECTED"
end
end
local function defuzzification(tab)
local goals = {}
for i, v in ipairs(tab) do
table.insert(goals, {
id = i,
score = sugeno_func(tab[i][1], tab[i][2], tab[i][3])
})
print(i, goals[i].score)
goals[i].label = label_defuzzification(goals[i].score)
end
return goals
end
local function membership(list, fp)
-- output will saved in family_process table
for _, v in pairs(list) do
linguistic_var_income(v.income, fp)
linguistic_var_charge(v.charge, fp)
end
end
local function fuzzy_logic(fl)
local tab_goal = {}
local family_process = {
income = {},
charge = {}
}
local family_result = {}
local family_selected = {}
membership(fl, family_process) -- fuzzification
family_result = inference(family_process.income, family_process.charge) -- inference
tab_goal = defuzzification(family_result) -- defuzzification
insertion_sort(tab_goal) -- sorting from the highest score
for i, v in ipairs(tab_goal) do
print(i .. ") " .. v.id, v.score, v.label)
end
local i=1
while (#family_selected < 20) do -- picking 20 first family of fittest
table.insert(family_selected, tab_goal[i].id) -- saving data to family_selected
i = i + 1
end
return family_selected
end
-- main program
local families = parse_CSV(QUEST_PATH)
local helped_family = fuzzy_logic(families)
table_to_CSV(ANSWER_PATH, helped_family)