update perhitungan kmeans controller
This commit is contained in:
parent
d6899cac61
commit
7fa65c1bb1
|
@ -16,7 +16,7 @@ class CuranmorController extends Controller
|
||||||
*/
|
*/
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
$curanmors = Curanmor::orderBy('jumlah_curanmor', 'asc')->get();
|
$curanmors = Curanmor::orderBy('jumlah_curanmor', 'desc')->get();
|
||||||
return view('admin.dashboardListCuranmor', compact('curanmors'));
|
return view('admin.dashboardListCuranmor', compact('curanmors'));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,7 +16,7 @@ class CurasController extends Controller
|
||||||
*/
|
*/
|
||||||
public function index()
|
public function index()
|
||||||
{
|
{
|
||||||
$curases = Curas::orderBy('jumlah_curas', 'asc')->get();
|
$curases = Curas::orderBy('jumlah_curas', 'desc')->get();
|
||||||
return view('admin.dashboardListCuras', compact('curases'));
|
return view('admin.dashboardListCuras', compact('curases'));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,88 +10,103 @@
|
||||||
class KmeansController extends Controller
|
class KmeansController extends Controller
|
||||||
{
|
{
|
||||||
public function KMeansCuras()
|
public function KMeansCuras()
|
||||||
{
|
{
|
||||||
$data = Curas::select('id', 'kecamatan_id', 'klaster_id', 'jumlah_curas')->orderBy('jumlah_curas', 'asc')->get();
|
|
||||||
|
|
||||||
$k = Klaster::count('id');
|
$data = Curas::select('id', 'kecamatan_id', 'klaster_id', 'jumlah_curas')->orderBy('jumlah_curas', 'asc')->get();
|
||||||
$maxIterasi = 100;
|
|
||||||
$centroids = $data->random($k)->values()->map(function ($item) {
|
|
||||||
return [
|
|
||||||
'jumlah_curas' => $item->jumlah_curas,
|
|
||||||
];
|
|
||||||
});
|
|
||||||
|
|
||||||
$iterasi = [];
|
$k = Klaster::count('id');
|
||||||
$prevAssignment = [];
|
$maxIterasi = 100;
|
||||||
|
|
||||||
for ($i = 0; $i < $maxIterasi; $i++) {
|
// Simpan centroid awal ke variabel terpisah
|
||||||
$clustered = [];
|
$uniqueJumlahCuras = $data->pluck('jumlah_curas')->unique()->shuffle()->take($k)->values();
|
||||||
$currentAssignment = [];
|
|
||||||
|
|
||||||
foreach ($data as $item) {
|
$initialCentroids = $uniqueJumlahCuras->map(function ($jumlah) {
|
||||||
$jarak = [];
|
return ['jumlah_curas' => $jumlah];
|
||||||
|
});
|
||||||
foreach ($centroids as $idx => $centroid) {
|
|
||||||
$dist = abs($item->jumlah_curas - $centroid['jumlah_curas']);
|
|
||||||
$jarak["jarakC" . ($idx + 1)] = $dist;
|
|
||||||
}
|
|
||||||
|
|
||||||
$iterasi[$i][] = array_merge(['kecamatan_id' => $item->kecamatan_id], $jarak);
|
|
||||||
|
|
||||||
$minIndex = array_keys($jarak, min($jarak))[0]; // e.g. "jarakC2"
|
|
||||||
$clusterNumber = (int) str_replace("jarakC", "", $minIndex);
|
|
||||||
|
|
||||||
$clustered[$clusterNumber][] = $item;
|
|
||||||
$item->temp_klaster = $clusterNumber;
|
|
||||||
$currentAssignment[$item->id] = $clusterNumber;
|
|
||||||
}
|
|
||||||
|
|
||||||
// ✨ Cek konvergensi: jika assignment sekarang == sebelumnya, break
|
|
||||||
if ($currentAssignment === $prevAssignment) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
$prevAssignment = $currentAssignment;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Update centroid berdasarkan rata-rata
|
|
||||||
foreach ($clustered as $key => $group) {
|
|
||||||
$avg = collect($group)->avg('jumlah_curas');
|
|
||||||
$centroids = $centroids->map(function ($item, $index) use ($key, $avg) {
|
|
||||||
return $index === ($key - 1)
|
|
||||||
? ['jumlah_curas' => $avg]
|
|
||||||
: $item;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Final mapping centroid ke klaster_id (aman/sedang/rawan)
|
|
||||||
$finalCentroids = $centroids->map(function ($item, $index) {
|
|
||||||
return ['index' => $index + 1, 'jumlah_curas' => $item['jumlah_curas']];
|
|
||||||
})->sortBy('jumlah_curas')->values();
|
|
||||||
|
|
||||||
$centroidToKlaster = [];
|
|
||||||
|
|
||||||
foreach ($finalCentroids as $i => $centroid) {
|
|
||||||
// Klaster ID mulai dari 1 (asumsi klaster di DB bernomor 1, 2, 3, ...)
|
|
||||||
$centroidToKlaster[$centroid['index']] = $i + 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// Update ke database
|
// Salin untuk digunakan dalam proses iterasi
|
||||||
|
$centroids = $initialCentroids->map(function ($item) {
|
||||||
|
return $item;
|
||||||
|
});
|
||||||
|
|
||||||
|
$iterasi = [];
|
||||||
|
$prevAssignment = [];
|
||||||
|
|
||||||
|
for ($i = 0; $i < $maxIterasi; $i++) {
|
||||||
|
$clustered = [];
|
||||||
|
$currentAssignment = [];
|
||||||
|
|
||||||
foreach ($data as $item) {
|
foreach ($data as $item) {
|
||||||
Curas::where('id', $item->id)->update([
|
$jarak = [];
|
||||||
'klaster_id' => $centroidToKlaster[$item->temp_klaster],
|
|
||||||
]);
|
foreach ($centroids as $idx => $centroid) {
|
||||||
|
$dist = abs($item->jumlah_curas - $centroid['jumlah_curas']);
|
||||||
|
$jarak["jarakC" . ($idx + 1)] = $dist;
|
||||||
|
}
|
||||||
|
|
||||||
|
$iterasi[$i][] = array_merge(['kecamatan_id' => $item->kecamatan_id], $jarak);
|
||||||
|
|
||||||
|
$minIndex = array_keys($jarak, min($jarak))[0]; // e.g. "jarakC2"
|
||||||
|
$clusterNumber = (int) str_replace("jarakC", "", $minIndex);
|
||||||
|
|
||||||
|
$clustered[$clusterNumber][] = $item;
|
||||||
|
$item->temp_klaster = $clusterNumber;
|
||||||
|
$currentAssignment[$item->id] = $clusterNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
session(['hasil_iterasi' => $iterasi]);
|
if ($currentAssignment === $prevAssignment) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
return response()->json($iterasi);
|
$prevAssignment = $currentAssignment;
|
||||||
|
|
||||||
|
// Update centroid berdasarkan rata-rata
|
||||||
|
foreach ($clustered as $key => $group) {
|
||||||
|
$avg = collect($group)->avg('jumlah_curas');
|
||||||
|
$centroids = $centroids->map(function ($item, $index) use ($key, $avg) {
|
||||||
|
return $index === ($key - 1)
|
||||||
|
? ['jumlah_curas' => $avg]
|
||||||
|
: $item;
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Final mapping centroid ke klaster_id
|
||||||
|
$finalCentroids = $centroids->map(function ($item, $index) {
|
||||||
|
return ['index' => $index + 1, 'jumlah_curas' => $item['jumlah_curas']];
|
||||||
|
})->sortBy('jumlah_curas')->values();
|
||||||
|
|
||||||
|
$centroidToKlaster = [];
|
||||||
|
foreach ($finalCentroids as $i => $centroid) {
|
||||||
|
$centroidToKlaster[$centroid['index']] = $i + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($data as $item) {
|
||||||
|
Curas::where('id', $item->id)->update([
|
||||||
|
'klaster_id' => $centroidToKlaster[$item->temp_klaster],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Simpan hasil iterasi dan centroid awal ke session
|
||||||
|
session([
|
||||||
|
'hasil_iterasi' => $iterasi,
|
||||||
|
'centroid_awal' => $initialCentroids
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Format ulang centroid awal dengan label 'Centroid 1', dst.
|
||||||
|
$formattedInitialCentroids = [];
|
||||||
|
foreach ($initialCentroids as $i => $centroid) {
|
||||||
|
$formattedInitialCentroids['Centroid ' . ($i + 1)] = $centroid['jumlah_curas'];
|
||||||
|
}
|
||||||
|
|
||||||
|
return response()->json([
|
||||||
|
'centroid_awal' => $formattedInitialCentroids,
|
||||||
|
'hasil_iterasi' => $iterasi,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public function KMeansCuranmor()
|
public function KMeansCuranmor()
|
||||||
{
|
{
|
||||||
$data = Curanmor::select('id', 'kecamatan_id', 'klaster_id', 'jumlah_curanmor')->orderBy('jumlah_curanmor', 'asc')->get();
|
$data = Curanmor::select('id', 'kecamatan_id', 'klaster_id', 'jumlah_curanmor')->orderBy('jumlah_curanmor', 'asc')->get();
|
||||||
|
|
|
@ -8,10 +8,10 @@
|
||||||
content="width=device-width, initial-scale=1, shrink-to-fit=no"
|
content="width=device-width, initial-scale=1, shrink-to-fit=no"
|
||||||
/>
|
/>
|
||||||
<title>
|
<title>
|
||||||
Markethon - Digital Marketing Agency Responsive HTML5 Template
|
KPROTECT | Probolinggo Threat & Crime Tracker
|
||||||
</title>
|
</title>
|
||||||
<!-- Favicon -->
|
<!-- Favicon -->
|
||||||
<link rel="shortcut icon" href="{{ asset('assets/assetLanding/images/favicon.ico') }}" />
|
<link rel="shortcut icon" href="{{ asset('assets/assetLanding/images/favicon2.png') }}" />
|
||||||
<!-- Bootstrap CSS -->
|
<!-- Bootstrap CSS -->
|
||||||
<link rel="stylesheet" href="{{ asset('assets/assetLanding/css/bootstrap.min.css') }}" />
|
<link rel="stylesheet" href="{{ asset('assets/assetLanding/css/bootstrap.min.css') }}" />
|
||||||
<!-- Typography CSS -->
|
<!-- Typography CSS -->
|
||||||
|
|
Loading…
Reference in New Issue