bdelespierre /
php-kmeans
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Kmeans; |
||
| 4 | |||
| 5 | use Kmeans\Interfaces\ClusterCollectionInterface; |
||
| 6 | use Kmeans\Interfaces\InitializationSchemeInterface; |
||
| 7 | use Kmeans\Interfaces\PointCollectionInterface; |
||
| 8 | use Kmeans\Interfaces\PointInterface; |
||
| 9 | |||
| 10 | class RandomInitialization implements InitializationSchemeInterface |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @throws \InvalidArgumentException when $nbClusters is lesser than 1 |
||
| 14 | */ |
||
| 15 | public function initializeClusters(PointCollectionInterface $points, int $nbClusters): ClusterCollectionInterface |
||
| 16 | { |
||
| 17 | // validate cluster count |
||
| 18 | if ($nbClusters < 1) { |
||
| 19 | throw new \InvalidArgumentException("Invalid cluster count: {$nbClusters}"); |
||
| 20 | } |
||
| 21 | |||
| 22 | $clusters = new ClusterCollection($points->getSpace()); |
||
| 23 | |||
| 24 | // initialize N clusters with a random point |
||
| 25 | for ($n = 0; $n < $nbClusters; $n++) { |
||
| 26 | // assign all points to the first cluster only |
||
| 27 | $clusters->attach(new Cluster($this->getRandomPoint($points), $n == 0 ? $points : null)); |
||
| 28 | } |
||
| 29 | |||
| 30 | return $clusters; |
||
| 31 | } |
||
| 32 | |||
| 33 | protected function getRandomPoint(PointCollectionInterface $points): PointInterface |
||
| 34 | { |
||
| 35 | if (count($points) == 0) { |
||
| 36 | throw new \LogicException("Unable to pick a random point out of an empty point collection"); |
||
| 37 | } |
||
| 38 | |||
| 39 | $num = mt_rand(0, count($points) - 1); |
||
| 40 | foreach ($points as $i => $point) { |
||
| 41 | if ($i > $num) { |
||
| 42 | break; |
||
| 43 | } |
||
| 44 | } |
||
| 45 | |||
| 46 | assert(isset($point)); |
||
| 47 | return $point; |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Loading history...
|
|||
| 48 | } |
||
| 49 | } |
||
| 50 |