BinarySolution.py 860 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. # main imports
  2. import numpy as np
  3. # modules imports
  4. from .Solution import Solution
  5. # Solution which stores solution data as binary array
  6. class BinarySolution(Solution):
  7. def __init__(self, _data, _size):
  8. """
  9. Initialize data of solution using specific data
  10. - `data` field is array of binary values
  11. - `size` field is the size of array binary values
  12. """
  13. self.data = _data
  14. self.size = _size
  15. def random(self, _validator):
  16. """
  17. Intialize binary array using size solution data
  18. Use of validator to generate valid random solution
  19. """
  20. while not self.isValid(_validator):
  21. self.data = np.random.randint(2, size=self.size)
  22. return self
  23. def __str__(self):
  24. return "Binary solution %s of size %s" % (self.data, self.size)