SimpleMutation.py 1.0 KB

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