Total Complexity | 8 |
Total Lines | 38 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
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 within space boundaries |
||
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 |
||
48 | } |
||
49 | } |
||
50 |