Changeset 1127
- Timestamp:
- 04/11/21 03:15:42 (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
framspy/evolalg/experiment.py
r1113 r1127 1 import os 1 2 from typing import List, Callable, Union 2 3 3 4 from evolalg.base.step import Step 4 5 import pickle 6 import time 5 7 6 8 from evolalg.base.union_step import UnionStep … … 19 21 20 22 self.init_population = init_population 23 self.running_time = 0 21 24 self.step = StableGeneration( 22 25 selection=selection, … … 47 50 def run(self, num_generations): 48 51 for i in range(self.generation + 1, num_generations + 1): 52 start_time = time.time() 49 53 self.generation = i 50 54 self.population = self.step(self.population) 51 55 self.population = self.generation_modification(self.population) 52 56 57 self.running_time += time.time() - start_time 53 58 if (self.checkpoint_path is not None 54 59 and self.checkpoint_interval is not None 55 60 and i % self.checkpoint_interval == 0): 56 with open(self.checkpoint_path, "wb") as file: 57 pickle.dump(self, file) 61 self.save_checkpoint() 58 62 59 63 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 60 74 61 75 @staticmethod
Note: See TracChangeset
for help on using the changeset viewer.