Beispiel #1
0
    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)
Beispiel #2
0
    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, print_output=False)
        self.assertEqual(population.generation, 4)
Beispiel #3
0
    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_pso_with_gambler(self):
        name = "score"
        turns = 50
        noise = 0
        repetitions = 5
        num_plays = 1
        num_op_plays = 1
        num_op_start_plays = 1
        params_kwargs = {"plays": num_plays,
                         "op_plays": num_op_plays,
                         "op_start_plays": num_op_start_plays}
        population = 10
        generations = 100
        opponents = [axl.Cooperator() for _ in range(5)]

        objective = dojo.prepare_objective(name=name,
                                           turns=turns,
                                           noise=noise,
                                           repetitions=repetitions)

        pso = dojo.PSO(dojo.GamblerParams, params_kwargs, objective=objective, debug=False,
                  opponents=opponents, population=population, generations=generations)

        axl.seed(0)
        opt_vector, opt_objective_value = pso.swarm()

        self.assertTrue(np.allclose(opt_vector, np.array([[0. , 0. , 0.36158016,
                                                           0.35863128, 0. , 1.,
                                                           0.72670793, 0.67951873]])))
        self.assertEqual(abs(opt_objective_value), 4.96)
    def test_init(self):
        params = [2, 1, 1]
        objective = prepare_objective('score', 2, 0, 1, nmoran=False)
        opponents = [axl.Defector(), axl.Cooperator()]
        population = 2
        generations = 10
        debug = False
        phip = 0.6
        phig = 0.6
        omega = 0.6

        pso = PSO(GamblerParams, params, objective=objective,
                  opponents=opponents, population=population,
                  generations=generations, debug=debug, phip=phip, phig=phig,
                  omega=omega)

        self.assertIsInstance(pso.objective, functools.partial)
        self.assertEqual(len(pso.opponents_information), len(opponents))
        self.assertEqual(pso.population, population)
        self.assertEqual(pso.generations, generations)
        self.assertFalse(pso.debug)
        self.assertEqual(pso.phip, phip)
        self.assertEqual(pso.phig, phig)
        self.assertEqual(pso.omega, omega)
        self.assertEqual(pso.processes, 1)
Beispiel #6
0
    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)
Beispiel #7
0
    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_init(self):
        params = [2, 1, 1]
        objective = prepare_objective('score', 2, 0, 1, nmoran=False)
        opponents = [axl.Defector(), axl.Cooperator()]
        population = 2
        generations = 10
        debug = False
        phip = 0.6
        phig = 0.6
        omega = 0.6

        pso = PSO(GamblerParams,
                  params,
                  objective=objective,
                  opponents=opponents,
                  population=population,
                  generations=generations,
                  debug=debug,
                  phip=phip,
                  phig=phig,
                  omega=omega)

        self.assertIsInstance(pso.objective, functools.partial)
        self.assertEqual(len(pso.opponents_information), len(opponents))
        self.assertEqual(pso.population, population)
        self.assertEqual(pso.generations, generations)
        self.assertFalse(pso.debug)
        self.assertEqual(pso.phip, phip)
        self.assertEqual(pso.phig, phig)
        self.assertEqual(pso.omega, omega)
        self.assertEqual(pso.processes, 1)
Beispiel #9
0
    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_pso_with_fsm(self):
        name = "score"
        turns = 10
        noise = 0
        repetitions = 5
        num_states = 4
        params_kwargs = {"num_states": num_states}
        population = 10
        generations = 100
        opponents = [axl.Defector() for _ in range(5)]

        objective = dojo.prepare_objective(name=name,
                                           turns=turns,
                                           noise=noise,
                                           repetitions=repetitions)

        pso = PSO(FSMParams, params_kwargs, objective=objective, debug=False,
                  opponents=opponents, population=population, generations=generations)

        axl.seed(0)
        opt_vector, opt_objective_value = pso.swarm()

        self.assertTrue(np.allclose(opt_vector, np.array([0.0187898, 0.6176355,
                                                          0.61209572, 0.616934,
                                                          0.94374808, 0.6818203,
                                                          0.3595079, 0.43703195,
                                                          0.6976312, 0.06022547,
                                                          0.66676672, 0.67063787,
                                                          0.21038256, 0.1289263,
                                                          0.31542835, 0.36371077,
                                                          0.57019677])))
        self.assertEqual(abs(opt_objective_value), 1)
    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_dojo.CyclerParams

        # params to pass through
        cycler_kwargs = {
            "sequence_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(params_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()
Beispiel #12
0
    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)
