base.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. """Abstract Operator initialiser
  17. """
  18. pass
  19. @abstractmethod
  20. def apply(self, solution):
  21. """Apply the current operator transformation
  22. Args:
  23. solution: {:class:`~macop.solutions.base.Solution`} -- Solution instance
  24. """
  25. pass
  26. def setAlgo(self, algo):
  27. """Keep into operator reference of the whole algorithm
  28. The reason is to better manage operator instance
  29. Args:
  30. algo: {:class:`~macop.algorithms.base.Algorithm`} -- the algorithm reference runned
  31. """
  32. self.algo = algo
  33. class Mutation(Operator):
  34. """Abstract Mutation extend from Operator
  35. Attributes:
  36. kind: {:class:`~macop.operators.base.KindOperator`} -- specify the kind of operator
  37. """
  38. def __init__(self):
  39. """Mutation initialiser in order to specify kind of Operator
  40. """
  41. self._kind = KindOperator.MUTATOR
  42. def apply(self, solution):
  43. """Apply mutation over solution in order to obtained new solution
  44. Args:
  45. solution: {:class:`~macop.solutions.base.Solution`} -- solution to use in order to create new solution
  46. Return:
  47. {:class:`~macop.solutions.base.Solution`} -- new generated solution
  48. """
  49. raise NotImplementedError
  50. class Crossover(Operator):
  51. """Abstract crossover extend from Operator
  52. Attributes:
  53. kind: {:class:`~macop.operators.base.KindOperator`} -- specify the kind of operator
  54. """
  55. def __init__(self):
  56. """Crossover initialiser in order to specify kind of Operator
  57. """
  58. self._kind = KindOperator.CROSSOVER
  59. def apply(self, solution1, solution2=None):
  60. """Apply crossover using two solutions in order to obtained new solution
  61. Args:
  62. solution1: {:class:`~macop.solutions.base.Solution`} -- the first solution to use for generating new solution
  63. solution2: {:class:`~macop.solutions.base.Solution`} -- the second solution to use for generating new solution
  64. Return:
  65. {:class:`~macop.solutions.base.Solution`} -- new generated solution
  66. """
  67. raise NotImplementedError