def simulate_transfer(state, action, next_state_id): aug_traj=self.transferer.transfer(self.demos[action], state, plotting=False) self.lfd_env.execute_augmented_trajectory(aug_traj, step_viewer=0) result_state = self.lfd_env.observe_scene() # Get the rope simulation object and determine if it's a knot for sim_obj in self.lfd_env.sim.sim_objs: if isinstance(sim_obj, simulation_object.RopeSimulationObject): rope_sim_obj = sim_obj break rope_knot = is_knot(rope_sim_obj.rope.GetControlPoints()) return (result_state, next_state_id, rope_knot)
def simulate_transfer(state, action, next_state_id): aug_traj = self.transferer.transfer(self.demos[action], state, plotting=False) self.lfd_env.execute_augmented_trajectory(aug_traj, step_viewer=0) result_state = self.lfd_env.observe_scene() # Get the rope simulation object and determine if it's a knot for sim_obj in self.lfd_env.sim.sim_objs: if isinstance(sim_obj, simulation_object.RopeSimulationObject): rope_sim_obj = sim_obj break rope_knot = is_knot(rope_sim_obj.rope.GetControlPoints()) return (result_state, next_state_id, rope_knot)
def eval_on_holdout(args, action_selection, reg_and_traj_transferer, lfd_env, sim): """TODO Args: action_selection: ActionSelection reg_and_traj_transferer: RegistrationAndTrajectoryTransferer lfd_env: LfdEnvironment sim: DynamicSimulation """ holdoutfile = h5py.File(args.eval.holdoutfile, 'r') holdout_items = eval_util.get_indexed_items(holdoutfile, task_list=args.tasks, task_file=args.taskfile, i_start=args.i_start, i_end=args.i_end) rope_params = sim_util.RopeParams() if args.eval.rope_param_radius is not None: rope_params.radius = args.eval.rope_param_radius if args.eval.rope_param_angStiffness is not None: rope_params.angStiffness = args.eval.rope_param_angStiffness num_successes = 0 num_total = 0 for i_task, demo_id_rope_nodes in holdout_items: redprint("task %s" % i_task) init_rope_nodes = demo_id_rope_nodes["rope_nodes"][:] rope = RopeSimulationObject("rope", init_rope_nodes, rope_params) sim.add_objects([rope]) sim.settle(step_viewer=args.animation) for i_step in range(args.eval.num_steps): redprint("task %s step %i" % (i_task, i_step)) sim_util.reset_arms_to_side(sim) if args.animation: sim.viewer.Step() sim_state = sim.get_state() sim.set_state(sim_state) scene_state = lfd_env.observe_scene() # plot cloud of the test scene handles = [] if args.plotting: handles.append(sim.env.plot3(scene_state.cloud[:,:3], 2, scene_state.color if scene_state.color is not None else (0,0,1))) sim.viewer.Step() eval_stats = eval_util.EvalStats() start_time = time.time() if len(scene_state.cloud) == 0: redprint("Detected 0 points in scene") break try: (agenda, q_values_root), goal_found = action_selection.plan_agenda(scene_state, i_step) except ValueError: #e.g. if cloud is empty - any action is hopeless redprint("**Raised Value Error during action selection") break eval_stats.action_elapsed_time += time.time() - start_time eval_stats.generalized = True num_actions_to_try = MAX_ACTIONS_TO_TRY if args.eval.search_until_feasible else 1 for i_choice in range(num_actions_to_try): if q_values_root[i_choice] == -np.inf: # none of the demonstrations generalize eval_stats.generalized = False break redprint("TRYING %s"%agenda[i_choice]) best_root_action = str(agenda[i_choice]) start_time = time.time() try: test_aug_traj = reg_and_traj_transferer.transfer(GlobalVars.demos[best_root_action], scene_state, plotting=args.plotting) except ValueError: # If something is cloud/traj is empty or something redprint("**Raised value error during traj transfer") break eval_stats.feasible, eval_stats.misgrasp = lfd_env.execute_augmented_trajectory(test_aug_traj, step_viewer=args.animation, interactive=args.interactive, check_feasible=args.eval.check_feasible) eval_stats.exec_elapsed_time += time.time() - start_time if not args.eval.check_feasible or eval_stats.feasible: # try next action if TrajOpt cannot find feasible action and we care about feasibility break else: sim.set_state(sim_state) knot = is_knot(rope.rope.GetControlPoints()) results = {'scene_state':scene_state, 'best_action':best_root_action, 'values':q_values_root, 'aug_traj':test_aug_traj, 'eval_stats':eval_stats, 'sim_state':sim_state, 'knot':knot, 'goal_found': goal_found} eval_util.save_task_results_step(args.resultfile, i_task, i_step, results) if not eval_stats.generalized: assert not knot break if args.eval.check_feasible and not eval_stats.feasible: # Skip to next knot tie if the action is infeasible -- since # that means all future steps (up to 5) will have infeasible trajectories assert not knot break if knot: num_successes += 1 break; sim.remove_objects([rope]) num_total += 1 redprint('Eval Successes / Total: ' + str(num_successes) + '/' + str(num_total)) redprint('Success Rate: ' + str(float(num_successes)/num_total))