LSSurrogate.py 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. # 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. solutionSize = self.currentSolution.size
  36. # local search algorithm implementation
  37. while not self.stop():
  38. for _ in range(solutionSize):
  39. # update current solution using policy
  40. newSolution = self.update(self.currentSolution)
  41. # if better solution than currently, replace it
  42. if self.isBetter(newSolution):
  43. self.bestSolution = newSolution
  44. # increase number of evaluations
  45. self.increaseEvaluation()
  46. self.progress()
  47. logging.info("---- Current %s - SCORE %s" %
  48. (newSolution, newSolution.fitness()))
  49. # add to surrogate pool file if necessary (using ILS parent reference)
  50. # if self.parent.start_train_surrogate >= self.getGlobalEvaluation():
  51. # self.parent.add_to_surrogate(newSolution)
  52. # stop algorithm if necessary
  53. if self.stop():
  54. break
  55. # after applying local search on currentSolution, we switch into new local area using known current bestSolution
  56. self.currentSolution = self.bestSolution
  57. logging.info("End of %s, best solution found %s" %
  58. (type(self).__name__, self.bestSolution))
  59. return self.bestSolution
  60. def addCallback(self, callback):
  61. """Add new callback to algorithm specifying usefull parameters
  62. Args:
  63. callback: {Callback} -- specific Callback instance
  64. """
  65. # specify current main algorithm reference
  66. if self.parent is not None:
  67. callback.setAlgo(self.parent)
  68. else:
  69. callback.setAlgo(self)
  70. # set as new
  71. self.callbacks.append(callback)