def test_dmp_save_and_load_bug_83(): random_state = np.random.RandomState(0) dmp = DMPBehavior(execution_time=1.0, dt=0.001) dmp.init(6, 6) x0 = np.zeros(2) g = np.ones(2) dmp.set_meta_parameters(["x0", "g"], [x0, g]) w = dmp.get_params() w = random_state.randn(*w.shape) * 1000.0 dmp.set_params(w) Y, Yd, Ydd = dmp.trajectory() try: dmp.save("tmp_dmp_model2.yaml") dmp.save_config("tmp_dmp_config2.yaml") dmp = DMPBehavior(configuration_file="tmp_dmp_model2.yaml") dmp.init(6, 6) dmp.load_config("tmp_dmp_config2.yaml") finally: if os.path.exists("tmp_dmp_model2.yaml"): os.remove("tmp_dmp_model2.yaml") if os.path.exists("tmp_dmp_config2.yaml"): os.remove("tmp_dmp_config2.yaml") Y2, Yd2, Ydd2 = dmp.trajectory() assert_array_almost_equal(Y, Y2) assert_array_almost_equal(Yd, Yd2) assert_array_almost_equal(Ydd, Ydd2)
def test_dmp_get_set_params(): beh = DMPBehavior() beh.init(3 * n_task_dims, 3 * n_task_dims) assert_equal(beh.get_n_params(), 50 * n_task_dims) params = beh.get_params() assert_array_equal(params, np.zeros(50 * n_task_dims)) random_state = np.random.RandomState(0) expected_params = random_state.randn(50 * n_task_dims) beh.set_params(expected_params) actual_params = beh.get_params() assert_array_equal(actual_params, expected_params)
def test_dmp_change_weights(): beh = DMPBehavior() beh.init(3 * n_task_dims, 3 * n_task_dims) beh.set_params(np.ones(50 * n_task_dims)) xva = np.zeros(3 * n_task_dims) beh.reset() while beh.can_step(): eval_loop(beh, xva) assert_array_almost_equal(xva[:n_task_dims], np.zeros(n_task_dims), decimal=3) assert_array_almost_equal(xva[n_task_dims:-n_task_dims], np.zeros(n_task_dims), decimal=2) assert_array_almost_equal(xva[-n_task_dims:], np.zeros(n_task_dims), decimal=1)
class DMPBehavior(BlackBoxBehavior): """Dynamical Movement Primitive. Parameters ---------- execution_time : float, optional (default: 1) Execution time of the DMP in seconds. dt : float, optional (default: 0.01) Time between successive steps in seconds. n_features : int, optional (default: 50) Number of RBF features for each dimension of the DMP. configuration_file : string, optional (default: None) Name of a configuration file that should be used to initialize the DMP. If it is set all other arguments will be ignored. """ def __init__(self, execution_time=1.0, dt=0.01, n_features=50, configuration_file=None): self.dmp = DMPBehaviorImpl(execution_time, dt, n_features, configuration_file) def init(self, n_inputs, n_outputs): """Initialize the behavior. Parameters ---------- n_inputs : int number of inputs n_outputs : int number of outputs """ self.dmp.init(3 * n_inputs, 3 * n_outputs) self.n_joints = n_inputs self.x = np.empty(3 * self.n_joints) self.x[:] = np.nan def reset(self): self.dmp.reset() self.x[:] = 0.0 def set_inputs(self, inputs): self.x[:self.n_joints] = inputs[:] def can_step(self): return self.dmp.can_step() def step(self): self.dmp.set_inputs(self.x) self.dmp.step() self.dmp.get_outputs(self.x) def get_outputs(self, outputs): outputs[:] = self.x[:self.n_joints] def get_n_params(self): return self.dmp.get_n_params() def get_params(self): return self.dmp.get_params() def set_params(self, params): self.dmp.set_params(params) def set_meta_parameters(self, keys, values): self.dmp.set_meta_parameters(keys, values) def trajectory(self): return self.dmp.trajectory()