Example #1
0
    def __init__(self, logdata=True, net=None):
        self.steering_ctrl = CompositeController(
            ProportionalController(0.4),
            IntegrationController(0.2, integral_limit=1.5),
            DerivativeController(2)
        )
        self.acceleration_ctrl = CompositeController(
            ProportionalController(3.7),
        )
        self.data_logger = DataLogWriter() if logdata else None
        self.counter = 0

        # import the neural net that drives the car
        self.model = simpleNetV2()
        self.weights = 'simpleNetV2_epoch_3_all_tracks.csv.pkl'
        self.model.load_state_dict(torch.load(self.weights))
        self.input = torch.FloatTensor(D_in)
        self.track_check1 = False
        self.track_check2 = False

        # import the neat neural network to handle the gear
        config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
                         neat.DefaultSpeciesSet, neat.DefaultStagnation,
                         'config-neat')
        with open('winner-feedforward', 'rb') as f:
            winner = pickle.load(f)
        self.net = neat.nn.FeedForwardNetwork.create(winner, config)

        self.clock = time.time()
        self.done = False

        # initialize the fitness with zero
        self.temp_fitness = 0
Example #2
0
    def __init__(self, logdata=True):
        # filenames = ['aalborg.csv', 'alpine-1.csv', 'f-speedway.csv', 'data_track_2.csv']
        #filenames = ['aalborg.csv', 'alpine-1.csv', 'f-speedway.csv']
        filenames = ['forza_urja.csv', 'newdata5.csv', 'aalborg_urja.csv']
        #, 'aalborg_urja.csv', 'aalborg_urja_vx80.csv']
        #filenames = ['aalborg_new.csv', 'forza_new.csv']
        data, labels = helper_functions.readData(filenames)
        # self.network = do_the_thing(data, labels)
        learning_rate = 1e-6
        #self.network = Network(data, labels, learning_rate)
        #self.network.train()

        #torch.save(self.network, 'current_network.pt')
        self.network = torch.load('current_network_17_WINEF.pt')
        self.id = random.uniform(0, 1)

        fh = open("cooperation" + str(self.id) + ".txt", "w")
        write(str(self.id) + ": 0.0")
        fh.close()

        self.set = False

        self.steering_ctrl = CompositeController(
            ProportionalController(0.4),
            IntegrationController(0.2, integral_limit=1.5),
            DerivativeController(2))
        self.acceleration_ctrl = CompositeController(
            ProportionalController(3.7), )
        self.data_logger = DataLogWriter() if logdata else None
Example #3
0
    def __init__(self, logdata=True):
        self.steering_ctrl = CompositeController(
            ProportionalController(0.4),
            IntegrationController(0.2, integral_limit=1.5),
            DerivativeController(2))
        self.acceleration_ctrl = CompositeController(
            ProportionalController(3.7), )
        self.data_logger = DataLogWriter() if logdata else None

        # Setup the PyTorch model and load the weights
        self.model = simpleNetV3()
        weights = 'simpleNetV3_lr=0.01_epoch_1_all_tracks.csv.pkl'
        self.model.load_state_dict(torch.load(weights))

        # Initialize inputs, counters and history
        self.input = torch.zeros(D_in)
        self.crashCounter = 0
        self.reverseCounter = 0
        self.forwardCounter = 0
        self.resetGear = False
        self.crashed = False
        self.counter = 0
        self.name = '3001'
        self.history = np.zeros((5, 2), dtype=float)

        # Initialize SWARM
        self.pheromones = []
        pickle.dump(self.pheromones, open("sent_3001.txt", "wb"))
        self.straight_begin = 0
        self.straight_end = 0
        self.list_straight = []
        self.list_corner = []
        self.received = []
        self.list_average_corner = []
Example #4
0
 def __init__(self, logdata=True):
     self.steering_ctrl = CompositeController(
         ProportionalController(0.4),
         IntegrationController(0.2, integral_limit=1.5),
         DerivativeController(2))
     self.acceleration_ctrl = CompositeController(
         ProportionalController(3.7), )
     self.data_logger = DataLogWriter() if logdata else None
