def train(self, no_scenarios, print_log, plot_graphs, save_graphs, collect_comparison=True): # Stats is the Dictionary of this object which has various useful fields mentioned below stats = { 'scenarios': [], 'rewards': [], 'durations': [], 'detected': [], 'missed': [], 'ttf': [], 'napfd': [], 'recall': [], 'avg_precision': [], 'result': [], 'step': [], 'env': self.scenario_provider.name, 'agent': self.agent.name, # 'action_size': self.agent.action_size, 'history_length': self.agent.histlen, 'rewardfun': self.reward_function.__name__, 'sched_time': self.scenario_provider.avail_time_ratio, 'hidden_size': 'x'.join(str(x) for x in self.agent.hidden_size) if hasattr( self.agent, 'hidden_size') else 0 } if collect_comparison: cmp_agents = { 'heur_sort': agents.HeuristicSortAgent(self.agent.histlen), 'heur_weight': agents.HeuristicWeightAgent(self.agent.histlen), 'heur_random': agents.RandomAgent(self.agent.histlen) } stats['comparison'] = {} # stats['comparison']['heur_sort/heur_weight/rand'] initialized for comparison for key in cmp_agents.keys(): stats['comparison'][key] = { 'detected': [], 'missed': [], 'ttf': [], 'napfd': [], 'recall': [], 'avg_precision': [], 'durations': [] } sum_actions = 0 sum_scenarios = 0 sum_detected = 0 sum_missed = 0 sum_reward = 0 # Enumerate forms a tuple of (count,Element) # write_file.write("Agent is "+str(self.agent)) for (i, sc) in enumerate(self.scenario_provider, start=1): if i > no_scenarios: break start = time.time() if print_log: print('ep %d:\tscenario %s\t' % (sum_scenarios + 1, sc.name), end='') (result, reward) = self.process_scenario(sc) end = time.time() # Statistics of the CI cycle after Prioritization and Selection of test cases from the test suite sum_detected += result[0] sum_missed += result[1] # In future if we want to include the priority of the test cases also, np.average() could be used, where weights can be adjusted depending # upon the priority of the test cases sum_reward += np.mean(reward) sum_actions += 1 sum_scenarios += 1 duration = end - start stats['scenarios'].append(sc.name) stats['rewards'].append(np.mean(reward)) stats['durations'].append(duration) stats['detected'].append(result[0]) stats['missed'].append(result[1]) ## TTF is the time to failure or in simple words, it is the position at which the first test case that failed was placed by our algorithm stats['ttf'].append(result[2]) stats['napfd'].append(result[3]) stats['recall'].append(result[4]) stats['avg_precision'].append(result[5]) stats['result'].append(result) stats['step'].append(sum_scenarios) if print_log: print( ' finished, reward: %.2f,\trunning mean: %.4f,\tduration: %.1f,\tresult: %s' % (np.mean(reward), sum_reward / sum_scenarios, duration, result)) global total_failures_detected global total_failures_missed total_failures_detected += result[0] total_failures_missed += result[1] # Collect Comparison becomes True if we set args.comparable as True ## Formulates the results of the heur_sort, heur_random and heur_weight . if collect_comparison: for key in stats['comparison'].keys(): start = time.time() cmp_res = process_scenario(cmp_agents[key], sc, preprocess_discrete) end = time.time() stats['comparison'][key]['detected'].append(cmp_res[0]) stats['comparison'][key]['missed'].append(cmp_res[1]) stats['comparison'][key]['ttf'].append(cmp_res[2]) stats['comparison'][key]['napfd'].append(cmp_res[3]) stats['comparison'][key]['recall'].append(cmp_res[4]) stats['comparison'][key]['avg_precision'].append( cmp_res[5]) stats['comparison'][key]['durations'].append(end - start) # # Data Dumping ## The below two commented lines of code write the stats into the file after certain interval. No specific need of this, because anyways, we are writing the entire stats ## at the end of the program # if self.dump_interval > 0 and sum_scenarios % self.dump_interval == 0: # pickle.dump(stats, open(self.stats_file + '.p', 'wb')) if self.validation_interval > 0 and ( sum_scenarios == 1 or sum_scenarios % self.validation_interval == 0): if print_log: print('ep %d:\tRun test... ' % sum_scenarios, end='') self.run_validation(sum_scenarios) pickle.dump(self.validation_res, open(self.val_file + '.p', 'wb')) if print_log: print('done') ## Dumping the Stats of all the CI Cycles into the Stats_File if self.dump_interval > 0: self.agent.save(self.agent_file) pickle.dump(stats, open(self.stats_file + '.p', 'wb')) ## Plotting Graphs if plot_graphs: plot_stats.plot_stats_single_figure(self.file_prefix, self.stats_file + '.p', self.val_file + '.p', 1, plot_graphs=plot_graphs, save_graphs=save_graphs) ## Save the generated Graphs if save_graphs: plot_stats.plot_stats_separate_figures(self.file_prefix, self.stats_file + '.p', self.val_file + '.p', 1, plot_graphs=False, save_graphs=save_graphs) return np.mean(stats['napfd']), np.mean(stats['recall'])
def train(self, no_scenarios, print_log, plot_graphs, save_graphs, collect_comparison=False): stats = { 'scenarios': [], 'rewards': [], 'durations': [], 'detected': [], 'missed': [], 'ttf': [], 'napfd': [], 'recall': [], 'avg_precision': [], 'result': [], 'step': [], 'env': self.scenario_provider.name, 'agent': self.agent.name, 'action_size': self.agent.action_size, 'history_length': self.agent.histlen, 'rewardfun': self.reward_function.__name__, 'sched_time': self.scenario_provider.avail_time_ratio, 'hidden_size': 'x'.join(str(x) for x in self.agent.hidden_size) if hasattr(self.agent, 'hidden_size') else 0 } if collect_comparison: cmp_agents = { 'heur_sort': agents.HeuristicSortAgent(self.agent.histlen), 'heur_weight': agents.HeuristicWeightAgent(self.agent.histlen), 'heur_random': agents.RandomAgent(self.agent.histlen) } stats['comparison'] = {} for key in cmp_agents.keys(): stats['comparison'][key] = { 'detected': [], 'missed': [], 'ttf': [], 'napfd': [], 'recall': [], 'avg_precision': [], 'durations': [] } sum_actions = 0 sum_scenarios = 0 sum_detected = 0 sum_missed = 0 sum_reward = 0 for (i, sc) in enumerate(self.scenario_provider, start=1): if i > no_scenarios: break start = time.time() if print_log: print('ep %d:\tscenario %s\t' % (sum_scenarios + 1, sc.name), end='') (result, reward) = self.process_scenario(sc) end = time.time() # Statistics sum_detected += result[0] sum_missed += result[1] sum_reward += np.mean(reward) sum_actions += 1 sum_scenarios += 1 duration = end - start stats['scenarios'].append(sc.name) stats['rewards'].append(np.mean(reward)) stats['durations'].append(duration) stats['detected'].append(result[0]) stats['missed'].append(result[1]) stats['ttf'].append(result[2]) stats['napfd'].append(result[3]) stats['recall'].append(result[4]) stats['avg_precision'].append(result[5]) stats['result'].append(result) stats['step'].append(sum_scenarios) if print_log: print(' finished, reward: %.2f,\trunning mean: %.4f,\tduration: %.1f,\tresult: %s' % (np.mean(reward), sum_reward / sum_scenarios, duration, result)) if collect_comparison: for key in stats['comparison'].keys(): start = time.time() cmp_res = process_scenario(cmp_agents[key], sc, preprocess_discrete) end = time.time() stats['comparison'][key]['detected'].append(cmp_res[0]) stats['comparison'][key]['missed'].append(cmp_res[1]) stats['comparison'][key]['ttf'].append(cmp_res[2]) stats['comparison'][key]['napfd'].append(cmp_res[3]) stats['comparison'][key]['recall'].append(cmp_res[4]) stats['comparison'][key]['avg_precision'].append(cmp_res[5]) stats['comparison'][key]['durations'].append(end - start) # Data Dumping if self.dump_interval > 0 and sum_scenarios % self.dump_interval == 0: pickle.dump(stats, open(self.stats_file + '.p', 'wb')) if self.validation_interval > 0 and (sum_scenarios == 1 or sum_scenarios % self.validation_interval == 0): if print_log: print('ep %d:\tRun test... ' % sum_scenarios, end='') self.run_validation(sum_scenarios) pickle.dump(self.validation_res, open(self.val_file + '.p', 'wb')) if print_log: print('done') if self.dump_interval > 0: self.agent.save(self.agent_file) pickle.dump(stats, open(self.stats_file + '.p', 'wb')) if plot_graphs: plot_stats.plot_stats_single_figure(self.file_prefix, self.stats_file + '.p', self.val_file + '.p', 1, plot_graphs=plot_graphs, save_graphs=save_graphs) if save_graphs: plot_stats.plot_stats_separate_figures(self.file_prefix, self.stats_file + '.p', self.val_file + '.p', 1, plot_graphs=False, save_graphs=save_graphs) return np.mean(stats['napfd'])