Solution.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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 apply(self, _updator):
  12. """
  13. Apply custom modification of solution and return the transformed solution
  14. """
  15. return _updator(self)
  16. def isValid(self, _validator):
  17. """
  18. Use of custom method which validates if solution is valid or not
  19. """
  20. return _validator(self)
  21. def evaluate(self, _evaluator):
  22. """
  23. Evaluate function using specific `_evaluator`
  24. """
  25. self.score = _evaluator(self)
  26. return self.score
  27. def fitness(self):
  28. """
  29. Returns fitness score
  30. """
  31. return self.score
  32. def random(self):
  33. """
  34. Initialize solution using random data
  35. """
  36. raise NotImplementedError
  37. def __str__(self):
  38. print("Generic solution with ", self.data)