game.run_game() elif testing == "simulator": p1 = [0, 0] p2 = [0, 90] simulator.angle_between(p1, p2) print simulator.polar_coordinates(90, 1) elif testing == "simulation1": turns = 0 attempts = 10000 best_animals = [] for sim in range(attempts): animals = [animal.animal() for i in range(1)] animals[0].health.value = 2.0 simulation_data = game.simulate_game(3000, animals) turns = simulation_data[0] animals = simulation_data[1] anim = simulation_data[2][0] print turns, animals if turns > 2500: best_animals.append(anim) break if best_animals: for sim in range(10): best_animals[ 0].health.value = best_animals[0].max_health.value + 1.0 print best_animals simulation_data = game.simulate_game(3000, copy.deepcopy(best_animals)) turns = simulation_data[0]
if __name__ == "__main__": from agents import RandomAgent from game import simulate_game from view import display from example_two_layer_agent_think_test import create_random_agent as create_random_two_layer_agent # Initialize 4 instances of the RandomAgent class and store them in a list. These guys just accelerate at random (Every frame there's a 30 % probability to accelerate with any acceleration between -1 and 1) agents = [ RandomAgent(0.3), create_random_two_layer_agent(), RandomAgent(0.3), create_random_two_layer_agent() ] # Simulate a game. It returns the ID (0,1,2,3) of the agent who touched it, a ball data (position and velocity of every frame) and pad data (positions and velocities) last_pad_id, ball_data, pads_data, bounce_counter = simulate_game( agents, True) # The ball_positions are the first elements in the ball_data, and so on. Extract it. ball_position_list = ball_data[0] pad_positions_list = pads_data[0] print bounce_counter # Send these to the view-module for plotting display(ball_position_list, pad_positions_list)
if __name__ == "__main__": from game import simulate_game from agents import RandomAgent # Init agents agents = [create_random_agent() for i in range(4)] n_games = 1000 agent_score = [0, 0, 0, 0] import time tic = time.clock() # Run a lot of games, save only the winners for game_counter in range(n_games): last_pad_id = simulate_game(agents, False) if last_pad_id != None: # If not a draw. agent_score[last_pad_id] += 1 if game_counter % 100 == 0: print game_counter toc = time.clock() # Calculate the score of the teams team_one_score = sum(agent_score[:2]) team_two_score = sum(agent_score[2:]) print "{} games played. Duration: {}. Average time per game = {}".format( n_games, toc - tic, n_games / float(toc - tic) / 1000.0)
game.run_game() elif testing == "simulator": p1 = [0,0] p2 = [0,90] simulator.angle_between(p1,p2) print simulator.polar_coordinates(90,1) elif testing == "simulation1": turns = 0 attempts = 10000 best_animals = [] for sim in range(attempts): animals = [animal.animal() for i in range(1)] animals[0].health.value = 2.0 simulation_data = game.simulate_game(3000,animals) turns = simulation_data[0] animals = simulation_data[1] anim = simulation_data[2][0] print turns,animals if turns > 2500: best_animals.append(anim) break if best_animals: for sim in range(10): best_animals[0].health.value = best_animals[0].max_health.value + 1.0 print best_animals simulation_data = game.simulate_game(3000,copy.deepcopy(best_animals)) turns = simulation_data[0] animals = simulation_data[1] print turns,animals
def evaluate_populations(generation, number_of_matchups, number_of_games_per_matchup, populations, agent_generating_functions, score_functions): number_of_populations = len(populations) # a.k.a number of agents population_size = len(populations[0]) # Size of an individual population score_card = np.zeros(number_of_games_per_matchup ) # Keeps track of winning agent for a given matchup last_ball_position_list = np.zeros((number_of_games_per_matchup, 2)) last_pad_centers_list = np.zeros((number_of_games_per_matchup, 4)) bounce_counter_list = np.zeros((number_of_games_per_matchup, 4)) agents = [None] * number_of_populations scores = np.zeros((number_of_populations, population_size)) games = np.zeros((number_of_populations, population_size)) for matchup_counter in range(number_of_matchups): # Select random participants from individuals participants = np.random.randint(0, population_size, number_of_populations) # Generate agents, i.e., create a match-up for population_index in range(number_of_populations): population = populations[population_index] participant = participants[population_index] chromosome = population[ participant] # Select the chromosome of current participant # Generate an agent using the chromosome agents[population_index] = agent_generating_functions[ population_index](chromosome) # Play a number of games for this match-up for game_counter in range(number_of_games_per_matchup): scoring_agent, last_ball_position, last_pad_centers, bounce_counter = simulate_game( agents, False) score_card[game_counter] = scoring_agent last_ball_position_list[game_counter, :] = last_ball_position last_pad_centers_list[game_counter, :] = last_pad_centers bounce_counter_list[game_counter, :] = bounce_counter # For every participating participant, evaluate its fitness for population_index in range(number_of_populations): individual = participants[population_index] score_function = score_functions[population_index] # Increase score of this guy and number of games played scores[population_index, individual] += score_function(generation, score_card, last_ball_position_list, last_pad_centers_list, bounce_counter_list) games[population_index, individual] += number_of_games_per_matchup # Fitness = score per game games[games == 0.0] = 1.0 fitnesses = scores / games return fitnesses