base.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. """Abstract Checkpoint classes for callback process
  2. """
  3. # main imports
  4. import os
  5. import logging
  6. from abc import abstractmethod
  7. class Callback():
  8. """
  9. Callback abstract class in order to compute some instruction every evaluation
  10. Attributes:
  11. algo: {Algorithm} -- main algorithm instance reference
  12. every: {int} -- checkpoint frequency used (based on number of evaluations)
  13. filepath: {str} -- file path where checkpoints will be saved
  14. """
  15. def __init__(self, every, filepath):
  16. self._algo = None
  17. self._every = every
  18. self._filepath = filepath
  19. # build path if not already exists
  20. head, _ = os.path.split(self._filepath)
  21. if not os.path.exists(head):
  22. os.makedirs(head)
  23. def setAlgo(self, algo):
  24. """Specify the main algorithm instance reference
  25. Args:
  26. algo: {Algorithm} -- main algorithm instance reference
  27. """
  28. self._algo = algo
  29. @abstractmethod
  30. def run(self):
  31. """
  32. Check if necessary to do backup based on `every` variable
  33. """
  34. pass
  35. @abstractmethod
  36. def load(self):
  37. """
  38. Load last backup line of solution and set algorithm state at this backup
  39. """
  40. pass