multi.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. """Multi-objective Checkpoints classes implementations
  2. """
  3. # main imports
  4. import os
  5. import logging
  6. import numpy as np
  7. # module imports
  8. from macop.callbacks.base import Callback
  9. from macop.utils.progress import macop_text, macop_line
  10. class MultiCheckpoint(Callback):
  11. """
  12. MultiCheckpoint is used for loading previous computations and start again after loading checkpoint
  13. Attributes:
  14. algo: {:class:`~macop.algorithms.base.Algorithm`} -- main algorithm instance reference
  15. every: {int} -- checkpoint frequency used (based on number of evaluations)
  16. filepath: {str} -- file path where checkpoints will be saved
  17. """
  18. def run(self):
  19. """
  20. Check if necessary to do backup based on `every` variable
  21. """
  22. # get current population
  23. population = self.algo.population
  24. currentEvaluation = self.algo.getGlobalEvaluation()
  25. # backup if necessary
  26. if currentEvaluation % self._every == 0:
  27. logging.info("Checkpoint is done into " + self._filepath)
  28. with open(self._filepath, 'w') as f:
  29. for solution in population:
  30. solution_data = ""
  31. solutionSize = len(solution.data)
  32. for index, val in enumerate(solution.data):
  33. solution_data += str(val)
  34. if index < solutionSize - 1:
  35. solution_data += ' '
  36. line = str(currentEvaluation) + ';'
  37. for i in range(len(self.algo.evaluator)):
  38. line += str(solution.scores[i]) + ';'
  39. line += solution_data + ';\n'
  40. f.write(line)
  41. def load(self):
  42. """
  43. Load backup lines as population and set algorithm state (population and pareto front) at this backup
  44. """
  45. if os.path.exists(self._filepath):
  46. logging.info('Load best solution from last checkpoint')
  47. with open(self._filepath) as f:
  48. # read data for each line
  49. for i, line in enumerate(f.readlines()):
  50. data = line.replace(';\n', '').split(';')
  51. # only the first time
  52. if i == 0:
  53. # get evaluation information
  54. globalEvaluation = int(data[0])
  55. if self.algo.getParent() is not None:
  56. self.algo.getParent(
  57. )._numberOfEvaluations = globalEvaluation
  58. else:
  59. self.algo._numberOfEvaluations = globalEvaluation
  60. nObjectives = len(self.algo.evaluator)
  61. scores = [float(s) for s in data[1:nObjectives + 1]]
  62. # get best solution data information
  63. current_data = list(map(int, data[-1].split(' ')))
  64. # initialise and fill with data
  65. self.algo.population[i] = self.algo.initialiser()
  66. self.algo.population[i].data = np.array(current_data)
  67. self.algo.population[i].scores = scores
  68. self.algo.result.append(self.algo.population[i])
  69. macop_line(self.algo)
  70. macop_text(
  71. self.algo,
  72. f'Load of available population from `{self._filepath}`')
  73. macop_text(
  74. self.algo,
  75. f'Restart algorithm from evaluation {self.algo._numberOfEvaluations}.'
  76. )
  77. else:
  78. macop_text(
  79. self.algo,
  80. 'No backup found... Start running algorithm from evaluation 0.'
  81. )
  82. logging.info(
  83. "Can't load backup... Backup filepath not valid in Checkpoint")
  84. macop_line(self.algo)
  85. class ParetoCheckpoint(Callback):
  86. """
  87. Pareto checkpoint is used for loading previous computations and start again after loading checkpoint
  88. Attributes:
  89. algo: {:class:`~macop.algorithms.base.Algorithm`} -- main algorithm instance reference
  90. every: {int} -- checkpoint frequency used (based on number of evaluations)
  91. filepath: {str} -- file path where checkpoints will be saved
  92. """
  93. def run(self):
  94. """
  95. Check if necessary to do backup based on `every` variable
  96. """
  97. # get current population
  98. pfPop = self.algo.result
  99. currentEvaluation = self.algo.getGlobalEvaluation()
  100. # backup if necessary
  101. if currentEvaluation % self._every == 0:
  102. logging.info("Checkpoint is done into " + self._filepath)
  103. with open(self._filepath, 'w') as f:
  104. for solution in pfPop:
  105. solution_data = ""
  106. solutionSize = len(solution.data)
  107. for index, val in enumerate(solution.data):
  108. solution_data += str(val)
  109. if index < solutionSize - 1:
  110. solution_data += ' '
  111. line = ''
  112. for i in range(len(self.algo.evaluator)):
  113. line += str(solution.scores[i]) + ';'
  114. line += solution_data + ';\n'
  115. f.write(line)
  116. def load(self):
  117. """
  118. Load backup lines as population and set algorithm state (population and pareto front) at this backup
  119. """
  120. if os.path.exists(self._filepath):
  121. logging.info('Load best solution from last checkpoint')
  122. with open(self._filepath) as f:
  123. # read data for each line
  124. for i, line in enumerate(f.readlines()):
  125. data = line.replace(';\n', '').split(';')
  126. nObjectives = len(self.algo.evaluator)
  127. scores = [float(s) for s in data[0:nObjectives]]
  128. # get best solution data information
  129. current_data = list(map(int, data[-1].split(' ')))
  130. self.algo.result[i].data = np.array(current_data)
  131. self.algo.result[i].scores = scores
  132. macop_text(
  133. self.algo,
  134. f'Load of available pareto front backup from `{ self._filepath}`'
  135. )
  136. else:
  137. macop_text(
  138. self.algo,
  139. 'No pareto front found... Start running algorithm with new pareto front population.'
  140. )
  141. logging.info("No pareto front backup used...")
  142. macop_line(self.algo)