Exemplo n.º 1
0
    def __init__(self, hyperparams, agent):
        self._agent = agent

        self._hyperparams = hyperparams
        self._log_filename = self._hyperparams['log_filename']
        if 'target_filename' in self._hyperparams:
            self._target_filename = self._hyperparams['target_filename']
        else:
            self._target_filename = None

        # GPS Training Status.
        self.mode = config['initial_mode']  # Modes: run, wait, end, request, process.
        self.request = None                 # Requests: stop, reset, go, fail, None.
        self.err_msg = None
        self._colors = {
            'run': 'cyan',
            'wait': 'orange',
            'end': 'red',
        }

        self._actuator_types = config['actuator_types']
        self._actuator_names = config['actuator_names']
        self._first_update = True
        self._actuator_number = 0
        self._actuator_type = self._actuator_types[self._actuator_number]
        self._initial_position = ('unknown', 'unknown', 'unknown')
        self._target_position = ('unknown', 'unknown', 'unknown')

        # Actions.
        actions_arr = [
            Action('stop', 'stop', self.request_stop, axis_pos=0),
            Action('reset', 'reset', self.request_reset, axis_pos=1),
            Action('GCM go', 'go', self.request_go, axis_pos=2),
            Action('transfer learning', 'transfer_learning', self.request_tl, axis_pos=3),

            Action('set initial state', 'initstate', self.request_init_state, axis_pos=4),
            Action('set goal state', 'goalstate', self.request_goal_state, axis_pos=5),
            Action('test transfer learning', 'test_tl', self.request_test_tl, axis_pos=6),
            Action('generalize', 'generalize', self.request_generalize, axis_pos=7),

            Action('mti', 'move_to_initial', self.move_to_initial, axis_pos=8),
            Action('mtt', 'move_to_target', self.move_to_target, axis_pos=9),
            Action('rc', 'relax_controller', self.relax_controller, axis_pos=10),
        ]

        # Setup figure.
        plt.ion()
        plt.rcParams['toolbar'] = 'None'
        for key in plt.rcParams:
            if key.startswith('keymap.'):
                plt.rcParams[key] = ''

        self._fig = plt.figure(figsize=config['figsize'])
        self._fig.subplots_adjust(left=0.01, bottom=0.01, right=0.99, top=0.99,
                wspace=0, hspace=0)

        # Assign GUI component locations.
        self._gs = gridspec.GridSpec(18, 8)
        self._gs_action_panel           = self._gs[0:4,  0:8]
        self._gs_action_output          = self._gs[4:5,  0:4]
        self._gs_status_output          = self._gs[5:6,  0:4]
        self._gs_cost_plotter           = self._gs[4:10,  4:8]
        self._gs_algthm_output          = self._gs[6:10,  0:4]
        self._gs_traj_visualizer    = self._gs[10:18, 0:8]

        # Create GUI components.
        self._action_panel = ActionPanel(self._fig, self._gs_action_panel, 3, 4, actions_arr)
        self._action_output = Textbox(self._fig, self._gs_action_output, border_on=True)
        self._status_output = Textbox(self._fig, self._gs_status_output, border_on=False)
        self._algthm_output = Textbox(self._fig, self._gs_algthm_output,
                max_display_size=config['algthm_output_max_display_size'],
                log_filename=self._log_filename,
                fontsize=config['algthm_output_fontsize'],
                font_family='monospace')
        self._cost_plotter = MeanPlotter(self._fig, self._gs_cost_plotter,
                color='blue', label='mean cost')
        self._traj_visualizer = Plotter3D(self._fig, self._gs_traj_visualizer,
                num_plots=self._hyperparams['conditions'])

        # Setup GUI components.
        self._algthm_output.log_text('\n')
        self.set_output_text(self._hyperparams['info'])
        if config['initial_mode'] == 'run':
            self.run_mode()
        else:
            self.wait_mode()

        # Setup 3D Trajectory Visualizer plot titles and legends
        for m in range(self._hyperparams['conditions']):
            self._traj_visualizer.set_title(m, 'Condition %d' % (m))
        self._traj_visualizer.add_legend(linestyle='-', marker='None',
                color='green', label='Trajectory Samples')
        self._traj_visualizer.add_legend(linestyle='-', marker='None',
                color='blue', label='Policy Samples')
        self._traj_visualizer.add_legend(linestyle='None', marker='x',
                color=(0.5, 0, 0), label='LG Controller Means')
        self._traj_visualizer.add_legend(linestyle='-', marker='None',
                color='red', label='LG Controller Distributions')

        self._fig.canvas.draw()

        # Display calculating thread
        def display_calculating(delay, run_event):
            while True:
                if not run_event.is_set():
                    run_event.wait()
                if run_event.is_set():
                    self.set_status_text('Calculating.')
                    time.sleep(delay)
                if run_event.is_set():
                    self.set_status_text('Calculating..')
                    time.sleep(delay)
                if run_event.is_set():
                    self.set_status_text('Calculating...')
                    time.sleep(delay)

        self._calculating_run = threading.Event()
        self._calculating_thread = threading.Thread(target=display_calculating,
                args=(1, self._calculating_run))
        self._calculating_thread.daemon = True
        self._calculating_thread.start()
