예제 #1
0
    def test_call(self):
        factory1 = QuadCostFactory()
        factory2 = GaussRegFactory()
        sum_factory = factory1 + factory2

        cs = sum_factory.get_configuration_space(self.system, self.task,
                                                 self.Model)
        cfg = cs.get_default_configuration()
        cs1 = factory1.get_configuration_space(self.system, self.task,
                                               self.Model)
        cfg1 = cs1.get_default_configuration()
        cs2 = factory2.get_configuration_space(self.system, self.task,
                                               self.Model)
        cfg2 = cs2.get_default_configuration()

        cost = sum_factory(self.system, self.task, self.model, self.trajs, cfg)
        cost1 = factory1(self.system, self.task, self.model, self.trajs, cfg1)
        cost2 = factory2(self.system, self.task, self.model, self.trajs, cfg2)

        self.assertIsInstance(cost, SumCost)

        obs = np.array([-1, 2])
        val = cost.eval_obs_cost(obs)
        val1 = cost1.eval_obs_cost(obs)
        val2 = cost2.eval_obs_cost(obs)

        self.assertEqual(val, val1 + val2)
예제 #2
0
    def test_config_space(self):
        factory1 = QuadCostFactory()
        factory2 = GaussRegFactory()

        sum_factory = factory1 + factory2
        cs = sum_factory.get_configuration_space(self.system, self.task,
                                                 self.Model)
        self.assertIsInstance(cs, CS.ConfigurationSpace)

        cfg = cs.get_default_configuration()
        cfg_dict = cfg.get_dictionary()
        extr_dicts = []
        for i in range(2):
            extr_dict = dict()
            prfx = "_sum_{}:".format(i)
            for key, val in cfg_dict.items():
                if key.startswith(prfx):
                    extr_key = key.split(":")[1]
                    extr_dict[extr_key] = val
            extr_dicts.append(extr_dict)
        cs1 = factory1.get_configuration_space(self.system, self.task,
                                               self.Model)
        cs2 = factory2.get_configuration_space(self.system, self.task,
                                               self.Model)
        cfg1_dict = cs1.get_default_configuration().get_dictionary()
        cfg2_dict = cs2.get_default_configuration().get_dictionary()
        self.assertEqual(extr_dicts[0], cfg1_dict)
        self.assertEqual(extr_dicts[1], cfg2_dict)
예제 #3
0
class PipelineTest(unittest.TestCase):
    def setUp(self):
        double_int = ampc.System(["x", "y"], ["u"])
        self.system = double_int
        self.Model = ARX
        self.Controller = IterativeLQR
        self.model_cs = self.Model.get_configuration_space(self.system)
        self.model_cfg = self.model_cs.get_default_configuration()
        self.model = ampc.make_model(self.system, self.Model, self.model_cfg)
        self.cost_factory = QuadCostFactory() + GaussRegFactory()

        # Initialize task
        Q = np.eye(2)
        R = np.eye(1)
        F = np.eye(2)
        cost = QuadCost(self.system, Q, R, F, goal=[-1, 0])
        self.task = Task(self.system)
        self.task.set_cost(cost)
        self.task.set_ctrl_bound("u", -20.0, 20.0)

        # Generate trajectories
        self.trajs = uniform_random_generate(
            double_int,
            self.task,
            lambda y, u: dt_doubleint_dynamics(y, u, dt=0.05),
            np.random.default_rng(42),
            init_min=[-1.0, -1.0],
            init_max=[1.0, 1.0],
            traj_len=20,
            n_trajs=20)

    def test_config_space(self):
        pipeline = ampc.Pipeline(self.Model, self.Controller,
                                 self.cost_factory, None)
        cs = pipeline.get_configuration_space(self.system, self.task)
        self.assertIsInstance(cs, CS.ConfigurationSpace)

        cfg = cs.get_default_configuration()
        cfg_dict = cfg.get_dictionary()
        extr_dicts = []
        for prfx in ["_model", "_ctrlr", "_cost"]:
            extr_dict = dict()
            for key, val in cfg_dict.items():
                if key.startswith(prfx):
                    extr_key = ":".join(key.split(":")[1:])
                    extr_dict[extr_key] = val
            extr_dicts.append(extr_dict)
        model_cs = self.Model.get_configuration_space(self.system)
        ctrlr_cs = self.Controller.get_configuration_space(
            self.system, self.task, self.Model)
        cost_fact_cs = self.cost_factory.get_configuration_space(
            self.system, self.task, self.Model)
        cfg1_dict = model_cs.get_default_configuration().get_dictionary()
        cfg2_dict = ctrlr_cs.get_default_configuration().get_dictionary()
        cfg3_dict = cost_fact_cs.get_default_configuration().get_dictionary()
        self.assertEqual(extr_dicts[0], cfg1_dict)
        self.assertEqual(extr_dicts[1], cfg2_dict)
        self.assertEqual(extr_dicts[2], cfg3_dict)
