mono.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. """Mono-objective evaluators classes for continuous problem
  2. """
  3. # main imports
  4. from macop.evaluators.base import Evaluator
  5. class ZdtEvaluator(Evaluator):
  6. """Generic Zdt evaluator class which enables to compute custom Zdt function for continuous problem
  7. - stores into its `_data` dictionary attritute required measures when computing a knapsack solution
  8. - `_data['f']` stores lambda Zdt function
  9. - `compute` method enables to compute and associate a score to a given knapsack solution
  10. Example:
  11. >>> import random
  12. >>>
  13. >>> # binary solution import
  14. >>> from macop.solutions.continuous import ContinuousSolution
  15. >>>
  16. >>> # evaluator import
  17. >>> from macop.evaluators.continuous.mono import ZdtEvaluator
  18. >>> solution_data = [2, 3, 4, 1, 2, 3, 3]
  19. >>> size = len(solution_data)
  20. >>> solution = ContinuousSolution(solution_data, size)
  21. >>>
  22. >>> # evaluator initialization (Shere function)
  23. >>> f_sphere = lambda s: sum([ x * x for x in s.data])
  24. >>> evaluator = ZdtEvaluator(data={'f': f_sphere})
  25. >>>
  26. >>> # compute solution score
  27. >>> evaluator.compute(solution)
  28. 45
  29. """
  30. def compute(self, solution):
  31. """Apply the computation of fitness from solution
  32. Args:
  33. solution: {:class:`~macop.solutions.base.Solution`} -- Solution instance
  34. Returns:
  35. {float}: fitness score of solution
  36. """
  37. return self._data['f'](solution)