evaluators.rst 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. Use of evaluators
  2. ====================
  3. Now that it is possible to generate a solution randomly or not. It is important to know the value associated with this solution. We will then speak of evaluation of the solution. With the score associated with it, the `fitness`.
  4. Generic evaluator
  5. ~~~~~~~~~~~~~~~~~~~~~~
  6. As for the management of solutions, a generic evaluator class ``macop.evaluators.base.Evaluator`` is developed within **Macop**:
  7. Abstract Evaluator class is used for computing fitness score associated to a solution. To evaluate all the solutions, this class:
  8. - stores into its ``_data`` dictionary attritute required measures when computing a solution
  9. - has a ``compute`` abstract method enable to compute and associate a score to a given solution
  10. - stores into its ``algo`` attritute the current algorithm to use (we will talk about algorithm later)
  11. .. code-block: python
  12. class Evaluator():
  13. """
  14. Abstract Evaluator class which enables to compute solution using specific `_data`
  15. """
  16. def __init__(self, data):
  17. self._data = data
  18. @abstractmethod
  19. def compute(self, solution):
  20. """
  21. Apply the computation of fitness from solution
  22. """
  23. pass
  24. def setAlgo(self, algo):
  25. """
  26. Keep into evaluator reference of the whole algorithm
  27. """
  28. self.algo = algo
  29. We must therefore now create our own evaluator based on the proposed structure.
  30. Custom evaluator
  31. ~~~~~~~~~~~~~~~~~~~~~
  32. To create our own evaluator, we need both:
  33. - data useful for evaluating a solution
  34. - calculate the score (fitness) associated with the state of the solution from these data. Hence, implement specific ``compute`` method.
  35. We will define the ``KnapsackEvaluator`` class, which will therefore allow us to evaluate solutions to our current problem.
  36. .. code-block:: python
  37. """
  38. modules imports
  39. """
  40. from macop.evaluators.base import Evaluator
  41. class KnapsackEvaluator(Evaluator):
  42. def compute(solution):
  43. # `_data` contains worths array values of objects
  44. fitness = 0
  45. for index, elem in enumerate(solution.getdata = )):
  46. fitness += self._data['worths'][index] * elem
  47. return fitness
  48. It is now possible to initialise our new evaluator with specific data of our problem instance:
  49. .. code-block:: python
  50. """
  51. Problem instance definition
  52. """
  53. elements_score = [ 4, 2, 10, 1, 2 ] # worth of each object
  54. elements_weight = [ 12, 1, 4, 1, 2 ] # weight of each object
  55. """
  56. Evaluator problem instance
  57. """
  58. evaluator = KnapsackEvaluator(data={'worths': elements_score})
  59. # using defined BinarySolution
  60. solution = BinarySolution.random(5)
  61. # obtaining current solution score
  62. solution_fitness = solution.evaluate(evaluator)
  63. # score is also stored into solution
  64. solution_fitness = solution.fitness
  65. .. note::
  66. The current developed ``KnapsackEvaluator`` is available into ``macop.evaluators.mono.KnapsackEvaluator`` in **Macop**.
  67. In the next part we will see how to modify our current solution with the use of modification operator.