Changeset 1127 for framspy


Ignore:
Timestamp:
04/11/21 03:15:42 (4 years ago)
Author:
Maciej Komosinski
Message:

Experiment: measure running time and save checkpoints safely

File:
1 edited

Legend:

Unmodified
Added
Removed
  • framspy/evolalg/experiment.py

    r1113 r1127  
     1import os
    12from typing import List, Callable, Union
    23
    34from evolalg.base.step import Step
    45import pickle
     6import time
    57
    68from evolalg.base.union_step import UnionStep
     
    1921
    2022        self.init_population = init_population
     23        self.running_time = 0
    2124        self.step = StableGeneration(
    2225            selection=selection,
     
    4750    def run(self, num_generations):
    4851        for i in range(self.generation + 1, num_generations + 1):
     52            start_time = time.time()
    4953            self.generation = i
    5054            self.population = self.step(self.population)
    5155            self.population = self.generation_modification(self.population)
    5256
     57            self.running_time += time.time() - start_time
    5358            if (self.checkpoint_path is not None
    5459                    and self.checkpoint_interval is not None
    5560                    and i % self.checkpoint_interval == 0):
    56                 with open(self.checkpoint_path, "wb") as file:
    57                     pickle.dump(self, file)
     61                self.save_checkpoint()
    5862
    5963        self.population = self.end_steps(self.population)
     64
     65    def save_checkpoint(self):
     66        tmp_filepath = self.checkpoint_path+"_tmp"
     67        try:
     68            with open(tmp_filepath, "wb") as file:
     69                pickle.dump(self, file)
     70            os.replace(tmp_filepath, self.checkpoint_path)  # ensures the new file was first saved OK (e.g. enough free space on device), then replace
     71        except Exception as ex:
     72            raise RuntimeError("Failed to save checkpoint '%s' (because: %s). This does not prevent the experiment from continuing, but let's stop here to fix the problem with saving checkpoints." % (tmp_filepath, ex))
     73
    6074
    6175    @staticmethod
Note: See TracChangeset for help on using the changeset viewer.