Exemple #1
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 #2
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 #3
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)))