multi.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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 .base import Callback
  9. from ..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: {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. solutionData = ""
  31. solutionSize = len(solution._data)
  32. for index, val in enumerate(solution._data):
  33. solutionData += str(val)
  34. if index < solutionSize - 1:
  35. solutionData += ' '
  36. line = str(currentEvaluation) + ';'
  37. for i in range(len(self._algo._evaluator)):
  38. line += str(solution._scores[i]) + ';'
  39. line += solutionData + ';\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.getParen()._numberOfEvaluations = globalEvaluation
  57. else:
  58. self._algo._numberOfEvaluations = globalEvaluation
  59. nObjectives = len(self._algo._evaluator)
  60. scores = [float(s) for s in data[1:nObjectives + 1]]
  61. # get best solution data information
  62. solutionData = list(map(int, data[-1].split(' ')))
  63. # initialize and fill with data
  64. self._algo._population[i] = self._algo._initializer()
  65. self._algo._population[i]._data = np.array(solutionData)
  66. self._algo._population[i]._scores = scores
  67. self._algo._pfPop.append(self._algo._population[i])
  68. macop_line(self._algo)
  69. macop_text(self._algo, f'Load of available population from `{self._filepath}`')
  70. macop_text(self._algo, f'Restart algorithm from evaluation {self._algo._numberOfEvaluations}.')
  71. else:
  72. macop_text(self._algo, 'No backup found... Start running algorithm from evaluation 0.')
  73. logging.info("Can't load backup... Backup filepath not valid in Checkpoint")
  74. macop_line(self._algo)
  75. class ParetoCheckpoint(Callback):
  76. """
  77. Pareto checkpoint is used for loading previous computations and start again after loading checkpoint
  78. Attributes:
  79. algo: {Algorithm} -- main algorithm instance reference
  80. every: {int} -- checkpoint frequency used (based on number of evaluations)
  81. filepath: {str} -- file path where checkpoints will be saved
  82. """
  83. def run(self):
  84. """
  85. Check if necessary to do backup based on `every` variable
  86. """
  87. # get current population
  88. pfPop = self._algo._pfPop
  89. currentEvaluation = self._algo.getGlobalEvaluation()
  90. # backup if necessary
  91. if currentEvaluation % self._every == 0:
  92. logging.info("Checkpoint is done into " + self._filepath)
  93. with open(self._filepath, 'w') as f:
  94. for solution in pfPop:
  95. solutionData = ""
  96. solutionSize = len(solution._data)
  97. for index, val in enumerate(solution._data):
  98. solutionData += str(val)
  99. if index < solutionSize - 1:
  100. solutionData += ' '
  101. line = ''
  102. for i in range(len(self._algo._evaluator)):
  103. line += str(solution._scores[i]) + ';'
  104. line += solutionData + ';\n'
  105. f.write(line)
  106. def load(self):
  107. """
  108. Load backup lines as population and set algorithm state (population and pareto front) at this backup
  109. """
  110. if os.path.exists(self._filepath):
  111. logging.info('Load best solution from last checkpoint')
  112. with open(self._filepath) as f:
  113. # read data for each line
  114. for i, line in enumerate(f.readlines()):
  115. data = line.replace(';\n', '').split(';')
  116. nObjectives = len(self._algo._evaluator)
  117. scores = [float(s) for s in data[0:nObjectives]]
  118. # get best solution data information
  119. solutionData = list(map(int, data[-1].split(' ')))
  120. self._algo._pfPop[i]._data = solutionData
  121. self._algo._pfPop[i]._scores = scores
  122. macop_text(self._algo, f'Load of available pareto front backup from `{ self._filepath}`')
  123. else:
  124. macop_text(self._algo, 'No pareto front found... Start running algorithm with new pareto front population.')
  125. logging.info("No pareto front backup used...")
  126. macop_line(self._algo)