Example #1
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 #2
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
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 #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()
        reward = (1 + cylinder_reward)**2 - distance_reward
        print('FINISHED TEST WITH REWARD {}'.format(reward))

        return reward

    return get_reward


if __name__ == '__main__':

    # Start simulation and launch experiment
    csv_name = "cylinder_position.csv"

    try:
        from hbp_nrp_virtual_coach.virtual_coach import VirtualCoach
        vc = VirtualCoach(environment='local', storage_username='******')
    except ImportError as e:
        print(e)
        print("You have to start this notebook with the command:\
              cle-virtual-coach jupyter notebook")
        raise e

    sim = vc.launch_experiment('hbpprak_2018_throwing')

    sim.register_status_callback(on_status)  #solution

    # Network params and init

    topology = [6, 50, 20, 6]

    weights = []
Example #8
0
class MotionChallenge:
    # Constants

    # Experiment name
    EXC_NAME = 'ExDHBPPrak_Motion'

    # Csv file name to save distance
    CSV_NAME = 'distance.csv'

    # Placeholder for the smallest (unreal) distance value
    MIN_VAL = -1000.0

    # New transfer function to save distance in csv file
    RECORD_DISTANCE_TF = '''# Imported Python Transfer Function
@nrp.MapRobotSubscriber("ball_distance", Topic("/ball_distance", Float32))
@nrp.MapCSVRecorder("distance_recorder", filename="''' + CSV_NAME + '''", headers=["time", "distance"])
@nrp.Robot2Neuron()
def record_distance(t, ball_distance, distance_recorder):
    distance_recorder.record_entry(t, ball_distance.value.data if ball_distance.value else ''' + str(
        MIN_VAL) + ''')'''

    # Brain code from challenge with ability to set weigths
    BRAIN_TEMPLATE = '''# -*- coding: utf-8 -*-
# pragma: no cover

__author__ = 'Template'

from hbp_nrp_cle.brainsim import simulator as sim
import numpy as np
import logging

logger = logging.getLogger(__name__)

sim.setup(timestep=0.1, min_delay=0.1, max_delay=20.0, threads=1, rng_seeds=[1234])

# Following parameters were taken from the husky braitenberg brain experiment (braitenberg.py)

sensors = sim.Population(3, cellclass=sim.IF_curr_exp())
actors = sim.Population(3, cellclass=sim.IF_curr_exp())

sim.Projection(sensors, actors, sim.AllToAllConnector(), sim.StaticSynapse(weight={syn_weights}))

circuit = sensors + actors'''

    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)

    def getData(self):
        return np.reshape(self._data, (-1, 10))

    def getDataCount(self):
        return self._data.shape[0] / 10

    def clearData(self):
        self._data = np.empty(0)

    def popData(self):
        data = self.getData()
        self.clearData()
        return data

    def initialized(self):
        return self._vc != None

    def stopSim(self):
        try:
            self._done = True
            self._sim.stop()
            time.sleep(2.0)
        except:
            pass

    def receiveDistance(self):
        try:
            csv_data = self._sim.get_csv_data(self.CSV_NAME)
            if not csv_data:
                return self.MIN_VAL
            return float(csv_data[-1][-1])
        except:
            print('Unable to get csv data')
            return self.MIN_VAL

    def makeOnStatus(self):
        def onStatus(msg):
            if not self._done:
                print("Current simulation time: {}".format(
                    msg['simulationTime']))
                distance = self.receiveDistance()
                if distance > self.MIN_VAL:
                    self._sim.pause()
                    self._done = True
                    self._data = np.append(self._data,
                                           np.append(self._weights, distance))
                    print('Weights:\n' + str(self._weights) + '\nDistance: ' +
                          str(distance))
                    #print('Distance: ' + str(distance))

        return onStatus

    def run(self, weights):
        if not self.initialized():
            print('Virtual coach hasn\'t been initialized')
            return
        self._done = False

        # Launch experiment
        try:
            self._sim = self._vc.launch_experiment(self.EXC_NAME)
        except:
            print('Unable to launch experiment')
            if self._sim:
                self.stopSim()
            return

        # Set experiment
        try:
            self._sim.register_status_callback(self.makeOnStatus())
            self._sim.add_transfer_function(self.RECORD_DISTANCE_TF)
            self._weights = weights
            self._sim.edit_brain(
                self.BRAIN_TEMPLATE.format(
                    **{'syn_weights': numpyArrayToInitStr(self._weights)}))
        except:
            print(
                'Unable to set callback function, transfer function or brain')
            self._sim.stopSim()
            time.sleep(2.0)
            return

        # Start experiment
        try:
            self._sim.start()
        except:
            print('Unable to start simulation')
            self.stopSim()
            time.sleep(2.0)
            return

        # Wait until end of experiment and stop it
        while not self._done:
            time.sleep(0.2)
        self.stopSim()
        time.sleep(3.0)
