IntegerSolution.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. super().__init__(data, size)
  24. def random(self, validator):
  25. """
  26. Intialize integer array with use of validator to generate valid random solution
  27. Args:
  28. validator: {function} -- specific function which validates or not a solution
  29. Returns:
  30. {IntegerSolution} -- new generated integer solution
  31. """
  32. self._data = np.random.randint(self._size, size=self._size)
  33. while not self.isValid(validator):
  34. self._data = np.random.randint(self._size, size=self._size)
  35. return self
  36. def __str__(self):
  37. return "Integer solution %s" % (self._data)