Callback.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. """Abstract Checkpoint class
  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