LSSurrogate.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. """Local Search algorithm
  2. """
  3. # main imports
  4. import logging
  5. # module imports
  6. from macop.algorithms.Algorithm 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. if self.parent:
  31. self.bestSolution = self.parent.bestSolution
  32. # initialize current solution
  33. self.initRun()
  34. solutionSize = self.currentSolution.size
  35. # local search algorithm implementation
  36. while not self.stop():
  37. for _ in range(solutionSize):
  38. # update current solution using policy
  39. newSolution = self.update(self.currentSolution)
  40. # if better solution than currently, replace it
  41. if self.isBetter(newSolution):
  42. self.bestSolution = newSolution
  43. # increase number of evaluations
  44. self.increaseEvaluation()
  45. self.progress()
  46. logging.info("---- Current %s - SCORE %s" %
  47. (newSolution, newSolution.fitness()))
  48. # add to surrogate pool file if necessary (using ILS parent reference)
  49. if self.parent.start_train_surrogate >= self.getGlobalEvaluation():
  50. self.parent.add_to_surrogate(newSolution)
  51. # stop algorithm if necessary
  52. if self.stop():
  53. break
  54. logging.info("End of %s, best solution found %s" %
  55. (type(self).__name__, self.bestSolution))
  56. return self.bestSolution
  57. def addCallback(self, _callback):
  58. """Add new callback to algorithm specifying usefull parameters
  59. Args:
  60. _callback: {Callback} -- specific Callback instance
  61. """
  62. # specify current main algorithm reference
  63. if self.parent is not None:
  64. _callback.setAlgo(self.parent)
  65. else:
  66. _callback.setAlgo(self)
  67. # set as new
  68. self.callbacks.append(_callback)