HillClimberFirstImprovment.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. """Hill Climber First Improvment algorithm starting from new solution and explore using neighborhood and loop over the best one obtained from neighborhood search space
  2. """
  3. # main imports
  4. import logging
  5. # module imports
  6. from ..Algorithm import Algorithm
  7. class HillClimberFirstImprovment(Algorithm):
  8. """Hill Climber First Improvment used as quick exploration optimisation algorithm
  9. This algorithm do a neighborhood exploration of a new generated solution (by doing operation on the current solution obtained) in order to find a better solution from the neighborhood space.
  10. Then replace the current solution by the first one from the neighbordhood space which is better than the current solution.
  11. Do these steps until a number of evaluation (stopping criterion) is reached.
  12. Attributes:
  13. initalizer: {function} -- basic function strategy to initialize solution
  14. evaluator: {function} -- basic function in order to obtained fitness (mono or multiple objectives)
  15. operators: {[Operator]} -- list of operator to use when launching algorithm
  16. policy: {Policy} -- Policy class implementation strategy to select operators
  17. validator: {function} -- basic function to check if solution is valid or not under some constraints
  18. maximise: {bool} -- specify kind of optimisation problem
  19. currentSolution: {Solution} -- current solution managed for current evaluation
  20. bestSolution: {Solution} -- best solution found so far during running algorithm
  21. callbacks: {[Callback]} -- list of Callback class implementation to do some instructions every number of evaluations and `load` when initializing algorithm
  22. """
  23. def run(self, evaluations):
  24. """
  25. Run the local search algorithm
  26. Args:
  27. evaluations: {int} -- number of Local search evaluations
  28. Returns:
  29. {Solution} -- best solution found
  30. """
  31. # by default use of mother method to initialize variables
  32. super().run(evaluations)
  33. # initialize current solution and best solution
  34. self.initRun()
  35. solutionSize = self._currentSolution.size
  36. # local search algorithm implementation
  37. while not self.stop():
  38. for _ in range(solutionSize):
  39. # update current solution using policy
  40. newSolution = self.update(self._currentSolution)
  41. # if better solution than currently, replace it and stop current exploration (first improvment)
  42. if self.isBetter(newSolution):
  43. self._bestSolution = newSolution
  44. break
  45. # increase number of evaluations
  46. self.increaseEvaluation()
  47. self.progress()
  48. logging.info(f"---- Current {newSolution} - SCORE {newSolution.fitness()}")
  49. # stop algorithm if necessary
  50. if self.stop():
  51. break
  52. # set new current solution using best solution found in this neighbor search
  53. self._currentSolution = self._bestSolution
  54. logging.info(f"End of {type(self).__name__}, best solution found {self._bestSolution}")
  55. return self._bestSolution