LocalSearch.py 1.1 KB

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