classicals.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. """Classical policies classes implementations
  2. """
  3. # main imports
  4. import random
  5. # module imports
  6. from macop.policies.base import Policy
  7. class RandomPolicy(Policy):
  8. """Policy class implementation which is used for select operator randomly from the `operators` list
  9. Attributes:
  10. operators: {[Operator]} -- list of selected operators for the algorithm
  11. Example:
  12. >>> import random
  13. >>> random.seed(42)
  14. >>> from macop.operators.discrete.crossovers import SimpleCrossover
  15. >>> from macop.operators.discrete.mutators import SimpleMutation
  16. >>> from macop.policies.classicals import RandomPolicy
  17. >>> policy = RandomPolicy([SimpleCrossover(), SimpleMutation()])
  18. >>> operator = policy.select()
  19. >>> type(operator).__name__
  20. 'SimpleCrossover'
  21. """
  22. def select(self):
  23. """Select randomly the next operator to use
  24. Returns:
  25. {Operator}: the selected operator
  26. """
  27. # choose operator randomly
  28. index = random.randint(0, len(self._operators) - 1)
  29. return self._operators[index]