IteratedLocalSearch.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 avoir 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. checkpoint: {Checkpoint} -- Checkpoint class implementation to keep track of algorithm and restart
  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 checkpoint for ILS
  33. if self.checkpoint is not None:
  34. self.resume()
  35. # passing global evaluation param from ILS
  36. ls = LocalSearch(self.initializer,
  37. self.evaluator,
  38. self.operators,
  39. self.policy,
  40. self.validator,
  41. self.maximise,
  42. _parent=self)
  43. # set same checkpoint if exists
  44. if self.checkpoint is not None:
  45. ls.setCheckpoint(self.checkpoint)
  46. # local search algorithm implementation
  47. while not self.stop():
  48. # create and search solution from local search
  49. newSolution = ls.run(_ls_evaluations)
  50. # if better solution than currently, replace it
  51. if self.isBetter(newSolution):
  52. self.bestSolution = newSolution
  53. # number of evaluatins increased from LocalSearch
  54. # increase number of evaluations and progress are then not necessary there
  55. #self.increaseEvaluation()
  56. #self.progress()
  57. self.information()
  58. logging.info("End of %s, best solution found %s" %
  59. (type(self).__name__, self.bestSolution))
  60. return self.bestSolution