class_docstr: Batch Knowledge Gradient using one-shot optimization.
  
  This computes the batch Knowledge Gradient using fantasies for the outer
  expectation and either the model posterior mean or MC-sampling for the inner
  expectation.
  
  In addition to the design variables, the input `X` also includes variables
  for the optimal designs for each of the fantasy models. For a fixed number
  of fantasies, all parts of `X` can be optimized in a "one-shot" fashion.
class_docstr_tok: ["Batch","Knowledge","Gradient","using","one-shot","optimization",".","This","computes","the","batch","Knowledge","Gradient","using","fantasies","for","the","outer","expectation","and","either","the","model","posterior","mean","or","MC-sampling","for","the","inner","expectation",".","In","addition","to","the","design","variables",",","the","input","`","X","`","also","includes","variables","for","the","optimal","designs","for","each","of","the","fantasy","models",".","For","a","fixed","number","of","fantasies",",","all","parts","of","`","X","`","can","be","optimized","in","a","``","one-shot","''","fashion","."]
docstr: q-Knowledge Gradient (one-shot optimization).
  
  Args:
      model: A fitted model. Must support fantasizing.
      num_fantasies: The number of fantasy points to use. More fantasy
          points result in a better approximation, at the expense of
          memory and wall time. Unused if `sampler` is specified.
      sampler: The sampler used to sample fantasy observations. Optional
          if `num_fantasies` is specified.
      objective: The objective under which the samples are evaluated. If
          `None`, then the analytic posterior mean is used. Otherwise, the
          objective is MC-evaluated (using inner_sampler).
      posterior_transform: An optional PosteriorTransform. If given, this
          transforms the posterior before evaluation. If `objective is None`,
          then the analytic posterior mean of the transformed posterior is
          used. If `objective` is given, the `inner_sampler` is used to draw
          samples from the transformed posterior, which are then evaluated under
          the `objective`.
      inner_sampler: The sampler used for inner sampling. Ignored if the
          objective is `None`.
      X_pending: A `m x d`-dim Tensor of `m` design points that have
          points that have been submitted for function evaluation
          but have not yet been evaluated.
      current_value: The current value, i.e. the expected best objective
          given the observed points `D`. If omitted, forward will not
          return the actual KG value, but the expected best objective
          given the data set `D u X`.
