class_docstr: Population Based Training algorithm
  
  Warning:PBT was broken in version v0.2.4. Make sure to use latest release.
  
  Population based training is an evolutionary algorithm that evolve trials
  from low fidelity levels to high fidelity levels (ex: number of epochs).
  For a population of size `m`, it first samples `m` trials at lowest fidelity level.
  When trials are completed, it decides based on the ``exploit`` configuration whether
  the trial should be promoted to next fidelity level or whether another trial
  should be selected instead and forked. When a trial is forked, new hyperparameters are
  selected based on the trials hyperparameters and the ``explore`` configuration.
  The original trial's working_dir is then copied over to the new trial's working_dir
  so that the user script can resume execution from model parameters of original trial.
  
  It is important that the weights of models trained for each trial are saved in the corresponding
  directory at path ``trial.working_dir``. The file name does not matter. The entire directory is
  copied to a new ``trial.working_dir`` when PBT selects a good model and explore new
  hyperparameters. The new trial can be resumed by the user by loading the weights found in the
  freshly copied ``new_trial.working_dir``, and saved back at the same path at end of trial
  execution. To access ``trial.working_dir`` from Oríon's commandline API, see documentation at
  https://orion.readthedocs.io/en/stable/user/script.html#command-line-templating. To access
  ``trial.working_dir`` from Oríon's Python API, set argument ``trial_arg="trial"`` when executing
  method :py:meth:`orion.client.experiment.ExperimentClient.workon`.
  
  The number of fidelity levels is determined by the argument ``generations``. The lowest
  and highest fidelity levels, and the distrubition, is determined by the search space's
  dimension that will have a prior ``fidelity(low, high, base)``, where ``base`` is the
  logarithm base of the dimension. Original PBT algorithm uses a base of 1.
  
  PBT will try to return as many trials as possible when calling ``suggest(num)``, up to ``num``.
  When ``population_size`` trials are sampled and more trials are requested, it will try to
  generate new trials by promoting or forking existing trials in a queue. This queue will get
  filled when calling ``observe(trials)`` on completed or broken trials.
  
  If trials are broken at lowest fidelity level, they are ignored and will not count
  in population size so that PBT can sample additional trials to reach ``population_size``
  completed trials at lowest fidelity. If a trial is broken at higher fidelity, the
  original trial leading to the broken trial is examined again for ``exploit`` and ``explore``.
  If the broken trial was the result of a fork, then we backtrack to the trial that was dropped
  during ``exploit`` in favor of the forked trial. If the broken trial was a promotion, then
  we backtrack to the original trial that was promoted.
  
  For more information on the algorithm,
  see original paper at https://arxiv.org/abs/1711.09846.
  
  Jaderberg, Max, et al. "Population based training of neural networks."
  arXiv preprint, arXiv:1711.09846 (2017).
  
  Notes
  -----
  It is important that the experiment using this algorithm has a working directory properly
  set. The experiment's working dir serve as the base for the trial's working directories.
  
  The trial's working directory is ``trial.working_dir``. This is where the weights of the model
  should be saved. Using ``trial.hash_params`` to determine a unique working dir for the trial
  will result in working on a different directory than the one copied by PBT, hence missing the
  copied model parameters.
  
  Parameters
  ----------
  space: `orion.algo.space.Space`
      Optimisation space with priors for each dimension.
  seed: None, int or sequence of int
      Seed for the random number generator used to sample new trials.
      Default: ``None``
  population_size: int, optional
      Size of the population. No trial will be continued until there are `population_size`
      trials executed until lowest fidelity. If a trial is broken during execution at lowest
      fidelity, the algorithm will sample a new trial, keeping the population of *non-broken*
      trials at `population_size`.  For efficiency it is better to have less workers running than
      population_size. Default: 50.
  generations: int, optional
      Number of generations, from lowest fidelity to highest one. This will determine how
      many branchings occur during the execution of PBT. Default: 10
  exploit: dict or None, optional
      Configuration for a ``pbt.exploit.BaseExploit`` object that determines
      when if a trial should be exploited or not. If None, default configuration
      is a ``PipelineExploit`` with ``BacktrackExploit`` and ``TruncateExploit``.
  explore: dict or None, optional
      Configuration for a ``pbt.explore.BaseExplore`` object that returns new parameter
      values for exploited trials. If None, default configuration is a ``PipelineExplore`` with
      ``ResampleExplore`` and ``PerturbExplore``.
  fork_timeout: int, optional
      Maximum amount of time in seconds that an attempt to mutate a trial should take, otherwise
      algorithm.suggest() will raise ``SuggestionTimeout``. Default: 60
docstr: Try to promote or fork up to ``num`` trials from the queue.
docstr_tok: ["Try","to","promote","or","fork","up","to","``","num","``","trials","from","the","queue","."]
code: def _fork_lineages(self, num: int) -> list[Trial]:
      """Try to promote or fork up to ``num`` trials from the queue."""
  
      branched_trials: list[Trial] = []
      skipped_trials: list[Trial] = []
  
      while len(branched_trials) < num and self._queue:
          trial = self._queue.pop(0)
  
          trial_to_branch, new_trial = self._generate_offspring(trial)
          if trial_to_branch is None:
              logger.debug("Skipping trial %s", trial)
              skipped_trials.append(trial)
              continue
          # NOTE: new_trial isn't None if trial_to_branch is not None.
          assert new_trial is not None
          self.lineages.fork(trial_to_branch, new_trial)
  
          if trial is not trial_to_branch:
              logger.debug(
                  "Dropped trial %s in favor of %s",
                  trial,
                  trial_to_branch,
              )
              self.lineages.set_jump(trial, new_trial)
  
          logger.debug(
              "Forking trial %s to %s", trial_to_branch, new_trial
          )
  
          branched_trials.append(new_trial)
          self.register(new_trial)
  
      self._queue = skipped_trials + self._queue
  
      return branched_trials
  
imports: from __future__ import annotations
  import copy
  import logging
  import time
  from typing import Any, ClassVar, Iterable, Sequence
  import numpy
  from orion.algo.base import BaseAlgorithm
  from orion.algo.pbt.exploit import exploit_factory
  from orion.algo.pbt.explore import explore_factory
  from orion.algo.random import Random
  from orion.algo.space import Fidelity, Space
  from orion.core.utils.flatten import flatten
  from orion.core.utils.tree import TreeNode
  from orion.core.worker.transformer import TransformedDimension
  from orion.core.worker.trial import Trial
label_desc: Use image node_id 14 for calling the PBT obj's underlying member method code with example usage: obj._fork_lineages(num) and returns: branched_trials