BinarySolution.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. self.data = _data
  23. self.size = _size
  24. def random(self, _validator):
  25. """
  26. Intialize binary 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. {BinarySolution} -- new generated binary solution
  31. """
  32. self.data = np.random.randint(2, size=self.size)
  33. while not self.isValid(_validator):
  34. self.data = np.random.randint(2, size=self.size)
  35. return self
  36. def __str__(self):
  37. return "Binary solution %s" % (self.data)