Example #5
0
    def __init__(self, logdata=True, models=None, explore=0.9, optimizer=None):
        self.steering_ctrl = CompositeController(
            ProportionalController(0.4),
            IntegrationController(0.2, integral_limit=1.5),
            DerivativeController(2))
        self.acceleration_ctrl = CompositeController(
            ProportionalController(3.7), )
        self.data_logger = DataLogWriter() if logdata else None

        # Algorithm variables
        self.previous_state = None
        if os.path.isfile("memory.txt") == True:
            with open("memory.txt", "rb") as fp:
                self.replay_memory = pickle.load(fp)
        else:
            self.replay_memory = []
        # Maximum size of the replay memory
        self.max_RM_size = 100000
        self.model, self.target = models
        self.criterion = torch.nn.MSELoss()
        self.model_optim = optimizer
        self.restart = False
        # Discretize the action space
        self.action_space = [
            0.9, 0.7, 0.5, 0.4, 0.3, 0.2, 0.1, 0, -0.1, -0.2, -0.3, -0.4, -0.5,
            -0.7, -0.9
        ]
        self.action = 0
        self.reward = 0

        # Model that is used for accelerating and braking
        self.model_acc_brake = simpleNetV2()

        self.pheromones = []
        # self.receiver = Receiver(6001)

        weights = 'simpleNetV2_epoch_3_all_tracks.csv.pkl'

        self.model_acc_brake.load_state_dict(torch.load(weights))
        self.input = torch.zeros(D_in)

        # STATS
        if os.path.isfile("counter.txt") == True:
            with open("counter.txt", "rb") as fp:
                self.counter = pickle.load(fp)
        else:
            self.counter = 0
        self.counter_per_game = 0
        self.train_counter = 0
        self.average_reward = 0
        self.average_loss = 0

        # Hyperparameters
        self.exploration_rate = explore
        self.batch_size = 128
        self.gamma = 0.99
Example #6
0
 def __init__(self, logdata=True):
     self.steering_ctrl = CompositeController(
         ProportionalController(0.4),
         IntegrationController(0.2, integral_limit=1.5),
         DerivativeController(2))
     self.acceleration_ctrl = CompositeController(
         ProportionalController(3.7), )
     self.data_logger = DataLogWriter() if logdata else None
     self.path = []
     self.pid_steer = PID(Kp=0.5, Ki=0.0, Kd=1.0, setpoint=0, sample_time=0.02,\
          output_limits=(-1.0, 1.0), auto_mode=True, proportional_on_measurement=True)
Example #7
0
 def __init__(self, logdata=True):
     self.steering_ctrl = CompositeController(
         ProportionalController(0.4),
         IntegrationController(0.2, integral_limit=1.5),
         DerivativeController(2)
     )
     self.acceleration_ctrl = CompositeController(
         ProportionalController(3.7),
     )
     self.data_logger = DataLogWriter() if logdata else None
     self.model = TwoLayerNet(D_in, hidden_size, hidden_size_2, D_out)
     self.model.load_state_dict(torch.load('LaurensNet30.pkl'))
    def instantiatePIDControllers(self):
        self.steering_ctrl = CompositeController(
            ProportionalController(-0.5),
            IntegrationController(-0.1, integral_limit=1),
            DerivativeController(-1))
        self.reverse_ctrl = CompositeController(
            ProportionalController(3.7),
            # IntegrationController(0.1, integral_limit=1.5),
            # DerivativeController(0.5)
        )

        self.forward_ctrl = CompositeController(
            ProportionalController(3.7),
            # IntegrationController(0.1, integral_limit=1.5),
            # DerivativeController(0.5)
        )
Example #9
0
    def __init__(self, logdata=True):
        # filenames = ['aalborg.csv', 'alpine-1.csv', 'f-speedway.csv', 'data_track_2.csv']
        #filenames = ['aalborg.csv', 'alpine-1.csv', 'f-speedway.csv']
        filenames = ['forza_urja.csv']
        #filenames = ['aalborg_new.csv', 'forza_new.csv']
        data, labels = helper_functions.readData(filenames)
        # self.network = do_the_thing(data, labels)
        learning_rate = 1e-6
        self.network = Network(data, labels, learning_rate)
        self.network.train()

        torch.save(self.network, 'current_network.pt')

        self.steering_ctrl = CompositeController(
            ProportionalController(0.4),
            IntegrationController(0.2, integral_limit=1.5),
            DerivativeController(2))
        self.acceleration_ctrl = CompositeController(
            ProportionalController(3.7), )
        self.data_logger = DataLogWriter() if logdata else None