Exemplo n.º 2
0
    def __init__(self, hyperparams):
        self._hyperparams = hyperparams
        self._log_filename = self._hyperparams['log_filename']
        if 'target_filename' in self._hyperparams:
            self._target_filename = self._hyperparams['target_filename']
        else:
            self._target_filename = None

        # GPS Training Status.
        self.mode = config[
            'initial_mode']  # Modes: run, wait, end, request, process.
        self.request = None  # Requests: stop, reset, go, fail, None.
        self.err_msg = None
        self._colors = {
            'run': 'cyan',
            'wait': 'orange',
            'end': 'red',
            'stop': 'red',
            'reset': 'yellow',
            'go': 'green',
            'fail': 'magenta',
        }
        self._first_update = True

        # Actions.
        actions_arr = [
            Action('stop', 'stop', self.request_stop, axis_pos=0),
            Action('reset', 'reset', self.request_reset, axis_pos=1),
            Action('go', 'go', self.request_go, axis_pos=2),
            Action('fail', 'fail', self.request_fail, axis_pos=3),
        ]

        # Setup figure.
        plt.ion()
        plt.rcParams['toolbar'] = 'None'
        for key in plt.rcParams:
            if key.startswith('keymap.'):
                plt.rcParams[key] = ''

        self._fig = plt.figure(figsize=config['figsize'])
        self._fig.subplots_adjust(left=0.01,
                                  bottom=0.01,
                                  right=0.99,
                                  top=0.99,
                                  wspace=0,
                                  hspace=0)

        # Assign GUI component locations.
        self._gs = gridspec.GridSpec(16, 8)
        self._gs_action_panel = self._gs[0:1, 0:8]
        self._gs_action_output = self._gs[1:2, 0:4]
        self._gs_status_output = self._gs[2:3, 0:4]
        self._gs_cost_plotter = self._gs[1:3, 4:8]
        self._gs_gt_cost_plotter = self._gs[4:6, 4:8]
        self._gs_algthm_output = self._gs[3:9, 0:4]
        if config['image_on']:
            self._gs_traj_visualizer = self._gs[9:16, 0:4]
            self._gs_image_visualizer = self._gs[9:16, 4:8]
        else:
            self._gs_traj_visualizer = self._gs[9:16, 0:8]

        # Create GUI components.
        self._action_panel = ActionPanel(self._fig, self._gs_action_panel, 1,
                                         4, actions_arr)
        self._action_output = Textbox(self._fig,
                                      self._gs_action_output,
                                      border_on=True)
        self._status_output = Textbox(self._fig,
                                      self._gs_status_output,
                                      border_on=False)
        self._algthm_output = Textbox(
            self._fig,
            self._gs_algthm_output,
            max_display_size=config['algthm_output_max_display_size'],
            log_filename=self._log_filename,
            fontsize=config['algthm_output_fontsize'],
            font_family='monospace')
        self._cost_plotter = MeanPlotter(self._fig,
                                         self._gs_cost_plotter,
                                         color='blue',
                                         label='mean cost')
        self._gt_cost_plotter = MeanPlotter(self._fig,
                                            self._gs_gt_cost_plotter,
                                            color='red',
                                            label='ground truth cost')
        self._traj_visualizer = Plotter3D(
            self._fig,
            self._gs_traj_visualizer,
            num_plots=self._hyperparams['conditions'])
        if config['image_on']:
            self._image_visualizer = ImageVisualizer(
                self._fig,
                self._gs_image_visualizer,
                cropsize=config['image_size'],
                rostopic=config['image_topic'],
                show_overlay_buttons=True)

        # Setup GUI components.
        self._algthm_output.log_text('\n')
        self.set_output_text(self._hyperparams['info'])
        if config['initial_mode'] == 'run':
            self.run_mode()
        else:
            self.wait_mode()

        # Setup 3D Trajectory Visualizer plot titles and legends
        for m in range(self._hyperparams['conditions']):
            self._traj_visualizer.set_title(m, 'Condition %d' % (m))
        self._traj_visualizer.add_legend(linestyle='-',
                                         marker='None',
                                         color='green',
                                         label='Trajectory Samples')
        self._traj_visualizer.add_legend(linestyle='-',
                                         marker='None',
                                         color='blue',
                                         label='Policy Samples')
        self._traj_visualizer.add_legend(linestyle='None',
                                         marker='x',
                                         color=(0.5, 0, 0),
                                         label='LG Controller Means')
        self._traj_visualizer.add_legend(linestyle='-',
                                         marker='None',
                                         color='red',
                                         label='LG Controller Distributions')

        self._fig.canvas.draw()

        # Display calculating thread
        def display_calculating(delay, run_event):
            while True:
                if not run_event.is_set():
                    run_event.wait()
                if run_event.is_set():
                    self.set_status_text('Calculating.')
                    time.sleep(delay)
                if run_event.is_set():
                    self.set_status_text('Calculating..')
                    time.sleep(delay)
                if run_event.is_set():
                    self.set_status_text('Calculating...')
                    time.sleep(delay)

        self._calculating_run = threading.Event()
        self._calculating_thread = threading.Thread(
            target=display_calculating, args=(1, self._calculating_run))
        self._calculating_thread.daemon = True
        self._calculating_thread.start()
realtime_plotter = RealtimePlotter(fig, gs[3],
        labels=['i', 'j', 'i+j', 'i-j', 'mean'],
        alphas=[0.15, 0.15, 0.15, 0.15, 1.0])
run_demo(demo_realtime_plotter)

# Mean Plotter
def demo_mean_plotter():
    i, j = 0, 0
    while True:
        i += random.randint(-10, 10)
        j += random.randint(-10, 10)
        data = [i, j, i + j, i - j]
        mean_plotter.update(data)
        time.sleep(1)

mean_plotter = MeanPlotter(fig, gs[4])
run_demo(demo_mean_plotter)

# Plotter 3D
def demo_plotter_3d():
    xyzs = np.zeros((3, 1))
    while True:
        plotter_3d.clear_all()
        xyz = np.random.randint(-10, 10, size=3).reshape((3,1))
        xyzs = np.append(xyzs, xyz, axis=1)
        xs, ys, zs = xyzs
        plotter_3d.plot(0, xs, ys, zs)
        plotter_3d.draw()  # this must be called explicitly
        time.sleep(1)

plotter_3d = Plotter3D(fig, gs[5], num_plots=1, rows=1, cols=1)