SimpleBinaryMutation.py 667 B

12345678910111213141516171819202122232425262728
  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 SimpleBinaryMutation(Mutation):
  9. def apply(self, solution):
  10. size = solution.size
  11. cell = random.randint(0, size - 1)
  12. # copy data of solution
  13. currentData = solution.data.copy()
  14. # swicth values
  15. if currentData[cell]:
  16. currentData[cell] = 0
  17. else:
  18. currentData[cell] = 1
  19. # create solution of same kind with new data
  20. return globals()[type(solution).__name__](currentData, size)