MultiCheckpoint.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. """Multi Checkpoint class implementation
  2. """
  3. # main imports
  4. import os
  5. import logging
  6. import numpy as np
  7. # module imports
  8. from .Callback import Callback
  9. from ..utils.color 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.parent is not None:
  56. self.algo.parent.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. print(macop_line())
  69. print(
  70. macop_text('Load of available population from `{}`'.format(
  71. self.filepath)))
  72. print(
  73. macop_text('Restart algorithm from evaluation {}.'.format(
  74. self.algo.numberOfEvaluations)))
  75. else:
  76. print(
  77. macop_text(
  78. 'No backup found... Start running algorithm from evaluation 0.'
  79. ))
  80. logging.info(
  81. "Can't load backup... Backup filepath not valid in Checkpoint")
  82. print(macop_line())