IteratedLocalSearch.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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, self.evaluator, self.operators, self.policy, self.validator, self.maximise, _parent=self)
  15. # set same checkpoint if exists
  16. if self.checkpoint is not None:
  17. ls.setCheckpoint(self.checkpoint)
  18. # local search algorithm implementation
  19. while self.getGlobalEvaluation() < self.maxEvalutations:
  20. # create and search solution from local search
  21. newSolution = ls.run(_ls_evaluations)
  22. # if better solution than currently, replace it
  23. if self.isBetter(newSolution):
  24. self.bestSolution = newSolution
  25. # number of evaluatins increased from LocalSearch
  26. # increase number of evaluations and progress are then not necessary there
  27. #self.increaseEvaluation()
  28. #self.progress()
  29. self.information()
  30. logging.info("End of %s, best solution found %s" % (type(self).__name__, self.bestSolution))
  31. return self.bestSolution