예제 #4
0
    def test_config_space(self):
        factory = QuadCostFactory()
        cs = factory.get_configuration_space(self.system, self.task,
                                             self.Model)
        self.assertIsInstance(cs, CS.ConfigurationSpace)

        hyper_names = cs.get_hyperparameter_names()
        target_hyper_names = ["x_Q", "y_Q", "x_F", "y_F", "u_R"]
        self.assertEqual(set(hyper_names), set(target_hyper_names))
예제 #5
0
    def test_call_factory(self):
        factory = QuadCostFactory()
        cs = factory.get_configuration_space(self.system, self.task,
                                             self.Model)
        cfg = cs.get_default_configuration()

        cost = factory(self.system, self.task, self.model, None, cfg)

        self.assertIsInstance(cost, QuadCost)
        Q, R, F = cost.get_cost_matrices()

        self.assertTrue((Q == np.eye(self.system.obs_dim)).all())
        self.assertTrue((F == np.eye(self.system.obs_dim)).all())
        self.assertTrue((R == np.eye(self.system.ctrl_dim)).all())
예제 #6
0
class PipelineTest(unittest.TestCase):
    def setUp(self):
        simple_sys = ampc.System(["x", "y"], ["u"])
        simple_sys.dt = 0.05
        self.system = simple_sys
        self.model_factory = SINDyFactory(self.system)
        self.cost_factory = QuadCostFactory(self.system)
        self.controller_factory = IterativeLQRFactory(self.system)

        # Initialize task
        Q = np.eye(2)
        R = np.eye(1)
        F = np.eye(2)
        cost = QuadCost(self.system, Q, R, F, goal=[-1, 0])
        self.task = Task(self.system)
        self.task.set_cost(cost)
        self.task.set_ctrl_bound("u", -20.0, 20.0)

        rng = np.random.default_rng(42)
        dynamics = lambda x, u: dt_doubleint_dynamics(x, u, dt=0.05)
        self.trajs = uniform_random_generate(self.system,
                                             self.task,
                                             dynamics,
                                             rng,
                                             init_min=-np.ones(2),
                                             init_max=np.ones(2),
                                             traj_len=100,
                                             n_trajs=100)

    def test_full_config_space(self):
        pipeline = Pipeline(self.system, self.model_factory, self.cost_factory,
                            self.controller_factory)
        pipeline_cs = pipeline.get_configuration_space()
        model_cs = self.model_factory.get_configuration_space()
        cost_cs = self.cost_factory.get_configuration_space()
        controller_cs = self.controller_factory.get_configuration_space()

        pipeline_hns = pipeline_cs.get_hyperparameter_names()
        combined_hns = (
            ["_model:" + hn for hn in model_cs.get_hyperparameter_names()] +
            ["_cost:" + hn for hn in cost_cs.get_hyperparameter_names()] + [
                "_ctrlr:" + hn
                for hn in controller_cs.get_hyperparameter_names()
            ])

        self.assertEqual(set(pipeline_hns), set(combined_hns))

    def test_config_space_fixed_model(self):
        model_cs = self.model_factory.get_configuration_space()
        model_cfg = model_cs.get_default_configuration()
        model = self.model_factory(model_cfg, self.trajs)
        self.assertTrue(isinstance(model, SINDy))

        pipeline = Pipeline(self.system, model, self.cost_factory,
                            self.controller_factory)
        pipeline_cs = pipeline.get_configuration_space()
        model_cs = self.model_factory.get_configuration_space()
        cost_cs = self.cost_factory.get_configuration_space()
        controller_cs = self.controller_factory.get_configuration_space()

        pipeline_hns = pipeline_cs.get_hyperparameter_names()
        combined_hns = (
            ["_cost:" + hn for hn in cost_cs.get_hyperparameter_names()] + [
                "_ctrlr:" + hn
                for hn in controller_cs.get_hyperparameter_names()
            ])

        self.assertEqual(set(pipeline_hns), set(combined_hns))

    def test_config_space_fixed_cost(self):
        cost = self.task.get_cost()

        pipeline = Pipeline(self.system, self.model_factory, cost,
                            self.controller_factory)
        pipeline_cs = pipeline.get_configuration_space()
        model_cs = self.model_factory.get_configuration_space()
        cost_cs = self.cost_factory.get_configuration_space()
        controller_cs = self.controller_factory.get_configuration_space()

        pipeline_hns = pipeline_cs.get_hyperparameter_names()
        combined_hns = (
            ["_model:" + hn for hn in model_cs.get_hyperparameter_names()] + [
                "_ctrlr:" + hn
                for hn in controller_cs.get_hyperparameter_names()
            ])

        self.assertEqual(set(pipeline_hns), set(combined_hns))

    def test_pipeline_call(self):
        pipeline = Pipeline(self.system, self.model_factory, self.cost_factory,
                            self.controller_factory)
        pipeline_cs = pipeline.get_configuration_space()
        pipeline_cfg = pipeline_cs.get_default_configuration()
        controller, task, model = pipeline(pipeline_cfg, self.task, self.trajs)

        self.assertIsInstance(controller, IterativeLQR)
        self.assertIsInstance(task, Task)
        self.assertIsInstance(model, SINDy)