knapsacks.py 877 B

12345678910111213141516171819202122232425262728
  1. """Knapsack evaluators classes
  2. """
  3. # main imports
  4. from .base import Evaluator
  5. class KnapsackEvaluator(Evaluator):
  6. """Knapsack evaluator class which enables to compute solution using specific `_data`
  7. - stores into its `_data` dictionary attritute required measures when computing a knapsack solution
  8. - `_data['worths']` stores knapsack objects worths information
  9. - `compute` method enables to compute and associate a score to a given knapsack solution
  10. """
  11. def compute(self, solution):
  12. """Apply the computation of fitness from solution
  13. Args:
  14. solution: {Solution} -- Solution instance
  15. Returns:
  16. {float} -- fitness score of solution
  17. """
  18. fitness = 0
  19. for index, elem in enumerate(solution._data):
  20. fitness += self._data['worths'][index] * elem
  21. return fitness