-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPopulation.php
More file actions
53 lines (47 loc) · 1.41 KB
/
Population.php
File metadata and controls
53 lines (47 loc) · 1.41 KB
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
<?php
/*
* Populatia cu tururi candidate
*/
class Population
{
// Populatie de tururi
public $tours = [];
// Constructor populatie
public function __construct(int $populationSize, $initialise) {
for ($i = 0; $i < $populationSize; $i++) {
$this->tours[] = new Tour;
}
// Verificam daca avem nevoie sa initializam populatia cu tururi
if ($initialise) {
// Creem tururi
for ($i = 0; $i < $this->populationSize(); $i++) {
$newTour = new Tour;
$newTour->generateIndividual();
$this->saveTour($i, $newTour);
}
}
}
// Salvam un tur
public function saveTour(int $index, Tour $tour) {
$this->tours[$index] = $tour;
}
// Extragem un tur din populatie
public function getTour(int $index) {
return $this->tours[$index];
}
// Alegem cel mai bun tur din populatie
public function getFittest() {
$fittest = $this->tours[0];
// Trecem prin fiecare pentru a detecta cel mai bun
for ($i = 0; $i < $this->populationSize(); $i++) {
if ($fittest->getFitness() <= $this->getTour($i)->getFitness()) {
$fittest = $this->getTour($i);
}
}
return $fittest;
}
// Extragem dimensiunea populatiei
public function populationSize() {
return count($this->tours);
}
}