Example #1
0
 def run(self):
     for i in range(0, self.iters):
         coverage = rnd.random()
         gol = GameOflife(50, 50, coverage)
         cycle_detected_flag = False
         grids_saved = {} 
         iterations = 0
         initial_grid_state = str(gol.get_as_tuple())
         living_cells_count = gol.get_living_count()
         all_dead = False
         
         while not cycle_detected_flag and iterations < self.MAX_CYCLES:
             current_grid_state = hash(gol.get_as_tuple())
             if current_grid_state not in grids_saved:
                 grids_saved[current_grid_state] = iterations
                 iterations += 1
             else:
                 cycle_detected_flag = True
                 final_living_cells_count = gol.get_living_count()
                 if final_living_cells_count == 0:
                     all_dead = True
                 entry = Dataset(percent_fill = coverage,
                                 intial_fill = living_cells_count, 
                                 final_fill = final_living_cells_count,
                                 is_empty = all_dead,
                                 generations = iterations,
                                 generation_loop = grids_saved[current_grid_state],
                                 loop_length = iterations - grids_saved[current_grid_state],
                                 initial_generation = initial_grid_state)
                 self.db_session.add(entry)
                 self.db_session.commit()
             gol.run_rules()
         print(self.thread_name, i)