Esempio n. 1
0
def run_tests(cfg):

    start_time = time.time()

    try:
        cfg.update(defaultcfg, overwrite = False)

        data = datafile.load_data(cfg.hardware.datafile)
        date, expcfg, history = data['date'], data['cfg'], data['history']
        cfg.test.setdefault('max', value = len(history))
        cfg.test.max = min(len(history), cfg.test.max)

        cfg.update(expcfg, overwrite = False)

        bot = robots.build_robot(cfg.sim)

        assert cfg.goals.effect.s_feats  == bot.s_feats
        assert cfg.goals.effect.s_bounds == bot.s_bounds
        assert cfg.goals.motor.m_feats   == bot.m_feats
        assert cfg.goals.motor.m_bounds  == bot.m_bounds

        learner  = Learner(bot.m_feats, bot.s_feats, bot.m_bounds, fwd = 'ES-LWLR', inv = 'L-BFGS-B')
        if cfg.test.testsetfile is None:
            testset  = exp.create_tests(bot.s_bounds, cfg.test.res, fixed_dim = cfg.test.fixed_dim)
        else:
            testset  = datafile.load_testset(cfg.test.testsetfile)

        print(cfg.makeReport())

        ticks, results, averages, stds = [], [], [], []
        for counter, (order, goal, _, effect) in enumerate(history):
            if (counter) % cfg.test.freq == 0:
                ticks.append(counter)

                result   = exp.test_results(bot, learner, testset)
                avg, std = exp.test_perfs(testset, result)

                results.append(result)
                averages.append(avg)
                stds.append(std)
                print("{}{:5d}: {}{:6.4f}{}".format(gfx.purple, counter, gfx.cyan, avg, gfx.end))

            if counter > cfg.test.max:
                break

            learner.add_xy(order, effect)

        exp.save_test(cfg, date, tuple(testset), tuple(ticks), tuple(results),
                      tuple(averages), tuple(stds))

    except Exception:
        import traceback
        traceback.print_exc()
    finally:
        bot.close()

    print("ran for {}s.".format(int(time.time() - start_time)))
Esempio n. 2
0
def _main():
    # Set random seed for training
    seed = 8964
    tf.set_random_seed(seed)
    np.random.seed(seed)
    random.seed(seed)

    pp = pprint.PrettyPrinter()
    print_flags_dict = {}
    for key in FLAGS.__flags.keys():
        print_flags_dict[key] = getattr(FLAGS, key)

    pp.pprint(print_flags_dict)

    if not os.path.exists(FLAGS.checkpoint_dir):
        os.makedirs(FLAGS.checkpoint_dir)

    assert os.path.exists(FLAGS.train_dir), "Specify training data directory!"
    assert os.path.exists(FLAGS.val_dir), "Specify validation data directory!"
    trl = Learner()
    trl.train(FLAGS)
