Ejemplo n.º 1
0
 def configure(self, sim_args):
     print("varrrr")
     print(vars(sim_args))
     self.arg = sim_args
     self.set_scene(number='11be354e8dbd5d7275c486a5037ea949')
     self.set_goal()
     self._sim = Simulator(vars(sim_args))
     common.attach_exit_handler(self._sim)
     self._sim.start()
Ejemplo n.º 2
0
def main():
    parser = argparse.ArgumentParser(description='Simulator console client')
    args = parse_sim_args(parser)
    sim = Simulator(vars(args))
    try:
        print('Starting simulator...')
        if sim.start():
            print('Simulator started.')
            interactive_loop(sim, args.scene_ids, 0)
    except:
        traceback.print_exc()
        print('Error running simulator. Aborting.')

    if sim is not None:
        sim.kill()
        del sim
Ejemplo n.º 3
0
def run(args):
    if args.scenes:
        scenes = edict(common.load_scenes_file(args.scenes))
        scene_ids = [
            r.id for r in scenes[args.split]
            if args.min_rooms < r.nrooms < args.max_rooms
        ]
    else:
        scene_ids = args.scene_ids

    sim = Simulator(vars(args))
    common.attach_exit_handler(sim)
    sim.init()
    sim.seed(random.randint(0, 12345678))

    f = open(args.output, 'w')
    n_scenes = len(scene_ids)
    for i in range(0, n_scenes):
        process_scene(sim, args.scene.dataset, scene_ids[i], f, args.level,
                      args.num_levels, args.samples_per_scene, i)
Ejemplo n.º 4
0
    nepisodes = args.episodes_per_scene
    nscenes = args.num_scenes or len(scene_ids)
    async = args. async

    print(
        'Benchmarking %d simulators (%s) with %d scenes, %d episodes each scene, %d steps each episode'
        % (nsims, 'async' if async else 'sync', nscenes, nepisodes, nsteps))

    total_secs_from_start = 0
    total_secs_from_init = 0
    total_steps = 0
    episode = 0

    sims = []
    for i in range(0, nsims):
        sims.append(Simulator(vars(args)))
    common.attach_exit_handler(sims)
    process_simulators(sims, act=lambda s: s.init(), async=async)

    init_time = timer()
    print(
        'scene,episode,nsteps,secs_no_setup,fps_no_setup,secs_with_setup,fps_with_setup'
    )
    try:

        def start_sim(sim, reset_only):
            sim.seed(random.randint(0, 12345678))
            if reset_only:
                sim.reset()
            else:
                sim.start()
