Skip to content

Instantly share code, notes, and snippets.

@aldiskatel
Created June 22, 2022 18:50
Show Gist options
  • Save aldiskatel/bda07e2e69fc91b3926f220784f2bbf9 to your computer and use it in GitHub Desktop.
Save aldiskatel/bda07e2e69fc91b3926f220784f2bbf9 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Controllers;
use App\Models\Hasil;
use App\Models\Kriteria;
use App\Models\Penilaian;
use App\Models\Periode;
class TopsisController extends Controller
{
public function __construct()
{
$this->middleware('auth');
}
public function index()
{
$periode = Periode::all();
return view('topsis.index', compact('periode'));
}
// perhitungan metode TOPSIS Laravel
public function detail(Periode $periode)
{
$kriteria = Kriteria::all();
$alternatif = Penilaian::where('periode_id', $periode->periode_id)->groupBy('pegawai_id')->get();
$bobot = [];
foreach ($alternatif as $a) {
foreach ($kriteria as $k) {
$result = Penilaian::where('periode_id', $periode->periode_id)
->where('pegawai_id', $a->pegawai_id)
->where('kriteria_id', $k->kriteria_id)
->get();
$total = empty($result) ? 0 : $result->sum('nilai');
$bobot[$a->pegawai_id][$k->kriteria_id] = $total;
}
}
$pembagi = [];
foreach ($kriteria as $k) {
$total = 0;
foreach ($alternatif as $a) {
$total += pow($bobot[$a->pegawai_id][$k->kriteria_id], 2);
}
$pembagi[$k->kriteria_id] = round(sqrt($total), 5);
}
$matrik_r = [];
$matrik_y = [];
foreach ($alternatif as $a) {
foreach ($kriteria as $k) {
$hasil = $bobot[$a->pegawai_id][$k->kriteria_id] / $pembagi[$k->kriteria_id];
$matrik_r[$a->pegawai_id][$k->kriteria_id] = round($hasil, 5);
$matrik_y[$a->pegawai_id][$k->kriteria_id] = $matrik_r[$a->pegawai_id][$k->kriteria_id] * $k->bobot_kriteria;
}
}
$positif = [];
$negatif = [];
foreach ($kriteria as $k) {
$positif[$k->kriteria_id] = max(array_column($matrik_y, $k->kriteria_id));
$negatif[$k->kriteria_id] = min(array_column($matrik_y, $k->kriteria_id));
}
$jarak_positif = [];
$jarak_negatif = [];
$nilai_v = [];
$hasil = [];
foreach ($alternatif as $a) {
$total_p = 0;
$total_n = 0;
foreach ($kriteria as $k) {
$total_p += pow($positif[$k->kriteria_id] - $matrik_y[$a->pegawai_id][$k->kriteria_id], 2);
$total_n += pow($matrik_y[$a->pegawai_id][$k->kriteria_id] - $negatif[$k->kriteria_id], 2);
}
$jarak_positif[$a->pegawai_id] = round(sqrt($total_p), 5);
$jarak_negatif[$a->pegawai_id] = round(sqrt($total_n), 5);
$nilai_v[$a->pegawai_id] = round($jarak_negatif[$a->pegawai_id] / ($jarak_negatif[$a->pegawai_id] + $jarak_positif[$a->pegawai_id]), 5);
$hasil[] = [
'pegawai_id' => $a->pegawai_id,
'nilai_topsis' => $nilai_v[$a->pegawai_id],
];
}
$col = array_column($hasil, 'nilai_topsis');
array_multisort($col, SORT_DESC, $hasil);
$peringkat = 1;
foreach ($hasil as $val) {
Hasil::updateOrCreate([
'periode_id' => $periode->periode_id,
'pegawai_id' => $val['pegawai_id'],
], [
'nilai_topsis' => $val['nilai_topsis'],
'peringkat' => $peringkat++,
]);
}
$hasil = Hasil::where('periode_id', $periode->periode_id)->orderBy('nilai_topsis', 'desc')->get();
return view('topsis.detail', compact(
'kriteria',
'alternatif',
'bobot',
'pembagi',
'matrik_r',
'matrik_y',
'positif',
'negatif',
'jarak_positif',
'jarak_negatif',
'nilai_v',
'hasil'
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment