def inaccurate_physics(): physics = scene_if.Physics() physics.friction = 0.1 physics.restitution = 0.5 physics.density = 0.5 physics.gravity = -5 return physics
def simulate_task( task: task_if.Task, steps: int = DEFAULT_MAX_STEPS, stride: int = DEFAULT_STRIDE, physics: scene_if.Physics = scene_if.Physics() ) -> task_if.TaskSimulation: result = simulator_bindings.simulate_task(serialize(task), steps, stride, serialize(physics)) return deserialize(task_if.TaskSimulation(), result)
def convert_scenes_to_task_simulation( task: task_if.Task, scenes: List[scene_if.Scene], physics: scene_if.Physics = scene_if.Physics(), px_threshold: float = 0.1): threshold = px_threshold / PIXELS_IN_METER result = simulator_bindings.convert_scenes_to_task_simulation( serialize(task), serialize(scenes), serialize(physics), threshold) return deserialize(task_if.TaskSimulation(), result)
def simulate_scene( scene: scene_if.Scene, steps: int = DEFAULT_MAX_STEPS, stride: int = 1, physics: scene_if.Physics = scene_if.Physics() ) -> List[scene_if.Scene]: serialized_scenes = simulator_bindings.simulate_scene( serialize(scene), steps, stride, serialize(physics)) scenes = [deserialize(scene_if.Scene(), b) for b in serialized_scenes] return scenes
def inaccurate_noisy_physics(rng=np.random.default_rng()): physics = scene_if.Physics() physics.friction = sample_gaussian(0.1, 0.05, rng=rng) physics.restitution = sample_gaussian(1, 0.05, rng=rng) physics.density = sample_gaussian(0.5, 0.05, rng=rng) physics.angularDamping = sample_gaussian(0.1, 0.01, rng=rng) physics.linearDamping = sample_gaussian(0.1, 0.01, rng=rng) physics.gravity = sample_gaussian(-9.8, 0.05, rng=rng) return physics
def default_noisy_physics(rng=np.random.default_rng()): physics = scene_if.Physics() physics.friction = sample_gaussian(physics.friction, 0.1, rng=rng) physics.angularDamping = sample_gaussian(physics.angularDamping, 0.05, rng=rng) physics.linearDamping = sample_gaussian(physics.linearDamping, 0.05, rng=rng) physics.density = sample_gaussian(physics.density, 0.1, rng=rng) physics.restitution = sample_gaussian(physics.restitution, 0.1, rng=rng) physics.gravity = sample_gaussian(physics.gravity, 0.1, rng=rng) return physics
def simulate_task_with_input(task, user_input, steps=DEFAULT_MAX_STEPS, stride=DEFAULT_STRIDE, keep_space_around_bodies=True, physics=scene_if.Physics()): """Check a solution for a task and return SimulationResult. This is un-optimized version of magic_ponies that should be used for debugging or vizualization purposes only. """ if not isinstance(user_input, scene_if.UserInput): user_input = build_user_input(*user_input) # Creating a shallow copy. task = copy.copy(task) task.scene = add_user_input_to_scene(task.scene, user_input, keep_space_around_bodies) return simulate_task(task, steps, stride, physics)
def simulate_single(self, task_index: int, action: ActionLike, need_images: bool = True, stride: int = phyre.simulator.DEFAULT_STRIDE, stable: bool = False, physics:scene_if.Physics=scene_if.Physics() ) -> Tuple[SimulationStatus, MaybeImages]: """Deprecated in version 0.2.0 in favor of simulate_action. Runs simluation for the action. Args: task_index: index of the task. action: numpy array or list of self.action_space_dim floats in [0, 1]. need_images: whether simulation images are needed. stride: int, defines the striding for the simulation images array. Higher striding will result in less images and less compute. Note, that this parameter doesn't affect simulation FPS. Ignored if need_images is False. stable: if True, will simulate a few actions in the neigborhood of the actions and return STABLY_SOLVED status iff all neigbour actions are either SOLVED or INVALID. Otherwise UNSTABLY_SOLVED is returned. SOLVED is never returned if stable is set. Returns: * If need_images is True, returns a pair (status, images). * If status is INVALID_INPUT images is None. * Otherwise images is an array contains intermediate observations. * If need_images is False: returns (status, None). """ simulation = self.simulate_action(task_index, action, need_images=need_images, need_featurized_objects=False, stride=stride, stable=stable,physics=physics) return simulation.status, simulation.images
def batched_magic_ponies(tasks, user_inputs, num_workers, steps=DEFAULT_MAX_STEPS, stride=DEFAULT_STRIDE, keep_space_around_bodies=True, with_times=False, need_images=False, need_featurized_objects=False, physics=scene_if.Physics()): del num_workers # Not used. return tuple( zip(*[ magic_ponies(t, ui, steps=steps, stride=stride, keep_space_around_bodies=keep_space_around_bodies, with_times=with_times, need_images=need_images, need_featurized_objects=need_featurized_objects, physics=physics) for t, ui in zip(tasks, user_inputs) ]))
def magic_ponies(task, user_input, steps=DEFAULT_MAX_STEPS, stride=DEFAULT_STRIDE, keep_space_around_bodies=True, with_times=False, need_images=False, need_featurized_objects=False, physics=scene_if.Physics()): """Check a solution for a task and return intermidiate images. Args: task: task_if.Task or bytes, in the latter case a serialzed task is expected. user_input: scene_if.UserInput or a triple(points, rectangulars, balls) points: None or a list or an array of points. Should be of shape (N, 2). In the latter case is assumed to be in row-major format. rectangulars: A list of lists of 4 verticies. Each vertix should be a pair of floats. Vertices must be on positive order and must form a convex polygon. Otherwise the input will be deemed invalid. balls: A list of triples (x, y, radius). steps: Maximum number of steps to simulate for. stride: Stride for the returned image array. Negative values will produce not images. keep_space_around_bodies: bool, if True extra empty space will be enforced around scene bodies. with_times: A boolean flag indicating whether timing info is required. need_images: A boolean flag indicating whether images should be returned. need_featurized_objects: A boolean flag indicating whether objects should be returned. Returns: A tuple (is_solved, had_occlusions, images, objects) if with_times is False. is_solved: bool. had_occlusions: bool. images a numpy arrays of shape (num_steps, height, width). objects is a numpy array of shape (num_steps, num_objects, feature_size). A tuple (is_solved, had_occlusions, images, scenes, simulation_time, pack_time) if with_times is set. simulation_time: time spent inside C++ code to unpack and simulate. pack_time: time spent inside C++ code to pack the result. """ if isinstance(task, bytes): serialized_task = task height, width = creator.SCENE_HEIGHT, creator.SCENE_WIDTH else: serialized_task = serialize(task) height, width = task.scene.height, task.scene.width if isinstance(user_input, scene_if.UserInput): is_solved, had_occlusions, packed_images, packed_featurized_objects, number_objects, sim_time, pack_time = ( simulator_bindings.magic_ponies_general(serialized_task, serialize(user_input), keep_space_around_bodies, steps, stride, need_images, need_featurized_objects, serialize(physics))) else: points, rectangulars, balls = _prepare_user_input(*user_input) is_solved, had_occlusions, packed_images, packed_featurized_objects, number_objects, sim_time, pack_time = ( simulator_bindings.magic_ponies(serialized_task, points, rectangulars, balls, keep_space_around_bodies, steps, stride, need_images, need_featurized_objects, serialize(physics))) packed_images = np.array(packed_images, dtype=np.uint8) images = packed_images.reshape((-1, height, width)) packed_featurized_objects = np.array(packed_featurized_objects, dtype=np.float32) if packed_featurized_objects.size == 0: # Custom task without any known objects. packed_featurized_objects = np.zeros( (0, number_objects, OBJECT_FEATURE_SIZE)) else: packed_featurized_objects = packed_featurized_objects.reshape( (-1, number_objects, OBJECT_FEATURE_SIZE)) packed_featurized_objects = phyre.simulation.finalize_featurized_objects( packed_featurized_objects) if with_times: return is_solved, had_occlusions, images, packed_featurized_objects, sim_time, pack_time else: return is_solved, had_occlusions, images, packed_featurized_objects
def default_physics(): # all noise params set to 0, and physics params set to default values physics = scene_if.Physics() return physics
def simulate_action(self, task_index: int, action: ActionLike, *, need_images: bool = True, need_featurized_objects: bool = False, stride: int = phyre.simulator.DEFAULT_STRIDE, stable: bool = False, physics:scene_if.Physics=scene_if.Physics()) -> phyre.simulation.Simulation: """Runs simluation for the action. Args: task_index: index of the task. action: numpy array or list of self.action_space_dim floats in [0, 1]. need_images: whether simulation images are needed. need_featurized_objects: whether simulation featurized_objects are needed. stride: int, defines the striding for the simulation images array. Higher striding will result in less images and less compute. Note, that this parameter doesn't affect simulation FPS. Ignored if need_images is False. stable: if True, will simulate a few actions in the neigborhood of the actions and return STABLY_SOLVED status iff all neigbour actions are either SOLVED or INVALID. Otherwise UNSTABLY_SOLVED is returned. SOLVED is never returned if stable is set. Returns: * phyre.simulation.Simulation object containing the result of the simulation. * SimulationStatus, images, and featurized_objects are easily accesible with simulation.simulation_status, simulation.images, and simulation.featurized_objects. """ user_input, is_valid = self._get_user_input(action) if not is_valid: return phyre.simulation.Simulation( status=SimulationStatus.INVALID_INPUT) main_status, images, objects = self._simulate_user_input( task_index, user_input, need_images, need_featurized_objects, stride,physics) if not stable or not main_status.is_solved(): return phyre.simulation.Simulation(status=main_status, images=images, featurized_objects=objects) for modified_user_input in _yield_user_input_neighborhood(user_input): status, _, _ = self._simulate_user_input( task_index, modified_user_input, need_images=False, need_featurized_objects=False, stride=stride,physics=physics) if status.is_not_solved(): return phyre.simulation.Simulation( status=SimulationStatus.UNSTABLY_SOLVED, images=images, featurized_objects=objects) return phyre.simulation.Simulation( status=SimulationStatus.STABLY_SOLVED, images=images, featurized_objects=objects)