Ejemplo n.º 5
0
class DangEnv(gym.Env):
    metadata = {'render.modes': ['human', 'rgb_array']}

    def __init__(self):
        self._last_state = None
        self._sim = None
        self.viewer = None
        self.actions = []
        action_name = [
            'idle', 'moveTo', 'forwards', 'backwards', 'strafeLeft',
            'strafeRight', 'turnLeft', 'turnRight', 'lookUp', 'lookDown'
        ]

        for name in action_name:
            action = {'name': name, 'strength': 1, 'angle': math.radians(5)}
            self.actions.append(action)
        self.arg = None
        self.id_scene = None
        self.id_object = None
        self.goalObject = None
        #TODO: select id scene as well as set id object
        # default=['00a76592d5cc7d92eef022393784a2de', '066e8a32fd15541e814a9eafa82abf5d',
        # '11be354e8dbd5d7275c486a5037ea949'],

    def configure(self, sim_args):
        print("varrrr")
        print(vars(sim_args))
        self.arg = sim_args
        self.set_scene(number='11be354e8dbd5d7275c486a5037ea949')
        self.set_goal()
        self._sim = Simulator(vars(sim_args))
        common.attach_exit_handler(self._sim)
        self._sim.start()
        # for i, x in enumerate(sim_args):
        #     print(x)
        #     print(sim_args[x])

        # self._sim_obs_space = self._sim.get_observation_space(sim_args['outputs'])
        # print("Enterrrrrrrrrrrrrrrrrrrrrrrrrrrrrrreeddddddddddddddddddddddddddddddddd configure")
        # #self.action_space = spaces.Discrete(self._sim.num_buttons)
        # self.action_space = spaces.MultiBinary(self._sim.num_buttons)
        # self.screen_height = self._sim_obs_space['color'].shape[1]
        # self.screen_width = self._sim_obs_space['color'].shape[0]
        # self.observation_space = spaces.Box(low=0, high=255,
        #     shape=(self.screen_height, self.screen_width, 3))
        # # TODO: have more complex observation space with additional modalities and measurements
        # # obs_space = self._sim.get_observation_space
        # #self.observation_space = spaces.Dict({"images": ..., "depth": ...})
    def set_scene(self, number=None):
        self.id_scene = number
        self.arg['scene_ids'][0] = number
        self.arg['fullId'] = self.arg.source + '.' + number
        self.arg['scene']['fullId'] = self.arg['fullId']
        return self.id_scene

    # select goal by collection in target.py
    # todo goal can be selected freely
    def set_goal(self, goal_number=None):
        if self.id_scene == None:
            print(
                "selecting default goal for scene cause dont not exist scene ID"
            )
            self.goalObject = None
            return
        self.goalObject = list_sceneLocation[self.id_scene][0]
        try:
            self.goalObject = list_sceneLocation[self.id_scene][goal_number]
        except Exception as e:
            print("Warning error in set_goal ")

        return True

    def set_object(self, number=None):
        self.id_object = number
        return self.id_object

    @property
    def simulator(self):
        return self._sim

    def _seed(self, seed=None):

        self.np_random, seed = seeding.np_random(seed)
        return [seed]

    def _reset(self):
        """Resets the state of the environment and returns an initial observation.
        Returns: observation (object): the initial observation of the
            space.
        """
        if self._last_state != None:
            res = self._sim.reset()
            observation = self._sim.get_last_observation()
            return observation
        else:
            return None

    def _step(self, action):

        response = self._sim.step(action, 1)

        if response is None:
            print("error")
            sys.exit()

        # state = self._sim.step(action)
        self._last_state = self._sim.get_last_observation(
        )  # Last observed state
        observation = self._last_state
        print(observation)
        reward, done = self.check_reward(observation)
        return observation, reward, done

    def _render(self, mode='human', close=False):

        if close:
            if self.viewer is not None:
                self.viewer.close()
                self.viewer = None  # If we don't None out this reference pyglet becomes unhappy
            return
        if self._last_state is not None:
            img = self._last_state['observation']['sensors']['color']['data']
            if len(img.shape) == 2:  # assume gray
                img = np.dstack([img, img, img])
            else:  # assume rgba
                img = img[:, :, :-1]
            img = img.reshape((img.shape[1], img.shape[0], img.shape[2]))
            if mode == 'human':
                from gym.envs.classic_control import rendering
                if self.viewer is None:
                    if self.viewer is None:
                        self.viewer = rendering.SimpleImageViewer()
                self.viewer.imshow(img)
            elif mode == 'rgb_array':
                return img

    def _close(self):
        if self._sim is not None:
            self._sim.kill()
            del self._sim

    def action_list(self):
        # # Generic actions
        # { name: 'idle' }
        # { name: 'moveTo', position: <vector3>, angle: <radians> }

        # # Movement
        # { name: 'forwards', strength: <multiplier> }
        # { name: 'backwards', strength: <multiplier> }
        # { name: 'strafeLeft', strength: <multiplier> }
        # { name: 'strafeRight', strength: <multiplier> }

        # # Rotation
        # { name: 'turnLeft', strength: <multiplier> }
        # { name: 'turnRight', strength: <multiplier> }

        # # Look
        # { name: 'lookUp', angle: <radians> }
        # { name: 'lookDown', angle: <radians> }

        return self.actions

    def check_reward(self, observation):
        reward = -1
        done = False
        position = observation['info']['agent_state']['position']
        print(position)
        print(self.goalObject)
        similarity = distance(position, self.goalObject)
        print(similarity)
        if similarity < 0.1:
            reward = 1000
        if reward == 1000:
            done = True
        return reward, done