Beispiel #13
0
    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(params_class=dojo.FSMParams,
                                     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)
Beispiel #14
0
    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)
Beispiel #15
0
    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 test_init_default(self):
        params = [1, 1, 1]
        objective = prepare_objective('score', 2, 0, 1, nmoran=False)

        pso = PSO(GamblerParams, params, objective=objective)

        self.assertIsInstance(pso.objective, functools.partial)
        self.assertEqual(len(pso.opponents_information), len(axl.short_run_time_strategies))
        self.assertEqual(pso.population, 1)
        self.assertEqual(pso.generations, 1)
        self.assertTrue(pso.debug)
        self.assertEqual(pso.phip, 0.8)
        self.assertEqual(pso.phig, 0.8)
        self.assertEqual(pso.omega, 0.8)
        self.assertEqual(pso.processes, 1)
    def test_init_default(self):
        params = [1, 1, 1]
        objective = prepare_objective('score', 2, 0, 1, nmoran=False)

        pso = PSO(GamblerParams, params, objective=objective)

        self.assertIsInstance(pso.objective, functools.partial)
        self.assertEqual(len(pso.opponents_information),
                         len(axl.short_run_time_strategies))
        self.assertEqual(pso.population, 1)
        self.assertEqual(pso.generations, 1)
        self.assertTrue(pso.debug)
        self.assertEqual(pso.phip, 0.8)
        self.assertEqual(pso.phig, 0.8)
        self.assertEqual(pso.omega, 0.8)
        self.assertEqual(pso.processes, 1)
    def test_pso_with_gambler(self):
        name = "score"
        turns = 10
        noise = 0
        repetitions = 5
        num_plays = 1
        num_op_plays = 1
        num_op_start_plays = 1
        params_kwargs = {
            "plays": num_plays,
            "op_plays": num_op_plays,
            "op_start_plays": num_op_start_plays
        }
        population = 10
        generations = 100
        opponents = [axl.Cooperator() for _ in range(5)]

        objective = dojo.prepare_objective(name=name,
                                           turns=turns,
                                           noise=noise,
                                           repetitions=repetitions)

        pso = PSO(GamblerParams,
                  params_kwargs,
                  objective=objective,
                  debug=False,
                  opponents=opponents,
                  population=population,
                  generations=generations)

        axl.seed(0)
        opt_vector, opt_objective_value = pso.swarm()

        self.assertTrue(
            np.allclose(
                opt_vector,
                np.array([
                    0.5488135, 0.71518937, 0.60276338, 0.54488318, 0.4236548,
                    0.64589411, 0.43758721, 0.891773
                ])))
        self.assertEqual(abs(opt_objective_value), 3)
