This repository has been archived by the owner on Apr 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathpso.py
63 lines (55 loc) · 2.31 KB
/
pso.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""Particle Swarm Optimized Clustering
Optimizing centroid using K-Means style. In hybrid mode will use K-Means to seed first particle's centroid
"""
import numpy as np
from particle import Particle
class ParticleSwarmOptimizedClustering:
def __init__(self,
n_cluster: int,
n_particles: int,
data: np.ndarray,
hybrid: bool = True,
max_iter: int = 100,
print_debug: int = 10):
self.n_cluster = n_cluster
self.n_particles = n_particles
self.data = data
self.max_iter = max_iter
self.particles = []
self.hybrid = hybrid
self.print_debug = print_debug
self.gbest_score = np.inf
self.gbest_centroids = None
self.gbest_sse = np.inf
self._init_particles()
def _init_particles(self):
for i in range(self.n_particles):
particle = None
if i == 0 and self.hybrid:
particle = Particle(self.n_cluster, self.data, use_kmeans=True)
else:
particle = Particle(self.n_cluster, self.data, use_kmeans=False)
if particle.best_score < self.gbest_score:
self.gbest_centroids = particle.centroids.copy()
self.gbest_score = particle.best_score
self.particles.append(particle)
self.gbest_sse = min(particle.best_sse, self.gbest_sse)
def run(self):
print('Initial global best score', self.gbest_score)
history = []
for i in range(self.max_iter):
for particle in self.particles:
particle.update(self.gbest_centroids, self.data)
# print(i, particle.best_score, self.gbest_score)
for particle in self.particles:
if particle.best_score < self.gbest_score:
self.gbest_centroids = particle.centroids.copy()
self.gbest_score = particle.best_score
history.append(self.gbest_score)
if i % self.print_debug == 0:
print('Iteration {:04d}/{:04d} current gbest score {:.18f}'.format(
i + 1, self.max_iter, self.gbest_score))
print('Finish with gbest score {:.18f}'.format(self.gbest_score))
return history
if __name__ == "__main__":
pass