def validate_MDP_policy(root_path, flag_with_obs=True, flag_plan=True): s_g = np.array([4.0, 3.0, 0.0]) modality = "haptic" obs_list = [(1.5, 2.00, 1.0, 1.25), (2.0, 1.0, 1.25, 0.5)] if flag_with_obs: file_name = root_path + "/mdp_planenr_obs_" + modality + ".pkl" else: file_name = root_path + "/mdp_planner_" + modality + ".pkl" if flag_plan: human_model = MovementModel() human_model.load_model(root_path) human_model.set_default_param() planner = MDPFixedTimePolicy(tmodel=human_model) if flag_with_obs: planner.gen_env(obs_list) planner.compute_policy(s_g, modality, max_iter=30) else: with open(file_name) as f: planner = pickle.load(f) fig, axes = plt.subplots() planner.visualize_policy(axes) plt.show() if flag_plan: with open(file_name, "w") as f: pickle.dump(planner, f) sim = Simulator(planner) n_trials = 30 traj_list = [] start_time = time.time() for i in range(n_trials): traj_list.append(sim.run_trial((0.5, 0.5, 0.0), s_g, modality, 30.0, tol=0.5)) print "--- %s seconds ---" % (time.time() - start_time) fig, axes = plt.subplots() for i in range(n_trials): t, traj = traj_list[i] axes.plot(traj[:, 0], traj[:, 1]) axes.axis("equal") axes.scatter(s_g[0], s_g[1]) if flag_with_obs: for x, y, w, h in obs_list: rect = Rectangle((x, y), w, h) axes.add_patch(rect) plt.show()
def validate_MDP_policy(root_path, flag_with_obs=True, flag_plan=True): s_g = np.array([4.0, 3.0, 0.0]) modality = "haptic" obs_list = [(1.5, 2.00, 1.0, 1.25), (2.0, 1.0, 1.25, 0.5)] if flag_with_obs: file_name = root_path + "/mdp_planenr_obs_" + modality + ".pkl" else: file_name = root_path + "/mdp_planner_" + modality + ".pkl" if flag_plan: human_model = MovementModel() human_model.load_model(root_path) human_model.set_default_param() planner = MDPFixedTimePolicy(tmodel=human_model) if flag_with_obs: planner.gen_env(obs_list) planner.compute_policy(s_g, modality, max_iter=30) else: with open(file_name) as f: planner = pickle.load(f) fig, axes = plt.subplots() planner.visualize_policy(axes) plt.show() if flag_plan: with open(file_name, "w") as f: pickle.dump(planner, f) sim = Simulator(planner) n_trials = 30 traj_list = [] start_time = time.time() for i in range(n_trials): traj_list.append( sim.run_trial((0.5, 0.5, 0.0), s_g, modality, 30.0, tol=0.5)) print "--- %s seconds ---" % (time.time() - start_time) fig, axes = plt.subplots() for i in range(n_trials): t, traj = traj_list[i] axes.plot(traj[:, 0], traj[:, 1]) axes.axis("equal") axes.scatter(s_g[0], s_g[1]) if flag_with_obs: for x, y, w, h in obs_list: rect = Rectangle((x, y), w, h) axes.add_patch(rect) plt.show()
def simulate_naive_policy(n_trials, s_g, modality, usr): planner = NaivePolicy() model_path = "/home/yuhang/Documents/proactive_guidance/training_data/user" + str(usr) sim = Simulator(planner, model_path) traj_list = [] for i in range(n_trials): traj_list.append(sim.run_trial((-1.0, 2.0, 0.0), s_g, modality, 20.0, tol=0.5)) fig, axes = plt.subplots() for i in range(n_trials): t, traj = traj_list[i] axes.plot(traj[:, 0], traj[:, 1]) axes.axis("equal") axes.scatter(s_g[0], s_g[1]) plt.show()
def validate_free_space_policy(planner, s_g, modality, path, model_path): fig, axes = plt.subplots() planner.visualize_policy(axes) fig.savefig(path + "/value_func.png") sim = Simulator(planner, model_path) n_trials = 30 traj_list = [] for i in range(n_trials): traj_list.append(sim.run_trial((-1.0, 2.0, 0.0), s_g, modality, 30.0, tol=0.5)) fig, axes = plt.subplots() for i in range(n_trials): t, traj = traj_list[i] axes.plot(traj[:, 0], traj[:, 1]) axes.axis("equal") axes.scatter(s_g[0], s_g[1]) fig.savefig(path + "/simulation.png")
def validate_free_space_policy(planner, s_g, modality, path, model_path): fig, axes = plt.subplots() planner.visualize_policy(axes) fig.savefig(path + "/value_func.png") sim = Simulator(planner, model_path) n_trials = 30 traj_list = [] for i in range(n_trials): traj_list.append( sim.run_trial((-1.0, 2.0, 0.0), s_g, modality, 30.0, tol=0.5)) fig, axes = plt.subplots() for i in range(n_trials): t, traj = traj_list[i] axes.plot(traj[:, 0], traj[:, 1]) axes.axis("equal") axes.scatter(s_g[0], s_g[1]) fig.savefig(path + "/simulation.png")
def simulate_naive_policy(n_trials, s_g, modality, usr): planner = NaivePolicy() model_path = "/home/yuhang/Documents/proactive_guidance/training_data/user" + str( usr) sim = Simulator(planner, model_path) traj_list = [] for i in range(n_trials): traj_list.append( sim.run_trial((-1.0, 2.0, 0.0), s_g, modality, 20.0, tol=0.5)) fig, axes = plt.subplots() for i in range(n_trials): t, traj = traj_list[i] axes.plot(traj[:, 0], traj[:, 1]) axes.axis("equal") axes.scatter(s_g[0], s_g[1]) plt.show()