def test_score_with_particular_players(self): """ These are players that are known to be difficult to pickle """ name = "score" turns = 10 noise = 0 repetitions = 5 num_states = 2 opponents = [axl.ThueMorse(), axl.MetaHunter(), axl.BackStabber(), axl.Alexei()] size = 10 objective = dojo.prepare_objective(name=name, turns=turns, noise=noise, repetitions=repetitions) population = dojo.Population(player_class=player_class, params_kwargs={"num_states": num_states}, size=size, objective=objective, output_filename=self.temporary_file.name, opponents=opponents, bottleneck=2, mutation_probability = .01, processes=0) generations = 4 axl.seed(0) population.run(generations, print_output=False) self.assertEqual(population.generation, 4)
def test_scores(self): temporary_file = tempfile.NamedTemporaryFile() params_kwargs = {"parameters": (1, 1, 2)} population = dojo.Population(player_class=axl.EvolvableGambler, params_kwargs=params_kwargs, size=self.size, objective=self.objective, output_filename=temporary_file.name, opponents=self.opponents, bottleneck=2, mutation_probability=.01, processes=1) generations = 10 axl.seed(1) population.run(generations, print_output=False) results = [] with open(temporary_file.name, "r") as f: reader = csv.reader(f) for row in reader: results.append(row) self.assertTrue(all([0 <= float(result[3]) <= 5 for result in results]))
def test_population_init_with_given_rate(self): name = "score" turns = 10 noise = 0 repetitions = 5 num_states = 2 opponents = [s() for s in axl.demo_strategies] size = 10 objective = dojo.prepare_objective(name=name, turns=turns, noise=noise, repetitions=repetitions) population = dojo.Population(params_class=dojo.FSMParams, params_kwargs={ "num_states": num_states, "mutation_probability": .5 }, size=size, objective=objective, output_filename=self.temporary_file.name, opponents=opponents, bottleneck=2, mutation_probability=.01, processes=1) for p in population.population: self.assertEqual(p.mutation_probability, .5) generations = 1 axl.seed(0) population.run(generations, print_output=False) self.assertEqual(population.generation, 1)
def test_basic_pop_size(self): # Set up Tmp file temp_file = tempfile.NamedTemporaryFile() # we will set the objective to be cycler_objective = axl_dojo.prepare_objective(name="score", turns=10, repetitions=1) # Lets use an opponent_list of just one: opponent_list = [axl.TitForTat()] # params to pass through cycler_kwargs = { "sequence_length": 10 } population_size = 20 population = axl_dojo.Population(params_class=axl_dojo.CyclerParams, params_kwargs=cycler_kwargs, size=population_size, objective=cycler_objective, output_filename=temp_file.name, opponents=opponent_list) # Before run self.assertEqual(len(population.population), population_size) # After Run population.run(generations=5, print_output=False) self.assertEqual(len(population.population), population_size) # close the temp file temp_file.close()
def test_default_single_opponent_e2e(self): temp_file = tempfile.NamedTemporaryFile() # we will set the objective to be cycler_objective = axl_dojo.prepare_objective(name="score", turns=10, repetitions=1) # Lets use an opponent_list of just one: opponent_list = [axl.TitForTat(), axl.Calculator()] cycler = axl.EvolvableCycler # params to pass through cycler_kwargs = {"cycle_length": 10} # assert file is empty to start self.assertEqual(temp_file.readline(), b'') # note that .readline() reads bytes hence b'' population = axl_dojo.Population(player_class=cycler, params_kwargs=cycler_kwargs, size=20, objective=cycler_objective, output_filename=temp_file.name, opponents=opponent_list) generations = 5 population.run(generations, print_output=False) # assert the output file exists and is not empty self.assertTrue(os.path.exists(temp_file.name)) self.assertNotEqual(temp_file.readline(), b'') # note that .readline() reads bytes hence b'' # close the temp file temp_file.close()
def test_score_with_particular_players(self): name = "score" turns = 10 noise = 0 repetitions = 5 num_states = 2 opponents = [s() for s in axl.basic_strategies] size = 10 objective = dojo.prepare_objective(name=name, turns=turns, noise=noise, repetitions=repetitions) population = dojo.Population(params_class=dojo.HMMParams, params_kwargs={"num_states": num_states}, size=size, objective=objective, output_filename=self.temporary_file.name, opponents=opponents, bottleneck=2, mutation_probability=.01, processes=0) generations = 4 axl.seed(0) population.run(generations) self.assertEqual(population.generation, 4)
def test_score_with_sample_count_and_weights(self): name = "score" turns = 10 noise = 0 repetitions = 5 num_states = 2 opponents = [s() for s in axl.demo_strategies] size = 10 objective = dojo.prepare_objective(name=name, turns=turns, noise=noise, repetitions=repetitions) population = dojo.Population( params_class=dojo.FSMParams, params_kwargs={"num_states": num_states}, size=size, objective=objective, output_filename=self.temporary_file.name, opponents=opponents, sample_count=2, # Randomly sample 2 opponents at each step weights=[5, 1, 1, 1, 1], bottleneck=2, mutation_probability=.01, processes=1) generations = 4 axl.seed(0) population.run(generations, print_output=False) self.assertEqual(population.generation, 4) # Manually read from tempo file to find best strategy best_score, best_params = 0, None with open(self.temporary_file.name, "r") as f: reader = csv.reader(f) for row in reader: _, mean_score, sd_score, max_score, arg_max = row if float(max_score) > best_score: best_score = float(max_score) best_params = arg_max # Test the load params function for num in range(1, 4 + 1): best = dojo.load_params(params_class=dojo.FSMParams, filename=self.temporary_file.name, num=num) self.assertEqual(len(best), num) for parameters in best: self.assertIsInstance(parameters, dojo.FSMParams) self.assertEqual(best[0].__repr__(), best_params)
def runGeneticAlgo(opponent, population_size=150, number_of_game_turns=200, cycle_length=200, generations=250, mutation_probability=0.1, mutation_potency=1, reset_file=True): cycler_class = axl_dojo.CyclerParams cycler_objective = axl_dojo.prepare_objective(name="score", turns=number_of_game_turns, repetitions=1) cycler_kwargs = { "sequence_length": cycle_length, "mutation_probability": mutation_probability, "mutation_potency": mutation_potency } output_file_name = "data/" + str(opponent).replace(" ", "_") + ".csv" try: if reset_file and os.path.isfile(output_file_name): os.remove(output_file_name) finally: print(str(opponent), "|| pop size:", population_size, "\tturns:", number_of_game_turns, "\tcycle len:", cycle_length, "\tgens:", generations, "\tmut. rate:", mutation_probability, "\t, potency:", mutation_potency) axl.seed(1) population = axl_dojo.Population(params_class=cycler_class, params_kwargs=cycler_kwargs, size=population_size, objective=cycler_objective, output_filename=output_file_name, opponents=[opponent], print_output=False) population.run(generations) print("\tAnalysis Complete:", output_file_name) # Store the file name and opponent name as a tuple return output_file_name, str(opponent)
def start(self): print("-------- RUNNING ANALYSIS --------") print("SEQUENCE_LENGTH:", self.sequence_length) print("POPULATION_SIZE:", self.population_size) print("GENERATION_LENGTH:", self.generation_length) print("MUTATION_FREQUENCY:", self.mutation_frequency) print("MUTATION_POTENCY:", self.mutation_potency) print() print("Save directory is ", "\'" + self.save_directory + "\'") print("Global seed is set to", self.global_seed) if self.save_file_prefix == "": print("No file prefix was given") if self.save_file_suffix == "": print("No file suffix was given") print("--------------------------") print() print("There are", len(self.opponent_list), "opponents to analyse") if not os.path.exists(self.save_directory): os.makedirs(self.save_directory) if self.overwrite_files: print("Overwriting files during run") cycler_objective = axl_dojo.prepare_objective( name="score", turns=self.sequence_length, repetitions=1) cycler_kwargs = { "sequence_length": self.sequence_length, "mutation_probability": self.mutation_frequency, "mutation_potency": self.mutation_potency } i = 1 print("Starting analysis...") print() for opponent in self.opponent_list: print(i, "of", len(self.opponent_list), "| Analysing player:", str(opponent), "...") if self.overwrite_files: opponent_file = self._get_file_name(opponent) try: os.remove(opponent_file) print("\tremoved file: " + opponent_file) except FileNotFoundError: print("\tcould not remove file: " + opponent_file) population = axl_dojo.Population( params_class=axl_dojo.CyclerParams, params_kwargs=cycler_kwargs, size=self.population_size, processes=1, population=self.get_pre_made_pop(self.population_size), objective=cycler_objective, output_filename=self._get_file_name(opponent), opponents=[opponent]) population.run(self.generation_length, print_output=False) print( "{:.2f}% Done.\tSaved to:".format( (100 * i) / len(self.opponent_list)), self._get_file_name(opponent)) self.output_files[str(opponent)] = self._get_file_name(opponent) i += 1
def test_score(self): name = "score" turns = 10 noise = 0 repetitions = 5 num_states = 2 opponents = [s() for s in axl.demo_strategies] size = 10 objective = dojo.prepare_objective(name=name, turns=turns, noise=noise, repetitions=repetitions) population = dojo.Population(player_class=player_class, params_kwargs={"num_states": num_states}, size=size, objective=objective, output_filename=self.temporary_file.name, opponents=opponents, bottleneck=2, mutation_probability = .01, processes=1) generations = 4 axl.seed(0) population.run(generations, print_output=False) self.assertEqual(population.generation, 4) # Manually read from tempo file to find best strategy best_score, best_params = 0, None with open(self.temporary_file.name, "r") as f: reader = csv.reader(f) for row in reader: _, mean_score, sd_score, max_score, arg_max = row if float(max_score) > best_score: best_score = float(max_score) best_params = arg_max # Test the load params function for num in range(1, 4 + 1): best = dojo.load_params(player_class=player_class, filename=self.temporary_file.name, num=num) self.assertEqual(len(best), num) # Test that can use these loaded params in a new algorithm instance population = dojo.Population(player_class=player_class, params_kwargs={"num_states": num_states}, size=size, objective=objective, output_filename=self.temporary_file.name, opponents=opponents, population=best, bottleneck=2, mutation_probability = .01, processes=1) generations = 4 axl.seed(0) population.run(generations, print_output=False) self.assertEqual(population.generation, 4)
def test_pso_to_ea(self): name = "score" turns = 10 noise = 0 repetitions = 2 num_states = 3 opponents = [s() for s in axl.demo_strategies] size = 30 generations = 3 objective = dojo.prepare_objective(name=name, turns=turns, noise=noise, repetitions=repetitions) winners = [] for _ in range(5): axl.seed(_) pso = dojo.PSO(dojo.HMMParams, params_kwargs={"num_states": num_states}, objective=objective, opponents=opponents, population=size, generations=generations) xopt, fopt = pso.swarm() # You can put the optimal vector back into a HMM. hmm_opt = dojo.HMMParams(num_states=num_states) hmm_opt.receive_vector(xopt) winners.append(hmm_opt) # Put the winners of the PSO into an EA. population = dojo.Population(params_class=dojo.HMMParams, params_kwargs={"num_states": num_states}, size=size, objective=objective, output_filename=self.temporary_file.name, opponents=opponents, population=winners, bottleneck=2, mutation_probability=.01, processes=1) axl.seed(0) population.run(generations) # Block resource (?) with open(self.temporary_file.name, "w") as f: pass scores = population.score_all() record, record_holder = 0, -1 for i, s in enumerate(scores): if s >= record: record = s record_holder = i xopt, fopt = population.population[record_holder], record print(xopt)