mutators.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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: {:class:`~macop.operators.base.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.data)
  18. [1, 0, 0, 0, 1]
  19. >>> mutator = SimpleMutation()
  20. >>> mutation_solution = mutator.apply(solution)
  21. >>> list(mutation_solution.data)
  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: {:class:`~macop.solutions.base.Solution`} -- the solution to use for generating new solution
  28. Returns:
  29. {:class:`~macop.solutions.base.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.data[firstCell]
  40. # swicth values
  41. copy_solution.data[firstCell] = copy_solution.data[secondCell]
  42. copy_solution.data[secondCell] = temp
  43. return copy_solution
  44. class SimpleBinaryMutation(Mutation):
  45. """Mutation implementation for binary solution, swap bit randomly from solution
  46. Attributes:
  47. kind: {:class:`~macop.operators.base.KindOperator`} -- specify the kind of operator
  48. Example:
  49. >>> # import of solution and simple binary mutation operator
  50. >>> from macop.solutions.discrete import BinarySolution
  51. >>> from macop.operators.discrete.mutators import SimpleBinaryMutation
  52. >>> solution = BinarySolution.random(5)
  53. >>> list(solution.data)
  54. [0, 1, 0, 0, 0]
  55. >>> mutator = SimpleBinaryMutation()
  56. >>> mutation_solution = mutator.apply(solution)
  57. >>> list(mutation_solution.data)
  58. [1, 1, 0, 0, 0]
  59. """
  60. def apply(self, solution):
  61. """Create new solution based on solution passed as parameter
  62. Args:
  63. solution: {:class:`~macop.solutions.base.Solution`} -- the solution to use for generating new solution
  64. Returns:
  65. {:class:`~macop.solutions.base.Solution`}: new generated solution
  66. """
  67. size = solution._size
  68. cell = random.randint(0, size - 1)
  69. # copy of solution
  70. copy_solution = solution.clone()
  71. # swicth values
  72. if copy_solution.data[cell]:
  73. copy_solution.data[cell] = 0
  74. else:
  75. copy_solution.data[cell] = 1
  76. return copy_solution