docstr_tok: ["q-Knowledge","Gradient","(","one-shot","optimization",")",".","Args",":","model",":","A","fitted","model",".","Must","support","fantasizing",".","num_fantasies",":","The","number","of","fantasy","points","to","use",".","More","fantasy","points","result","in","a","better","approximation",",","at","the","expense","of","memory","and","wall","time",".","Unused","if","`","sampler","`","is","specified",".","sampler",":","The","sampler","used","to","sample","fantasy","observations",".","Optional","if","`","num_fantasies","`","is","specified",".","objective",":","The","objective","under","which","the","samples","are","evaluated",".","If","`","None","`",",","then","the","analytic","posterior","mean","is","used",".","Otherwise",",","the","objective","is","MC-evaluated","(","using","inner_sampler",")",".","posterior_transform",":","An","optional","PosteriorTransform",".","If","given",",","this","transforms","the","posterior","before","evaluation",".","If","`","objective","is","None","`",",","then","the","analytic","posterior","mean","of","the","transformed","posterior","is","used",".","If","`","objective","`","is","given",",","the","`","inner_sampler","`","is","used","to","draw","samples","from","the","transformed","posterior",",","which","are","then","evaluated","under","the","`","objective","`",".","inner_sampler",":","The","sampler","used","for","inner","sampling",".","Ignored","if","the","objective","is","`","None","`",".","X_pending",":","A","`","m","x","d","`","-dim","Tensor","of","`","m","`","design","points","that","have","points","that","have","been","submitted","for","function","evaluation","but","have","not","yet","been","evaluated",".","current_value",":","The","current","value",",","i.e",".","the","expected","best","objective","given","the","observed","points","`","D","`",".","If","omitted",",","forward","will","not","return","the","actual","KG","value",",","but","the","expected","best","objective","given","the","data","set","`","D","u","X","`","."]
code: def __init__(
      self,
      model: Model,
      num_fantasies: Optional[int] = 64,
      sampler: Optional[MCSampler] = None,
      objective: Optional[MCAcquisitionObjective] = None,
      posterior_transform: Optional[PosteriorTransform] = None,
      inner_sampler: Optional[MCSampler] = None,
      X_pending: Optional[Tensor] = None,
      current_value: Optional[Tensor] = None,
  ) -> None:
      r"""q-Knowledge Gradient (one-shot optimization).
  
      Args:
          model: A fitted model. Must support fantasizing.
          num_fantasies: The number of fantasy points to use. More fantasy
              points result in a better approximation, at the expense of
              memory and wall time. Unused if `sampler` is specified.
          sampler: The sampler used to sample fantasy observations. Optional
              if `num_fantasies` is specified.
          objective: The objective under which the samples are evaluated. If
              `None`, then the analytic posterior mean is used. Otherwise, the
              objective is MC-evaluated (using inner_sampler).
          posterior_transform: An optional PosteriorTransform. If given, this
              transforms the posterior before evaluation. If `objective is None`,
              then the analytic posterior mean of the transformed posterior is
              used. If `objective` is given, the `inner_sampler` is used to draw
              samples from the transformed posterior, which are then evaluated under
              the `objective`.
          inner_sampler: The sampler used for inner sampling. Ignored if the
              objective is `None`.
          X_pending: A `m x d`-dim Tensor of `m` design points that have
              points that have been submitted for function evaluation
              but have not yet been evaluated.
          current_value: The current value, i.e. the expected best objective
              given the observed points `D`. If omitted, forward will not
              return the actual KG value, but the expected best objective
              given the data set `D u X`.
      """
      if sampler is None:
          if num_fantasies is None:
              raise ValueError(
                  "Must specify `num_fantasies` if no `sampler` is provided."
              )
          # base samples should be fixed for joint optimization over X, X_fantasies
          sampler = SobolQMCNormalSampler(
              sample_shape=torch.Size([num_fantasies])
          )
      elif num_fantasies is not None:
          if sampler.sample_shape != torch.Size([num_fantasies]):
              raise ValueError(
                  f"The sampler shape must match num_fantasies={num_fantasies}."
              )
      else:
          num_fantasies = sampler.sample_shape[0]
      super(MCAcquisitionFunction, self).__init__(model=model)
      MCSamplerMixin.__init__(self, sampler=sampler)
      # if not explicitly specified, we use the posterior mean for linear objs
      if (
          isinstance(objective, MCAcquisitionObjective)
          and inner_sampler is None
      ):
          inner_sampler = SobolQMCNormalSampler(
              sample_shape=torch.Size([128])
          )
      elif objective is not None and not isinstance(
          objective, MCAcquisitionObjective
      ):
          raise UnsupportedError(
              "Objectives that are not an `MCAcquisitionObjective` are not supported."
          )
  
      if objective is None and model.num_outputs != 1:
          if posterior_transform is None:
              raise UnsupportedError(
                  "Must specify an objective or a posterior transform when using "
                  "a multi-output model."
              )
          elif not posterior_transform.scalarize:
              raise UnsupportedError(
                  "If using a multi-output model without an objective, "
                  "posterior_transform must scalarize the output."
              )
      self.objective = objective
      self.posterior_transform = posterior_transform
      self.set_X_pending(X_pending)
      self.X_pending: Tensor = self.X_pending
      self.inner_sampler = inner_sampler
      self.num_fantasies: int = num_fantasies
      self.current_value = current_value
  
imports: from __future__ import annotations
  from copy import deepcopy
  from typing import Any, Callable, Dict, Optional, Tuple, Type
  import torch
  from botorch import settings
  from botorch.acquisition.acquisition import AcquisitionFunction, MCSamplerMixin, OneShotAcquisitionFunction
  from botorch.acquisition.analytic import PosteriorMean
  from botorch.acquisition.cost_aware import CostAwareUtility
  from botorch.acquisition.monte_carlo import MCAcquisitionFunction, qSimpleRegret
  from botorch.acquisition.objective import MCAcquisitionObjective, PosteriorTransform
  from botorch.exceptions.errors import UnsupportedError
  from botorch.models.model import Model
  from botorch.sampling.base import MCSampler
  from botorch.sampling.normal import SobolQMCNormalSampler
  from botorch.utils.transforms import concatenate_pending_points, match_batch_shape, t_batch_mode_transform
  from torch import Tensor
label_desc: Use image node_id 1 to create a new qKnowledgeGradient object from inherited base classes: MCAcquisitionFunction, OneShotAcquisitionFunction with example: obj = qKnowledgeGradient(model, num_fantasies, sampler, objective, posterior_transform, inner_sampler, X_pending, current_value)