def main():
    # set parameters
    episodes = 100
    steps = 32

    gamma = .98
    epsilon = np.linspace(.6, .1, episodes)
    alpha = .1

    n_env = 16
    n_actions = 3

    # start virtual coach
    vc = VirtualCoach(environment='local',
                      storage_username='******',
                      storage_password='******')

    # run training
    model = Model(episodes=episodes,
                  steps=steps,
                  gamma=gamma,
                  epsilon=epsilon,
                  alpha=alpha,
                  n_env=n_env,
                  n_actions=n_actions)
    model.run_training(vc)
Example #2
0
    def __init__(self):
        # Handlers
        self._vc = None
        self._sim = None
        try:
            self._vc = VirtualCoach(environment='local')
        except:
            self._vc = None

        # Flags
        self._done = False

        # Data
        self._weights = None
        self._data = np.empty(0)
Example #3
0
    def start_experiment(experiment_id):
        # Instantiate Virtual Coach and launch Experiment
        vc = VirtualCoach(environment='local', storage_username='******')
        sim = vc.launch_experiment(experiment_id)

        # roslaunch fh_desc controllers.launch
        fh_desc_launch_file = "/home/akl-ma/.opt/nrpStorage/{}/controllers.launch".format(
            experiment_id)
        uuid = roslaunch.rlutil.get_or_generate_uuid(None, False)
        roslaunch.configure_logging(uuid)
        launch = roslaunch.parent.ROSLaunchParent(uuid, [fh_desc_launch_file])
        launch.start()
        time.sleep(10)

        sim.start()
        return sim
Example #4
0
def main():
    # initialize agents
    dqn = Agent(param.alpha, param.n_observations, param.n_actions,
                param.n_neurons)

    # if you want to run the experiment from an external program, use VC
    # this will allow you to use frontend interface from python
    vc = VirtualCoach(environment='local',
                      storage_username='******',
                      storage_password='******')

    for episode in range(param.episodes):
        count = 0
        while True:
            try:
                sim = vc.launch_experiment('template_husky_0')
                break
            except:
                count += 1
                print('Try:', count)
                time.sleep(2)

        # subscribe to topics published by ros
        nhl.perform_subscribers()

        # start the experiment
        nhl.sync_params(episode + 1)
        sim.start()
        time.sleep(2)

        # inner-loop for running an episode
        run_episode(episode, dqn)

        # stop experiment
        sim.stop()
        time.sleep(2)

        # save metrics and network for postprocessing
        if (episode + 1) % 100 == 0:
            dhl.save_objects(dqn, episode + 1)

    print('Target score:', sum(param.target_scores) / float(param.episodes))
    print('Reward visits:', np.sum(param.reward_visits))
import logging
import evolution_utils

import numpy as np

from gazebo_msgs.msg import ModelState
from IPython.display import clear_output
from tf.transformations import quaternion_from_euler
from hbp_nrp_virtual_coach.virtual_coach import VirtualCoach

# disable global logging from the virtual coach
logging.disable(logging.INFO)
logging.getLogger('rospy').propagate = False
logging.getLogger('rosout').propagate = False

vc = VirtualCoach(environment='local', storage_username='******', storage_password='******')

display_episode_tf = """@nrp.Robot2Neuron()
def display_episode_number(t):
    clientLogger.advertise('%s')
"""


class FloreanoExperiment(object):

    def __init__(self, experiment_name, population, generations):
        self.experiment_name = experiment_name
        self.experiment_dir = os.environ['HOME'] + '/.opt/nrpStorage/' + self.experiment_name
        self.last_status = [None]
        self.sim = None
        self.initial_pose = np.array([])
Example #6
0
import time
import os
import argparse
import timeit

parser = argparse.ArgumentParser(
    description='Run a number of trial and store data.')
parser.add_argument('--n', type=int, default=10, help='number of trials')
parser.add_argument('--duration',
                    type=float,
                    default=1.,
                    help='duration of a trial (in second)')

args = parser.parse_args()

vc = VirtualCoach(environment='local', storage_username='******')
fname_base = "/home/nrpuser/.ros/cdp4/experiment"


def setup(sim):
    pass


start = timeit.default_timer()
for i in range(args.n):
    print "### Running experiment %d" % i
    print "Launching"
    sim = vc.launch_experiment('CDP4_experiment_0')
    setup(sim)
    print "Starting"
    sim.start()
