mutators.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. """Mutation implementations for discrete solution
  2. """
  3. # main imports
  4. import random
  5. import sys
  6. # module imports
  7. from macop.operators.base import Mutation
  8. class SimpleMutation(Mutation):
  9. """Mutation implementation for binary solution, swap two bits randomly from solution
  10. Attributes:
  11. kind: {KindOperator} -- specify the kind of operator
  12. Example:
  13. >>> # import of solution and simple mutation operator
  14. >>> from macop.solutions.discrete import BinarySolution
  15. >>> from macop.operators.discrete.mutators import SimpleMutation
  16. >>> solution = BinarySolution.random(5)
  17. >>> list(solution.getData())
  18. [1, 0, 0, 0, 1]
  19. >>> mutator = SimpleMutation()
  20. >>> mutation_solution = mutator.apply(solution)
  21. >>> list(mutation_solution.getData())
  22. [0, 0, 1, 0, 1]
  23. """
  24. def apply(self, solution):
  25. """Create new solution based on solution passed as parameter
  26. Args:
  27. solution: {Solution} -- the solution to use for generating new solution
  28. Returns:
  29. {Solution} -- new generated solution
  30. """
  31. size = solution._size
  32. firstCell = 0
  33. secondCell = 0
  34. # copy of solution
  35. copy_solution = solution.clone()
  36. while firstCell == secondCell:
  37. firstCell = random.randint(0, size - 1)
  38. secondCell = random.randint(0, size - 1)
  39. temp = copy_solution.getData()[firstCell]
  40. # swicth values
  41. copy_solution.getData()[firstCell] = copy_solution.getData(
  42. )[secondCell]
  43. copy_solution.getData()[secondCell] = temp
  44. return copy_solution
  45. class SimpleBinaryMutation(Mutation):
  46. """Mutation implementation for binary solution, swap bit randomly from solution
  47. Attributes:
  48. kind: {KindOperator} -- specify the kind of operator
  49. Example:
  50. >>> # import of solution and simple binary mutation operator
  51. >>> from macop.solutions.discrete import BinarySolution
  52. >>> from macop.operators.discrete.mutators import SimpleBinaryMutation
  53. >>> solution = BinarySolution.random(5)
  54. >>> list(solution.getData())
  55. [0, 1, 0, 0, 0]
  56. >>> mutator = SimpleBinaryMutation()
  57. >>> mutation_solution = mutator.apply(solution)
  58. >>> list(mutation_solution.getData())
  59. [1, 1, 0, 0, 0]
  60. """
  61. def apply(self, solution):
  62. """Create new solution based on solution passed as parameter
  63. Args:
  64. solution: {Solution} -- the solution to use for generating new solution
  65. Returns:
  66. {Solution} -- new generated solution
  67. """
  68. size = solution._size
  69. cell = random.randint(0, size - 1)
  70. # copy of solution
  71. copy_solution = solution.clone()
  72. # swicth values
  73. if copy_solution.getData()[cell]:
  74. copy_solution.getData()[cell] = 0
  75. else:
  76. copy_solution.getData()[cell] = 1
  77. return copy_solution