Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from cmath import sin
- from copy import deepcopy
- from random import randint, random
- import numpy as np
- class Particle:
- def __init__(self, pos, l, f):
- """
- pos : problem::individual
- """
- self.__position = pos
- self.__size = l
- self.fit = f
- self.evaluate()
- # self.velocity = [[0 for i in range(l)] for j in range(l)]
- self.velocity = 0
- self._bestPosition = self.__position.copy()
- self._bestFitness = self._fitness
- def evaluate(self):
- self._fitness = self.fit(self.__position)
- @property
- def position(self):
- return self.__position
- @property
- def fitness(self):
- return self._fitness
- @property
- def best_position(self):
- return self._bestPosition
- @property
- def best_fitness(self):
- return self._bestFitness
- @position.setter
- def position(self, newPozition):
- self.__position = newPozition.copy()
- # automatic evaluation of particle's fitness
- self.evaluate()
- # automatic update of particle's memory
- if (self._fitness < self._bestFitness):
- self._bestPosition = self.__position
- self._bestFitness = self._fitness
- class PSO:
- def __init__(self, size, f):
- self.__size = size
- self.fitness = f
- self.__pop = self.population(size) #!!!!
- self.__neighbours = self.select_neighbours(self.__pop, 20)
- self.w = 1.0
- self.c1 = 1.0
- self.c2 = 2.5
- def get_pop(self):
- return self.__pop
- def individual(self):
- m=[]
- for i in range(self.__size*2):
- m.append(list((np.random.permutation(self.__size)+1)))
- return m
- def population(self, num):
- '''Generates num individuals'''
- pop = []
- for i in range(num):
- pop.append(Particle(self.individual(), self.__size, self.fitness))
- return pop
- def select_neighbours(self, pop, size):
- if (size > len(pop)):
- size = len(pop)
- neighbors = []
- for i in range(len(pop)):
- localNeighbuor = []
- for j in range(size):
- x = randint(0, len(pop) - 1)
- while (x in localNeighbuor):
- x = randint(0, len(pop) - 1)
- localNeighbuor.append(x)
- neighbors.append(localNeighbuor.copy())
- return neighbors
- def moving(self, position, best_position, best_neighbor, velocity):
- pos = deepcopy(position)
- n = len(position)
- for i in range(0, velocity):
- rand1 = randint(0, n - 1)
- rand2 = randint(0, n - 1)
- if i <= velocity // 2:
- pos[rand1][rand2] = best_position[rand1][rand2]
- else:
- pos[rand1][rand2] = best_neighbor[rand1][rand2]
- return pos
- def iteration(self, pop, f):
- best_neighbors = []
- for i in range(len(pop)):
- best_neighbors.append(self.__neighbours[i][0])
- for j in range(1, len(self.__neighbours[i])):
- if (pop[best_neighbors[i]].fitness > pop[self.__neighbours[i][j]].fitness):
- best_neighbors[i] = self.__neighbours[i][j]
- for i in range(len(pop)):
- new_velocity = self.w * pop[i].velocity
- new_velocity = new_velocity + self.c1 * random() * (f(pop[best_neighbors[i]].position) - f(pop[i].position))
- new_velocity = new_velocity + self.c2 * random() * (f(pop[i].best_position) - f(pop[i].position))
- pop[i].velocity = new_velocity
- for i in range(len(pop)):
- new_position = self.moving(pop[i].position, pop[i].best_position, pop[best_neighbors[i]].best_position, int(pop[i].velocity))
- # for j in range(len(pop[0].velocity)):
- # new_position.append(pop[i].position[j] + pop[i].velocity[j])
- pop[i].position = new_position
- return pop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement