CombinatoryIntegerSolution.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. """Combinatory integer solution class implementation
  2. """
  3. # main imports
  4. import numpy as np
  5. # modules imports
  6. from .Solution import Solution
  7. # Solution which stores solution data as combinatory integer array
  8. class CombinatoryIntegerSolution(Solution):
  9. """
  10. Combinatory integer solution class
  11. Attributes:
  12. data: {ndarray} -- array of binary values
  13. size: {int} -- size of binary array values
  14. score: {float} -- fitness score value
  15. """
  16. def __init__(self, _data, _size):
  17. """
  18. Initialize binary solution using specific data
  19. Args:
  20. data: {ndarray} -- array of binary values
  21. size: {int} -- size of binary array values
  22. """
  23. self.data = _data
  24. self.size = _size
  25. def random(self, _validator):
  26. """
  27. Intialize combinatory integer array with use of validator to generate valid random solution
  28. Args:
  29. _validator: {function} -- specific function which validates or not a solution
  30. Returns:
  31. {CombinatoryIntegerSolution} -- new generated combinatory integer solution
  32. """
  33. self.data = np.random.shuffle(np.arange(self.size))
  34. while not self.isValid(_validator):
  35. self.data = np.random.shuffle(np.arange(self.size))
  36. return self
  37. def __str__(self):
  38. return "Combinatory integer solution %s" % (self.data)