Solution.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. """Abstract solution class
  2. """
  3. class Solution():
  4. def __init__(self, _data, _size):
  5. """
  6. Binary integer solution class
  7. Attributes:
  8. data: {ndarray} -- array of binary values
  9. size: {int} -- size of binary array values
  10. score: {float} -- fitness score value
  11. """
  12. self.data = _data
  13. self.size = _size
  14. self.score = None
  15. def isValid(self, _validator):
  16. """
  17. Use of custom method which validates if solution is valid or not
  18. Args:
  19. _validator: {function} -- specific function which validates or not a solution
  20. Returns:
  21. {bool} -- `True` is solution is valid
  22. """
  23. return _validator(self)
  24. def evaluate(self, _evaluator):
  25. """
  26. Evaluate solution using specific `_evaluator`
  27. Args:
  28. _evaluator: {function} -- specific function which computes fitness of solution
  29. Returns:
  30. {float} -- fitness score value
  31. """
  32. self.score = _evaluator(self)
  33. return self.score
  34. def fitness(self):
  35. """
  36. Returns fitness score
  37. Returns:
  38. {float} -- fitness score value
  39. """
  40. return self.score
  41. def random(self, _validator):
  42. """
  43. Initialize solution using random data
  44. """
  45. raise NotImplementedError
  46. def __str__(self):
  47. print("Generic solution with ", self.data)