def rotate(actor, rot, safe=True):
    """
    Rotates the actor to a given rotation in degrees

    | If safe mode is enabled, rotate becomes a blocking function which retries if it failed
    | Rotate seems to work even if safe mode is disabled"""
    loc = actor.get_transform().location

    while rot.pitch <= -180.0:
        rot.pitch += 360.0
    while rot.yaw <= -180.0:
        rot.yaw += 360.0
    while rot.roll <= -180.0:
        rot.roll += 360.0

    t = transform(loc.x, loc.y, loc.z, rot.pitch, rot.yaw, rot.roll)
    actor.set_transform(t)

    if safe:
        # for some reason we must wait for the simulator to process this
        time.sleep(0.5)
        # Then we test if it worked
        r1 = actor.get_transform().rotation
        r2 = rot
        diff = ((r1.pitch - r2.pitch) ** 2 + (r1.yaw - r2.yaw) ** 2 + (r1.roll - r2.roll) ** 2) ** 0.5
        if diff > 1.0:
            logger.warning(f'Failed rotating {actor.type_id} to {rot}')
            logger.info(f'Rotation is {r1}')
Exemple #2
0
def normal_main_loop(env, dashboard):
    if AGENT_TYPE is AgentTypes.Network:
        agent = NetworkAgent(NETWORK_AGENT_MODEL_TYPE)
    elif AGENT_TYPE is AgentTypes.Keras:
        agent = KerasAgent()
    elif AGENT_TYPE is AgentTypes.Omniscient:
        agent = OmniscientAgent()
    else:
        logger.critical('Unknown agent type in main, raising error')
        raise RuntimeError('Unknown agent type in main')
    agent.load()

    statefilters = [ImageNoiseFilter(st_dev=0.001)]  # st_dev = 0.02 is alright
    outputfilters = [MotorNoiseFilter(st_dev=0.001)
                     ]  # st_dev = 0.05 is alright

    memory = []
    i = 0
    while True:
        env.load_path(i)

        traveled = do_run(agent, env, dashboard, statefilters, outputfilters,
                          memory)
        logger.info(f'Agent finished, traveled {traveled} meters')

        dashboard.clear()
        env.reset()

        train(agent, memory)
        logger.info('Continuing...')
        i += 1
 def __set_conditions(self):
     current_map_name = self.connection.world.get_map().name
     # Loading correct map
     if current_map_name != MAP_NAME:
         logger.info(f'Loading map: {MAP_NAME} <- {current_map_name}')
         try:
             self.connection.world = self.connection.client.load_world(
                 MAP_NAME)
         except RuntimeError as r:
             logger.critical(f'{r}')
             raise r
     else:
         # Destroying old actors
         actors = self.connection.world.get_actors()
         for actor in actors.filter('vehicle.*.*'):
             actor.destroy()
         for actor in actors.filter('sensor.*.*'):
             actor.destroy()
         if len(actors.filter('vehicle.*.*')) > 0 and len(
                 actors.filter('sensor.*.*')) > 0:
             logger.debug('Cleaned up old actors')
         else:
             logger.warning('Issues while cleaning up old actors')
     # Setting nice weather
     self.__set_weather()
Exemple #4
0
 def _get_environment_variable(self) -> str:
     try:
         return os.environ['ENVIRONMENT']
     except KeyError:
         logger.info(
             'No variable passed, using default ENVIRONMENT: staging.')
         return 'staging'
 def __connect(self):
     try:
         logger.info(f'Connecting to {HOST}:{PORT}...')
         self.client = icarla.client(HOST, PORT)
         # Even on a local machine, CARLA server takes about 2-3 seconds to respond - especially loading maps is long
         self.client.set_timeout(10.0)
         self.world = self.client.get_world()
         logger.info(f'Connected successfully')
     except RuntimeError as r:
         logger.error(f'Could not connect to server: {r}')
Exemple #6
0
def train(agent, memory):
    # memory is a list, containing (prev_state, prev_action, state) triplets
    if TRAIN and not TRAIN_PER_DECISION:
        if TRAIN_MEMORY_SIZE is None or len(memory) >= TRAIN_MEMORY_SIZE:
            logger.info(
                f'{type(agent).__name__} is starting training with memory of {len(memory)}'
            )
            agent.train_on_memory(memory)
            memory.clear()
        else:
            logger.info(f'Memory not full, {len(memory)}/{TRAIN_MEMORY_SIZE}')
Exemple #7
0
 def load(self, path=NETWORKAGENT_MODEL_PATH):
     logger.info(f'Loading model from {path}')
     try:
         self.model.load_model(path,
                               dim_features=get_feature_dimension,
                               image_height=AGENT_IM_HEIGHT,
                               image_width=AGENT_IM_WIDTH,
                               n_actions=choices_count,
                               model=self.model_class)
     except FileNotFoundError as f:
         logger.error(f'Failed to find file at {path} - {f}')
     except RuntimeError as r:
         logger.critical(f'Failed to load agent from {path} - {r}')
