Exemple #1
0
    def get_run_loop(self, show_pbar: bool = None) -> Iterable[int]:
        """
        Return a tqdm progress bar or a regular range iterator.

        If the code is running in an IPython kernel it will also display the \
        internal ``_notebook_container``.

        Args:
            show_pbar: If ``False`` the progress bar will not be displayed.

        Returns:
            A Progressbar if ``show_pbar`` is ``True`` and the code is running \
            in an IPython kernel. If the code is running in a terminal the logging \
            level must be set at least to "INFO". Otherwise return a range iterator \
            for ``self.max_range`` iteration.

        """
        show_pbar = show_pbar if show_pbar is not None else self.show_pbar
        use_tqdm = (
            show_pbar if running_in_ipython() else self._log.level < logging.WARNING and show_pbar
        )
        loop_iterable = (
            trange(self.max_epochs, desc="%s" % self.__class__.__name__)
            if use_tqdm
            else range(self.max_epochs)
        )
        if running_in_ipython() and self._use_notebook_widget:
            from IPython.core.display import display

            display(self._notebook_container)
        return loop_iterable
Exemple #2
0
 def report_progress(self):
     """Report information of the current run."""
     if running_in_ipython() and self._use_notebook_widget:
         line_break = '<br style="line-height:1px; content: "  ";>'
         html = str(self).replace("\n\n", "\n").replace("\n", line_break)
         # Add strong formatting for headers
         html = html.replace("Walkers States", "<strong>Walkers States</strong>")
         html = html.replace("Model States", "<strong>Model States</strong>")
         html = html.replace("Environment States", "<strong>Environment Model</strong>")
         self._notebook_container.value = "%s" % html
     elif not running_in_ipython():
         self._log.info(repr(self))
Exemple #3
0
    def setup_notebook_container(self):
        """Display the display widgets if the Swarm is running in an IPython kernel."""
        if running_in_ipython() and self._use_notebook_widget:
            from ipywidgets import HTML
            from IPython.core.display import display, HTML as cell_html

            # Set font weight of tqdm progressbar
            display(cell_html("<style> .widget-label {font-weight: bold !important;} </style>"))
            self._notebook_container = HTML()
Exemple #4
0
    def __init__(self,
                 n_walkers: int,
                 env: Callable[[], BaseEnvironment],
                 model: Callable[[BaseEnvironment], BaseModel],
                 walkers: Callable[..., Walkers] = Walkers,
                 reward_scale: float = 1.0,
                 distance_scale: float = 1.0,
                 tree: Callable[[], HistoryTree] = None,
                 report_interval: int = numpy.inf,
                 show_pbar: bool = True,
                 use_notebook_widget: bool = True,
                 force_logging: bool = False,
                 *args,
                 **kwargs):
        """
        Initialize a :class:`Swarm`.

        Args:
            n_walkers: Number of walkers of the swarm.
            env: A callable that returns an instance of an :class:`Environment`.
            model: A callable that returns an instance of a :class:`Model`.
            walkers: A callable that returns an instance of :class:`BaseWalkers`.
            reward_scale: Virtual reward exponent for the reward score.
            distance_scale: Virtual reward exponent for the distance score.
            tree: class:`StatesTree` that keeps track of the visited states.
            report_interval: Display the algorithm progress every ``report_interval`` epochs.
            show_pbar: If ``True`` A progress bar will display the progress of \
                       the algorithm run.
            use_notebook_widget: If ``True`` and the class is running in an IPython \
                                kernel it will display the evolution of the swarm \
                                in a widget.
            force_logging: If ``True``, disable al ``ipython`` related behaviour.
            *args: Additional args passed to init_swarm.
            **kwargs: Additional kwargs passed to init_swarm.

        """
        self._prune_tree = False
        self._epoch = 0
        self.show_pbar = show_pbar
        self.report_interval = report_interval
        super(Swarm, self).__init__(walkers=walkers,
                                    env=env,
                                    model=model,
                                    n_walkers=n_walkers,
                                    reward_scale=reward_scale,
                                    distance_scale=distance_scale,
                                    tree=tree,
                                    *args,
                                    **kwargs)
        self._notebook_container = None
        self._use_notebook_widget = use_notebook_widget
        self._ipython_mode = running_in_ipython() and not force_logging
        self.setup_notebook_container()
