crossovers.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. """Crossover implementations for discrete solutions kind
  2. """
  3. # main imports
  4. import random
  5. import sys
  6. # module imports
  7. from ..base import Crossover
  8. class SimpleCrossover(Crossover):
  9. """Crossover implementation which generated new solution by splitting at mean size best solution and current solution
  10. Attributes:
  11. kind: {Algorithm} -- specify the kind of operator
  12. Example:
  13. >>> # operators import
  14. >>> from macop.operators.discrete.crossovers import SimpleCrossover
  15. >>> from macop.operators.discrete.mutators import SimpleMutation
  16. >>> # policy import
  17. >>> from macop.policies.reinforcement import UCBPolicy
  18. >>> # solution and algorithm
  19. >>> from macop.solutions.discrete import BinarySolution
  20. >>> from macop.algorithms.mono import IteratedLocalSearch
  21. >>> # evaluator import
  22. >>> from macop.evaluators.discrete.mono import KnapsackEvaluator
  23. >>> # evaluator initialization (worths objects passed into data)
  24. >>> worths = [ random.randint(0, 20) for i in range(10) ]
  25. >>> evaluator = KnapsackEvaluator(data={'worths': worths})
  26. >>> # validator specification (based on weights of each objects)
  27. >>> weights = [ random.randint(20, 30) for i in range(10) ]
  28. >>> validator = lambda solution: True if sum([weights[i] for i, value in enumerate(solution._data) if value == 1]) < 200 else False
  29. >>> # initializer function with lambda function
  30. >>> initializer = lambda x=10: BinarySolution.random(x, validator)
  31. >>> # operators list with crossover and mutation
  32. >>> simple_crossover = SimpleCrossover()
  33. >>> simple_mutation = SimpleMutation()
  34. >>> operators = [simple_crossover, simple_mutation]
  35. >>> policy = UCBPolicy(operators)
  36. >>> algo = IteratedLocalSearch(initializer, evaluator, operators, policy, validator, maximise=True, verbose=False)
  37. >>> # using best solution, simple crossover is applied
  38. >>> best_solution = algo.run(100)
  39. >>> list(best_solution._data)
  40. [1, 1, 0, 1, 0, 1, 1, 1, 0, 1]
  41. >>> new_solution = initializer()
  42. >>> mutate_solution = simple_crossover.apply(new_solution)
  43. >>> list(mutate_solution._data)
  44. [0, 1, 0, 0, 0, 1, 1, 1, 0, 1]
  45. """
  46. def apply(self, solution):
  47. """Create new solution based on best solution found and solution passed as parameter
  48. Args:
  49. solution: {Solution} -- the solution to use for generating new solution
  50. Returns:
  51. {Solution} -- new generated solution
  52. """
  53. size = solution._size
  54. # copy data of solution
  55. firstData = solution._data.copy()
  56. # get best solution from current algorithm
  57. copy_solution = self._algo._bestSolution.clone()
  58. splitIndex = int(size / 2)
  59. if random.uniform(0, 1) > 0.5:
  60. copy_solution._data[splitIndex:] = firstData[splitIndex:]
  61. else:
  62. copy_solution._data[:splitIndex] = firstData[:splitIndex]
  63. return copy_solution
  64. class RandomSplitCrossover(Crossover):
  65. """Crossover implementation which generated new solution by randomly splitting best solution and current solution
  66. Attributes:
  67. kind: {KindOperator} -- specify the kind of operator
  68. Example:
  69. >>> # operators import
  70. >>> from macop.operators.discrete.crossovers import RandomSplitCrossover
  71. >>> from macop.operators.discrete.mutators import SimpleMutation
  72. >>> # policy import
  73. >>> from macop.policies.reinforcement import UCBPolicy
  74. >>> # solution and algorithm
  75. >>> from macop.solutions.discrete import BinarySolution
  76. >>> from macop.algorithms.mono import IteratedLocalSearch
  77. >>> # evaluator import
  78. >>> from macop.evaluators.discrete.mono import KnapsackEvaluator
  79. >>> # evaluator initialization (worths objects passed into data)
  80. >>> worths = [ random.randint(0, 20) for i in range(10) ]
  81. >>> evaluator = KnapsackEvaluator(data={'worths': worths})
  82. >>> # validator specification (based on weights of each objects)
  83. >>> weights = [ random.randint(20, 30) for i in range(10) ]
  84. >>> validator = lambda solution: True if sum([weights[i] for i, value in enumerate(solution._data) if value == 1]) < 200 else False
  85. >>> # initializer function with lambda function
  86. >>> initializer = lambda x=10: BinarySolution.random(x, validator)
  87. >>> # operators list with crossover and mutation
  88. >>> random_split_crossover = RandomSplitCrossover()
  89. >>> simple_mutation = SimpleMutation()
  90. >>> operators = [random_split_crossover, simple_mutation]
  91. >>> policy = UCBPolicy(operators)
  92. >>> algo = IteratedLocalSearch(initializer, evaluator, operators, policy, validator, maximise=True, verbose=False)
  93. >>> # using best solution, simple crossover is applied
  94. >>> best_solution = algo.run(100)
  95. >>> list(best_solution._data)
  96. [1, 1, 1, 0, 1, 0, 1, 1, 1, 0]
  97. >>> new_solution = initializer()
  98. >>> mutate_solution = random_split_crossover.apply(new_solution)
  99. >>> list(mutate_solution._data)
  100. [1, 0, 0, 1, 1, 0, 0, 1, 0, 0]
  101. """
  102. def apply(self, solution):
  103. """Create new solution based on best solution found and solution passed as parameter
  104. Args:
  105. solution: {Solution} -- the solution to use for generating new solution
  106. Returns:
  107. {Solution} -- new generated solution
  108. """
  109. size = solution._size
  110. # copy data of solution
  111. firstData = solution._data.copy()
  112. # get best solution from current algorithm
  113. copy_solution = self._algo._bestSolution.clone()
  114. splitIndex = random.randint(0, size)
  115. if random.uniform(0, 1) > 0.5:
  116. copy_solution._data[splitIndex:] = firstData[splitIndex:]
  117. else:
  118. copy_solution._data[:splitIndex] = firstData[:splitIndex]
  119. return copy_solution