Ejemplo n.º 6
0
def main():
    global VIDEO_WRITER
    parser = argparse.ArgumentParser(description='Interactive interface to Simulator')
    parser.add_argument('--navmap', action='store_true',
                        default=False,
                        help='Use navigation map')
    group = parser.add_mutually_exclusive_group()
    group.add_argument('--state_set_file',
                       help='State set file')
    group.add_argument('--replay',
                       help='Load and replay action trace from file')
    group.add_argument('--replay_mode',
                       choices=REPLAY_MODES,
                       default='positions',
                       help='Use actions or positions for replay')
    group.add_argument('--show_goals', action='store_true',
                       default=False,
                       help='show goal observations')

    args = parse_sim_args(parser)
    args.visualize_sensors = True
    sim = Simulator(vars(args))
    common.attach_exit_handler(sim)

    if 'state_set_file' in args and args.state_set_file is not None:
        args.state_set = StateSet(args.state_set_file, 1)
    if 'save_video' in args and len(args.save_video):
        filename = args.save_video if type(args.save_video) is str else 'out.mp4'
        is_rgb = args.color_encoding == 'rgba'
        VIDEO_WRITER = VideoWriter(filename, framerate=24, resolution=(args.width, args.height), rgb=is_rgb)
    if 'replay' in args and args.replay is not None:
        print('Initializing simulator using action traces %s...' % args.replay)
        args.action_traces = ActionTraces(args.replay)
        action_trace = args.action_traces.next_trace()
        sim.init()
        start_state = action_trace.start_state()
        print('start_state', start_state)
        sim.configure(start_state)
    else:
        args.action_traces = None
        args.replay = None

    try:
        print('Starting simulator...')
        ep_info = sim.start()
        if ep_info:
            print('observation_space', sim.get_observation_space())
            sim.episode_info = ep_info
            print('Simulator started.')
            interactive_loop(sim, args)
    except:
        traceback.print_exc()
        print('Error running simulator. Aborting.')

    if sim is not None:
        sim.kill()
        del sim

    if VIDEO_WRITER is not None:
        VIDEO_WRITER.close()
Ejemplo n.º 7
0
def main():
    global VIDEO_WRITER
    parser = argparse.ArgumentParser(description='Interactive interface to Simulator')
    parser.add_argument('--navmap', action='store_true',
                        default=False,
                        help='Use navigation map')
    group = parser.add_mutually_exclusive_group()
    group.add_argument('--state_set_file',
                       help='State set file')
    group.add_argument('--replay',
                       help='Load and replay action trace from file')
    parser.add_argument('--rightcamera',
                       default='True',
                       help='Right camera setting')
    parser.add_argument('--depthright',
                       default='True',
                       help='Right camera setting')
    parser.add_argument('--leftcamera',
                       default='True',
                       help='Left camera setting')
    parser.add_argument('--depthleft',
                       default='True',
                       help='Left camera setting')
    parser.add_argument('--replay_mode',
                       choices=REPLAY_MODES,
                       default='positions',
                       help='Use actions or positions for replay')
    group.add_argument('--show_goals', action='store_true',
                       default=False,
                       help='show goal observations')
    parser.add_argument('--patch_len', action='store_true',
                       default=30,
                       help='patch_length used to calculate toc in center of the image')
    parser.add_argument('--save_toc', action='store_true',
                       default=True,
                       help='associates images with time of contact')
    parser.add_argument('--save_rootdir',
                       default="/media/fadhil/ThirdPartition/3_3DVision/2_Dataset/minosdata/",
                       help='save rootdir for the generated dataset')

    args = parse_sim_args(parser)
    args.visualize_sensors = True
    sim = Simulator(vars(args))
    common.attach_exit_handler(sim)

    if 'state_set_file' in args and args.state_set_file is not None:
        args.state_set = StateSet(args.state_set_file, 1)
    if 'save_video' in args and len(args.save_video):
        filename = args.save_video if type(args.save_video) is str else 'out.mp4'
        is_rgb = args.color_encoding == 'rgba'
        VIDEO_WRITER = VideoWriter(filename, framerate=24, resolution=(args.width, args.height), rgb=is_rgb)
    if 'replay' in args and args.replay is not None:
        print('Initializing simulator using action traces %s...' % args.replay)
        args.action_traces = ActionTraces(args.replay)
        action_trace = args.action_traces.next_trace()
        sim.init()
        sim.seed(random.randint(0, 12345678))
        start_state = action_trace.start_state()
        print('start_state', start_state)
        sim.configure(start_state)
    else:
        args.action_traces = None
        args.replay = None

    try:
        print('Starting simulator...')
        ep_info = sim.start()
        if ep_info:
            print('observation_space', sim.get_observation_space())
            sim.episode_info = ep_info
            print('Simulator started.')
            interactive_loop(sim, args)
    except:
        traceback.print_exc()
        print('Error running simulator. Aborting.')

    if sim is not None:
        sim.kill()
        del sim

    if VIDEO_WRITER is not None:
        VIDEO_WRITER.close()