IteratedLocalSearch.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. """Iterated Local Search Algorithm implementation
  2. """
  3. # main imports
  4. import logging
  5. # module imports
  6. from ..Algorithm import Algorithm
  7. from .LocalSearch import LocalSearch
  8. class IteratedLocalSearch(Algorithm):
  9. """Iterated Local Search used to avoid local optima and increave EvE (Exploration vs Exploitation) compromise
  10. Attributes:
  11. initalizer: {function} -- basic function strategy to initialize solution
  12. evaluator: {function} -- basic function in order to obtained fitness (mono or multiple objectives)
  13. operators: {[Operator]} -- list of operator to use when launching algorithm
  14. policy: {Policy} -- Policy class implementation strategy to select operators
  15. validator: {function} -- basic function to check if solution is valid or not under some constraints
  16. maximise: {bool} -- specify kind of optimization problem
  17. currentSolution: {Solution} -- current solution managed for current evaluation
  18. bestSolution: {Solution} -- best solution found so far during running algorithm
  19. callbacks: {[Callback]} -- list of Callback class implementation to do some instructions every number of evaluations and `load` when initializing algorithm
  20. """
  21. def run(self, _evaluations, _ls_evaluations=100):
  22. """
  23. Run the iterated local search algorithm using local search (EvE compromise)
  24. Args:
  25. _evaluations: {int} -- number of global evaluations for ILS
  26. _ls_evaluations: {int} -- number of Local search evaluations (default: 100)
  27. Returns:
  28. {Solution} -- best solution found
  29. """
  30. # by default use of mother method to initialize variables
  31. super().run(_evaluations)
  32. # enable resuming for ILS
  33. self.resume()
  34. # initialize solution if no backup
  35. if self.bestSolution is None:
  36. self.initRun()
  37. # passing global evaluation param from ILS
  38. ls = LocalSearch(self.initializer,
  39. self.evaluator,
  40. self.operators,
  41. self.policy,
  42. self.validator,
  43. self.maximise,
  44. _parent=self)
  45. # add same callbacks
  46. for callback in self.callbacks:
  47. ls.addCallback(callback)
  48. # local search algorithm implementation
  49. while not self.stop():
  50. # create and search solution from local search
  51. newSolution = ls.run(_ls_evaluations)
  52. # if better solution than currently, replace it
  53. if self.isBetter(newSolution):
  54. self.bestSolution = newSolution
  55. # number of evaluatins increased from LocalSearch
  56. # increase number of evaluations and progress are then not necessary there
  57. #self.increaseEvaluation()
  58. #self.progress()
  59. self.information()
  60. logging.info("End of %s, best solution found %s" %
  61. (type(self).__name__, self.bestSolution))
  62. self.end()
  63. return self.bestSolution