def move(actor, loc, safe=True):
    """
    Moves the actor to a given location

    | If safe mode is enabled, move becomes a blocking function which retries if it failed"""
    actor.set_location(loc)

    if safe:
        time.sleep(0.5)  # for some reason we must wait for the simulator to process this
        d = actor.get_location().distance(loc)
        if d > 1.0:
            logger.warning(f'Failed moving {actor.type_id} to {loc}, retry...')
            logger.info(f'Actors transform is {actor.get_transform()}')
            actor.set_transform(carla.Transform(add_locations(loc, location(0, 0, 1)), rotation([0, 0, 0])))
            time.sleep(0.5)
            d = actor.get_location().distance(loc)
            if d > 3.0:
                logger.error(f'Failed moving {actor.type_id} to {loc}')
                logger.info(f'Actors transform is {actor.get_transform()}')
                return
Exemple #9
0
def log_list(li_name, li):
    if li is not None and len(li) > 0:
        logger.info(f'{li_name}:')
        for i, x in enumerate(li):
            logger.info(f'\t{i}. {type(x).__name__}')
    else:
        logger.info(f'No {li_name}')
 def __update_path(self, i):
     self.path = get_path()
     # TODO (3) make prettier
     # if r < 1/6:
     #     logger.info('Environment: normal short')
     #     self.path.slice(None, 20)
     # elif r < 2/6:
     #     logger.info('Environment: backwards short')
     #     self.path.slice(70, None)
     #     self.path.invert()
     # elif
     if i % 4 is 0:
         logger.info('Environment: normal full')
         pass
     elif i % 4 is 1:
         logger.info('Environment: backwards full')
         self.path.invert()
     elif i % 4 is 2:
         logger.info('Environment: normal turn (left)')
         self.path.slice(30, 60)
     elif i % 4 is 3:
         logger.info('Environment: backwards turn (right)')
         self.path.slice(40, 70)
         self.path.invert()
Exemple #11
0
def log_settings(env):
    logger.info(f'Train is {TRAIN}')
    logger.info(f'Train per decision is {TRAIN_PER_DECISION}')
    logger.info(f'Environment is {type(env).__name__}')
 def finish(self):
     logger.info(f'{self.__class__.__name__} finishing')
     pass
import glob
import os
import sys
import time
import numpy as np

from support.logger import logger

try:
    sys.path.append(glob.glob('files/carla-*%d.%d-%s.egg' % (
        sys.version_info.major,
        sys.version_info.minor,
        'win-amd64' if os.name == 'nt' else 'linux-x86_64'))[0])
except IndexError:
    logger.critical('IndexError while trying to find carla egg')
    logger.info('Place carla egg into files directory')
    sys.exit()
try:
    import carla  # Carla package uses the egg from Carla - Python installed package doesn't work
except ModuleNotFoundError:
    logger.critical('ModuleNotFoundError while importing carla from egg')
    sys.exit()

# TODO (8) hide ICarla with a proper interface or smth


def client(host, port):
    return carla.Client(host, port)


def transform(x=0.0, y=0.0, z=0.0, pitch=0.0, yaw=0.0, roll=0.0):
Exemple #14
0
def train_multiple_main_loop(env, dashboard):
    # TODO (6) filters name, log, show properly
    trainer_agent = OmniscientAgent()

    trainees = [NetworkAgent(NetworkAgentModelTypes.SCNN), KerasAgent()]

    trainer_statefilters = [ImageNoiseFilter(st_dev=0.0001)
                            ]  # st_dev = 0.02 is alright
    trainee_statefilters = []  # st_dev = 0.02 is alright
    trainer_outputfilters = [MotorNoiseFilter(st_dev=0.05)
                             ]  # st_dev = 0.05 is alright
    trainee_outputfilters = [MotorNoiseFilter(st_dev=0.001)
                             ]  # st_dev = 0.05 is alright

    memory = []

    trainer_agent.load()
    for t in trainees:
        t.load()

    logger.info(f'TrainerAgent is {type(trainer_agent).__name__}')
    log_list('Trainees', trainees)
    log_list('Trainer StateFilters', trainer_statefilters)
    log_list('Trainee StateFilters', trainee_statefilters)
    log_list('Trainer OutputFilters', trainer_outputfilters)
    log_list('Trainee OutputFilters', trainee_outputfilters)

    for j in range(10):
        logger.info(f'Starting epoch {j}')
        for i in range(4):
            env.load_path(i)

            traveled = do_run(trainer_agent, env, dashboard,
                              trainer_statefilters, trainer_outputfilters,
                              memory)
            logger.info(
                f'TrainerAgent finished Course {i}, traveled {traveled} meters'
            )

            dashboard.clear()
            env.reset()
            logger.info('Continuing...')

        for trainee in trainees:
            train(trainee, memory[:])
            for i in range(4):
                env.load_path(i)

                traveled = do_run(trainee, env, dashboard,
                                  trainee_statefilters, trainee_outputfilters,
                                  [])
                logger.info(
                    f'Trainee {type(trainee).__name__} finished Course {i}, traveled {traveled} meters'
                )
                # TODO (6) give names to agents - using the hash of their model

                dashboard.clear()
                env.reset()
                logger.info('Continuing...')
        memory.clear()
Exemple #15
0
 def save(self, path=NETWORKAGENT_MODEL_PATH):
     logger.info(f'Saving model to {path}')
     self.model.save_model(path)