MOSubProblem.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. """Local Search algorithm
  2. """
  3. # main imports
  4. import logging
  5. # module imports
  6. from ..Algorithm import Algorithm
  7. class MOSubProblem(Algorithm):
  8. """Specific MO sub problem used into MOEAD
  9. Attributes:
  10. index: {int} -- sub problem index
  11. weights: {[float]} -- sub problems objectives weights
  12. initalizer: {function} -- basic function strategy to initialize solution
  13. evaluator: {function} -- basic function in order to obtained fitness (mono or multiple objectives)
  14. operators: {[Operator]} -- list of operator to use when launching algorithm
  15. policy: {Policy} -- Policy class implementation strategy to select operators
  16. validator: {function} -- basic function to check if solution is valid or not under some constraints
  17. maximise: {bool} -- specify kind of optimization problem
  18. currentSolution: {Solution} -- current solution managed for current evaluation
  19. bestSolution: {Solution} -- best solution found so far during running algorithm
  20. checkpoint: {Checkpoint} -- Checkpoint class implementation to keep track of algorithm and restart
  21. """
  22. def __init__(self,
  23. _index,
  24. _weights,
  25. _initalizer,
  26. _evaluator,
  27. _operators,
  28. _policy,
  29. _validator,
  30. _maximise=True,
  31. _parent=None):
  32. super().__init__(_initalizer, _evaluator, _operators, _policy, _validator, _maximise, _parent)
  33. self.index = _index
  34. self.weights = _weights
  35. def run(self, _evaluations):
  36. """
  37. Run the local search algorithm
  38. Args:
  39. _evaluations: {int} -- number of Local search evaluations
  40. Returns:
  41. {Solution} -- best solution found
  42. """
  43. # by default use of mother method to initialize variables
  44. super().run(_evaluations)
  45. solutionSize = self.bestSolution.size
  46. # local search algorithm implementation
  47. while not self.stop():
  48. for _ in range(solutionSize):
  49. # update solution using policy
  50. newSolution = self.update(self.bestSolution)
  51. # if better solution than currently, replace it
  52. if self.isBetter(newSolution):
  53. self.bestSolution = newSolution
  54. # increase number of evaluations
  55. self.increaseEvaluation()
  56. self.progress()
  57. logging.info("---- Current %s - SCORE %s" %
  58. (newSolution, newSolution.fitness()))
  59. # stop algorithm if necessary
  60. if self.stop():
  61. break
  62. logging.info("End of %s, best solution found %s" %
  63. (type(self).__name__, self.bestSolution))
  64. return self.bestSolution