base.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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: {:class:`~macop.algorithms.base.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. """Callback abstract initialiser
  17. Args:
  18. every: {int} -- checkpoint frequency used (based on number of evaluations)
  19. filepath: {str} -- file path where checkpoints will be saved
  20. """
  21. self.algo = None
  22. self._every = every
  23. self._filepath = filepath
  24. # build path if not already exists
  25. head, _ = os.path.split(self._filepath)
  26. if not os.path.exists(head):
  27. os.makedirs(head)
  28. def setAlgo(self, algo):
  29. """Specify the main algorithm instance reference
  30. Args:
  31. algo: {:class:`~macop.algorithms.base.Algorithm`} -- main algorithm instance reference
  32. """
  33. self.algo = algo
  34. @abstractmethod
  35. def run(self):
  36. """
  37. Check if necessary to do backup based on `every` variable
  38. """
  39. pass
  40. @abstractmethod
  41. def load(self):
  42. """
  43. Load last backup line of solution and set algorithm state at this backup
  44. """
  45. pass