1 | <?php |
||
12 | class Space extends SplObjectStorage |
||
13 | { |
||
14 | /** |
||
15 | * @var int |
||
16 | */ |
||
17 | protected $dimension; |
||
18 | |||
19 | public function __construct(int $dimension) |
||
27 | |||
28 | public function toArray(): array |
||
37 | |||
38 | public function newPoint(array $coordinates): Point |
||
46 | |||
47 | /** |
||
48 | * @param null $data |
||
49 | */ |
||
50 | public function addPoint(array $coordinates, $data = null): void |
||
54 | |||
55 | /** |
||
56 | * @param Point $point |
||
57 | * @param null $data |
||
58 | */ |
||
59 | public function attach($point, $data = null): void |
||
67 | |||
68 | public function getDimension(): int |
||
72 | |||
73 | /** |
||
74 | * @return array|bool |
||
75 | */ |
||
76 | public function getBoundaries() |
||
94 | |||
95 | public function getRandomPoint(Point $min, Point $max): Point |
||
105 | |||
106 | /** |
||
107 | * @return array|Cluster[] |
||
108 | */ |
||
109 | public function cluster(int $clustersNumber, int $initMethod = KMeans::INIT_RANDOM): array |
||
118 | |||
119 | /** |
||
120 | * @return array|Cluster[] |
||
121 | */ |
||
122 | protected function initializeClusters(int $clustersNumber, int $initMethod): array |
||
143 | |||
144 | protected function iterate($clusters): bool |
||
145 | { |
||
146 | $convergence = true; |
||
147 | |||
148 | $attach = new SplObjectStorage(); |
||
149 | $detach = new SplObjectStorage(); |
||
150 | |||
151 | foreach ($clusters as $cluster) { |
||
152 | foreach ($cluster as $point) { |
||
153 | $closest = $point->getClosest($clusters); |
||
154 | |||
155 | if ($closest !== $cluster) { |
||
156 | $attach[$closest] ?? $attach[$closest] = new SplObjectStorage(); |
||
157 | $detach[$cluster] ?? $detach[$cluster] = new SplObjectStorage(); |
||
158 | |||
159 | $attach[$closest]->attach($point); |
||
160 | $detach[$cluster]->attach($point); |
||
161 | |||
162 | $convergence = false; |
||
163 | } |
||
164 | } |
||
165 | } |
||
166 | |||
167 | foreach ($attach as $cluster) { |
||
168 | $cluster->attachAll($attach[$cluster]); |
||
169 | } |
||
170 | |||
171 | foreach ($detach as $cluster) { |
||
172 | $cluster->detachAll($detach[$cluster]); |
||
173 | } |
||
174 | |||
175 | foreach ($clusters as $cluster) { |
||
176 | $cluster->updateCentroid(); |
||
177 | } |
||
178 | |||
179 | return $convergence; |
||
180 | } |
||
181 | |||
182 | protected function initializeKMPPClusters(int $clustersNumber): array |
||
212 | |||
213 | private function initializeRandomClusters(int $clustersNumber): array |
||
224 | } |
||
225 |
This check looks for
do
loops that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.Consider removing the loop.