def main(): parser = argparse.ArgumentParser() parser.add_argument('-fps', "--fps", type=int, default=50, help='fps') parser.add_argument('-gen', "--network_generation", type=int, default=0, help='gen') parser.add_argument('-i', "--inputs", type=int, default=5, help='inputs') parser.add_argument('-s', "--stochastic_spawning", type=int, default=0) parser.add_argument('-hl', "--hidden_layers", type=int, default=0) parser.add_argument( '-p', "--pause_time", type=int, default=0, help= 'Pause the game for [] seconds before playing. Good for recording purposes.' ) parser.add_argument('-u', "--use_network", type=int, default=1, help='0 or 1. Use 0 human play.') args = parser.parse_args() # Params for loading network inputs = args.inputs stoch_bool_network = (args.stochastic_spawning == 1) nbr_hidden_layers = args.hidden_layers nbr_neurons_per_hidden = 20 # default = 20 network_generation = args.network_generation pause_time = args.pause_time network.load(path='ResultsGustafFinal/' + 'i=' + str(inputs) + '_s=' + str(stoch_bool_network) + '_n=' + str(nbr_neurons_per_hidden) + '_' + str(nbr_hidden_layers) + '/network_generation_' + str(network_generation)) # Params for playing the game fps = args.fps max_playtime = 200 max_nbr_frames = max_playtime * fps stoch_bool_game = stoch_bool_network use_network = args.use_network if use_network == 0: fps = 50 score, frames_run = play_game(network, use_network=use_network, display_game=1, fps=fps, max_nbr_frames=max_nbr_frames, stochastic_spawning=(stoch_bool_game == 1), network_generation=network_generation, pause_time=pause_time) print("Score=" + str(score))
def performance_to_csv(): S = [False, True] I = [5, 77] N = [20] HL = [0, 1, 2, 3] fps = 5000 max_playtime = 30 max_nbr_frames = max_playtime * fps fname = "final_res_table.csv" line = "Stochastic & Inputs & Hidden layers & Mean & $\\sigma$ & $\\sigma$/mean(\\%) \\\\ \\hline" print(line) with open(fname, 'w') as f: f.write(line + '\n') res_lines = [] for stoch in S: for n_inputs in I: for neurons in N: for n_hidden_layers in HL: network = Network([77, 10, 3]) path = 'ResultsGustafFinal/i={}_s={}_n={}_{}'.format( n_inputs, stoch, neurons, n_hidden_layers) network.load(path=path) scores = [] n = 100 if stoch else 1 for _ in range(n): score, frames_run = play_game( network, use_network=1, display_game=0, fps=fps, max_nbr_frames=max_nbr_frames, stochastic_spawning=stoch, network_generation=200, pause_time=1) #print(score) scores.append(score) mean_score = sum(scores) / len(scores) std = '-' if not stoch else round(statistics.stdev(scores), 1) std_perc = '-' if not stoch else round( 100 * std / mean_score, 1) line = "{} & {} & {} & {} & {} & {} \\\\".format( stoch, n_inputs, n_hidden_layers, round(mean_score, 1), std, std_perc) print(line) with open(fname, 'a') as f: f.write(line + '\n')
def decode_chromosome(chromosome, courses, frames, stochastic_spawning): """ Decode chromosome by letting it play the game""" network = chromosome scores, play_times = [], [] for course in courses: score, play_time = play_game(network=network, use_network=1, display_game=0, course_nbr=course, max_nbr_frames=frames, fps=5000, stochastic_spawning=stochastic_spawning) scores.append(score) play_times.append(play_time) return np.array(scores), np.array(play_times)
def test_play_game_with_network(): path = 'results/oskar12_2/network_generation_16' print_header('Testing game with network {}'.format(path)) try: network = Network([77, 10, 3]) network.load(path=path) score, frames_run = play_game(network, use_network=1, course_nbr=666, display_game=1, fps=50, max_nbr_frames=10000) print(score) print_success('TEST SUCCESSFUL!') return True except Exception as e: print_failure('TEST FAILED!') print('Reason: {}'.format(str(e))) return False