class_docstr: Population Based Bandits
  
  Warning: PB2 is broken in current version v0.2.4. We are working on a fix to be released in
  v0.2.5, ETA July 2022.
  
  Population Based Bandits is a variant of Population Based Training using probabilistic model
  to guide the search instead of relying on purely random perturbations.
  PB2 implementation uses a time-varying Gaussian process to model the optimization curves
  during training. This implementation is based on ray-tune implementation. Oríon's version
  supports discrete and categorical dimensions, and offers better resiliency to broken
  trials by using back-tracking.
  
  See PBT documentation for more information on how to use PBT algorithms.
  
  For more information on the algorithm,
  see original paper at https://arxiv.org/abs/2002.02518.
  
  Parker-Holder, Jack, Vu Nguyen, and Stephen J. Roberts.
  "Provably efficient online hyperparameter optimization with population-based bandits."
  Advances in Neural Information Processing Systems 33 (2020): 17200-17211.
  
  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``.
  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
class_docstr_tok: ["Population","Based","Bandits","Warning",":","PB2","is","broken","in","current","version","v0.2.4",".","We","are","working","on","a","fix","to","be","released","in","v0.2.5",",","ETA","July","2022",".","Population","Based","Bandits","is","a","variant","of","Population","Based","Training","using","probabilistic","model","to","guide","the","search","instead","of","relying","on","purely","random","perturbations",".","PB2","implementation","uses","a","time-varying","Gaussian","process","to","model","the","optimization","curves","during","training",".","This","implementation","is","based","on","ray-tune","implementation",".","Or\u00edon","'s","version","supports","discrete","and","categorical","dimensions",",","and","offers","better","resiliency","to","broken","trials","by","using","back-tracking",".","See","PBT","documentation","for","more","information","on","how","to","use","PBT","algorithms",".","For","more","information","on","the","algorithm",",","see","original","paper","at","https",":","\/\/arxiv.org\/abs\/2002.02518",".","Parker-Holder",",","Jack",",","Vu","Nguyen",",","and","Stephen","J.","Roberts",".","``","Provably","efficient","online","hyperparameter","optimization","with","population-based","bandits",".","''","Advances","in","Neural","Information","Processing","Systems","33","(","2020",")",":","17200-17211",".","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","``",".","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: Generate new hyperparameters for given trial.
  
  Derived from PB2 explore implementation in Ray (2022/02/18):
  https://github.com/ray-project/ray/blob/master/python/ray/tune/schedulers/pb2.py#L131
docstr_tok: ["Generate","new","hyperparameters","for","given","trial",".","Derived","from","PB2","explore","implementation","in","Ray","(","2022\/02\/18",")",":","https",":","\/\/github.com\/ray-project\/ray\/blob\/master\/python\/ray\/tune\/schedulers\/pb2.py","#","L131"]
code: def _explore(self, space, base: Trial):
      """Generate new hyperparameters for given trial.
  
      Derived from PB2 explore implementation in Ray (2022/02/18):
      https://github.com/ray-project/ray/blob/master/python/ray/tune/schedulers/pb2.py#L131
      """
  
      base_params = flatten(base.params)
  
      data, current = self._get_data_and_current()
      bounds = {dim.name: dim.interval() for dim in space.values()}
  
      df = data.copy()
  
      # Group by trial ID and hyperparams.
      # Compute change in timesteps and reward.
      diff_reward = (
          df.groupby(["Trial"] + list(bounds.keys()))["Reward"]
          .mean()
          .diff()
          .reset_index(drop=True)
      )
      df["y"] = diff_reward
  
      df["R_before"] = df.Reward - df.y
  
      df = df[~df.y.isna()].reset_index(drop=True)
  
      # Only use the last 1k datapoints, so the GP is not too slow.
      df = df.iloc[-1000:, :].reset_index(drop=True)
  
      # We need this to know the T and Reward for the weights.
      if not df[df["Trial"] == self.get_id(base)].empty:
          # N ow specify the dataset for the GP.
          y_raw = np.array(df.y.values)
          # Meta data we keep -> episodes and reward.
          t_r = df[["Budget", "R_before"]]
          hparams = df[bounds.keys()]
          x_raw = pandas.concat([t_r, hparams], axis=1).values
          newpoint = (
              df[df["Trial"] == self.get_id(base)]
              .iloc[-1, :][["Budget", "R_before"]]
              .values
          )
          with control_randomness(self):
              new = select_config(
                  x_raw,
                  y_raw,
                  current,
                  newpoint,
                  bounds,
                  num_f=len(t_r.columns),
              )
  
          new_config = base_params.copy()
          for i, col in enumerate(hparams.columns):
              if isinstance(base_params[col], int):
                  new_config[col] = int(new[i])
              else:
                  new_config[col] = new[i]
  
      else:
          new_config = base_params
  
      return new_config
  
imports: from __future__ import annotations
  import copy
  import logging
  import time
  from typing import Any, ClassVar, Sequence
  import numpy
  import pandas
  from orion.algo.pbt.pb2_utils import import_optional, select_config
  from orion.algo.pbt.pbt import PBT
  from orion.core.utils.flatten import flatten
  from orion.core.utils.random_state import RandomState, control_randomness
  from orion.core.worker.transformer import ReshapedSpace, TransformedSpace
  from orion.core.worker.trial import Trial
label_desc: Use image node_id 7 for calling the PB2 obj's underlying member method code with example usage: obj._explore(space, base) and returns: new_config