IteratedLocalSearch.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # main imports
  2. import logging
  3. # module imports
  4. from .Algorithm import Algorithm
  5. from .LocalSearch import LocalSearch
  6. class IteratedLocalSearch(Algorithm):
  7. def run(self, _evaluations, _ls_evaluations=100):
  8. # by default use of mother method to initialize variables
  9. super().run(_evaluations)
  10. # enable checkpoint for ILS
  11. if self.checkpoint is not None:
  12. self.resume()
  13. # passing global evaluation param from ILS
  14. ls = LocalSearch(self.initializer,
  15. self.evaluator,
  16. self.operators,
  17. self.policy,
  18. self.validator,
  19. self.maximise,
  20. _parent=self)
  21. # set same checkpoint if exists
  22. if self.checkpoint is not None:
  23. ls.setCheckpoint(self.checkpoint)
  24. # local search algorithm implementation
  25. while not self.stop():
  26. # create and search solution from local search
  27. newSolution = ls.run(_ls_evaluations)
  28. # if better solution than currently, replace it
  29. if self.isBetter(newSolution):
  30. self.bestSolution = newSolution
  31. # number of evaluatins increased from LocalSearch
  32. # increase number of evaluations and progress are then not necessary there
  33. #self.increaseEvaluation()
  34. #self.progress()
  35. self.information()
  36. logging.info("End of %s, best solution found %s" %
  37. (type(self).__name__, self.bestSolution))
  38. return self.bestSolution