SimpleMutation.py 843 B

1234567891011121314151617181920212223242526272829303132333435
  1. # main imports
  2. import random
  3. import sys
  4. # module imports
  5. from .Mutation import Mutation
  6. from ...solutions.BinarySolution import BinarySolution
  7. from ...solutions.Solution import Solution
  8. class SimpleMutation(Mutation):
  9. def apply(self, solution):
  10. size = solution.size
  11. firstCell = 0
  12. secondCell = 0
  13. # copy data of solution
  14. currentData = solution.data.copy()
  15. while firstCell == secondCell:
  16. firstCell = random.randint(0, size - 1)
  17. secondCell = random.randint(0, size - 1)
  18. temp = currentData[firstCell]
  19. # swicth values
  20. currentData[firstCell] = currentData[secondCell]
  21. currentData[secondCell] = temp
  22. # create solution of same kind with new data
  23. return globals()[type(solution).__name__](currentData, size)