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: Evaluate qKnowledgeGradient on the candidate set `X_actual` by
  solving the inner optimization problem.
  
  Args:
      X: A `b x q x d` Tensor with `b` t-batches of `q` design points
          each. Unlike `forward()`, this does not include solutions of the
          inner optimization problem.
      bounds: A `2 x d` tensor of lower and upper bounds for each column of
          the solutions to the inner problem.
      kwargs: Additional keyword arguments. This includes the options for
          optimization of the inner problem, i.e. `num_restarts`, `raw_samples`,
          an `options` dictionary to be passed on to the optimization helpers, and
          a `scipy_options` dictionary to be passed to `scipy.minimize`.
  
  Returns:
      A Tensor of shape `b`. For t-batch b, the q-KG value of the design
          `X[b]` is averaged across the fantasy models.
          NOTE: If `current_value` is not provided, then this is not the
          true KG value of `X[b]`.
docstr_tok: ["Evaluate","qKnowledgeGradient","on","the","candidate","set","`","X_actual","`","by","solving","the","inner","optimization","problem",".","Args",":","X",":","A","`","b","x","q","x","d","`","Tensor","with","`","b","`","t-batches","of","`","q","`","design","points","each",".","Unlike","`","forward","(",")","`",",","this","does","not","include","solutions","of","the","inner","optimization","problem",".","bounds",":","A","`","2","x","d","`","tensor","of","lower","and","upper","bounds","for","each","column","of","the","solutions","to","the","inner","problem",".","kwargs",":","Additional","keyword","arguments",".","This","includes","the","options","for","optimization","of","the","inner","problem",",","i.e",".","`","num_restarts","`",",","`","raw_samples","`",",","an","`","options","`","dictionary","to","be","passed","on","to","the","optimization","helpers",",","and","a","`","scipy_options","`","dictionary","to","be","passed","to","`","scipy.minimize","`",".","Returns",":","A","Tensor","of","shape","`","b","`",".","For","t-batch","b",",","the","q-KG","value","of","the","design","`","X","[","b","]","`","is","averaged","across","the","fantasy","models",".","NOTE",":","If","`","current_value","`","is","not","provided",",","then","this","is","not","the","true","KG","value","of","`","X","[","b","]","`","."]
code: def evaluate(
      self, X: Tensor, bounds: Tensor, **kwargs: Any
  ) -> Tensor:
      r"""Evaluate qKnowledgeGradient on the candidate set `X_actual` by
      solving the inner optimization problem.
  
      Args:
          X: A `b x q x d` Tensor with `b` t-batches of `q` design points
              each. Unlike `forward()`, this does not include solutions of the
              inner optimization problem.
          bounds: A `2 x d` tensor of lower and upper bounds for each column of
              the solutions to the inner problem.
          kwargs: Additional keyword arguments. This includes the options for
              optimization of the inner problem, i.e. `num_restarts`, `raw_samples`,
              an `options` dictionary to be passed on to the optimization helpers, and
              a `scipy_options` dictionary to be passed to `scipy.minimize`.
  
      Returns:
          A Tensor of shape `b`. For t-batch b, the q-KG value of the design
              `X[b]` is averaged across the fantasy models.
              NOTE: If `current_value` is not provided, then this is not the
              true KG value of `X[b]`.
      """
      if hasattr(self, "expand"):
          X = self.expand(X)
  
      # construct the fantasy model of shape `num_fantasies x b`
      fantasy_model = self.model.fantasize(
          X=X,
          sampler=self.sampler,
      )
  
      # get the value function
      value_function = _get_value_function(
          model=fantasy_model,
          objective=self.objective,
          posterior_transform=self.posterior_transform,
          sampler=self.inner_sampler,
          project=getattr(self, "project", None),
      )
  
      from botorch.generation.gen import gen_candidates_scipy
  
      # optimize the inner problem
      from botorch.optim.initializers import (
          gen_value_function_initial_conditions,
      )
  
      initial_conditions = gen_value_function_initial_conditions(
          acq_function=value_function,
          bounds=bounds,
          num_restarts=kwargs.get("num_restarts", 20),
          raw_samples=kwargs.get("raw_samples", 1024),
          current_model=self.model,
          options={
              **kwargs.get("options", {}),
              **kwargs.get("scipy_options", {}),
          },
      )
  
      _, values = gen_candidates_scipy(
          initial_conditions=initial_conditions,
          acquisition_function=value_function,
          lower_bounds=bounds[0],
          upper_bounds=bounds[1],
          options=kwargs.get("scipy_options"),
      )
      # get the maximizer for each batch
      values, _ = torch.max(values, dim=0)
      if self.current_value is not None:
          values = values - self.current_value
      # NOTE: using getattr to cover both no-attribute with qKG and None with qMFKG
      if getattr(self, "cost_aware_utility", None) is not None:
          values = self.cost_aware_utility(
              X=X, deltas=values, sampler=self.cost_sampler
          )
      # return average over the fantasy samples
      return values.mean(dim=0)
  
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 3 for calling the qKnowledgeGradient obj's underlying member method code with example usage: obj.evaluate(X, bounds) and returns: values