def __init__(self, torcs_client):
     self.torcs = torcs_client
     self.population = []
     self.previous_population = []
     self.fitness_history = []
     self.pop_fitness_history = []
     self.track = 'random'
     self.tracks_used = []
     self.laps = 3
     self.output_file = trained_files.get_file("best_evo_driver.pkl")
     # set this to true to have fitness values comparable across different tracks
     self.normalize_fitness=False
    def get_action(self, sensors):
        try:
            x = sensors.get_array(self.sensor_ids)
            if self.w_in is None and self.w_back is None and self.w_out is None:
                self.random_weights(n_input=len(x)+1)
            u = np.matrix(np.concatenate(([self.bias], x), axis=0))
            x_n_update = self.activation(self.w_in.dot(u.T) + self.w.dot(self.x_n) + self.w_back.dot(self.y))
            self.x_n = self.leak_rate * x_n_update + (1 - self.leak_rate) * self.x_n
            self.y = np.squeeze(np.array(self.activation(self.w_out.T.dot(self.x_n)))) * 2 - 1.0
            return self.y
        except Exception as e:
            print "ESN Error:", e
            return 0.0


if __name__ == '__main__':
    import sys
    import os
    sys.path.insert(0, os.path.dirname(os.path.realpath('network.py')) + '/../')

    import logs
    import trained_files
    from client import SENSOR_IDXS
    steer = FFNetwork(sensor_ids=SENSOR_IDXS, action_ids=['steering'], n_hidden=5)
    training_files = [logs.get_file("lau/"+f) for f in os.listdir(logs.get_file("lau/"))][:5]
    print training_files
    steer.train(training_files=training_files, scaling=False)
    with open(trained_files.get_file("pybrain_test.pkl"), 'wb') as f:
        pickle.dump(steer, f)
        sys.exit(1)
    c = Client(torcs_path=sys.argv[1])

    # CONFIGURATION #
    POPULATION_SIZE = 10
    BATCHES = 5
    EVO_ITERATIONS = 3
    TRACK = 'random'
    LAPS = 2
    NORMALIZED_FITNESS = True
    c.timeout = LAPS * 500
    c.auto_recover = True
    #################

    trained_pop = []
    trained_pop.append(NetworkDriver.from_directory(trained_files.get_file("Human")))
    trained_pop.append(NetworkDriver.from_directory(trained_files.get_file("SimpleDriver")))
    trained_pop.append(NetworkDriver.from_directory(trained_files.get_file("SimpleExample2")))
    trained_pop.append(NetworkDriver.from_directory(trained_files.get_file("bot1")))
    trained_pop.append(NetworkDriver.from_directory(trained_files.get_file("bot2")))

    evo = Evolution(torcs_client=c)
    global_population_fitness_history = []
    global_tracks = []
    start = timeit.default_timer()
    pop = list(trained_pop)
    while len(pop) < POPULATION_SIZE:
        new_driver = random.choice(trained_pop).mutate()
        pop.append(new_driver)
    evo.laps = LAPS
    evo.track = TRACK
示例#4
0
                # print "distance raced:", s['distRaced']
                return driver.compute_fitness(last_sensor=s, lap_times=lap_times, max_speed=max_speed,
                                              average_speed=avg_speed.avg, timeout_reached=timeout_reached)
            else:
                return 0.0
        except KeyboardInterrupt:
            print "Exit client"
        except Exception as e:
            print "Client Error:", e
        finally:
            #print "race call took %0.1f seconds." % (timeit.default_timer() - start_time)
            self.close()


if __name__ == '__main__':
    if len(sys.argv) < 2:
        print "Usage: python client.py torcs_install_path"
        sys.exit(1)

    ndr = NetworkDriver.from_directory(trained_files.get_file("mlp_single_layer"))
    # ndr = NetworkDriver.from_file(trained_files.get_file("best_evo_driver_01.pkl"))

    #c = Client(torcs_path=sys.argv[1])
    #c.configure(track_name='g-track-2', laps=2)
    c = Client(torcs_path=None)  # Use this for GUI test

    c.timeout = 1200
    c.auto_recover = False
    c.race(driver=ndr)
    print "fitness:", ndr.fitness