SimpleMutation.py 1004 B

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