12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- """Local Search algorithm
- """
- import logging
- from macop.algorithms.Algorithm import Algorithm
- class LocalSearchSurrogate(Algorithm):
- """Local Search with surrogate used as exploitation optimization algorithm
- Attributes:
- initalizer: {function} -- basic function strategy to initialize solution
- evaluator: {function} -- basic function in order to obtained fitness (mono or multiple objectives)
- operators: {[Operator]} -- list of operator to use when launching algorithm
- policy: {Policy} -- Policy class implementation strategy to select operators
- validator: {function} -- basic function to check if solution is valid or not under some constraints
- maximise: {bool} -- specify kind of optimization problem
- currentSolution: {Solution} -- current solution managed for current evaluation
- bestSolution: {Solution} -- best solution found so far during running algorithm
- callbacks: {[Callback]} -- list of Callback class implementation to do some instructions every number of evaluations and `load` when initializing algorithm
- """
- def run(self, evaluations):
- """
- Run the local search algorithm
- Args:
- evaluations: {int} -- number of Local search evaluations
-
- Returns:
- {Solution} -- best solution found
- """
-
- super().run(evaluations)
-
-
-
-
- self.initRun()
- solutionSize = self.currentSolution.size
-
- while not self.stop():
- for _ in range(solutionSize):
-
- newSolution = self.update(self.currentSolution)
-
- if self.isBetter(newSolution):
- self.bestSolution = newSolution
-
- self.increaseEvaluation()
- self.progress()
- logging.info("---- Current %s - SCORE %s" %
- (newSolution, newSolution.fitness()))
-
-
-
-
- if self.stop():
- break
-
- self.currentSolution = self.bestSolution
- logging.info("End of %s, best solution found %s" %
- (type(self).__name__, self.bestSolution))
- return self.bestSolution
- def addCallback(self, callback):
- """Add new callback to algorithm specifying usefull parameters
- Args:
- callback: {Callback} -- specific Callback instance
- """
-
- if self.parent is not None:
- callback.setAlgo(self.parent)
- else:
- callback.setAlgo(self)
-
- self.callbacks.append(callback)
|