def fit(self, data): self.n_attributes = data.shape[1] self.pso = PSO(PSOCObjectiveFunction(data, self.n_clusters, self.n_attributes), dim=self.n_clusters * self.n_attributes, swarm_size=self.swarm_size, n_iter=self.n_iter, lo_w=self.lo_w, c1=self.c1, c2=self.c2, v_max=self.v_max) self.pso.optimize() self.centroids = {} raw_centroids = self.pso.gbest.pos.reshape((self.n_clusters, self.n_attributes)) self.convergence = self.pso.optimum_cost_tracking_iter for c in range(len(raw_centroids)): self.centroids[c] = raw_centroids[c] self.clusters = self.get_clusters(self.centroids, data) self.number_of_effective_clusters = 0 for c in range(len(self.centroids)): if len(self.clusters[c]) > 0: self.number_of_effective_clusters = self.number_of_effective_clusters + 1
class PSOC(object): def __init__(self, n_clusters=2, swarm_size=50, n_iter=50, lo_w=0.72, up_w=0.9, c1=1.49, c2=1.49): self.n_clusters = n_clusters self.swarm_size = swarm_size self.n_iter = n_iter self.up_w = up_w self.lo_w = lo_w self.c1 = c1 self.c2 = c2 self.v_max = 0.5 def fit(self, data): self.n_attributes = data.shape[1] self.pso = PSO(PSOCObjectiveFunction(data, self.n_clusters, self.n_attributes), dim=self.n_clusters * self.n_attributes, swarm_size=self.swarm_size, n_iter=self.n_iter, lo_w=self.lo_w, c1=self.c1, c2=self.c2, v_max=self.v_max) self.pso.optimize() self.centroids = {} raw_centroids = self.pso.gbest.pos.reshape((self.n_clusters, self.n_attributes)) self.convergence = self.pso.optimum_cost_tracking_iter for c in range(len(raw_centroids)): self.centroids[c] = raw_centroids[c] self.clusters = self.get_clusters(self.centroids, data) self.number_of_effective_clusters = 0 for c in range(len(self.centroids)): if len(self.clusters[c]) > 0: self.number_of_effective_clusters = self.number_of_effective_clusters + 1 @staticmethod def get_clusters(centroids, data): clusters = {} for c in centroids: clusters[c] = [] for xi in data: dist = [np.linalg.norm(xi - centroids[c]) for c in centroids] class_ = dist.index(min(dist)) clusters[class_].append(xi) return clusters
def main(): opt1 = PSO(dim=30, minf=-100, maxf=100, min_init=50, max_init=100, swarm_size=50, n_iter=1000, w=0.8, lb_w=0.4, w_damp=0.99, c1=2.05, c2=2.05) opt1.optimize(ObjectiveFunction.sphere_function) opt2 = PSO(dim=30, minf=-30, maxf=30, min_init=15, max_init=30, swarm_size=50, n_iter=1000, w=0.8, lb_w=0.4, w_damp=0.99, c1=2.05, c2=2.05) opt2.optimize(ObjectiveFunction.rosenbrock_function) opt3 = PSO(dim=30, minf=-5.12, maxf=5.12, min_init=2.56, max_init=5.12, swarm_size=50, n_iter=1000, w=0.8, lb_w=0.4, w_damp=0.99, c1=2.05, c2=2.05) opt3.optimize(ObjectiveFunction.rastrigin_function) opt4 = PSO(dim=30, minf=-100, maxf=100, min_init=50, max_init=100, swarm_size=50, n_iter=1000, w=0.8, lb_w=0.4, w_damp=0.99, c1=2.05, c2=2.05) opt4.optimize(ObjectiveFunction.schwefel_function) opt5 = PSO(dim=30, minf=-600, maxf=600, min_init=300, max_init=600, swarm_size=50, n_iter=1000, w=0.8, lb_w=0.4, w_damp=0.99, c1=2.05, c2=2.05) opt5.optimize(ObjectiveFunction.griewank_function) opt6 = PSO(dim=30, minf=-32, maxf=32, min_init=16, max_init=32, swarm_size=50, n_iter=1000, w=0.8, lb_w=0.4, w_damp=0.99, c1=2.05, c2=2.05) opt6.optimize(ObjectiveFunction.ackley_function) plt.subplot(231) plt.plot([(i+1) for i in range(len(opt1.best_cost))], opt1.best_cost, c='b') plt.legend(["Sphere"]) plt.xlabel('Iterations') plt.ylabel('Fitness') plt.subplot(232) plt.plot([(i+1) for i in range(len(opt2.best_cost))], opt2.best_cost, c='r') plt.legend(["Rosenbrock"]) plt.xlabel('Iterations') plt.ylabel('Fitness') plt.subplot(233) plt.plot([(i+1) for i in range(len(opt3.best_cost))], opt3.best_cost, c='g') plt.legend(["Rastrigin"]) plt.xlabel('Iterations') plt.ylabel('Fitness') plt.subplot(234) plt.plot([(i+1) for i in range(len(opt4.best_cost))], opt4.best_cost, c='c') plt.legend(["Schwefel"]) plt.xlabel('Iterations') plt.ylabel('Fitness') plt.subplot(235) plt.plot([(i+1) for i in range(len(opt5.best_cost))], opt5.best_cost, c='m') plt.legend(["Griewank"]) plt.xlabel('Iterations') plt.ylabel('Fitness') plt.subplot(236) plt.plot([(i+1) for i in range(len(opt6.best_cost))], opt6.best_cost, c='y') plt.legend(["Ackley"]) plt.xlabel('Iterations') plt.ylabel('Fitness') plt.show()
if __name__ == '__main__': opt = dict(options) opt['particle_num'] = 1000 opt['max_it'] = 1000 opt['bounds'] = (-100, 100) f = open("log8.txt", "w") num_exec = 1 time_diff_list = [0] * num_exec val_list = [0] * num_exec pos_list = [None] * num_exec for i in range(num_exec): start = time() pos, val = PSO(optimality_criterion, opt) time_diff = time() - start time_diff_list[i] = time_diff val_list[i] = val pos_list[i] = pos min_index = val_list.index(min(val_list)) f.write("Particle number: " + str(opt['particle_num']) + "\n") f.write("Max iteration: " + str(opt['max_it']) + "\n") f.write("Bounds: " + str(opt['bounds']) + "\n\n") f.write("Best position = [\n") for dim in pos_list[min_index]: f.write("\t" + str(dim) + "\n") f.write("]\n")
from optimization.ann_criterion import optimality_criterion from optimization.PSO import PSO if __name__ == '__main__': a = PSO(optimality_criterion) print(a[0], "\n", a[1])