Esempio n. 3
0
def run_second(cfg):

    start_time = time.time()

    try:
        if cfg.hardware.seed.get('secondary', None) is None:
            seed = random.randint(0, sys.maxint)
            cfg.hardware.seed.secondary = seed
        random.seed(cfg.hardware.seed.secondary)

        data = datafile.load_data(cfg.hardware.primaryfile)
        date, primcfg, history = data['date'], data['cfg'], data['history']

        cfg.update(primcfg, overwrite=False)
        primcfg = primcfg.copy(deep=True)

        bot = robots.build_robot(cfg.sim)

        # this should not change
        assert cfg.goals.motor.m_feats == bot.m_feats, 'expected {}, got {}'.format(
            cfg.goals.motor.m_feats, bot.m_feats)
        assert cfg.goals.motor.m_bounds == bot.m_bounds
        # sensors might change
        cfg.goals.effect.s_feats = bot.s_feats
        cfg.goals.effect.s_bounds = bot.s_bounds

        ## Configuration ##

        cfg.goals.effect.res = 10

        ## Bias loading ##

        grid_expl = GridExplorer(primcfg.goals.effect.s_feats,
                                 cfg=primcfg.goals)
        dataset = []
        for order, goal, _, effect in history:
            effect = [
                min(b_max, max(b_min, e_i))
                for e_i, (b_min,
                          b_max) in zip(effect, primcfg.goals.effect.s_bounds)
            ]
            grid_expl.add_effect(effect, goal=goal)
            dataset.append((order, effect))

        bs = bias.IMBias(dataset, effectexplorer=grid_expl)

        ## Instanciation of core modules ##

        #s_explorer = bias.GridBias(bs, cfg.effect.cfg.interest.max_deriv, s_feats, cfg = cfg.effect.cfg)
        s_explorer = BoundedRandomExplorer(bot.s_feats, cfg.goals)
        m_babble = MotorBabble(bot.m_feats, bot.m_bounds, cfg.goals)
        m_explorer = bias.MotorBias(bs, m_babble, cfg.goals)
        guide = Guide(bot.m_feats,
                      bot.s_feats,
                      bot.m_bounds,
                      cfg.goals,
                      goalexplorer=s_explorer,
                      motorbabble=m_explorer)

        learner = Learner(bot.m_feats,
                          bot.s_feats,
                          bot.m_bounds,
                          fwd='ES-LWLR',
                          inv='L-BFGS-B')

        cfg.freeze()
        print(cfg.makeReport())

        ## Running learning ##

        for i in xrange(cfg.exp.trials):
            exp.learn(bot, guide, learner)
            gfx.print_progress(i + 1,
                               cfg.exp.trials,
                               prefix='{}learning... {}'.format(
                                   gfx.purple, gfx.cyan),
                               suffix=gfx.end,
                               eta=1)
        print ''

        exp.save_data(cfg, guide)

    except Exception:
        import traceback
        traceback.print_exc()
    finally:
        bot.close()

    print("ran for {}s.".format(int(time.time() - start_time)))
Esempio n. 4
0
"""
Inverse model benchmark.
Inverse model performance is tested in function of the forward model, 
the inverse and forward parameters and the robots.
"""

import random
random.seed(30)

import robots
from models.learner import Learner
from models.plots.ercurve import ErrorCurve

# Robot
arm = robots.KinematicArm2D(dimM = 6)

# Learner
invlearners = ([Learner.from_robot(arm, fwd = 'WNN',   inv = 'BFGS',   k = 5, sigma = 5.0)]
			#+  [Learner.from_robot(arm, fwd = 'WNN',   inv = 'BFGS',          sigma = 10.0)]
			#+  [Learner.from_robot(arm, fwd = 'AvgNN', inv = 'BFGS',   k = 5, sigma = 5.0)]
			+  [Learner.from_robot(arm, fwd = 'ES-LWLR', inv = 'BFGS', k = 20)]
			)

