MOSubProblem.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. callbacks: {[Callback]} -- list of Callback class implementation to do some instructions every number of evaluations and `load` when initializing algorithm
  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,
  33. _validator, _maximise, _parent)
  34. self.index = _index
  35. self.weights = _weights
  36. def run(self, _evaluations):
  37. """
  38. Run the local search algorithm
  39. Args:
  40. _evaluations: {int} -- number of evaluations
  41. Returns:
  42. {Solution} -- best solution found
  43. """
  44. # by default use of mother method to initialize variables
  45. super().run(_evaluations)
  46. for _ in range(_evaluations):
  47. # update solution using policy
  48. newSolution = self.update(self.bestSolution)
  49. # if better solution than currently, replace it
  50. if self.isBetter(newSolution):
  51. self.bestSolution = newSolution
  52. # increase number of evaluations
  53. self.increaseEvaluation()
  54. self.progress()
  55. # stop algorithm if necessary
  56. if self.stop():
  57. break
  58. logging.info("---- Current %s - SCORE %s" %
  59. (newSolution, newSolution.fitness()))
  60. logging.info("End of %s, best solution found %s" %
  61. (type(self).__name__, self.bestSolution))
  62. return self.bestSolution