Beispiel #19
0
    def test_pso_with_gambler(self):
        name = "score"
        turns = 50
        noise = 0
        repetitions = 5
        num_plays = 1
        num_op_plays = 1
        num_op_start_plays = 1
        params_kwargs = {
            "parameters": (num_plays, num_op_plays, num_op_start_plays)
        }
        population = 10
        generations = 100
        opponents = [axl.Cooperator() for _ in range(5)]

        objective = dojo.prepare_objective(name=name,
                                           turns=turns,
                                           noise=noise,
                                           repetitions=repetitions)

        pso = dojo.PSO(EvolvableGambler,
                       params_kwargs,
                       objective=objective,
                       debug=False,
                       opponents=opponents,
                       population=population,
                       generations=generations)

        axl.seed(0)
        opt_vector, opt_objective_value = pso.swarm()

        self.assertTrue(
            np.allclose(
                opt_vector,
                np.array([[
                    0., 0.89327795, 0.04140453, 0.73676965, 0., 0.20622436, 1.,
                    0.68104353
                ]])))
        self.assertEqual(abs(opt_objective_value), 3.56)
    def test_pso_with_fsm(self):
        name = "score"
        turns = 10
        noise = 0
        repetitions = 5
        num_states = 4
        params_kwargs = {"num_states": num_states}
        population = 10
        generations = 100
        opponents = [axl.Defector() for _ in range(5)]

        objective = dojo.prepare_objective(name=name,
                                           turns=turns,
                                           noise=noise,
                                           repetitions=repetitions)

        pso = PSO(FSMParams,
                  params_kwargs,
                  objective=objective,
                  debug=False,
                  opponents=opponents,
                  population=population,
                  generations=generations)

        axl.seed(0)
        opt_vector, opt_objective_value = pso.swarm()

        self.assertTrue(
            np.allclose(
                opt_vector,
                np.array([
                    0.0187898, 0.6176355, 0.61209572, 0.616934, 0.94374808,
                    0.6818203, 0.3595079, 0.43703195, 0.6976312, 0.06022547,
                    0.66676672, 0.67063787, 0.21038256, 0.1289263, 0.31542835,
                    0.36371077, 0.57019677
                ])))
        self.assertEqual(abs(opt_objective_value), 1)
Beispiel #21
0
    def test_score_pso(self):
        name = "score"
        turns = 5
        noise = 0
        repetitions = 2
        num_states = 4
        opponents = [s() for s in axl.demo_strategies]
        size = 30
        generations = 5

        objective = dojo.prepare_objective(name=name,
                                           turns=turns,
                                           noise=noise,
                                           repetitions=repetitions)

        pso = dojo.PSO(dojo.HMMParams,
                       params_kwargs={"num_states": num_states},
                       objective=objective,
                       opponents=opponents,
                       population=size,
                       generations=generations)

        axl.seed(0)
        xopt, fopt = pso.swarm()

        self.assertTrue(len(xopt) == 2 * num_states**2 + num_states + 1)

        # You can put the optimal vector back into a HMM.
        simple_hmm_opt = dojo.HMMParams(num_states=num_states)
        simple_hmm_opt.receive_vector(xopt)
        simple_player = simple_hmm_opt.player()
        self.assertTrue(simple_player.hmm.is_well_formed()
                        )  # This should get asserted in initialization anyway

        self.assertIsInstance(simple_hmm_opt, dojo.HMMParams)
        print(xopt)  # As a vector still
        print(simple_hmm_opt)  # As a HMM
Beispiel #22
0
    def test_pso_with_fsm(self):
        name = "score"
        turns = 10
        noise = 0
        repetitions = 5
        num_states = 4
        params_kwargs = {"num_states": num_states}
        population = 10
        generations = 100
        opponents = [axl.Defector() for _ in range(5)]

        objective = dojo.prepare_objective(name=name,
                                           turns=turns,
                                           noise=noise,
                                           repetitions=repetitions)

        pso = PSO(EvolvableFSMPlayer,
                  params_kwargs,
                  objective=objective,
                  debug=False,
                  opponents=opponents,
                  population=population,
                  generations=generations)

        axl.seed(0)
        opt_vector, opt_objective_value = pso.swarm()

        self.assertTrue(
            np.allclose(
                opt_vector,
                np.array([
                    0.22825439, 0.06954976, 0.49462006, 0.27704876, 1.,
                    0.81240316, 0.11818378, 0., 0.4289995, 0.91397724, 1.,
                    0.7404604, 0.35865552, 1., 0.53483268, 0.41643427,
                    0.71756716
                ])))
        self.assertEqual(abs(opt_objective_value), 1)
