LSSurrogate.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. """Local Search algorithm
  2. """
  3. # main imports
  4. import logging
  5. # module imports
  6. from macop.algorithms.base import Algorithm
  7. class LocalSearchSurrogate(Algorithm):
  8. """Local Search with surrogate used as exploitation optimization algorithm
  9. Attributes:
  10. initalizer: {function} -- basic function strategy to initialize solution
  11. evaluator: {function} -- basic function in order to obtained fitness (mono or multiple objectives)
  12. operators: {[Operator]} -- list of operator to use when launching algorithm
  13. policy: {Policy} -- Policy class implementation strategy to select operators
  14. validator: {function} -- basic function to check if solution is valid or not under some constraints
  15. maximise: {bool} -- specify kind of optimization problem
  16. currentSolution: {Solution} -- current solution managed for current evaluation
  17. bestSolution: {Solution} -- best solution found so far during running algorithm
  18. callbacks: {[Callback]} -- list of Callback class implementation to do some instructions every number of evaluations and `load` when initializing algorithm
  19. """
  20. def run(self, evaluations):
  21. """
  22. Run the local search algorithm
  23. Args:
  24. evaluations: {int} -- number of Local search evaluations
  25. Returns:
  26. {Solution} -- best solution found
  27. """
  28. # by default use of mother method to initialize variables
  29. super().run(evaluations)
  30. # do not use here the best solution known (default use of initRun and current solution)
  31. # if self.parent:
  32. # self.bestSolution = self.parent.bestSolution
  33. # initialize current solution
  34. # self.initRun()
  35. for callback in self._callbacks:
  36. callback.load()
  37. solutionSize = self._currentSolution.size
  38. # local search algorithm implementation
  39. while not self.stop():
  40. for _ in range(solutionSize):
  41. # update current solution using policy
  42. newSolution = self.update(self._currentSolution)
  43. # if better solution than currently, replace it
  44. if self.isBetter(newSolution):
  45. self._bestSolution = newSolution
  46. # increase number of evaluations
  47. self.increaseEvaluation()
  48. # self.progress()
  49. for callback in self._callbacks:
  50. callback.run()
  51. logging.info(f"---- Current {newSolution} - SCORE {newSolution.fitness}")
  52. # add to surrogate pool file if necessary (using ILS parent reference)
  53. # if self.parent.start_train_surrogate >= self.getGlobalEvaluation():
  54. # self.parent.add_to_surrogate(newSolution)
  55. # stop algorithm if necessary
  56. if self.stop():
  57. break
  58. # after applying local search on currentSolution, we switch into new local area using known current bestSolution
  59. self._currentSolution = self._bestSolution
  60. logging.info(f"End of {type(self).__name__}, best solution found {self._bestSolution}")
  61. return self._bestSolution
  62. def addCallback(self, callback):
  63. """Add new callback to algorithm specifying usefull parameters
  64. Args:
  65. callback: {Callback} -- specific Callback instance
  66. """
  67. # specify current main algorithm reference
  68. if self._parent is not None:
  69. callback.setAlgo(self._parent)
  70. else:
  71. callback.setAlgo(self)
  72. # set as new
  73. self._callbacks.append(callback)