This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathranking-topsis.php
471 lines (409 loc) · 12.9 KB
/
ranking-topsis.php
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
<?php
/* ---------------------------------------------
* SPK FUZZY TOPSIS
* Author: Wawan Edan
* ------------------------------------------- */
/* ---------------------------------------------
* Konek ke database & load fungsi-fungsi
* ------------------------------------------- */
require_once('includes/init.php');
/* ---------------------------------------------
* Load Header
* ------------------------------------------- */
$judul_page = 'Perankingan Menggunakan Metode Fuzzy TOPSIS';
require_once('template-parts/header.php');
/* ---------------------------------------------
* Set jumlah digit di belakang koma
* ------------------------------------------- */
$digit = 4;
/* ---------------------------------------------
* Fetch semua kriteria
* ------------------------------------------- */
$query = $pdo->prepare('SELECT id_kriteria, nama, type, bobot
FROM kriteria ORDER BY urutan_order ASC');
$query->execute();
$query->setFetchMode(PDO::FETCH_ASSOC);
$kriterias = $query->fetchAll();
/* ---------------------------------------------
* Fetch semua kenshin (alternatif)
* ------------------------------------------- */
$query2 = $pdo->prepare('SELECT id_kenshin, nama_kenshin FROM kenshin');
$query2->execute();
$query2->setFetchMode(PDO::FETCH_ASSOC);
$kenshins = $query2->fetchAll();
/* >>> STEP 1 ===================================
* Matrix Keputusan (X)
* ------------------------------------------- */
$matriks_x = array();
foreach($kriterias as $kriteria):
foreach($kenshins as $kenshin):
$id_kenshin = $kenshin['id_kenshin'];
$id_kriteria = $kriteria['id_kriteria'];
// Fetch nilai dari db
$query3 = $pdo->prepare('SELECT nilai FROM nilai_kenshin
WHERE id_kenshin = :id_kenshin AND id_kriteria = :id_kriteria');
$query3->execute(array(
'id_kenshin' => $id_kenshin,
'id_kriteria' => $id_kriteria,
));
$query3->setFetchMode(PDO::FETCH_ASSOC);
if($nilai_kenshin = $query3->fetch()) {
// Jika ada nilai kriterianya
$matriks_x[$id_kriteria][$id_kenshin] = $nilai_kenshin['nilai'];
} else {
$matriks_x[$id_kriteria][$id_kenshin] = 0;
}
endforeach;
endforeach;
/* >>> STEP 3 ===================================
* Matriks Ternormalisasi (R)
* ------------------------------------------- */
$matriks_r = array();
foreach($matriks_x as $id_kriteria => $nilai_kenshins):
// Mencari akar dari penjumlahan kuadrat
$jumlah_kuadrat = 0;
foreach($nilai_kenshins as $nilai_kenshin):
$jumlah_kuadrat += pow($nilai_kenshin, 2);
endforeach;
$akar_kuadrat = sqrt($jumlah_kuadrat);
// Mencari hasil bagi akar kuadrat
// Lalu dimasukkan ke array $matriks_r
foreach($nilai_kenshins as $id_kenshin => $nilai_kenshin):
$matriks_r[$id_kriteria][$id_kenshin] = $nilai_kenshin / $akar_kuadrat;
endforeach;
endforeach;
/* >>> STEP 4 ===================================
* Matriks Y
* ------------------------------------------- */
$matriks_y = array();
foreach($kriterias as $kriteria):
foreach($kenshins as $kenshin):
$bobot = $kriteria['bobot'];
$id_kenshin = $kenshin['id_kenshin'];
$id_kriteria = $kriteria['id_kriteria'];
$nilai_r = $matriks_r[$id_kriteria][$id_kenshin];
$matriks_y[$id_kriteria][$id_kenshin] = $bobot * $nilai_r;
endforeach;
endforeach;
/* >>> STEP 5 ================================
* Solusi Ideal Positif & Negarif
* ------------------------------------------- */
$solusi_ideal_positif = array();
$solusi_ideal_negatif = array();
foreach($kriterias as $kriteria):
$id_kriteria = $kriteria['id_kriteria'];
$type_kriteria = $kriteria['type'];
$nilai_max = max($matriks_y[$id_kriteria]);
$nilai_min = min($matriks_y[$id_kriteria]);
if($type_kriteria == 'c1'):
$s_i_p = $nilai_max;
$s_i_n = $nilai_min;
elseif($type_kriteria == 'c2'):
$s_i_p = $nilai_min;
$s_i_n = $nilai_max;
elseif($type_kriteria == 'c3'):
$s_i_p = $nilai_min;
$s_i_n = $nilai_max;
elseif($type_kriteria == 'c4'):
$s_i_p = $nilai_min;
$s_i_n = $nilai_max;
elseif($type_kriteria == 'c5'):
$s_i_p = $nilai_min;
$s_i_n = $nilai_max;
endif;
$solusi_ideal_positif[$id_kriteria] = $s_i_p;
$solusi_ideal_negatif[$id_kriteria] = $s_i_n;
endforeach;
/* >>> STEP 6 ================================
* Jarak Ideal Positif & Negatif
* ------------------------------------------- */
$jarak_ideal_positif = array();
$jarak_ideal_negatif = array();
foreach($kenshins as $kenshin):
$id_kenshin = $kenshin['id_kenshin'];
$jumlah_kuadrat_jip = 0;
$jumlah_kuadrat_jin = 0;
// Mencari penjumlahan kuadrat
foreach($matriks_y as $id_kriteria => $nilai_kenshins):
$hsl_pengurangan_jip = $nilai_kenshins[$id_kenshin] - $solusi_ideal_positif[$id_kriteria];
$hsl_pengurangan_jin = $nilai_kenshins[$id_kenshin] - $solusi_ideal_negatif[$id_kriteria];
$jumlah_kuadrat_jip += pow($hsl_pengurangan_jip, 2);
$jumlah_kuadrat_jin += pow($hsl_pengurangan_jin, 2);
endforeach;
// Mengakarkan hasil penjumlahan kuadrat
$akar_kuadrat_jip = sqrt($jumlah_kuadrat_jip);
$akar_kuadrat_jin = sqrt($jumlah_kuadrat_jin);
// Memasukkan ke array matriks jip & jin
$jarak_ideal_positif[$id_kenshin] = $akar_kuadrat_jip;
$jarak_ideal_negatif[$id_kenshin] = $akar_kuadrat_jin;
endforeach;
/* >>> STEP 7 ================================
* Perangkingan
* ------------------------------------------- */
$ranks = array();
foreach($kenshins as $kenshin):
$s_negatif = $jarak_ideal_negatif[$kenshin['id_kenshin']];
$s_positif = $jarak_ideal_positif[$kenshin['id_kenshin']];
$nilai_v = $s_negatif / ($s_positif + $s_negatif);
$ranks[$kenshin['id_kenshin']]['id_kenshin'] = $kenshin['id_kenshin'];
$ranks[$kenshin['id_kenshin']]['nama_kenshin'] = $kenshin['nama_kenshin'];
$ranks[$kenshin['id_kenshin']]['nilai'] = $nilai_v;
endforeach;
?>
<div class="main-content-row">
<div class="container clearfix">
<div class="main-content main-content-full the-content">
<h1><?php echo $judul_page; ?></h1>
<!-- STEP 1. Matriks Keputusan(X) ==================== -->
<h3>Step 1: Matriks Keputusan (X)</h3>
<table class="pure-table pure-table-striped">
<thead>
<tr class="super-top">
<th rowspan="2" class="super-top-left">Nama Kenshin</th>
<th colspan="<?php echo count($kriterias); ?>">Kriteria</th>
</tr>
<tr>
<?php foreach($kriterias as $kriteria ): ?>
<th><?php echo $kriteria['nama']; ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php foreach($kenshins as $kenshin): ?>
<tr>
<td><?php echo $kenshin['nama_kenshin']; ?></td>
<?php
foreach($kriterias as $kriteria):
$id_kenshin = $kenshin['id_kenshin'];
$id_kriteria = $kriteria['id_kriteria'];
echo '<td>';
echo $matriks_x[$id_kriteria][$id_kenshin];
echo '</td>';
endforeach;
?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<!-- STEP 2. Bobot Preferensi (W) ==================== -->
<h3>Step 2: Bobot Preferensi (W)</h3>
<table class="pure-table pure-table-striped">
<thead>
<tr>
<th>Nama Kriteria</th>
<th>Type</th>
<th>Bobot (W)</th>
</tr>
</thead>
<tbody>
<?php foreach($kriterias as $hasil): ?>
<tr>
<td><?php echo $hasil['nama']; ?></td>
<td>
<?php
if($hasil['type'] == 'c1') {
echo 'C-1';
} elseif($hasil['type'] == 'c2') {
echo 'C-2';
} elseif($hasil['type'] == 'c3') {
echo 'C-3';
} elseif($hasil['type'] == 'c4') {
echo 'C-4';
} elseif($hasil['type'] == 'c5') {
echo 'C-5';
}
?>
</td>
<td><?php echo $hasil['bobot']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<!-- Step 3: Matriks Ternormalisasi (R) ==================== -->
<h3>Step 3: Matriks Ternormalisasi (R)</h3>
<table class="pure-table pure-table-striped">
<thead>
<tr class="super-top">
<th rowspan="2" class="super-top-left">Nama Kenshin</th>
<th colspan="<?php echo count($kriterias); ?>">Kriteria</th>
</tr>
<tr>
<?php foreach($kriterias as $kriteria ): ?>
<th><?php echo $kriteria['nama']; ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php foreach($kenshins as $kenshin): ?>
<tr>
<td><?php echo $kenshin['nama_kenshin']; ?></td>
<?php
foreach($kriterias as $kriteria):
$id_kenshin = $kenshin['id_kenshin'];
$id_kriteria = $kriteria['id_kriteria'];
echo '<td>';
echo round($matriks_r[$id_kriteria][$id_kenshin], $digit);
echo '</td>';
endforeach;
?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<!-- Step 4: Matriks Y ==================== -->
<h3>Step 4: Matriks Y</h3>
<table class="pure-table pure-table-striped">
<thead>
<tr class="super-top">
<th rowspan="2" class="super-top-left">Nama Kenshin</th>
<th colspan="<?php echo count($kriterias); ?>">Kriteria</th>
</tr>
<tr>
<?php foreach($kriterias as $kriteria ): ?>
<th><?php echo $kriteria['nama']; ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<?php foreach($kenshins as $kenshin): ?>
<tr>
<td><?php echo $kenshin['nama_kenshin']; ?></td>
<?php
foreach($kriterias as $kriteria):
$id_kenshin = $kenshin['id_kenshin'];
$id_kriteria = $kriteria['id_kriteria'];
echo '<td>';
echo round($matriks_y[$id_kriteria][$id_kenshin], $digit);
echo '</td>';
endforeach;
?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<!-- Step 5.1: Solusi Ideal Positif ==================== -->
<h3>Step 5.1: Solusi Ideal Positif (A<sup>+</sup>)</h3>
<table class="pure-table pure-table-striped">
<thead>
<tr>
<?php foreach($kriterias as $kriteria ): ?>
<th><?php echo $kriteria['nama']; ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<tr>
<?php foreach($kriterias as $kriteria ): ?>
<td>
<?php
$id_kriteria = $kriteria['id_kriteria'];
echo round($solusi_ideal_positif[$id_kriteria], $digit);
?>
</td>
<?php endforeach; ?>
</tr>
</tbody>
</table>
<!-- Step 5.2: Solusi Ideal negative ==================== -->
<h3>Step 5.2: Solusi Ideal Negatif (A<sup>-</sup>)</h3>
<table class="pure-table pure-table-striped">
<thead>
<tr>
<?php foreach($kriterias as $kriteria ): ?>
<th><?php echo $kriteria['nama']; ?></th>
<?php endforeach; ?>
</tr>
</thead>
<tbody>
<tr>
<?php foreach($kriterias as $kriteria ): ?>
<td>
<?php
$id_kriteria = $kriteria['id_kriteria'];
echo round($solusi_ideal_negatif[$id_kriteria], $digit);
?>
</td>
<?php endforeach; ?>
</tr>
</tbody>
</table>
<!-- Step 6.1: Jarak Ideal Positif ==================== -->
<h3>Step 6.1: Jarak Ideal Positif (S<sub>i</sub>+)</h3>
<table class="pure-table pure-table-striped">
<thead>
<tr>
<th class="super-top-left">Nama Kenshin</th>
<th>Jarak Ideal Positif</th>
</tr>
</thead>
<tbody>
<?php foreach($kenshins as $kenshin ): ?>
<tr>
<td><?php echo $kenshin['nama_kenshin']; ?></td>
<td>
<?php
$id_kenshin = $kenshin['id_kenshin'];
echo round($jarak_ideal_positif[$id_kenshin], $digit);
?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<!-- Step 6.2: Jarak Ideal Negatif ==================== -->
<h3>Step 6.2: Jarak Ideal Negatif (S<sub>i</sub>-)</h3>
<table class="pure-table pure-table-striped">
<thead>
<tr>
<th class="super-top-left">Nama Kenshin</th>
<th>Jarak Ideal Negatif</th>
</tr>
</thead>
<tbody>
<?php foreach($kenshins as $kenshin ): ?>
<tr>
<td><?php echo $kenshin['nama_kenshin']; ?></td>
<td>
<?php
$id_kenshin = $kenshin['id_kenshin'];
echo round($jarak_ideal_negatif[$id_kenshin], $digit);
?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<!-- Step 7: Perangkingan ==================== -->
<?php
$sorted_ranks = $ranks;
// Sorting
if(function_exists('array_multisort')):
foreach ($sorted_ranks as $key => $row) {
$nama_kenshin[$key] = $row['nama_kenshin'];
$nilai[$key] = $row['nilai'];
}
array_multisort($nilai, SORT_DESC, $nama_kenshin, SORT_ASC, $sorted_ranks);
endif;
?>
<h3>Step 7: Perangkingan (V)</h3>
<table class="pure-table pure-table-striped">
<thead>
<tr>
<th class="super-top-left">Nama Kenshin</th>
<th>Ranking</th>
</tr>
</thead>
<tbody>
<?php foreach($sorted_ranks as $kenshin ): ?>
<tr>
<td><?php echo $kenshin['nama_kenshin']; ?></td>
<td><?php echo round($kenshin['nilai'], $digit); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div><!-- .container -->
</div><!-- .main-content-row -->
<?php
require_once('template-parts/footer.php');