diff --git a/.gitignore b/.gitignore index 53380c2c3e55ad35be1cb6e123ae67fb1ae22dd2..a206662474aac73cc18f10d9447e97adc498e338 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ __pycache__ .vscode saved_agents benchmarks +baselines workspace.code-workspace tech_demo.py *.png diff --git a/carla_environment.py b/carla_environment.py index e41164c6f43104bd8630b61b943e6602da9deec6..7edd56d3f9085fbdc1c300676edfcbd55a0a41be 100644 --- a/carla_environment.py +++ b/carla_environment.py @@ -47,7 +47,7 @@ class Camera: camera_blueprint.set_attribute('image_size_x', f'{self.width}') camera_blueprint.set_attribute('image_size_y', f'{self.height}') camera_blueprint.set_attribute('fov', '95') - camera_transform = carla.Transform(carla.Location(x=-3.0, z=2.2)) + camera_transform = carla.Transform(carla.Location(x=-7.0, z=5.2), carla.Rotation(pitch=-20.0, yaw=0, roll=0.0)) self.sensor = world.world.spawn_actor(camera_blueprint, camera_transform, attach_to=world.player) self.sensor.listen(lambda data: self.process_img(data)) @@ -87,6 +87,35 @@ class CollisionSensor: intensity = math.sqrt(impulse.x**2 + impulse.y**2 + impulse.z**2) # print(intensity) +class ObstacleSensor: + sensor = None + parent = None + ZONES = 4 + obstacle = [] + + def __init__(self, world, parent): + self.parent = parent + bp = world.get_blueprint_library().find('sensor.other.obstacle') + bp.set_attribute('distance', '10') + bp.set_attribute('hit_radius', '3') + print(bp.get_attribute('hit_radius')) + self.sensor = world.spawn_actor(bp, carla.Transform(), attach_to=parent, attachment_type=carla.AttachmentType.Rigid) + weak_self = weakref.ref(self) + self.sensor.listen(lambda event: ObstacleSensor._on_event(weak_self, event)) + + def _on_event(weak_self, event): + self = weak_self() + if not self: + return + self.collision = event + if not event.other_actor.type_id == 'static.road': + print(self.parent.get_location()) + print(self.parent.get_velocity()) + print(event.other_actor.type_id) + print(event.other_actor.transform) + print(event.distance) + + class LidarSensor: sensor = None data = None @@ -116,6 +145,7 @@ class World: """ Wrapper for the carla environment, incl. player/vehicle """ player = None collision_sensor = None + obstacle_sensor = None lidar_sensor = None world = None blueprint_library = None @@ -137,8 +167,7 @@ class World: """ Remove and create new player/vehicle. """ self.destroy() time.sleep(0.5) - self.spawn_player() - self.spawn_actors() + self.spawn_on_sidewalk() def observation(self): if self.player is not None: @@ -148,26 +177,32 @@ class World: else: return [0,0,0] - def spawn_player(self): + def spawn_player(self, transform): """ Add a vehicle to the world. """ while self.player is None: blueprint = random.choice(self.blueprint_library.filter('model3')) - position = carla.Transform(carla.Location(x=-13, y=57.5, z=0.2), carla.Rotation(pitch=0.0, yaw=-180, roll=0.0)) + position = transform self.player = self.world.try_spawn_actor(blueprint, position) start_location = self.player.get_location() print(str(start_location)) self.collision_sensor = CollisionSensor(self.world, self.player) - # self.lidar_sensor = LidarSensor(self.world, self.player) - # start_location.x = 288.0 - # start_location.y = 55.0 - # self.player.set_location(start_location) + self.obstacle_sensor = ObstacleSensor(self.world, self.player) + + def spawn_on_sidewalk(self): + position = carla.Transform(carla.Location(x=-13, y=57.5, z=0.2), carla.Rotation(pitch=0.0, yaw=-180, roll=0.0)) + self.spawn_player(position) + self.spawn_actors(self.sidewalk_parking_cars_transforms()) - def spawn_actors(self): - blueprint = random.choice(self.blueprint_library.filter('etron')) + + def sidewalk_parking_cars_transforms(self): transforms = [] transforms.append(carla.Transform(carla.Location(x=-19.2, y=54.5, z=0.2), carla.Rotation(pitch=0.0, yaw=-180, roll=0.0))) transforms.append(carla.Transform(carla.Location(x=-14.3, y=54.7, z=0.2), carla.Rotation(pitch=0.0, yaw=-180, roll=0.0))) transforms.append(carla.Transform(carla.Location(x=-37.3, y=53.7, z=0.2), carla.Rotation(pitch=0.0, yaw=-180, roll=0.0))) + return transforms + + def spawn_actors(self, transforms): + blueprint = random.choice(self.blueprint_library.filter('etron')) mass = carla.VehiclePhysicsControl(mass=900000.0) for transform in transforms: actor = self.world.try_spawn_actor(blueprint, transform) @@ -187,6 +222,9 @@ class World: if self.collision_sensor is not None: self.collision_sensor.sensor.destroy() self.collision_sensor = None + if self.obstacle_sensor is not None: + self.obstacle_sensor.sensor.destroy() + self.obstacle_sensor = None def step(self, action): @@ -204,35 +242,38 @@ class World: pos = self.player.get_transform() pos_diff = math.sqrt((target.location.x - pos.location.x)**2 + (target.location.y - pos.location.y)**2) rot_diff = (target.rotation.yaw - pos.rotation.yaw) - r = (-pos_diff / 10) + 1 + velocity = self.player.get_velocity() + v = math.sqrt((velocity.x)**2 + (velocity.y)**2) + + r = ((-pos_diff / 10) + 1) / 5 + + if v < 0.01: + r -= 0.5 + if v > 15.0: + r -= 10 + at_final_position = False if pos_diff < 1.8: if self.was_big_reward == False: - r += 100 + r += 50 self.was_big_reward = True - if abs(rot_diff) < 5: - r += 100 + if abs(rot_diff) < 5 and v < 0.01: at_final_position = True else: if self.was_big_reward == True: self.was_big_reward = False r -= 50 - - velocity = self.player.get_velocity() - v = math.sqrt((velocity.x)**2 + (velocity.y)**2) - if v < 0.01: - r -= 1 - if v > 15.0: - r -= 10 + done = False - if at_final_position and v < 0.01: + if at_final_position : print("Success") + r += 100 done = True if self.collision_sensor.collision is not None: r -= 250 done = True if action == IDLE: - r -= 1 + r -= 0.3 return self.observation(), r, done, None class ActionSpace: @@ -260,6 +301,7 @@ class CarlaEnvironment: if render: self.allow_render = True self.camera = Camera(self.world, camera_type='semantic_segmentation') + # self.camera = Camera(self.world, camera_type='rgb') def reset(self): self.world.reset() @@ -288,7 +330,7 @@ if __name__ == "__main__": ctrl = Controller() cumulated_reward = 0 while ctrl.is_running(): - clock.tick(20) + clock.tick(5) ctrl.on_update() obs, reward, done, _ = env.step(ctrl.get_action(), render=True) print(str(reward) + ' ' + str(done)) diff --git a/run_scripts/baselines.py b/run_scripts/baselines.py new file mode 100644 index 0000000000000000000000000000000000000000..04f5ad7bcb2c22c2c0f7d5652ee4479012eb3118 --- /dev/null +++ b/run_scripts/baselines.py @@ -0,0 +1,47 @@ +import main +import environment_wrapper as ew +import gym +from agents import DQAgent, QAgent +import threading +import copy +import time + +start = time.time() + +c_32 = ew.Config() +c_32.name = 'FirstLayerBase' +c_32.render = False +c_32.force_cpu = True +c_32.env = gym.make('CartPole-v0') +c_32.env_type = 'CartPole' +c_32.net_layout = [32, 32] +c_32.eps_decay = 0.9995 +c_32.learn_rate= 0.00075 +c_32.run_episodes = 350 +c_32.save_to = 'baselines/' + + +c_64 = copy.deepcopy(c_32) +c_64.net_layout = [64, 32] + +c_128 = copy.deepcopy(c_32) +c_128.net_layout = [128, 32] + +c_256 = copy.deepcopy(c_32) +c_256.net_layout = [256, 32] + +c_512 = copy.deepcopy(c_32) +c_512.net_layout = [512, 32] + +c_1024 = copy.deepcopy(c_32) +c_1024.net_layout = [1024, 32] + +conf = c_32 + +conf.conf_to_name() +conf.agent = QAgent(conf) +main.run(conf) + +print('Took: ' + str(time.time() - start)) + +print('Done') \ No newline at end of file