123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- """Abstract classes for Operator Selection Strategy
- """
- import logging
- from abc import abstractmethod
- from macop.operators.base import KindOperator
- # define policy to choose `operator` function at current iteration
- class Policy():
- """Abstract class which is used for applying strategy when selecting and applying operator
- Attributes:
- operators: {[Operator]} -- list of selected operators for the algorithm
- """
- def __init__(self, operators):
- """Initialise new Policy instance using specific list of operators
- Args:
- operators: [{}] -- list of operators to use
- """
- self._operators = operators
- @abstractmethod
- def select(self):
- """
- Select specific operator
- Returns:
- {Operator} -- selected operator
- """
- pass
- def apply(self, solution1, solution2=None):
- """
- Apply specific operator chosen to create new solution, compute its fitness and return solution
-
- Args:
- solution1: {Solution} -- the first solution to use for generating new solution
- solution2: {Solution} -- the second solution to use for generating new solution (in case of specific crossover, default is best solution from algorithm)
- Returns:
- {Solution} -- new generated solution
- """
- operator = self.select()
- logging.info("---- Applying %s on %s" %
- (type(operator).__name__, solution1))
- # default value of solution2 is current best solution
- if solution2 is None and self._algo is not None:
- solution2 = self._algo.getResult()
- # avoid use of crossover if only one solution is passed
- if solution2 is None and operator._kind == KindOperator.CROSSOVER:
- while operator._kind == KindOperator.CROSSOVER:
- operator = self.select()
- # apply operator on solution
- if operator._kind == KindOperator.CROSSOVER:
- newSolution = operator.apply(solution1, solution2)
- else:
- newSolution = operator.apply(solution1)
- logging.info("---- Obtaining %s" % (newSolution))
- return newSolution
- def setAlgo(self, algo):
- """Keep into policy reference of the whole algorithm
- The reason is to better manage the operator choices (use of rewards as example)
- Args:
- algo: {Algorithm} -- the algorithm reference runned
- """
- self._algo = algo
|