SimplePopCrossover.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. from macop.operators.base import Crossover
  2. import random
  3. class SimplePopCrossover(Crossover):
  4. def apply(self, solution1, solution2=None):
  5. """Create new solution based on best solution found and solution passed as parameter
  6. Args:
  7. solution1: {:class:`~macop.solutions.base.Solution`} -- the first solution to use for generating new solution
  8. solution2: {:class:`~macop.solutions.base.Solution`} -- the second solution to use for generating new solution (using population)
  9. Returns:
  10. {:class:`~macop.solutions.base.Solution`}: new generated solution
  11. """
  12. size = solution1._size
  13. # copy data of solution
  14. firstData = solution1.data.copy()
  15. population = self._algo.population if self._algo.population is not None else self._algo.getParent().population
  16. # copy of solution2 as output solution
  17. valid = False
  18. copy_solution = None
  19. # use of different random population solution
  20. ncounter = 0
  21. while not valid:
  22. chosen_solution = population[random.randint(0, len(population) - 1)]
  23. if not list(chosen_solution.data) == list(firstData) or ncounter > 10:
  24. valid = True
  25. copy_solution = chosen_solution.clone()
  26. # add security
  27. ncounter += 1
  28. # default empty solution
  29. if copy_solution is None:
  30. copy_solution = self._algo.initialiser()
  31. # random split index
  32. splitIndex = int(size / 2)
  33. if random.uniform(0, 1) > 0.5:
  34. copy_solution.data[splitIndex:] = firstData[splitIndex:]
  35. else:
  36. copy_solution.data[:splitIndex] = firstData[:splitIndex]
  37. return copy_solution
  38. class RandomPopCrossover(Crossover):
  39. def apply(self, solution1, solution2=None):
  40. """Create new solution based on best solution found and solution passed as parameter
  41. Args:
  42. solution1: {:class:`~macop.solutions.base.Solution`} -- the first solution to use for generating new solution
  43. solution2: {:class:`~macop.solutions.base.Solution`} -- the second solution to use for generating new solution (using population)
  44. Returns:
  45. {:class:`~macop.solutions.base.Solution`}: new generated solution
  46. """
  47. size = solution1._size
  48. # copy data of solution
  49. firstData = solution1.data.copy()
  50. population = self._algo.population if self._algo.population is not None else self._algo.getParent().population
  51. # copy of solution2 as output solution
  52. valid = False
  53. copy_solution = None
  54. # use of different random population solution
  55. ncounter = 0
  56. while not valid:
  57. chosen_solution = population[random.randint(0, len(population) - 1)]
  58. if not list(chosen_solution.data) == list(firstData) or ncounter > 10:
  59. valid = True
  60. copy_solution = chosen_solution.clone()
  61. # add security
  62. ncounter += 1
  63. # default empty solution
  64. if copy_solution is None:
  65. copy_solution = self._algo.initialiser()
  66. # random split index
  67. splitIndex = random.randint(0, len(population) - 1)
  68. if random.uniform(0, 1) > 0.5:
  69. copy_solution.data[splitIndex:] = firstData[splitIndex:]
  70. else:
  71. copy_solution.data[:splitIndex] = firstData[:splitIndex]
  72. return copy_solution