SimpleMutation.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. # main imports
  2. import random
  3. import sys
  4. # module imports
  5. from ...solutions.BinarySolution import BinarySolution
  6. from ...solutions.Solution import Solution
  7. def SimpleBinaryMutation(solution):
  8. size = solution.size
  9. cell = random.randint(0, size - 1)
  10. # copy data of solution
  11. currentData = solution.data.copy()
  12. # swicth values
  13. if currentData[cell]:
  14. currentData[cell] = 0
  15. else:
  16. currentData[cell] = 1
  17. # create solution of same kind with new data
  18. return globals()[type(solution).__name__](currentData, size)
  19. def SimpleMutation(solution):
  20. size = solution.size
  21. firstCell = 0
  22. secondCell = 0
  23. # copy data of solution
  24. currentData = solution.data.copy()
  25. while firstCell == secondCell:
  26. firstCell = random.randint(0, size - 1)
  27. secondCell = random.randint(0, size - 1)
  28. temp = currentData[firstCell]
  29. # swicth values
  30. currentData[firstCell] = currentData[secondCell]
  31. currentData[secondCell] = temp
  32. # create solution of same kind with new data
  33. return globals()[type(solution).__name__](currentData, size)