LocalSearch.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. # send random solution as second parameter for mutation
  15. newSolution = self.update(self.bestSolution, self.initializer())
  16. # if better solution than currently, replace it
  17. if self.isBetter(newSolution):
  18. self.bestSolution = newSolution
  19. # increase number of evaluations
  20. self.increaseEvaluation()
  21. self.progress()
  22. logging.info("---- Current %s - SCORE %s" % (newSolution, newSolution.fitness()))
  23. # stop algorithm if necessary
  24. if self.stop():
  25. break
  26. logging.info("End of %s, best solution found %s" % (type(self).__name__, self.bestSolution))
  27. return self.bestSolution