Beispiel #23
0
    def test_score_pso(self):
        name = "score"
        turns = 5
        noise = 0
        repetitions = 2
        num_states = 4
        opponents = [s() for s in axl.demo_strategies]
        size = 30
        generations = 5

        objective = dojo.prepare_objective(name=name,
                                           turns=turns,
                                           noise=noise,
                                           repetitions=repetitions)

        pso = dojo.PSO(dojo.HMMParams,
                       params_kwargs={"num_states": num_states},
                       objective=objective,
                       opponents=opponents,
                       population=size,
                       generations=generations)

        axl.seed(0)
        xopt, fopt = pso.swarm()
        
        self.assertTrue(len(xopt) == 2 * num_states ** 2 + num_states + 1)

        # You can put the optimal vector back into a HMM.
        simple_hmm_opt = dojo.HMMParams(num_states=num_states)
        simple_hmm_opt.receive_vector(xopt)
        simple_player = simple_hmm_opt.player()
        self.assertTrue(simple_player.hmm.is_well_formed())  # This should get asserted in initialization anyway
        
        self.assertIsInstance(simple_hmm_opt, dojo.HMMParams)
        print(xopt)  # As a vector still
        print(simple_hmm_opt)  # As a HMM
Beispiel #24
0
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
Beispiel #26
0
    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, print_output=False)
        
        # 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)
Beispiel #27
0
    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)
Beispiel #28
0
    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)
Beispiel #29
0
class TestFSMPopulation(unittest.TestCase):
    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)

    def test_generations(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 = 4
        axl.seed(0)
        population.run(generations, print_output=False)
        self.assertEqual(population.generation, generations)

        results = []
        with open(temporary_file.name, "r") as f:
            reader = csv.reader(f)
            for row in reader:
                results.append(row)

        self.assertEqual(population.generation, len(results))

    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]))
Beispiel #30
0
    generations = int(arguments['--generations'])
    bottleneck = int(arguments['--bottleneck'])
    output_filename = arguments['--output']

    # Objective
    name = str(arguments['--objective'])
    repetitions = int(arguments['--repetitions'])
    turns = int(arguments['--turns'])
    noise = float(arguments['--noise'])
    nmoran = int(arguments['--nmoran'])

    # Lookup Tables
    plays = int(arguments['--plays'])
    op_plays = int(arguments['--op_plays'])
    op_start_plays = int(arguments['--op_start_plays'])
    table_depth = max(plays, op_plays, op_start_plays)
    initial_actions = [C] * table_depth
    param_args = [
        plays, op_plays, op_start_plays, initial_actions, mutation_rate
    ]

    objective = prepare_objective(name, turns, noise, repetitions, nmoran)
    population = Population(LookerUpParams,
                            param_args,
                            population,
                            objective,
                            output_filename,
                            bottleneck,
                            processes=processes)
    population.run(generations)
    mutation_probability = float(arguments['--mu'])
    generations = int(arguments['--generations'])
    bottleneck = int(arguments['--bottleneck'])
    output_filename = arguments['--output']

    # Objective
    name = str(arguments['--objective'])
    repetitions = int(arguments['--repetitions'])
    turns = int(arguments['--turns'])
    noise = float(arguments['--noise'])
    nmoran = int(arguments['--nmoran'])

    # Lookup Tables
    plays = int(arguments['--plays'])
    op_plays = int(arguments['--op_plays'])
    op_start_plays = int(arguments['--op_start_plays'])
    table_depth = max(plays, op_plays, op_start_plays)
    initial_actions = [C] * table_depth
    param_kwargs = {
        "plays": plays,
        "op_plays": op_plays,
        "op_start_plays": op_start_plays,
        "initial_actions": initial_actions,
        "mutation_probability": mutation_probability
    }

    objective = prepare_objective(name, turns, noise, repetitions, nmoran)
    population = Population(LookerUpParams, param_kwargs, population, objective,
                            output_filename, bottleneck, processes=processes)
    population.run(generations)