MOSubProblem.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 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. # 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("---- Current %s - SCORE %s" %
  67. (newSolution, newSolution.fitness()))
  68. logging.info("End of %s, best solution found %s" %
  69. (type(self).__name__, self.bestSolution))
  70. return self.bestSolution