Example #9
0
import numpy as np
import rospy
from PIL import Image
from geometry_msgs.msg import Pose
from hbp_nrp_virtual_coach.virtual_coach import VirtualCoach
from cdp4_data_collection import CDP4DataCollection as data_collection

objects = list(['cube_a', 'coffee_machine', 'aeroplane_toy'])

vc = VirtualCoach(environment='local', storage_username='******')
sim = vc.launch_experiment('scene_understanding_icub_holodeck_0_1')
sim.start()

dc = data_collection()
path = 'training_data'

orig_pose = Pose()
orig_pose.position.z = 10
for i in range(3):
    dc.add_object(objects[i], orig_pose)

for i in range(100):
    pose = dc.generate_random_pose()
    idx = np.random.randint(3)
    dc.set_object_pose(objects[idx], pose, True)
    dc.move_eyes(pose.position)
    rospy.sleep(0.2)
    image = dc.capture_image().astype(np.uint8)
    image = Image.fromarray(image)
    file_name = '%s/images/snapshot_%.3d.jpg' % (path, i)
    image.save(file_name)
#!/usr/bin/python
from hbp_nrp_virtual_coach.virtual_coach import VirtualCoach
vc = VirtualCoach(environment='local',
                  storage_username='******',
                  storage_password='******')

import sys
from StringIO import StringIO
old_stdout = sys.stdout
result = StringIO()
sys.stdout = result
vc.print_cloned_experiments()
sys.stdout = old_stdout
result_string = result.getvalue()
if not ("ser_rl_ss20_0" in result_string):
    vc.import_experiment('/home/bbpnrsoa/nrp/src/Worker/ser_rl_ss20.zip')
else:
    print("Experiment already imported")

sim = vc.launch_experiment('ser_rl_ss20_0')
sim.start()
exit()
Example #11
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()
from hbp_nrp_virtual_coach.virtual_coach import VirtualCoach
import rospy
import time
import os

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

for i in range(0, 10):
    print "### Running experiment %d" % i
    print "Launching"
    sim = vc.launch_experiment('cdp4')
    print "Starting"
    sim.start()
    while rospy.get_time() <= 8.7:
        time.sleep(2)
    print "Pausing"
    sim.pause()
    print "Stopping"
    sim.stop()
    while os.path.isfile(fname_base + ".bag.active"):
        time.sleep(2)
    print "Renaming"
    os.rename(fname_base + ".bag", fname_base + "_" + str(i) + ".bag")
Example #15
0
import numpy as np
import rospy
from PIL import Image
from geometry_msgs.msg import Pose
from hbp_nrp_virtual_coach.virtual_coach import VirtualCoach 
from cdp4_data_collection import CDP4DataCollection as data_collection 


objects = list(['cube_a', 'coffee_machine','aeroplane_toy'])

# create directory to save training data if doesn't exist
if not os.path.exists('training_data'):
    os.mkdir('training_data')
    os.mkdir('training_data/images/')

vc = VirtualCoach(environment='local', storage_username='******', storage_password='******')
sim = vc.launch_experiment('cdp4_toy_experiment_0')
sim.start()

dc = data_collection()
path = 'training_data'


orig_pose = Pose()
orig_pose.position.z = 10
for i in range(3):
    dc.add_object(objects[i], orig_pose)



for i in range(100):