Solution.py 992 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. # Generic solution class
  2. class Solution():
  3. def __init__(self, _data, _size):
  4. """
  5. Initialize data of solution using specific data
  6. Note : `data` field can be anything, such as array/list of integer
  7. """
  8. self.data = _data
  9. self.size = _size
  10. self.score = None
  11. def isValid(self, _validator):
  12. """
  13. Use of custom method which validates if solution is valid or not
  14. """
  15. return _validator(self)
  16. def evaluate(self, _evaluator):
  17. """
  18. Evaluate function using specific `_evaluator`
  19. """
  20. self.score = _evaluator(self)
  21. return self.score
  22. def fitness(self):
  23. """
  24. Returns fitness score
  25. """
  26. return self.score
  27. def random(self, _validator):
  28. """
  29. Initialize solution using random data
  30. """
  31. raise NotImplementedError
  32. def __str__(self):
  33. print("Generic solution with ", self.data)