Skip to content

Commit 8b64d1c

Browse files
committed
Simple selection strategy
1 parent b8ff437 commit 8b64d1c

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/Population.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public function findBest()
3636
return $s->where('fitness', $s->max('fitness'))->first();
3737
}
3838

39+
public function select($number)
40+
{
41+
return collect($this->solutions)->sortByDesc('fitness')->take($number);
42+
}
43+
3944
protected function initialise()
4045
{
4146
for ($i = 0; $i < $this->size; $i++) {

tests/PopulationTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,13 @@ public function can_evaluate_a_population_with_evaluation_data()
5151
$this->assertEquals([64, -39, 1], $best->chromosomes(), '', 0.0001);
5252
$this->assertEquals(758, $best->fitness(), '', 0.0001);
5353
}
54+
55+
/** @test */
56+
public function apply_selection_strategy()
57+
{
58+
$population = new Population(Integers::class, 12);
59+
$selections = $population->select(6);
60+
61+
$this->assertCount(6, $selections);
62+
}
5463
}

0 commit comments

Comments
 (0)