Exemple #5
0
    def __init__(self,
                 n_walkers: int,
                 step_epochs: int = None,
                 root_model: Callable[[], RootModel] = MajorityDiscreteModel,
                 tree: Callable[[], BaseTree] = None,
                 report_interval: int = numpy.inf,
                 show_pbar: bool = True,
                 walkers: Callable[..., StepWalkers] = StepWalkers,
                 swarm: Callable[..., Swarm] = Swarm,
                 reward_limit: Scalar = None,
                 max_epochs: int = None,
                 accumulate_rewards: bool = True,
                 minimize: bool = False,
                 use_notebook_widget: bool = True,
                 force_logging: bool = False,
                 *args,
                 **kwargs):
        """
        Initialize a :class:`StepSwarm`.

        This class can be initialized the same way as a :class:`Swarm`. All the \
        parameters except ``max_epochs`` and ``tree`` will be used to initialize \
        the internal swarm, and the :class:`StepSwarm` will use them when necessary.

        The internal swarm will be initialized with no tree, and with its \
        notebook widgets deactivated.

        Args:
            n_walkers: Number of walkers of the internal swarm.
            step_epochs: Number of epochs that the internal swarm will be run \
                         before sampling an action.
            root_model: Callable that returns a :class:`RootModel` that will be \
                        used to sample the actions and dt of the root walker.
            tree: Disabled for now. It will be used by the root walker.
            report_interval: Display the algorithm progress every \
                            ``report_interval`` epochs.
            show_pbar: If ``True`` A progress bar will display the progress of \
                       the algorithm run.
            walkers: A callable that returns an instance of :class:`StepWalkers`.
            swarm: A callable that returns an instance of :class:`Swarm` and \
                  takes as input all the corresponding parameters provided. It \
                  will be wrapped with a :class:`StoreInitAction` before \
                  assigning it to the ``internal_swarm`` attribute.
            reward_limit: The algorithm run will stop after reaching this \
                          reward value. If you are running a minimization process \
                          it will be considered the minimum reward possible, and \
                          if you are maximizing a reward it will be the maximum \
                          value.
            max_epochs: Maximum number of steps that the root walker is allowed \
                       to take.
            accumulate_rewards: If ``True`` the rewards obtained after transitioning \
                                to a new state will accumulate. If ``False`` only the last \
                                reward will be taken into account.
            minimize: If ``True`` the algorithm will perform a minimization \
                      process. If ``False`` it will be a maximization process.
            use_notebook_widget: If ``True`` and the class is running in an IPython \
                                kernel it will display the evolution of the swarm \
                                in a widget.
            force_logging: If ``True``, disable al ``ipython`` related behaviour.
            *args: Passed to ``swarm``.
            **kwargs: Passed to ``swarm``.

        """
        self.internal_swarm = StoreInitAction(
            swarm(max_epochs=step_epochs,
                  show_pbar=False,
                  report_interval=numpy.inf,
                  n_walkers=n_walkers,
                  tree=None,
                  walkers=walkers,
                  accumulate_rewards=accumulate_rewards,
                  minimize=minimize,
                  use_notebook_widget=False,
                  *args,
                  **kwargs))
        self.internal_swarm.reset()
        self.root_model: RootModel = root_model()
        if reward_limit is None:
            reward_limit = -numpy.inf if self.internal_swarm.walkers.minimize else numpy.inf
        self.accumulate_rewards = accumulate_rewards
        self._max_epochs = int(max_epochs)
        self.reward_limit = reward_limit
        self.show_pbar = show_pbar
        self.report_interval = report_interval
        self.tree = tree() if tree is not None else tree
        self._epoch = 0
        self._walkers: StepWalkers = self.internal_swarm.walkers
        self._model = self.internal_swarm.model
        self._env = self.internal_swarm.env
        self.cum_reward = numpy.NINF
        self.minimize = minimize
        self.root_model_states = self.walkers.model_states[0]
        self.root_env_states = self.walkers.env_states[0]
        self.root_walkers_states = self.walkers.states[0]
        self.root_walker = OneWalker(
            reward=self.root_env_states.rewards[0],
            observ=self.root_env_states.observs[0],
            state=self.root_env_states.states[0],
        )
        self._notebook_container = None
        self._use_notebook_widget = use_notebook_widget
        self._ipython_mode = running_in_ipython() and not force_logging
        self.setup_notebook_container()