IntegerSolution.py 907 B

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