Advertisement
cinnamonandrew

PSO0.1

Mar 28th, 2020
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. from cmath import sin
  2. from copy import deepcopy
  3. from random import randint, random
  4.  
  5. import numpy as np
  6.  
  7.  
  8. class Particle:
  9. def __init__(self, pos, l, f):
  10. """
  11. pos : problem::individual
  12. """
  13. self.__position = pos
  14. self.__size = l
  15. self.fit = f
  16. self.evaluate()
  17. # self.velocity = [[0 for i in range(l)] for j in range(l)]
  18. self.velocity = 0
  19.  
  20. self._bestPosition = self.__position.copy()
  21. self._bestFitness = self._fitness
  22.  
  23. def evaluate(self):
  24. self._fitness = self.fit(self.__position)
  25.  
  26. @property
  27. def position(self):
  28. return self.__position
  29.  
  30. @property
  31. def fitness(self):
  32. return self._fitness
  33.  
  34. @property
  35. def best_position(self):
  36. return self._bestPosition
  37.  
  38. @property
  39. def best_fitness(self):
  40. return self._bestFitness
  41.  
  42. @position.setter
  43. def position(self, newPozition):
  44. self.__position = newPozition.copy()
  45. # automatic evaluation of particle's fitness
  46. self.evaluate()
  47. # automatic update of particle's memory
  48. if (self._fitness < self._bestFitness):
  49. self._bestPosition = self.__position
  50. self._bestFitness = self._fitness
  51.  
  52. class PSO:
  53. def __init__(self, size, f):
  54. self.__size = size
  55. self.fitness = f
  56. self.__pop = self.population(size) #!!!!
  57. self.__neighbours = self.select_neighbours(self.__pop, 20)
  58. self.w = 1.0
  59. self.c1 = 1.0
  60. self.c2 = 2.5
  61.  
  62. def get_pop(self):
  63. return self.__pop
  64.  
  65. def individual(self):
  66. m=[]
  67. for i in range(self.__size*2):
  68. m.append(list((np.random.permutation(self.__size)+1)))
  69. return m
  70.  
  71. def population(self, num):
  72. '''Generates num individuals'''
  73. pop = []
  74. for i in range(num):
  75. pop.append(Particle(self.individual(), self.__size, self.fitness))
  76. return pop
  77.  
  78. def select_neighbours(self, pop, size):
  79. if (size > len(pop)):
  80. size = len(pop)
  81.  
  82.  
  83. neighbors = []
  84. for i in range(len(pop)):
  85. localNeighbuor = []
  86. for j in range(size):
  87. x = randint(0, len(pop) - 1)
  88. while (x in localNeighbuor):
  89. x = randint(0, len(pop) - 1)
  90. localNeighbuor.append(x)
  91. neighbors.append(localNeighbuor.copy())
  92. return neighbors
  93.  
  94. def moving(self, position, best_position, best_neighbor, velocity):
  95. pos = deepcopy(position)
  96. n = len(position)
  97. for i in range(0, velocity):
  98. rand1 = randint(0, n - 1)
  99. rand2 = randint(0, n - 1)
  100. if i <= velocity // 2:
  101. pos[rand1][rand2] = best_position[rand1][rand2]
  102. else:
  103. pos[rand1][rand2] = best_neighbor[rand1][rand2]
  104. return pos
  105.  
  106. def iteration(self, pop, f):
  107. best_neighbors = []
  108.  
  109. for i in range(len(pop)):
  110. best_neighbors.append(self.__neighbours[i][0])
  111. for j in range(1, len(self.__neighbours[i])):
  112. if (pop[best_neighbors[i]].fitness > pop[self.__neighbours[i][j]].fitness):
  113. best_neighbors[i] = self.__neighbours[i][j]
  114.  
  115. for i in range(len(pop)):
  116. new_velocity = self.w * pop[i].velocity
  117. new_velocity = new_velocity + self.c1 * random() * (f(pop[best_neighbors[i]].position) - f(pop[i].position))
  118. new_velocity = new_velocity + self.c2 * random() * (f(pop[i].best_position) - f(pop[i].position))
  119. pop[i].velocity = new_velocity
  120.  
  121. for i in range(len(pop)):
  122. new_position = self.moving(pop[i].position, pop[i].best_position, pop[best_neighbors[i]].best_position, int(pop[i].velocity))
  123. # for j in range(len(pop[0].velocity)):
  124. # new_position.append(pop[i].position[j] + pop[i].velocity[j])
  125. pop[i].position = new_position
  126. return pop
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement