58 lines
1.6 KiB
PHP
58 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Scheduling;
|
|
|
|
/**
|
|
* Chromosome merepresentasikan 1 individu solusi jadwal mingguan lengkap.
|
|
* Terdiri dari sekumpulan Gen (Gene[]) untuk seluruh kelas aktif.
|
|
*/
|
|
class Chromosome
|
|
{
|
|
/** @var array<Gene> */
|
|
public array $genes = [];
|
|
public float $fitness = 0.0;
|
|
public int $totalPenalty = 0;
|
|
|
|
/** @var array<string, int> Rincian penalti per batasan (B1..B7) */
|
|
public array $penaltyBreakdown = [
|
|
'B1' => 0, // Bentrok Guru
|
|
'B2' => 0, // Bentrok Ruangan
|
|
'B3' => 0, // Ketersediaan Guru
|
|
'B4' => 0, // Bentrok Kelas
|
|
'B5' => 0, // Kualifikasi Level
|
|
'B6' => 0, // Sebaran Hari
|
|
'B7' => 0, // Konsistensi Guru
|
|
];
|
|
|
|
/**
|
|
* @param array<Gene> $genes
|
|
*/
|
|
public function __construct(array $genes = [])
|
|
{
|
|
$this->genes = $genes;
|
|
}
|
|
|
|
/**
|
|
* Set hasil evaluasi fitness
|
|
*/
|
|
public function setEvaluationResult(int $totalPenalty, array $breakdown): void
|
|
{
|
|
$this->totalPenalty = $totalPenalty;
|
|
$this->penaltyBreakdown = $breakdown;
|
|
$this->fitness = 1.0 / (1.0 + $totalPenalty);
|
|
}
|
|
|
|
/**
|
|
* Kloning kromosom beserta seluruh gen di dalamnya
|
|
*/
|
|
public function clone(): self
|
|
{
|
|
$clonedGenes = array_map(fn(Gene $g) => $g->clone(), $this->genes);
|
|
$chromosome = new self($clonedGenes);
|
|
$chromosome->fitness = $this->fitness;
|
|
$chromosome->totalPenalty = $this->totalPenalty;
|
|
$chromosome->penaltyBreakdown = $this->penaltyBreakdown;
|
|
return $chromosome;
|
|
}
|
|
}
|