SimpleMutation.py 1.2 KB

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