Exemple #1
0
def make_testset(cfg):

    start_time = time.time()

    try:
        bot = robots.build_robot(cfg.sim)

        cat, expname, cfg_id, m = cfg.exp.key
        mg = meshgrid.MeshGrid(bot.s_bounds, cfg.testset.res)

        datafile.update()
        for filename in datafile.examples_available():
            pre_key = names.name2key(filename)
            if pre_key[0] == names.kExamples:
                pre_cat, pre_expname, pre_cfg_id, pre_uid, pre_n = pre_key
                if pre_expname == expname and pre_cfg_id == cfg_id:
                    dataset = datafile.load_examples(filename)
                    for order, effect in dataset:
                        mg.add(effect)

        ts = [mg.draw(replace=False)[0] for _ in range(m)]

        datafile.save_testset(ts, cfg.hardware.testsetfile)

    except Exception as e:
        traceback.print_exc()
    finally:
        bot.close()

    print("ran for {}s.".format(int(time.time() - start_time)))
Exemple #2
0
def make_testset(cfg):

    start_time = time.time()

    try:
        bot = robots.build_robot(cfg.sim)

        cat, expname, cfg_id, m = cfg.exp.key
        mg = meshgrid.MeshGrid(bot.s_bounds, cfg.testset.res)

        datafile.update()
        for filename in datafile.examples_available():
            pre_key = names.name2key(filename)
            if pre_key[0] == names.kExamples:
                pre_cat, pre_expname, pre_cfg_id, pre_uid, pre_n = pre_key
                if pre_expname == expname and pre_cfg_id == cfg_id:
                    dataset = datafile.load_examples(filename)
                    for order, effect in dataset:
                        mg.add(effect)

        ts = [mg.draw(replace = False)[0] for _ in range(m)]

        datafile.save_testset(ts, cfg.hardware.testsetfile)

    except Exception as e:
        traceback.print_exc()
    finally:
        bot.close()

    print("ran for {}s.".format(int(time.time() - start_time)))
Exemple #3
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)))
Exemple #4
0
def test_filters0():
    """Test if KinematicArm2D instanciate properly with config"""
    check = True

    cfg = forest.Tree()
    cfg.robotclass = 'robots.KinematicArm2D'
    bot = robots.build_robot(cfg)

    check = bot.s_feats == (0, 1)
    return check
Exemple #5
0
def test_filters0():
    """Test if KinematicArm2D instanciate properly with config"""
    check = True

    cfg = forest.Tree()
    cfg.robotclass = 'robots.KinematicArm2D'
    bot = robots.build_robot(cfg)

    check = bot.s_feats == (0, 1)
    return check
Exemple #6
0
def test_filters1():
    """Test if KinematicArm2D instanciate properly with filters"""
    check = True

    cfg = forest.Tree()
    cfg.robotclass = 'robots.KinematicArm2D'
    cfg['filters.s_feats'] = (0,)
    bot = robots.build_robot(cfg)

    check = bot.s_feats == (0,)
    return check
Exemple #7
0
def test_filters1():
    """Test if KinematicArm2D instanciate properly with filters"""
    check = True

    cfg = forest.Tree()
    cfg.robotclass = 'robots.KinematicArm2D'
    cfg['filters.s_feats'] = (0, )
    bot = robots.build_robot(cfg)

    check = bot.s_feats == (0, )
    return check
Exemple #8
0
def make_examples(cfg):

    start_time = time.time()

    try:
        bot = robots.build_robot(cfg.sim)

        cat, expname, cfg_id, uid, n = cfg.exp.key
        dataset = compile_tests.create_examples(bot, n)

        filename = names.key2name(cfg.exp.key)
        datafile.save_examples(dataset, cfg.hardware.examplesfile)
    except Exception as e:
        traceback.print_exc()
    finally:
        bot.close()

    print("ran for {}s.".format(int(time.time() - start_time)))
Exemple #9
0
def make_examples(cfg):

    start_time = time.time()

    try:
        bot = robots.build_robot(cfg.sim)

        cat, expname, cfg_id, uid, n = cfg.exp.key
        dataset = compile_tests.create_examples(bot, n)

        filename = names.key2name(cfg.exp.key)
        datafile.save_examples(dataset, cfg.hardware.examplesfile)
    except Exception as e:
        traceback.print_exc()
    finally:
        bot.close()

    print("ran for {}s.".format(int(time.time() - start_time)))
Exemple #10
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)))
Exemple #11
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)))
Exemple #12
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)))
Exemple #13
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)))