Policy.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. return operator.apply(solution, secondSolution)
  24. if operator.kind == Operator.MUTATOR:
  25. return operator.apply(solution)
  26. # by default
  27. return operator.apply(solution)