LocalSearch.py 1.1 KB

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