IntegerSolution.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. """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 integer array
  8. class IntegerSolution(Solution):
  9. """
  10. 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 integer 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 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. {IntegerSolution} -- new generated integer solution
  32. """
  33. self.data = np.random.randint(self.size, size=self.size)
  34. while not self.isValid(_validator):
  35. self.data = np.random.randint(self.size, size=self.size)
  36. return self
  37. def __str__(self):
  38. return "Integer solution %s" % (self.data)