LocalSearch.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 not self.stop():
  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.increaseEvaluation()
  20. self.progress()
  21. logging.info("---- Current %s - SCORE %s" %
  22. (newSolution, newSolution.fitness()))
  23. # stop algorithm if necessary
  24. if self.stop():
  25. break
  26. logging.info("End of %s, best solution found %s" %
  27. (type(self).__name__, self.bestSolution))
  28. return self.bestSolution