def get_video_from_model(model_name, parameter_filename, save_directory, filename, reward_level="team", num_agents=2): fitness_calculator = FitnessCalculator(parameter_filename) results = None if reward_level == "team": full_genome = np.load(model_name) agent_list = [] for i in range(num_agents): start = i * int(len(full_genome) / num_agents) end = (i + 1) * int(len(full_genome) / num_agents) sub_genome = full_genome[start:end] agent = NNAgent(fitness_calculator.get_observation_size(), fitness_calculator.get_action_size(), parameter_filename, sub_genome) agent_list += [agent] '''full_genome = NNAgent.load_model_from_file(model_name) mid = int(len(full_genome) / num_agents) agent_list = [] for i in range(num_agents): genome = full_genome[mid*i : mid*(i+1)] agent = NNAgent(fitness_calculator.get_observation_size(), fitness_calculator.get_action_size(), parameter_filename, genome) agent_list += [agent]''' results = fitness_calculator.calculate_fitness(agent_list=agent_list, render=True, time_delay=0, render_mode="rgb_array") elif reward_level == "individual": agent_list = [] for i in range(num_agents): genome = NNAgent.load_model_from_file(model_name) agent = NNAgent(fitness_calculator.get_observation_size(), fitness_calculator.get_action_size(), parameter_filename, genome) agent_list += [agent] results = fitness_calculator.calculate_fitness(agent_list=agent_list, render=True, time_delay=0, render_mode="rgb_array") video_data = results['video_frames'] save_to = f"{save_directory}/{filename}" write_gif(video_data, save_to, fps=5)
def visualise_decentralised(): ''' agent_1_path = "/home/mriz9/Code/TS-Platform/results/2021_02_23_decentralised_test/results/decentralised_cma_with_seeding_heterogeneous_individual_nn_slope_1_2_4_1_4_8_4_1_3_7_1_3.0_0.2_2_1000_500_20_rnn_False_1_4_tanh_100_0.2_100_0.001_0.0_0_59324.79999999987_final.npy" agent_2_path = "/home/mriz9/Code/TS-Platform/results/2021_02_23_decentralised_test/results/decentralised_cma_with_seeding_heterogeneous_individual_nn_slope_1_2_4_1_4_8_4_1_3_7_1_3.0_0.2_2_1000_500_20_rnn_False_1_4_tanh_100_0.2_100_0.001_0.0_1_60900.0_final.npy" parameter_filename = "/home/mriz9/Code/TS-Platform/results/2021_02_23_decentralised_test/experiments/decentralised_test.json" ''' agent_1_path = "/Users/mostafa/Documents/Code/PhD/TS-Platform/results/2021_02_23_decentralised_test/results/decentralised_cma_with_seeding_heterogeneous_individual_nn_slope_1_2_4_1_4_8_4_1_3_7_1_3.0_0.2_2_1000_500_20_rnn_False_1_4_tanh_100_0.2_100_0.001_0.0_0_59324.79999999987_final.npy" agent_2_path = "/Users/mostafa/Documents/Code/PhD/TS-Platform/results/2021_02_23_decentralised_test/results/decentralised_cma_with_seeding_heterogeneous_individual_nn_slope_1_2_4_1_4_8_4_1_3_7_1_3.0_0.2_2_1000_500_20_rnn_False_1_4_tanh_100_0.2_100_0.001_0.0_1_60900.0_final.npy" parameter_filename = "/Users/mostafa/Documents/Code/PhD/TS-Platform/results/2021_02_23_decentralised_test/experiments/decentralised_test.json" fitness_calculator = FitnessCalculator(parameter_filename) genome_1 = np.load(agent_1_path) genome_2 = np.load(agent_2_path) agent_1 = NNAgent(fitness_calculator.get_observation_size(), fitness_calculator.get_action_size(), parameter_filename, genome_1) agent_2 = NNAgent(fitness_calculator.get_observation_size(), fitness_calculator.get_action_size(), parameter_filename, genome_2) results = fitness_calculator.calculate_fitness( agent_list=[agent_1, agent_2], render=False, time_delay=0, measure_specialisation=True, logging=False, logfilename=None, render_mode="human") #print(results) team_score = np.mean([ results['fitness_matrix'][0][i] + results['fitness_matrix'][1][i] for i in range(len(results['fitness_matrix'])) ]) print(team_score)
def sample_individual(path_to_genome): model_name = path_to_genome.split("/")[-1] main_folder = "/".join( str(param) for param in path_to_genome.split("/")[:-2]) experiments_folder = os.path.join(main_folder, "experiments") results_folder = os.path.join(main_folder, "results") parameter_filename = os.path.join( experiments_folder, "cma_with_seeding_" + "_".join(model_name.split("/")[-1].split("_")[1:-2]) + ".json") fitness_calculator = FitnessCalculator(parameter_filename) mean_array = np.load(path_to_genome) variance = None name_substring = "_".join( str(param) for param in model_name.split("/")[-1].split("_")[1:-2]) regex_string = f'{results_folder}/cma_with_seeding_*{name_substring}*.log' log_files = glob(regex_string) if len(log_files) > 1 or len(log_files) < 1: raise RuntimeError("Inappropriate number of log files") log_file = log_files[0] g = open(log_file, "r") log_data = g.read().strip().split("\n") g.close() # If last line starts with Best, get line before flag = True i = -1 while flag: if log_data[i].startswith("Best"): variance = float(log_data[i - 1].strip().split()[4]) / 10 flag = False else: i -= 1 # 1000 100000 -1.998880000000000e+04 2.4e+00 4.46e-02 4e-02 5e-02 526:49.5 seed = int(model_name.split("/")[-1].split("_")[5]) random_variable = multivariate_normal(mean=mean_array, cov=np.identity(len(mean_array)) * variance) team = random_variable.rvs(2, seed) agent_1 = NNAgent(fitness_calculator.get_observation_size(), fitness_calculator.get_action_size(), parameter_filename, team[0]) agent_2 = NNAgent(fitness_calculator.get_observation_size(), fitness_calculator.get_action_size(), parameter_filename, team[1]) results = fitness_calculator.calculate_fitness( agent_list=[agent_1, agent_2], render=True, time_delay=0.1, measure_specialisation=True, logging=False, logfilename=None, render_mode="human")
def visualise_centralised(): ''' team_path = "/home/mriz9/Code/TS-Platform/results/2021_02_23_decentralised_test/results/centralised_cma_with_seeding_heterogeneous_team_nn_slope_1_2_4_1_4_8_4_1_3_7_1_3.0_0.2_2_1000_500_20_rnn_False_1_4_tanh_100_0.2_100_0.001_0.0_33848.09000000001_final.npy" parameter_filename = "/home/mriz9/Code/TS-Platform/results/2021_02_23_decentralised_test/experiments/centralised_test.json" ''' team_path = "/Users/mostafa/Documents/Code/PhD/TS-Platform/results/2021_03_16_equal_games_per_learner_sequential/results/centralised_cma_heterogeneous_team_nn_slope_491264_2_4_1_4_8_4_1_3_7_1_3.0_0.2_2_1000_100_5_False_rnn_False_1_4_tanh_100_0.2_1000_0.001_0.0_1000_0.0_10735.8_final.npy" parameter_filename = "/Users/mostafa/Documents/Code/PhD/TS-Platform/results/2021_03_16_equal_games_per_learner_sequential/experiments/centralised_cma_heterogeneous_team_nn_slope_491264_2_4_1_4_8_4_1_3_7_1_3.0_0.2_2_1000_100_5_False_rnn_False_1_4_tanh_100_0.2_1000_0.001_0.0_1000_0.0.json" fitness_calculator = FitnessCalculator(parameter_filename) full_genome = np.load(team_path) mid = int(len(full_genome) / 2) genome_part_1 = full_genome[0:mid] genome_part_2 = full_genome[mid:] agent_1 = NNAgent(fitness_calculator.get_observation_size(), fitness_calculator.get_action_size(), parameter_filename, genome_part_1) agent_2 = NNAgent(fitness_calculator.get_observation_size(), fitness_calculator.get_action_size(), parameter_filename, genome_part_2) results = fitness_calculator.calculate_fitness( agent_list=[agent_1, agent_2], render=False, time_delay=0, measure_specialisation=True, logging=False, logfilename=None, render_mode="human") #print(results) team_score = np.mean([ results['fitness_matrix'][0][i] + results['fitness_matrix'][1][i] for i in range(len(results['fitness_matrix'])) ]) metric_index = 2 # R_spec specialisation = np.mean( [spec[metric_index] for spec in results['specialisation_list']]) print(team_score) print(specialisation)
arena_width = num_agents*2 num_resources = arena_width parameter_path = "default_empirical_parameters.json" parameter_dictionary = json.loads(open(parameter_path).read()) parameter_dictionary["environment"]["slope"]["sliding_speed"] = sliding_speed parameter_dictionary["environment"]["slope"]["num_agents"] = num_agents parameter_dictionary["environment"]["slope"]["arena_width"] = arena_width parameter_dictionary["environment"]["slope"]["num_resources"] = num_resources f = open("temp.json", "w") dictionary_string = json.dumps(parameter_dictionary, indent=4) f.write(dictionary_string) f.close() fitness_calculator = FitnessCalculator("temp.json") available_agents = [HardcodedHitchhikerAgent, HardcodedGeneralistAgent, HardcodedDropperAgent, HardcodedCollectorAgent] strategy_name = {HardcodedHitchhikerAgent: "Novice", HardcodedGeneralistAgent: "Generalist", HardcodedDropperAgent: "Dropper", HardcodedCollectorAgent: "Collector"} strategies = ["Novice", "Generalist", "Dropper", "Collector"] if num_agents == 2: for class1 in available_agents: row = [] for class2 in available_agents: agent1 = class1() agent2 = class2() agent_list = [agent1, agent2] #if type(agent1) == HardcodedCollectorAgent or type(agent2) == HardcodedCollectorAgent:
genome_to_vis = genome fitness_list = episode_scores mean_fitness = np.mean(fitness_list) spec_list = raw_spec_scores[rspec_index] # Exit loop if 0 spec is found if avg_spec == 0: break # Visualise print(min_spec) print(spec_list) print(mean_fitness) print(fitness_list) full_genome = genome_to_vis fitness_calculator = FitnessCalculator(parameter_filename) mid = int(len(full_genome) / 2) genome_part_1 = full_genome[0:mid] genome_part_2 = full_genome[mid:] agent_1 = NNAgent(fitness_calculator.get_observation_size(), fitness_calculator.get_action_size(), parameter_filename, genome_part_1) agent_2 = NNAgent(fitness_calculator.get_observation_size(), fitness_calculator.get_action_size(), parameter_filename, genome_part_2) results = fitness_calculator.calculate_fitness(agent_list=[agent_1, agent_2], render=True, time_delay=0.1, measure_specialisation=True, logging=False,
import argparse from fitness import FitnessCalculator ''' parser = argparse.ArgumentParser(description='Run an experiment') parser.add_argument('--parameters', action="store", dest="parameter_filename") parameter_filename = parser.parse_args().parameter_filename fitness_calculator = FitnessCalculator(parameter_filename) learner = RWGLearner(fitness_calculator) genome, fitness = learner.learn() ''' parameter_filename = "default_parameters.json" fitness_calculator = FitnessCalculator(parameter_filename) # Bootstrap """ learner = RWGLearner(fitness_calculator) genome, fitness = learner.learn() """ # Evolve """ learner = CMALearner(fitness_calculator) genome, fitness = learner.learn() """ # Visualise """ Homogeneous or Het-Ind genome = NNAgent.load_model_from_file(model_name)
def evaluate_model(model_path, episodes=None, rendering=None, time_delay=None, print_scores=None, ids_to_remove=None, dummy_observations=False, bc_measure=None, save_video=False, slope=None): if rendering == "True": rendering = True else: rendering = False if time_delay: time_delay = float(time_delay) else: time_delay = 0 data_directory = "/".join(model_path.split("/")[:-1]) model_filename = model_path.split("/")[-1] learning_type = model_filename.split("_")[0] algorithm_selected = model_filename.split("_")[1] if algorithm_selected != "rwg": generation = model_filename.split("_")[-1].strip(".npy") if learning_type == "centralised" or learning_type == "fully-centralised": parameter_list = model_filename.split("_")[:-2] parameter_filename = "_".join(parameter_list) + ".json" agent_index = "None" elif learning_type == "decentralised": parameter_list = model_filename.split("_")[:-3] parameter_filename = "_".join(parameter_list) + ".json" agent_index = model_filename.split("_")[-3] else: raise RuntimeError("Learning type must be centralised, decentralised or fully centralised") else: parameter_list = model_filename.split("_")[:-1] parameter_filename = "_".join(parameter_list) + ".json" agent_index = "None" parameter_path = os.path.join(data_directory, parameter_filename) parameter_dictionary = json.loads(open(parameter_path).read()) environment = parameter_dictionary["general"]["environment"] num_agents = parameter_dictionary["environment"][environment]["num_agents"] agent_list = [] if slope is not None: parameter_dictionary["environment"][environment]["sliding_speed"] = slope dictionary_changed = False if episodes and parameter_dictionary["environment"][environment]["num_episodes"] != episodes: parameter_dictionary["environment"][environment]["num_episodes"] = episodes dictionary_changed = True if bc_measure and parameter_dictionary["environment"][environment]["bc_measure"] != bc_measure: parameter_dictionary["environment"][environment]["bc_measure"] = bc_measure dictionary_changed = True if dictionary_changed: new_parameter_path = os.path.join(data_directory, "temp.json") f = open(new_parameter_path, "w") dictionary_string = json.dumps(parameter_dictionary, indent=4) f.write(dictionary_string) f.close() fitness_calculator = FitnessCalculator(new_parameter_path) else: fitness_calculator = FitnessCalculator(parameter_path) if parameter_dictionary["general"]["learning_type"] == "centralised" and \ parameter_dictionary["general"]["reward_level"] == "team" and \ parameter_dictionary["general"]["team_type"] == "heterogeneous": full_genome = np.load(model_path) for i in range(num_agents): if ids_to_remove and i in ids_to_remove: agent = HardcodedHitchhikerAgent() else: start = i * int(len(full_genome) / num_agents) end = (i + 1) * int(len(full_genome) / num_agents) sub_genome = full_genome[start:end] agent = NNAgent(fitness_calculator.get_observation_size(), fitness_calculator.get_action_size(), parameter_path, sub_genome) agent_list += [agent] elif (parameter_dictionary["general"]["learning_type"] == "centralised" and \ parameter_dictionary["general"]["reward_level"] == "individual") or \ parameter_dictionary["general"]["team_type"] == "homogeneous": for i in range(num_agents): if ids_to_remove and i in ids_to_remove: agent = HardcodedHitchhikerAgent() else: genome = np.load(model_path) agent = NNAgent(fitness_calculator.get_observation_size(), fitness_calculator.get_action_size(), parameter_path, genome) agent_list += [agent] elif parameter_dictionary["general"]["learning_type"] == "decentralised" and \ parameter_dictionary["general"]["reward_level"] == "individual": # Find genomes of all teammates genome_prefix = "_".join(str(item) for item in parameter_list) genome_suffix = f"{generation}.npy" all_genome_files = glob(f"{data_directory}/{genome_prefix}*{genome_suffix}") assert len(all_genome_files) == num_agents, "Number of genome files does not match number of agents on the team" for i, genome_file in enumerate(all_genome_files): if ids_to_remove and i in ids_to_remove: agent = HardcodedHitchhikerAgent() else: genome = np.load(genome_file) agent = NNAgent(fitness_calculator.get_observation_size(), fitness_calculator.get_action_size(), parameter_path, genome) agent_list += [agent] elif parameter_dictionary["general"]["learning_type"] == "fully-centralised": genome = np.load(model_path) if ids_to_remove: agent = NNAgent(fitness_calculator.get_observation_size() * num_agents, fitness_calculator.get_action_size() * num_agents, parameter_path, genome, ids_to_remove, HardcodedHitchhikerAgent, dummy_observations) else: agent = NNAgent(fitness_calculator.get_observation_size() * num_agents, fitness_calculator.get_action_size() * num_agents, parameter_path, genome) agent_list = [agent] if not save_video: results = fitness_calculator.calculate_fitness(controller_list=agent_list, render=rendering, time_delay=time_delay, measure_specialisation=True, logging=False, logfilename=None, render_mode="human") else: results = fitness_calculator.calculate_fitness(controller_list=agent_list, render=rendering, time_delay=time_delay, measure_specialisation=True, logging=False, logfilename=None, render_mode="rgb_array") team_fitness_list = [0] * len(results['fitness_matrix'][0]) for j in range(num_agents): team_fitness_list = list(map(add, team_fitness_list, results['fitness_matrix'][j])) team_score = np.mean(team_fitness_list) agent_scores = [np.mean(scores) for scores in results['fitness_matrix']] specialisation_each_episode = [spec[metric_index] for spec in results['specialisation_list']] specialisation = np.mean(specialisation_each_episode) if print_scores == "True": #print(results['fitness_matrix']) print(f"Team fitness each episode: {team_fitness_list}") print(f"Team specialisation each episode {specialisation_each_episode}") print(f"Mean score for each agent: {agent_scores}") print(f"Mean score for whole team: {team_score}") print(f"Mean specialisation for whole team {specialisation}") print("----") for index,agent_episodes in enumerate(results['fitness_matrix']): print(f"Agent {index}: {agent_episodes}") return results
def run_experiment(parameter_filename): parameter_dictionary = json.loads(open(parameter_filename).read()) fitness_calculator = FitnessCalculator(parameter_filename) if parameter_dictionary["general"]["algorithm_selected"] == "rwg": if parameter_dictionary["general"]["learning_type"] == "centralised": learner = CentralisedRWGLearner(fitness_calculator) elif parameter_dictionary["general"]["learning_type"] == "fully-centralised": learner = FullyCentralisedRWGLearner(fitness_calculator) else: raise RuntimeError("Invalid learning type for rwg") genome, fitness = learner.learn() elif parameter_dictionary["general"]["algorithm_selected"] == "cma": if parameter_dictionary["algorithm"]["cma"]["seeding_included"] == "True": #if parameter_dictionary["general"]["learning_type"] == "fully-centralised": # raise RuntimeError("Seeding included is not yet supported for fully centralised") # Load default rwg parameters if parameter_dictionary["general"]["environment"] == "slope": default_rwg_parameter_filename = 'default_slope_rwg_parameters_individual.json' rwg_parameter_dictionary = json.loads(open(default_rwg_parameter_filename).read()) # Copy general parameters from cma to rwg rwg_parameter_dictionary["general"] = copy.deepcopy(parameter_dictionary["general"]) rwg_parameter_dictionary["general"]["algorithm_selected"] = "rwg" if parameter_dictionary["general"]["learning_type"] == "decentralised": rwg_parameter_dictionary["general"]["learning_type"] = "centralised" if parameter_dictionary["general"]["reward_level"] == "team": rwg_parameter_dictionary["general"]["reward_level"] = "team" environment_name = parameter_dictionary["general"]["environment"] if parameter_dictionary["general"]["team_type"] == "heterogeneous": rwg_parameter_dictionary["algorithm"]["agent_population_size"] *= parameter_dictionary["environment"][environment_name]["num_agents"] # Copy environment parameters from cma to rwg rwg_parameter_dictionary["environment"] = copy.deepcopy(parameter_dictionary["environment"]) if rwg_parameter_dictionary["general"]["reward_level"] == "individual": environment_name = rwg_parameter_dictionary["general"]["environment"] rwg_parameter_dictionary["environment"][environment_name]["num_agents"] = 1 # Copy agent parameters from cma to rwg rwg_parameter_dictionary["agent"] = copy.deepcopy(parameter_dictionary["agent"]) # Create rwg json file and load to fitness calculator parameters_in_name = Learner.get_core_params_in_model_name(rwg_parameter_dictionary) if parameter_dictionary["general"]["learning_type"] == "centralised": parameters_in_name += CentralisedRWGLearner.get_additional_params_in_model_name(rwg_parameter_dictionary) elif parameter_dictionary["general"]["learning_type"] == "fully-centralised": parameters_in_name += FullyCentralisedRWGLearner.get_additional_params_in_model_name(rwg_parameter_dictionary) new_rwg_parameter_filename = "_".join([str(param) for param in parameters_in_name]) + ".json" f = open(new_rwg_parameter_filename, "w") rwg_dictionary_string = json.dumps(rwg_parameter_dictionary, indent=4) f.write(rwg_dictionary_string) f.close() rwg_fitness_calculator = FitnessCalculator(new_rwg_parameter_filename) # Seeding if parameter_dictionary["general"]["learning_type"] == "fully-centralised": learner1 = FullyCentralisedRWGLearner(rwg_fitness_calculator) else: learner1 = CentralisedRWGLearner(rwg_fitness_calculator) genome1, fitness1 = learner1.learn() # Learning if parameter_dictionary["general"]["learning_type"] == "centralised": learner2 = CentralisedCMALearner(fitness_calculator) genome2, fitness2 = learner2.learn() elif parameter_dictionary["general"]["learning_type"] == "decentralised": learner2 = DecentralisedCMALearner(fitness_calculator) genomes, fitnesses = learner2.learn() elif parameter_dictionary["general"]["learning_type"] == "fully-centralised": learner2 = FullyCentralisedCMALearner(fitness_calculator) genomes, fitnesses = learner2.learn() else: if parameter_dictionary["general"]["learning_type"] == "centralised": learner = CentralisedCMALearner(fitness_calculator) genome, fitness = learner.learn() elif parameter_dictionary["general"]["learning_type"] == "decentralised": learner = DecentralisedCMALearner(fitness_calculator) genomes, fitnesses = learner.learn() elif parameter_dictionary["general"]["learning_type"] == "fully-centralised": learner = FullyCentralisedCMALearner(fitness_calculator) genomes, fitnesses = learner.learn() elif parameter_dictionary["general"]["algorithm_selected"] == "ga": if parameter_dictionary["general"]["learning_type"] == "centralised": learner = CentralisedGALearner(fitness_calculator) genome, fitness = learner.learn()
def calculate_agent_fitness_distribution(reward_level, num_genomes, samples_per_genome, logfile_name): ind_parameter_files = { 2: "cma_heterogeneous_individual_nn_slope_4005303369_2_4_1_0_8_4_1_3_7_1_1.0_1.0_2_1000_500_20_rnn_False_1_4_tanh_100_0.2_1000_0.001_200.0.json", 4: "cma_heterogeneous_individual_nn_slope_550290314_4_8_1_4_8_8_1_3_7_1_3.0_0.2_2_1000_500_20_rnn_False_1_4_tanh_100_0.2_1000_0.001_200.0.json", 6: "cma_heterogeneous_individual_nn_slope_1298508492_6_12_1_4_8_12_1_3_7_1_3.0_0.2_2_1000_500_20_rnn_False_1_4_tanh_300_0.2_1000_0.001_200.0.json", 8: "cma_heterogeneous_individual_nn_slope_1298508492_8_16_1_4_8_16_1_3_7_1_3.0_0.2_2_1000_500_20_rnn_False_1_4_tanh_400_0.2_1000_0.001_200.0.json", 10: "cma_heterogeneous_individual_nn_slope_878115724_10_20_1_4_8_20_1_3_7_1_3.0_0.2_2_1000_500_20_rnn_False_1_4_tanh_500_0.2_1000_0.001_200.0.json" } team_parameter_files = { 2: "cma_heterogeneous_team_nn_slope_491264_2_4_1_4_8_4_1_3_7_1_3.0_0.2_2_1000_500_20_rnn_False_1_4_tanh_100_0.2_1000_0.001_200.0.json", 4: "cma_heterogeneous_team_nn_slope_630311760_4_8_1_4_8_8_1_3_7_1_3.0_0.2_2_1000_500_20_rnn_False_1_4_tanh_100_0.2_1000_0.001_200.0.json", 6: "cma_heterogeneous_team_nn_slope_4033523167_6_12_1_4_8_12_1_3_7_1_3.0_0.2_2_1000_500_20_rnn_False_1_4_tanh_120_0.2_1000_0.001_200.0.json", 8: "cma_heterogeneous_team_nn_slope_1355129330_8_16_1_4_8_16_1_3_7_1_3.0_0.2_2_1000_500_20_rnn_False_1_4_tanh_160_0.2_1000_0.001_200.0.json", 10: "cma_heterogeneous_team_nn_slope_1791095846_10_20_1_4_8_20_1_3_7_1_3.0_0.2_2_1000_500_20_rnn_False_1_4_tanh_100_0.2_1000_0.001_200.0.json" } ind_genomes = get_genomes(reward_level="individual", num_genomes=num_genomes) team_genomes = get_genomes(reward_level="team", num_genomes=num_genomes) default_num_episodes = 20 f = None assert len(ind_genomes[2]) == len( team_genomes[2] ), "Not testing the same number of genomes for ind and team setups" genomes_per_team = len(ind_genomes[2]) if not os.path.exists(logfile_name): f = open(logfile_name, "w") header_string = ",".join([ str(episode_num) for episode_num in range(samples_per_genome * genomes_per_team) ]) f.write(f"num_agents,reward_level,{header_string}\n") else: f = open(logfile_name, "a") for num_agents in range(2, 12, 2): # Get parameter and genome files parameter_filename = None top_genomes = None if reward_level == "individual": parameter_filename = ind_parameter_files[num_agents] top_genomes = ind_genomes elif reward_level == "team": parameter_filename = team_parameter_files[num_agents] top_genomes = team_genomes # Change number of episodes to the relevant number parameter_dictionary = json.loads(open(parameter_filename).read()) new_num_episodes = int(samples_per_genome / num_agents) parameter_dictionary["environment"]["slope"][ "num_episodes"] = new_num_episodes g = open(parameter_filename, "w") dictionary_string = json.dumps(parameter_dictionary, indent=4) g.write(dictionary_string) g.close() # Create fitness calculator fitness_calculator = FitnessCalculator(parameter_filename) # List of fitnesses for num_agents agent_fitnesses = [] for index in range(len(top_genomes[num_agents])): genome = top_genomes[num_agents][index] # Make genome list genomes_list = [] agents_list = [] if reward_level == "individual": for i in range(num_agents): genomes_list += [genome] agents_list += [ NNAgent(fitness_calculator.get_observation_size(), fitness_calculator.get_action_size(), parameter_filename, genomes_list[i]) ] elif reward_level == "team": full_genome = genome genome_part_length = int(len(full_genome) / num_agents) for i in range(num_agents): genomes_list += [ full_genome[i * genome_part_length:(i + 1) * genome_part_length] ] agents_list += [ NNAgent(fitness_calculator.get_observation_size(), fitness_calculator.get_action_size(), parameter_filename, genomes_list[i]) ] results = fitness_calculator.calculate_fitness(agents_list, render=False, time_delay=0) # Log fitness distribution for agent_scores in results["fitness_matrix"]: agent_fitnesses += agent_scores # Log to file std_string = ",".join([str(fitness) for fitness in agent_fitnesses]) f.write(f"{num_agents},{reward_level},{std_string}\n") # Change number of episodes back parameter_dictionary = json.loads(open(parameter_filename).read()) parameter_dictionary["environment"]["slope"][ "num_episodes"] = default_num_episodes g = open(parameter_filename, "w") dictionary_string = json.dumps(parameter_dictionary, indent=4) g.write(dictionary_string) g.close() f.close()