parser = argparse.ArgumentParser() parser.add_argument("-s", "--steps", type=int) parser.add_argument("-f", "--filename", type=str) parser.add_argument("-e", "--evaluate", type=bool) parser.add_argument("--seed", type=int) args = parser.parse_args() args.steps = 1000 args.seed = 100 args.filename = 'network_config_agent_0_layers_11_6_1.txt' print(args.steps, args.seed, args.filename, args.evaluate) steps = args.steps seed = args.seed if args.seed else 23 np.random.seed(seed) random.seed(seed) m = generate_map(8, 5, 3, 3) if args.filename: agent = SimpleCarAgent.from_file(args.filename) w = SimpleCarWorld(5, m, SimplePhysics, SimpleCarAgent, timedelta=0.2) if args.evaluate: print(w.evaluate_agent(agent, steps)) else: w.set_agents([agent]) w.run(steps) else: SimpleCarWorld(5, m, SimplePhysics, SimpleCarAgent, timedelta=0.2).run(steps)
import numpy as np import random import argparse parser = argparse.ArgumentParser() parser.add_argument("-s", "--steps", type=int) parser.add_argument("-f", "--filename", type=str) parser.add_argument("-e", "--evaluate", type=bool) parser.add_argument("--seed", type=int) args = parser.parse_args() print(args.steps, args.seed, args.filename, args.evaluate) steps = args.steps seed = args.seed if args.seed else 23 np.random.seed(seed) random.seed(seed) m = generate_map(8, 5, 3, 3) if args.filename: agent = SimpleCarAgent.from_file(args.filename) w = SimpleCarWorld(1, m, SimplePhysics, SimpleCarAgent, timedelta=0.2) if args.evaluate: print(w.evaluate_agent(agent, steps)) else: w.set_agents([agent]) w.run(steps) else: SimpleCarWorld(1, m, SimplePhysics, SimpleCarAgent, timedelta=0.2).run(steps)
def run_and_save_best(visual, steps, _map=None, file=None): """ Trains multiple networks with different hyperparameters, chooses the network with the best result and saves in to a file. :param file: File """ # create worlds to run the agents on if _map is None: worlds_number = 3 np.random.seed(None) random.seed(None) _map = generate_map() worlds = list(SimpleCarWorld(1, _map, SimplePhysics, SimpleCarAgent, visual, timedelta=0.2) for _ in range(worlds_number)) else: worlds = [SimpleCarWorld(1, _map, SimplePhysics, SimpleCarAgent, visual, timedelta=0.2)] # create agents with all possible hyperparameters agents = [] for (eta, reg_coef, epochs, reward_depth, train_every) \ in list(itertools.product( # etas [1e-01], # reg_coefs [32], # epochs [60], # reward_depth [15], # train_every / batch size [50] )): if file is None: print("Creating a new agent") agent = SimpleCarAgent() else: print(f"Using an agent with weights from {file}") agent = SimpleCarAgent.from_file(file) if eta is not None: agent.eta = eta if reg_coef is not None: agent.reg_coef = reg_coef if epochs is not None: agent.epochs = epochs if reward_depth is not None: agent.reward_depth = reward_depth if train_every is not None: agent.train_every = train_every agents += [agent] errors = [] for world in worlds: errors += [run_agent_for_worlds(agents, world, steps)] means = np.nanmean(errors, 0)[0] results = dict(zip(agents, means)) best_agent = max(results, key=results.get) best_reward = results[best_agent] if type(best_reward) is not np.float64: best_reward = None for agent, result in results.items(): print(f"Creating an agent with hyperparams: \n{agent.hyperparams_to_string()} \nError: {result}\n") print(f"🏆 This agent performed the best in all worlds with the error {best_reward}\n{best_agent.hyperparams_to_string()}") # write results to files file_path = str(file) dot_index = file_path.find(".") reward_file = file[:dot_index] + latest_error_file_suffix + file[dot_index:] with open(reward_file, 'a+') as f: f.seek(0) lines = f.readlines() last_reward = lines[-1] if len(lines) > 0 else None if best_reward is not None and (file is None or last_reward is None or float(last_reward) < float(best_reward)): save_to_file(best_agent) if last_reward is not None: f.write('\n') f.write(str(best_reward)) else: save_to_file(agent=best_agent, prefix="temp_") print(f"Reward ({best_reward}) was invalid or worse than {last_reward} and was saved to a temporary file")
parser = argparse.ArgumentParser() parser.add_argument("-s", "--steps", type=int) parser.add_argument("-f", "--filename", type=str) parser.add_argument("-e", "--evaluate", type=bool) parser.add_argument("--seed", type=int) args = parser.parse_args() print(args.steps, args.seed, args.filename, args.evaluate) steps = args.steps seed = args.seed if args.seed else 23 np.random.seed(seed) random.seed(seed) m = generate_map(20, 2, 1, 1) radii = np.random.normal(loc=2.5, scale=0.2, size=8) angles = get_partition(8, -np.pi, np.pi) o = generate_obstacles(8, radii, angles, 0) if args.filename: agent = SimpleCarAgent.from_file(args.filename) w = SimpleCarWorld(1, m, o, radii, angles, SimplePhysics, SimpleCarAgent, timedelta=0.2) if args.evaluate: with open('results.txt', 'a') as inf: circles, collisions = w.evaluate_agent(agent, SimplePhysics, steps, timedelta=0.2) inf.write('seed: {} circles: {} collisions: {} \n'.format(seed, circles, collisions)) else: w.set_agents([agent]) w.run(SimplePhysics, steps, timedelta=0.2) else: SimpleCarWorld(1, m, o, radii, angles, SimplePhysics, SimpleCarAgent, timedelta=0.2).run(SimplePhysics, steps, timedelta=0.2)
find_best = args.findbest def train(): if find_best: run_and_save_best(visual, steps, _map=m if seed is not None else None, file=file) else: w.set_agents([agent]) w.run(steps) if args.filename and os.path.isfile(file): agent = SimpleCarAgent.from_file(file) w = SimpleCarWorld(1, m, SimplePhysics, SimpleCarAgent, visual, timedelta=0.2) if args.evaluate: print(f"Evaluating on seed {seed}...") print(f"Error: {w.evaluate_agent(agent, steps)}") print(f"Circles: {w.circles}") else: train() else: train()
etas = [0.02, 0.05, 0.1] train_everies = [50, 100, 200] epochs_ns = [20, 50, 100] hidden_layers = [[11], [11, 11], [20]] # [[9,9],[9,12,9]] # понравились парам-ры: # etas = [0.1] # train_everies = [100] # epochs_ns = [50] if args.evaluate and args.filename: w = SimpleCarWorld(1, m, SimplePhysics, SimpleCarAgent, [9, 9], 100, 20, 0.02, timedelta=0.2) agent = SimpleCarAgent.from_file(args.filename, [9, 9]) w.set_agents([agent]) print(w.evaluate_agent(agent, 1200)) else: for e in etas: for t in train_everies: for ep in epochs_ns: for ls in hidden_layers: for seed in seeds: print( "E: {0}; train: {1}; Epochs: {2}; Layers: {3}: Seed: {4}"
import random import argparse parser = argparse.ArgumentParser() parser.add_argument("-s", "--steps", type=int) parser.add_argument("-f", "--filename", type=str) parser.add_argument("-e", "--evaluate", type=bool) parser.add_argument("--seed", type=int) args = parser.parse_args() print(args.steps, args.seed, args.filename, args.evaluate) steps = args.steps seed = args.seed if args.seed else 23 np.random.seed(seed) random.seed(seed) m = generate_map(8, 5, 3, 3) if args.filename: agent = SimpleCarAgent.from_file(args.filename) w = SimpleCarWorld(1, m, SimplePhysics, SimpleCarAgent, timedelta=0.2) if args.evaluate: with open('results.txt', 'a') as inf: inf.write('seed: ' + str(seed) + ' circles: ' + str(w.evaluate_agent(agent, steps)) + '\n') else: w.set_agents([agent]) w.run(steps) else: SimpleCarWorld(1, m, SimplePhysics, SimpleCarAgent, timedelta=0.2).run(steps)
parser.add_argument("-s", "--steps", type=int) parser.add_argument("-f", "--filename", type=str) #, default = 'network_config_agent_0_layers_25_25_60_60_60_1.txt') parser.add_argument("-e", "--evaluate", type=bool) parser.add_argument("--seed", type=int) parser.add_argument('--hiddenlayers', dest='hiddenLayers', metavar='N', type=int, nargs='+', help='amount of neurons in hidden layers') parser.add_argument('--rays', dest='rays', type=int, help='amount of ladar ray', default = 21) args = parser.parse_args() print(args.steps, args.seed, args.filename, args.evaluate) steps = args.steps seed = args.seed if args.seed else 23 np.random.seed(seed) random.seed(seed) m = generate_map(8, 5, 3, 3) hiddenLayersList = args.hiddenLayers if args.hiddenLayers else [] # [55, 45] if args.filename: agent = SimpleCarAgent.from_file(args.filename) w = SimpleCarWorld(1, m, SimplePhysics, SimpleCarAgent, timedelta=0.2) if args.evaluate: print(w.evaluate_agent(agent, steps)) else: w.set_agents([agent]) w.run(steps) else: SimpleCarWorld(1, m, SimplePhysics, SimpleCarAgent, timedelta=0.2, rays=args.rays, hiddenLayers=hiddenLayersList).run(steps)