Example #10
0
    def __init__(self, logdata=True):
        self.steering_ctrl = CompositeController(
            ProportionalController(0.4),
            IntegrationController(0.2, integral_limit=1.5),
            DerivativeController(2))
        self.acceleration_ctrl = CompositeController(
            ProportionalController(3.7), )
        self.data_logger = DataLogWriter() if logdata else None

        self.model = simpleNetV2()

        weights = 'simpleNetV2_epoch_3_all_tracks.csv.pkl'
        self.model.load_state_dict(torch.load(weights))
        self.input = torch.zeros(D_in)
        self.crashCounter = 0
        self.reverseCounter = 0
        self.forwardCounter = 0
        self.resetGear = False
        self.crashed = False
        self.counter = 0
        self.name = '3001'

        # NEAT
        self.history = np.zeros((5, 2), dtype=float)
        config = neat.Config(neat.DefaultGenome, neat.DefaultReproduction,
                             neat.DefaultSpeciesSet, neat.DefaultStagnation,
                             'config-neat')
        with open('winner-feedforward', 'rb') as f:
            winner = pickle.load(f)
        self.net = neat.nn.FeedForwardNetwork.create(winner, config)

        #SWARM
        self.pheromones = []
        pickle.dump(self.pheromones, open("../sent_3001.txt", "wb"))
        self.straight_begin = 0
        self.straight_end = 0
        self.list_straight = []
        self.list_corner = []
        self.received = []
        self.list_average_corcer = []
Example #11
0
    def __init__(self, logdata=True, net=None):
        self.steering_ctrl = CompositeController(
            ProportionalController(0.4),
            IntegrationController(0.2, integral_limit=1.5),
            DerivativeController(2)
        )
        self.acceleration_ctrl = CompositeController(
            ProportionalController(3.7),
        )
        self.data_logger = DataLogWriter() if logdata else None
        self.eta = 20
        self.counter = 0
        self.model = simpleNetV2()
        self.weights = 'simpleNetV2_epoch_3_all_tracks.csv.pkl'

        self.model.load_state_dict(torch.load(self.weights))
        self.input = torch.FloatTensor(D_in)
        self.track_check1 = False
        self.track_check2 = False
        self.action_neat = None
        self.net = net
        self.clock = time.time()
        self.done = False
        self.temp_fitness = 0
Example #12
0
    def __init__(self, logdata=False):
        """
        set the initialization mode
        """
        self.launch_torcs = True
        self.launch_torcs_and_second_driver = False
        """
        use_mlp_opponents: if True, the output of the ESN is adjusted by the Mulit-Layer Perceptron saved at PATH_TO_MLP
        """
        self.use_mlp_opponents = True

        self.PATH_TO_ESN = "./trained_nn/evesn10808.pkl"  # for MLP evolution
        #self.PATH_TO_ESN = "./trained_nn/simple_esn.pkl" # for reservoir evolution
        self.PATH_TO_MLP = "./trained_nn/mlp_opponents.pkl"

        self.CURRENT_SPEED_LIMIT = self.SPEED_LIMIT_NORMAL
        """
        Controllers needed for the simple driver
        """
        self.steering_ctrl = CompositeController(
            ProportionalController(0.4),
            IntegrationController(0.2, integral_limit=1.5),
            DerivativeController(2))
        self.acceleration_ctrl = CompositeController(
            ProportionalController(3.7), )
        self.data_logger = DataLogWriter() if logdata else None
        """
        launch torcs practice race when ./start.sh is executed (if torcs is not already running)
        comment out for normal use of torcs
        """

        # automatically start TORCS when './start.sh' is executed
        if self.launch_torcs:
            torcs_command = [
                "torcs", "-r",
                os.path.abspath(
                    "./config_files/current_config_file/config.xml")
            ]
            self.torcs_process = subprocess.Popen(torcs_command)

        # automatically start TORCS with two drivers when './start.sh' is executed
        elif self.launch_torcs_and_second_driver:

            # assure that TORCS is not launched again when the second car is initialized

            if not os.path.isfile('torcs_process.txt'):
                torcs_command = [
                    "torcs", "-r",
                    os.path.abspath(
                        "./config_files/current_config_file/config.xml")
                ]
                self.torcs_process = subprocess.Popen(torcs_command)

                with open('./torcs_process.txt', 'w') as file:
                    file.write('running torcs process')

                second_car_command = ["./start.sh", "-p", "3002"]
                subprocess.Popen(second_car_command)
            else:
                line = ''
                with open('./torcs_process.txt', 'r') as file:
                    line = file.readline()
                if not line.find('running torcs process') > -1:

                    torcs_command = [
                        "torcs", "-r",
                        os.path.abspath(
                            "./config_files/current_config_file/config.xml")
                    ]
                    self.torcs_process = subprocess.Popen(torcs_command)

                    with open('./torcs_process.txt', 'w') as file:
                        file.write('running torcs process')

                    second_car_command = ["./start.sh", "-p", "3002"]
                    subprocess.Popen(second_car_command)
                else:
                    with open('./torcs_process.txt', 'w') as file:
                        file.write('')
        """
        some global variables
        """

        self.previous_position = 0
        self.ID = 0
        self.is_first = True
        self.active_mlp = False
        self.current_damage = 0
        self.accel_deviation = 0
        self.steering_deviation = 0