BinarySolution.py 1.3 KB

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