MOSubProblem.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. """MOEAD sub problem algorithm class
  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 optimisation 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. # initialize solution if necessary
  47. if self._bestSolution is None:
  48. self.initRun()
  49. # new operators list keep track of current sub problem
  50. for op in self._operators:
  51. op.setAlgo(self)
  52. for _ in range(evaluations):
  53. # keep reference of sub problem used
  54. self._policy.setAlgo(self)
  55. # update solution using policy
  56. newSolution = self.update(self._bestSolution)
  57. # if better solution than currently, replace it
  58. if self.isBetter(newSolution):
  59. self._bestSolution = newSolution
  60. # increase number of evaluations
  61. self.increaseEvaluation()
  62. self.progress()
  63. # stop algorithm if necessary
  64. if self.stop():
  65. break
  66. logging.info(f"---- Current {newSolution} - SCORE {newSolution.fitness()}")
  67. logging.info(f"End of {type(self).__name__}, best solution found {self._bestSolution}")
  68. return self._bestSolution