LocalSearch.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. # main imports
  2. # module imports
  3. from .Algorithm import Algorithm
  4. class LocalSearch(Algorithm):
  5. def run(self, _evaluations):
  6. # by default use of mother method to initialize variables
  7. super().run(_evaluations)
  8. solutionSize = self.bestSolution.size
  9. # local search algorithm implementation
  10. while self.numberOfEvaluations < self.maxEvalutations:
  11. for _ in range(solutionSize):
  12. # update solution using policy
  13. newSolution = self.update(self.bestSolution)
  14. # if better solution than currently, replace it
  15. if self.isBetter(newSolution):
  16. self.bestSolution = newSolution
  17. # increase number of evaluations
  18. self.numberOfEvaluations += 1
  19. self.progress()
  20. # stop algorithm if necessary
  21. if self.numberOfEvaluations >= self.maxEvalutations:
  22. break
  23. self.information()
  24. logging.info("End of %s, best solution found %s" % (type(self).__name__, self.bestSolution))
  25. return self.bestSolution