base.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. """Abstract Operator classes
  2. """
  3. # main imports
  4. from enum import Enum
  5. from abc import abstractmethod
  6. class KindOperator(Enum):
  7. """Enum in order to recognize kind of operators
  8. """
  9. MUTATOR = 1
  10. CROSSOVER = 2
  11. class Operator():
  12. """Abstract Operator class which enables to update solution applying operator (computation)
  13. """
  14. @abstractmethod
  15. def __init__(self):
  16. pass
  17. @abstractmethod
  18. def apply(self, solution):
  19. """Apply the current operator transformation
  20. Args:
  21. solution: {Solution} -- Solution instance
  22. """
  23. pass
  24. def setAlgo(self, algo):
  25. """Keep into operator reference of the whole algorithm
  26. The reason is to better manage operator instance
  27. Args:
  28. algo: {Algorithm} -- the algorithm reference runned
  29. """
  30. self._algo = algo
  31. class Mutation(Operator):
  32. """Abstract Mutation extend from Operator
  33. Attributes:
  34. kind: {KindOperator} -- specify the kind of operator
  35. """
  36. def __init__(self):
  37. self._kind = KindOperator.MUTATOR
  38. def apply(self, solution):
  39. raise NotImplementedError
  40. class Crossover(Operator):
  41. """Abstract crossover extend from Operator
  42. Attributes:
  43. kind: {KindOperator} -- specify the kind of operator
  44. """
  45. def __init__(self):
  46. self._kind = KindOperator.CROSSOVER
  47. def apply(self, solution1, solution2=None):
  48. raise NotImplementedError