qpots.utils.pymoo_problem

Pymoo integration utilities used to optimize sampled posterior objectives with NSGA-II.

class qpots.utils.pymoo_problem.PyMooFunction(func: Callable, n_var: int = 2, n_obj: int = 2, xl=0.0, xu=1.0, device: str | device | None = None, dtype: dtype | None = None)[source]

Bases: Problem

Custom multi-objective test function for use with PyMoo.

This class allows the integration of a PyTorch-based function into the PyMoo framework, enabling multi-objective optimization within the PyMoo environment.

Parameters:
  • func (Callable) – The function to be optimized. It should accept a PyTorch tensor and return a tensor.

  • n_var (int, optional) – Number of input variables (default is 2).

  • n_obj (int, optional) – Number of objective functions (default is 2).

  • xl (float or array-like, optional) – Lower bound(s) for the input variables (default is 0.0).

  • xu (float or array-like, optional) – Upper bound(s) for the input variables (default is 1.0).

_evaluate(x, out, *args, **kwargs)[source]

Evaluate the function on a given set of input points.

This method converts input data from NumPy arrays to PyTorch tensors, computes function values, and stores the results.

Parameters:
  • x (numpy.ndarray) – The input variables as a NumPy array of shape (n_samples, n_var).

  • out (dict) – A dictionary where the function outputs are stored under the key “F”.

  • *args – Additional arguments for compatibility with PyMoo.

  • **kwargs – Additional arguments for compatibility with PyMoo.

Returns:

Updates the out dictionary in-place with computed function values.

Return type:

None

qpots.utils.pymoo_problem.nsga2(problem: Problem, ngen: int = 100, pop_size: int = 100, seed: int = 2436, callback: Callable | None = None)[source]

Perform NSGA-II (Non-dominated Sorting Genetic Algorithm II) optimization.

This function runs the NSGA-II algorithm on a given multi-objective optimization problem.

Parameters:
  • problem (pymoo.core.Problem) – The optimization problem to be solved.

  • ngen (int, optional) – The number of generations to run the optimization (default is 100).

  • pop_size (int, optional) – The size of the population for NSGA-II (default is 100).

  • seed (int, optional) – Random seed for reproducibility (default is 2436).

  • callback (Callable, optional) – An optional callback function to monitor the optimization process.

Returns:

The result of the optimization, containing the Pareto front and other relevant information.

Return type:

pymoo.core.result.Result