Example #7
0
def main():
    # initialize agents
    if param.episode:
        str_ = 'double' if param.double else 'single'
        str_ += '_replay' if param.replay else '_noreplay'
        agent = pickle.load(open('results/agent_%s_%d.pkl' % (str_, param.episode), 'rb'))
        memory = pickle.load(open('results/memory_%s_%d.pkl' % (str_, param.episode), 'rb'))
    else:
        agent = Agent(param.n_observations, param.n_actions, param.n_neurons,
                      param.gamma, param.batch_size, param.tau, param.double)
        memory = Memory(50000)

    # if you want to run the experiment from an external program, use VC
    # this will allow you to use frontend interface from python
    vc = VirtualCoach(environment='local', storage_username='******',
                      storage_password='******')

    # start training
    start = time.time()
    while param.episode < param.episodes:
        # choose initial position
        # start_pos = np.random.rand(2) * 10 + 3
        # start_dir = np.random.rand() * 2 + np.pi
        # print('Starting position: %.2f %.2f' % (start_pos[0], start_pos[1]))
        # print('Starting direction: %.2f' % start_dir)

        start_pos = np.array([8.0, 8.0])

        # with open(folder + 'experiment_configuration.exc', 'r') as file:
        #     data = file.readlines()
        # data[13] = '    <robotPose robotId="husky" x="%.2f" y="%.2f" ' \
        #            'z="0.5" roll="0.0" pitch="-0.0" yaw="%.2f" />\n' \
        #            % (start_pos[0], start_pos[1], start_dir)
        # with open(folder + 'experiment_configuration.exc', 'w') as file:
        #     file.writelines(data)

        # launch experiment
        count = 0
        while True:
            try:
                sim = vc.launch_experiment('template_husky_0')
                break
            except:
                count += 1
                print('Try:', count)
                time.sleep(2)

        # subscribe to topics published by ros
        nhl.perform_subscribers()

        # start the experiment
        sim.start()
        time.sleep(2)

        # inner-loop for running an episode
        done = run_episode(start_pos, param.episode, agent, memory)
        if done:
            param.episode += 1

        # stop experiment
        sim.stop()
        time.sleep(2)

        # save metrics and network for postprocessing
        if (param.episode + 1) % 100 == 0:
            dhl.save_objects(agent, memory, param.episode + 1)

    # print final score
    print('Time elapsed:', time.time() - start)
    print('Target score:', param.target_scores[-1])
    print('Reward visits:', np.sum(param.reward_visits))
def main():
    logger = init_logger()
    args = init_parser()
    num_generations = args['num_generations']
    population_size = args['population_size']
    mutation = args['mutation']
    load = args['load']

    subscribe_cylinder_distance()

    # create evolution
    evol = EvolutionaryAlgo(num_generations, population_size, mutation=mutation)
    if load:
        logger.warning('load generation {}'.format(load))
        try:
            evol.load(load)
        except:
            raise ValueError('Could not load generation: {}'.format(load))

    # log into the virtual coach
    vc = VirtualCoach(environment='local', storage_username='******', storage_password='******')

    # start simulation
    logger.warning('start expe !!!')
    sim = vc.launch_experiment('template_manipulation_0')

    for gen in range(evol.generation_size):
        logger.warning('Generation: {}/{}'.format(gen + 1, evol.generation_size))
        for individual in range(evol.population_size):
            logger.warning('Individual: {}/{}'.format(individual + 1, evol.population_size))
            # set id
            evol.set_id(gen, individual)
            logger.warning('ID: {}'.format(evol.get_id(gen, individual)))

            # get weights
            weights = evol.get_weights(gen, individual)

            # run simulation
            distance = test_weights(sim, weights)

            # save distance
            evol.set_distance(distance, gen, individual)
            logger.warning('Distance: {}'.format(distance))

        # save weights
        filen = evol.save(gen)
        logger.warning('Saved Generation {} to {}'.format(gen, filen))

        # mutate generation
        if gen < evol.generation_size - 1:
            evol.mutate()
        # reset simulation
        sim.reset('full')

    # stop simulation
    sim.stop()

    # print best weights per generation
    for g in evol.generations:
        logger.warning("Elite:")
        logger.warning(g.get_elite(1))
                new_individual = ind1.cross(ind2)
                new_individual.mutate()
                new_population.append(new_individual)

            population = new_population
    except (KeyboardInterrupt, rospy.exceptions.ROSInterruptException):
        pass
    finally:
        best = max(population, key=lambda x: x._fitness)
        logger.critical(best._fitness)
        logger.critical(repr(best.weights))

# log into the virtual coach, update with your credentials
try:
    from hbp_nrp_virtual_coach.virtual_coach import VirtualCoach
    vc = VirtualCoach(environment='local')
except ImportError as e:
    print(e)
    print("You have to start this file with the command:\
          cle-virtual-coach learn_weights.py")
    raise e

sim = vc.launch_experiment('ExDHBPPrak_Motion')
last_restart = time.time()
try:
    # If you can't keep your logging diarrhea in check, I will not respect your private variables
    sim._Simulation__logger.setLevel(logging.WARNING)
    # Additionally, you may want to change line 281 of nrp_virtual_coach/simulation.py to
    # if 'state' in status and status['state'] in ['stopped', 'halted']:
    # to avoid additional (unproblematic) error messages
    genetic_search()