classicals.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. """Classical 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 BasicCheckpoint(Callback):
  11. """
  12. BasicCheckpoint 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 best solution
  23. solution = self.algo.result
  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. solution_data = ""
  29. solutionSize = len(solution.data)
  30. for index, val in enumerate(solution.data):
  31. solution_data += str(val)
  32. if index < solutionSize - 1:
  33. solution_data += ' '
  34. line = str(currentEvaluation) + ';' + solution_data + ';' + str(
  35. solution.fitness) + ';\n'
  36. # check if file exists
  37. if not os.path.exists(self._filepath):
  38. with open(self._filepath, 'w') as f:
  39. f.write(line)
  40. else:
  41. with open(self._filepath, 'a') as f:
  42. f.write(line)
  43. def load(self):
  44. """
  45. Load last backup line of solution and set algorithm state (best solution and evaluations) at this backup
  46. """
  47. if os.path.exists(self._filepath):
  48. logging.info('Load best solution from last checkpoint')
  49. with open(self._filepath) as f:
  50. # get last line and read data
  51. lastline = f.readlines()[-1]
  52. data = lastline.split(';')
  53. # get evaluation information
  54. globalEvaluation = int(data[0])
  55. if self.algo.getParent() is not None:
  56. self.algo.getParent().setEvaluation(globalEvaluation)
  57. else:
  58. self.algo.setEvaluation(globalEvaluation)
  59. # get best solution data information
  60. solution_data = list(map(int, data[1].split(' ')))
  61. if self.algo.result is None:
  62. self.algo.result = self.algo.initialiser()
  63. self.algo.result.data = np.array(solution_data)
  64. self.algo.result.fitness = float(data[2])
  65. macop_line(self.algo)
  66. macop_text(self.algo,
  67. f'Checkpoint found from `{self._filepath}` file.')
  68. macop_text(
  69. self.algo,
  70. f'Restart algorithm from evaluation {self.algo.getEvaluation()}.'
  71. )
  72. else:
  73. macop_text(
  74. self.algo,
  75. 'No backup found... Start running algorithm from evaluation 0.'
  76. )
  77. logging.info(
  78. "Can't load backup... Backup filepath not valid in Checkpoint")
  79. macop_line(self.algo)
  80. class ContinuousCheckpoint(Callback):
  81. """
  82. ContinuousCheckpoint is used for loading previous computations and start again after loading checkpoint (only continuous solution)
  83. Attributes:
  84. algo: {:class:`~macop.algorithms.base.Algorithm`} -- main algorithm instance reference
  85. every: {int} -- checkpoint frequency used (based on number of evaluations)
  86. filepath: {str} -- file path where checkpoints will be saved
  87. """
  88. def run(self):
  89. """
  90. Check if necessary to do backup based on `every` variable
  91. """
  92. # get current best solution
  93. solution = self.algo.result
  94. currentEvaluation = self.algo.getGlobalEvaluation()
  95. # backup if necessary
  96. if currentEvaluation % self._every == 0:
  97. logging.info("Checkpoint is done into " + self._filepath)
  98. solution_data = ""
  99. solutionSize = len(solution.data)
  100. for index, val in enumerate(solution.data):
  101. solution_data += str(val)
  102. if index < solutionSize - 1:
  103. solution_data += ' '
  104. line = str(currentEvaluation) + ';' + solution_data + ';' + str(
  105. solution.fitness) + ';\n'
  106. # check if file exists
  107. if not os.path.exists(self._filepath):
  108. with open(self._filepath, 'w') as f:
  109. f.write(line)
  110. else:
  111. with open(self._filepath, 'a') as f:
  112. f.write(line)
  113. def load(self):
  114. """
  115. Load last backup line of solution and set algorithm state (best solution and evaluations) at this backup
  116. """
  117. if os.path.exists(self._filepath):
  118. logging.info('Load best solution from last checkpoint')
  119. with open(self._filepath) as f:
  120. # get last line and read data
  121. lastline = f.readlines()[-1]
  122. data = lastline.split(';')
  123. # get evaluation information
  124. globalEvaluation = int(data[0])
  125. if self.algo.getParent() is not None:
  126. self.algo.getParent().setEvaluation(globalEvaluation)
  127. else:
  128. self.algo.setEvaluation(globalEvaluation)
  129. # get best solution data information
  130. solution_data = list(map(float, data[1].split(' ')))
  131. if self.algo.result is None:
  132. self.algo.result = self.algo.initialiser()
  133. self.algo.result.data = np.array(solution_data)
  134. self.algo.result.fitness = float(data[2])
  135. macop_line(self.algo)
  136. macop_text(self.algo,
  137. f'Checkpoint found from `{self._filepath}` file.')
  138. macop_text(
  139. self.algo,
  140. f'Restart algorithm from evaluation {self.algo.getEvaluation()}.'
  141. )
  142. else:
  143. macop_text(
  144. self.algo,
  145. 'No backup found... Start running algorithm from evaluation 0.'
  146. )
  147. logging.info(
  148. "Can't load backup... Backup filepath not valid in Checkpoint")
  149. macop_line(self.algo)