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_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)