Policy.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # main imports
  2. import logging
  3. # module imports
  4. from ..Operator import Operator
  5. # define policy to choose `operator` function at current iteration
  6. class Policy():
  7. # here you can define your statistical variables for choosing next operator to apply
  8. def __init__(self, _operators):
  9. self.operators = _operators
  10. def select(self):
  11. """
  12. Select specific operator to solution and returns solution
  13. """
  14. raise NotImplementedError
  15. def apply(self, solution, secondSolution=None):
  16. """
  17. Apply specific operator chosen to solution and returns solution
  18. """
  19. operator = self.select()
  20. logging.info("---- Applying %s on %s" % (type(operator).__name__, solution))
  21. # check kind of operator
  22. if operator.kind == Operator.CROSSOVER:
  23. newSolution = operator.apply(solution, secondSolution)
  24. if operator.kind == Operator.MUTATOR:
  25. newSolution = operator.apply(solution)
  26. logging.info("---- Obtaining %s" % (solution))
  27. return newSolution