Passed
Pull Request — master (#34)
by Benjamin
12:13
created

RandomInitialization::initializeClusters()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 16
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
nc 3
nop 2
dl 0
loc 16
rs 10
c 1
b 0
f 0
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 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
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
The variable $point seems to be defined by a foreach iteration on line 40. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
48
    }
49
}
50