# Plot
curve = ErrorCurve(arm, invlearners, side = 'inverse', trials = 1000, tests = 250, uniformity = 'sensor')
curve.plot()
curve.show()
Esempio n. 5
0
Inverse model performance is tested in function of the forward model, 
the inverse and forward parameters and the robots.
"""

import random
random.seed(30)

import robots
from models.learner import Learner
from models.plots.ercurve import ErrorCurve

# Robot
arm = robots.KinematicArm2D(dimM=6)

# Learner
invlearners = (
    [Learner.from_robot(arm, fwd='WNN', inv='BFGS', k=5, sigma=5.0)]
    #+  [Learner.from_robot(arm, fwd = 'WNN',   inv = 'BFGS',          sigma = 10.0)]
    #+  [Learner.from_robot(arm, fwd = 'AvgNN', inv = 'BFGS',   k = 5, sigma = 5.0)]
    + [Learner.from_robot(arm, fwd='ES-LWLR', inv='BFGS', k=20)])

# Plot
curve = ErrorCurve(arm,
                   invlearners,
                   side='inverse',
                   trials=1000,
                   tests=250,
                   uniformity='sensor')
curve.plot()
curve.show()
Esempio n. 6
0
def run_first(cfg):

    start_time = time.time()

    try:
        if cfg.hardware.seed.get('primary', None) is None:
            seed = random.randint(0, sys.maxint)
            cfg.hardware.seed.primary   = seed
        random.seed(cfg.hardware.seed.primary)

        bot = robots.build_robot(cfg.sim)

            ## Configuration ##

        goalscfg = treedict.TreeDict()

        goalscfg.effect.explorer = BoundedRandomExplorer

        cfg.goals.update(goalscfg, overwrite = False)

        cfg.goals.effect.s_feats  = s_feats  = bot.s_feats
        cfg.goals.effect.s_bounds = s_bounds = bot.s_bounds
        cfg.goals.motor.m_feats   = m_feats  = bot.m_feats
        cfg.goals.motor.m_bounds  = m_bounds = bot.m_bounds

            ## Instanciation of core modules ##

        s_explorer = BoundedRandomExplorer(s_feats, cfg.goals)
        if 'explorename' in cfg.goals.effect:
            if cfg.goals.effect.explorername == 'cluster':
                s_explorer = cluster.ClusterExplorer(s_feats, cfg.goals)
            elif cfg.goals.effect.explorername == 'cluster_im':
                s_explorer = cluster.ClusterIMExplorer(s_feats, cfg.goals)

        m_explorer = MotorBabble(bot.m_feats, bot.m_bounds, cfg.goals)
        guide      = Guide(m_feats, s_feats, m_bounds, cfg.goals,
                           goalexplorer = s_explorer, motorbabble = m_explorer)
        learner    = Learner(m_feats, s_feats, m_bounds, fwd = 'ES-LWLR', inv = 'L-BFGS-B')

        cfg.freeze()
        print(cfg.makeReport())

            ## Runing learning ##

        for i in xrange(cfg.exp.trials):
            exp.learn(bot, guide, learner)
            if not cfg.sim.verbose:
                gfx.print_progress(i+1, cfg.exp.trials,
                                   prefix = '{}learning... {}'.format(gfx.purple, gfx.cyan),
                                   suffix = gfx.end, eta = 1)
        print ''

        exp.save_data(cfg, guide)

    except Exception:
        import traceback
        traceback.print_exc()
    finally:
        bot.close()

    print("ran for {}s.".format(int(time.time() - start_time)))
Esempio n. 7
0
"""Benchmarking a simple 3 DOF, 2D Kinematic Arm in function of timesteps."""
import random
random.seed(0)

import robots
import models
from models.learner import Learner, fwdclass
from models.plots.ercurve import ErrorCurve

#models.disable_cmodels()

# Robot
arm = robots.KinematicArm2D(dim = 3)

#fwdlearners = [Learner.from_robot(arm, fwd = fwd, inv = 'NN', k = 5, sigma = 5.0) for fwd in fwdclass]
fwdlearners = []

#fwdlearners.append(Learner.from_robot(arm, fwd = 'ES-LWLR', inv = 'NN', k = 4))
#fwdlearners.append(Learner.from_robot(arm, fwd = 'ES-LWLR', inv = 'NN', k = 7))
fwdlearners.append(Learner.from_robot(arm, fwd = 'ES-LWLR', inv = 'NN', k = 3))
fwdlearners.append(Learner.from_robot(arm, fwd = 'ES-LWLR', inv = 'NN', k = 7))
fwdlearners.append(Learner.from_robot(arm, fwd = 'ES-LWLR', inv = 'NN', k = 10))
fwdlearners.append(Learner.from_robot(arm, fwd = 'ES-LWLR', inv = 'NN', k = 20))

curve = ErrorCurve(arm, fwdlearners, side = 'forward', trials = 2000, tests = 200, uniformity = 'motor', test_period = 5, test_pace = 1.5)
curve.plot()
curve.show()
Esempio n. 8
0
"""Benchmarking a simple 3 DOF, 2D Kinematic Arm in function of timesteps."""
import random
random.seed(0)

import forest
import robots
from models.learner import Learner, fwdclass
from models.plots.ercurve import ErrorCurve

# Robot
cfg = forest.Tree()
cfg.dim     = 6
cfg.lengths = 1.0
cfg.limits  = (-360.0, 360.0)
arm = robots.KinematicArm2D(cfg)

fwdlearners = []
fwdlearners.append(Learner.from_robot(arm, fwd = 'ES-LWLR', inv = 'L-BFGS-B', k = 2*cfg.dim+1))

curve = ErrorCurve(arm, fwdlearners, side = 'forward', trials = 50000, tests = 200, uniformity = 'motor')
curve.plot()
curve.show()
Esempio n. 9
0
def run_tests(cfg):

    start_time = time.time()

    try:
        cfg.update(defaultcfg, overwrite=False)

        data = datafile.load_data(cfg.hardware.datafile)
        date, expcfg, history = data['date'], data['cfg'], data['history']
        cfg.test.setdefault('max', value=len(history))
        cfg.test.max = min(len(history), cfg.test.max)

        cfg.update(expcfg, overwrite=False)

        bot = robots.build_robot(cfg.sim)

        assert cfg.goals.effect.s_feats == bot.s_feats
        assert cfg.goals.effect.s_bounds == bot.s_bounds
        assert cfg.goals.motor.m_feats == bot.m_feats
        assert cfg.goals.motor.m_bounds == bot.m_bounds

        learner = Learner(bot.m_feats,
                          bot.s_feats,
                          bot.m_bounds,
                          fwd='ES-LWLR',
                          inv='L-BFGS-B')
        if cfg.test.testsetfile is None:
            testset = exp.create_tests(bot.s_bounds,
                                       cfg.test.res,
                                       fixed_dim=cfg.test.fixed_dim)
        else:
            testset = datafile.load_testset(cfg.test.testsetfile)

        print(cfg.makeReport())

        ticks, results, averages, stds = [], [], [], []
        for counter, (order, goal, _, effect) in enumerate(history):
            if (counter) % cfg.test.freq == 0:
                ticks.append(counter)

                result = exp.test_results(bot, learner, testset)
                avg, std = exp.test_perfs(testset, result)

                results.append(result)
                averages.append(avg)
                stds.append(std)
                print("{}{:5d}: {}{:6.4f}{}".format(gfx.purple, counter,
                                                    gfx.cyan, avg, gfx.end))

            if counter > cfg.test.max:
                break

            learner.add_xy(order, effect)

        exp.save_test(cfg, date, tuple(testset), tuple(ticks), tuple(results),
                      tuple(averages), tuple(stds))

    except Exception:
        import traceback
        traceback.print_exc()
    finally:
        bot.close()

    print("ran for {}s.".format(int(time.time() - start_time)))
Esempio n. 10
0
import robots
import models
from models.learner import Learner, fwdclass
from models.plots.ercurve import ErrorCurve

#models.disable_cmodels()

# Robot
arm = robots.KinematicArm2D(dim=3)

#fwdlearners = [Learner.from_robot(arm, fwd = fwd, inv = 'NN', k = 5, sigma = 5.0) for fwd in fwdclass]
fwdlearners = []

#fwdlearners.append(Learner.from_robot(arm, fwd = 'ES-LWLR', inv = 'NN', k = 4))
#fwdlearners.append(Learner.from_robot(arm, fwd = 'ES-LWLR', inv = 'NN', k = 7))
fwdlearners.append(Learner.from_robot(arm, fwd='ES-LWLR', inv='NN', k=3))
fwdlearners.append(Learner.from_robot(arm, fwd='ES-LWLR', inv='NN', k=7))
fwdlearners.append(Learner.from_robot(arm, fwd='ES-LWLR', inv='NN', k=10))
fwdlearners.append(Learner.from_robot(arm, fwd='ES-LWLR', inv='NN', k=20))

curve = ErrorCurve(arm,
                   fwdlearners,
                   side='forward',
                   trials=2000,
                   tests=200,
                   uniformity='motor',
                   test_period=5,
                   test_pace=1.5)
curve.plot()
curve.show()
Esempio n. 11
0
 def create_learner(self):
     learner = Learner(self, self.generate_id())
     self.learners.append(learner)