def __init__(self, trainee, feature_transformer, persist_path, optimizer_params=None, loss=None): super(SupervisedDriver, self).__init__() self.supervisor = MyDriver() self.feature_transformer = feature_transformer self.trainee = trainee self.persist_path = persist_path if optimizer_params is None: optimizer_params = {"lr": 0.001, "momentum": 0.9} if loss is None: loss = nn.MSELoss() # loss self.loss = loss # optimizer self.optimizer = optim.SGD(self.trainee.parameters(), **optimizer_params) self.call_number = 0
def run_client(client_id, genome, network, evaluation): driver = MyDriver(network=network, logdata=False) client = Client(driver=driver, port=3001 + client_id) print(client_id, 'driving...') client.run() # evaluation.extend(driver.eval(2587.54)) # track length for aalborg evaluation.extend(driver.eval(3260.43)) # track length for eroad
def eval_fitness(genomes, config): # start torcs serever # start torcs client # wait until finished # fitness = results for genome_id, genome in genomes: genome.fitness = 0 net = neat.nn.RecurrentNetwork.create(genome, config) pickle.dump(net,open('network.p','wb')) print("Starting simulation...") # run_torcs from yaml # last_path = (os.getcwd()) # os.chdir("../") # # print(os.getcwd()) # os.system('./torcs_tournament.py quickrace.yml') # forza # os.chdir(last_path) main(MyDriver(logdata=False)) # genome.fitness = # print(genome.fitness) fitness_file = open('fitness.txt', 'r') for f in fitness_file: genome.fitness = float(f) print("Fitness: ",genome.fitness)
def fitness(pop): """ Recieves a list of pop. Modify ONLY their fitness values """ for g in pop: g['fitness'] = 0 # nw_activate = neat.generate_network(g) # for xi, xo in zip(xor_inputs, xor_outputs): # output = nw_activate(xi) # g['fitness'] -= (output[0] - xo[0]) ** 2 print(type(g)) pickle.dump(g, open('network.p', 'wb')) main(MyDriver(logdata=False)) # predictions = [] # for input in inputs: # output_pred = nw_activate(input) # predictions.append(output_pred) # # genome.fitness += ( abs(output_real[0] - output_pred[0]) ) # fitness = 0 - sklearn.metrics.mean_squared_error(outputs, predictions) # f = open('fitness.txt', 'w') # f.write(str(fitness) + "\n") # f.close() # fitness_file = open('fitness.txt', 'r') for f in fitness_file: g['fitness'] = float(f)
def eval_genomes(genomes, config): # start torcs serever # start torcs client # wait until finished # fitness = results for genome_id, genome in genomes: genome.fitness = 0 net = neat.nn.RecurrentNetwork.create(genome, config) with open('network.p', 'wb') as handle: pickle.dump(net, handle, protocol=pickle.HIGHEST_PROTOCOL) main(MyDriver(logdata=False)) # predictions = [] # for input, output_real in zip((inputs), (outputs)): # output_pred = te.activate(input) # predictions.append(output_pred) # # genome.fitness += ( abs(output_real[0] - output_pred[0]) ) # fitness = 0 - sklearn.metrics.mean_squared_error(outputs,predictions) # f = open('fitness.txt', 'w') # f.write(str(fitness) + "\n") # f.close() fitness_file = open('fitness.txt', 'r') for f in fitness_file: fitness = f genome.fitness = float(f)
class SupervisedDriver(Driver): def __init__(self, trainee, feature_transformer, persist_path, optimizer_params=None, loss=None): super(SupervisedDriver, self).__init__() self.supervisor = MyDriver() self.feature_transformer = feature_transformer self.trainee = trainee self.persist_path = persist_path if optimizer_params is None: optimizer_params = {"lr": 0.001, "momentum": 0.9} if loss is None: loss = nn.MSELoss() # loss self.loss = loss # optimizer self.optimizer = optim.SGD(self.trainee.parameters(), **optimizer_params) self.call_number = 0 def drive(self, state): self.call_number += 1 command = self.supervisor.drive(state) x = self.feature_transformer.transform(state) y_predicted = self.trainee(x) y_true = Variable( torch.FloatTensor( [command.steering, command.brake, command.accelerator])) self.optimizer.zero_grad() loss = self.loss(y_predicted, y_true) loss.backward() self.optimizer.step() if self.call_number % 1000 == 0: print("Step: {}".format(self.call_number)) print("Prediction", y_predicted) print("True", y_true) print("loss", loss) print() return command def on_shutdown(self): torch.save(self.trainee, self.persist_path)
def eval_genomes(genomes, config): importlib.reload(my_driver) from my_driver import MyDriver best_time = float('inf') finished = 0 for idx, item in enumerate(genomes): print('idx:', idx) genome_id, genome = item net = neat.nn.FeedForwardNetwork.create(genome, config) print('start server') server_proc = subprocess.Popen(["torcs", "-r", torcs_config_file], stdout=subprocess.PIPE) driver = MyDriver(network=net, logdata=False) client = Client(driver=driver) print('driving...') client.run() try: server_proc.wait(timeout=20) except subprocess.TimeoutExpired as ex: process = psutil.Process(server_proc.pid) for proc in process.children(recursive=True): proc.kill() process.kill() genome.fitness = driver.eval(2057.56) # track length for speedway # genome.fitness = driver.eval(6355.65) # track length for alpine 1 # genome.fitness = driver.eval(3274.20) # track length for ruudskogen print('fitness: ', genome.fitness, '\n') if driver.prev_state.last_lap_time: finished += 1 if driver.prev_state.last_lap_time < best_time: best_time = driver.prev_state.last_lap_time print('Best time: ', best_time) print('Finished races: ', finished)
def eval_genome(genome, config): net = neat.nn.FeedForwardNetwork.create(genome, config) subprocess.call('myneat/autostart.sh', shell=True) main(MyDriver(net)) with open('myneat/fitnessFile', 'r') as fitnessFile: fitness = float(fitnessFile.read()) subprocess.call('myneat/autostop.sh', shell=True) print('fitness: {}'.format(fitness)) return fitness
def evaluate_xor(genome): net = NEAT.NeuralNetwork() try: subprocess.call('./autostart.sh', shell=True) # print("b") # time.sleep(1) # print("c") main(MyDriver(net=net)) # print("d") with open("mydata.txt", 'r') as f: fitt = f.read() # os.system('pkill torcs') subprocess.call('./autostop.sh', shell=True) print("fitness ******************* ", fitt) return float(fitt) except Exception as ex: print('Exception:', ex) return 0.0
def eval_genome(genome, config): # net = neat.nn.FeedForwardNetwork.create(genome, config) net = neat.nn.FeedForwardNetwork.create(genome, config) # print("meh") # os.system('pkill torcs') # time.sleep(5) # subprocess.call('torcs',shell=True) # os.system('torcs') # print("a") # time.sleep(1) # cwd=os.getcwd() subprocess.call('./autostart.sh', shell=True) # print("b") # time.sleep(1) # print("c") main(MyDriver(net=net)) # print("d") with open("mydata.txt", 'r') as f: fitt = f.read() # os.system('pkill torcs') subprocess.call('./autostop.sh', shell=True) print("fitness ******************* ", fitt) return float(fitt)
def evaluate_xor(genome): net = NEAT.NeuralNetwork() genome.BuildHyperNEATPhenotype(net, substrate) # nn=genome.BuildHyperNEATPhenotype(net, substrate) # error = 0 # depth = 5 # do stuff and return the fitness # net.Flush() net = NEAT.NeuralNetwork() genome.BuildPhenotype(net) img = np.zeros((400, 400, 3), dtype=np.uint8) img += 10 NEAT.DrawPhenotype(img, (0, 0, 400, 400), net) cv2.imshow("CPPN", img) # Visualize best network's Pheotype net = NEAT.NeuralNetwork() genome.BuildESHyperNEATPhenotype(net, substrate, params) img = np.zeros((800, 800, 3), dtype=np.uint8) img += 10 NEAT.DrawPhenotype(img, (0, 0, 800, 800), net, substrate=True) cv2.imshow("NN", img) cv2.waitKey(33) subprocess.call('./autostart.sh',shell=True) # print("b") # time.sleep(1) # print("c") main(MyDriver(net=net)) # print("d") with open("mydata.txt",'r') as f: fitt=f.read() # os.system('pkill torcs') subprocess.call('./autostop.sh',shell=True) # print("fitness ******************* ",fitt) return float(fitt)
#! /usr/bin/env python3 # 11 12 22:55 from pytocl.main import main from my_driver import MyDriver if __name__ == '__main__': main(MyDriver(logdata=False))
def evolve_population(self, population, two_cars=False, random_track=True, no_opponents=0, print_ea_info=True, print_car_values=False): dir_name = self.make_directory() # Make directory to store networks all_networks = [] # Initialize list with all networks for gen_no in range(self.NO_GENERATIONS): # For all generations self.adapt_xml( random_track=random_track, two_cars=two_cars, no_opponents=no_opponents) # Set random track and opponent individual_counter = 0 for network in population: # For every individual in a population individual_counter += 1 server_proc = self.init_process() # Initialize socket process result_queue = multiprocessing.Queue( ) # Queue to store fitness values p1 = multiprocessing.Process(target=main, args=(MyDriver(network=network), self.no_frames_list[gen_no], 3001, result_queue, print_car_values)) p1.start() if two_cars == True: p2 = multiprocessing.Process( target=main, args=(MyDriver(network=network), self.no_frames_list[gen_no], 3002, result_queue, print_car_values)) p2.start() p1.join() if two_cars == True: p2.join() self.fitness1 = result_queue.get() # Get fitness from queue if two_cars == True: self.fitness2 = result_queue.get() network.fitness = self.fitness1 + self.fitness2 # Assign fitness to network object if print_ea_info == True: print() print("-------------") print() print("No frames ", self.no_frames_list[gen_no]) print() print("generation ", gen_no) print("individual ", individual_counter) print() if two_cars == True: print("Fitness1 ", self.fitness1) print("Fitness2 ", self.fitness2) print() print("Fitness ", network.fitness) print() print("-------------") print() all_networks.append( network) # Append network to all_network object self.kill_process(server_proc) # Kill process self.save_networks(dir_name, all_networks, '_all') # Save file with all networks self.save_networks(dir_name, population, gen_no) # Save all networks print() print('__________________________________') print('__________________________________') print() best_networks = self.select_best( population ) # Select best fraction of the population to make offspring print() print("Fitness of best network from last generation", best_networks[0].fitness) children = self.generate_children( best_networks) # Generate children population population = self.mutate_population( children) # Mutate part of the population print("Population size ", len(population)) print() print('__________________________________') print('__________________________________') print() print() print("Training finished ( ͡° ͜ʖ ͡°). ") print("The dir name is ", dir_name) print("The amount of generations is ", self.NO_GENERATIONS) return best_networks[ 0] # Return final best network with highest fitness
for i in range(0,le): networks2.append(networks[i]) #networks2.append(networks[len(networks)-1]) return networks2 if __name__ == '__main__': print("started") MyDriver.networks=[] temp_networks=[] for i in range(0,62): temp_networks.append(read_networks(i)) #MyDriver.add_network(read_networks(i)) #MyDriver.add_network(read_networks(35)) temp_networks=sort(temp_networks) print(len(temp_networks)) for i in range(0,len(temp_networks)): temp_networks[i].fitness=0 MyDriver.add_network(temp_networks[i]) MyDriver.index=-1 MyDriver.network=MyDriver.get_best_network() my_driver=MyDriver(logdata=False) my_driver.msg='dd' my_driver.distance=0.0 my_driver.speed_x=0.0 my_driver.brake=0.0 my_driver.count=0 my_driver.net_score={} MyDriver.num_childs=50 main(my_driver,3001)
data = get_settings() platform = data["os"] start_index = data.get("start_index") excel_file = data.get("excel_file") output_excel_file = data.get("output_excel_file") num_to_download = data.get("num_to_download") headless = data.get("headless") excel_handler = ExcelHandler(output_excel_file) # Get keywords excel_parser = ExcelParser(excel_file) keywords = excel_parser.get_keywords() # Init a webdriver instance driver = MyDriver.chrome_driver(headless) driver = init_driver(driver) # Go to image search page search_page = GooglePage(driver) search_page.go_to_img_search_page() row = 1 try: for idx, keyword in enumerate(keywords): if idx >= start_index - 1: print("Number {}: {}".format(idx + 1, keyword)) # generate folder to store images img_folder = get_img_folder(idx + 1, keyword, platform) # folder = generate_folder_by_time(img_folder, platform) folder = img_folder
(datalist[29], datalist[30], datalist[31], datalist[32], datalist[33]) }) return state def predictionToFloat(prediction: Variable) -> float: return prediction.data.numpy()[0] # steeringData = SteeringTrainingData(pandas.read_csv('training-data/train_data/alpine-1.csv')) # brakingData = BrakingTrainingData(pandas.read_csv('training-data/train_data/alpine-1.csv')) steeringData = ExtendedSteeringData() observations = [] driver = MyDriver() for i in range(len(steeringData)): state = dataToStateExtended(steeringData[i][0]) sample = Trainer.stateToSample(state) command = driver.drive(state) # # observation = { # 'target': (brakingData[i][1]).numpy()[0], # 'actual': command.brake # } observation = { 'target': (steeringData[i][1]).numpy()[0],
# help='Model name.', # type=str # ) parser.add_argument('-o', '--output_file', help='File where to print results.', type=str) args, _ = parser.parse_known_args() print(args.parameters_file) print(args.output_file) #print(args.name) if args.parameters_file is not None: print('NN driver!!!!!!') main(MyDriver(args.parameters_file, out_file=args.output_file)) #, name=args.name)) else: main(Driver()) except Exception as exc: traceback.print_exc() sys.stdout = orig_stdout sys.stderr = orig_stderr file_out.close() file_err.close() copyfile('../debug/out.log', '../debug/out_{}.log'.format(tm.time())) copyfile('../debug/err.log', '../debug/err_{}.log'.format(tm.time())) raise sys.stdout = orig_stdout
#! /usr/bin/env python3 from pytocl.main import main from my_driver import MyDriver import os for root, dirs, files in os.walk('team_communication/positions'): for f in files: os.unlink(os.path.join(root, f)) if __name__ == '__main__': main(MyDriver())
if __name__ == '__main__': local_dir = os.path.dirname(__file__) config_path = os.path.join(local_dir, 'config-ctrnn') config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction, neat.DefaultSpeciesSet, neat.DefaultStagnation, config_path) with open('winner-feedforward', 'rb') as f: c = pickle.load(f) print('Loaded genome:') net=neat.ctrnn.CTRNN.create(c, config, 10) # subprocess.call('./start.sh',shell=True) main(MyDriver(net=net)) with open("mydata.txt",'r') as f: fitt=f.read() # subprocess.call('./autostop.sh',shell=True) print("fitness ******************* ",fitt)
#! /usr/bin/env python3 from pytocl.main import main from my_driver import MyDriver if __name__ == '__main__': main(MyDriver(None))