API Reference

Problem API

Problem API

UQPyL.problem

The problem module defines the shared modeling protocol used by sampling, analysis, optimization, inference, calibration, and surrogate modeling.

Import

python
from UQPyL.problem import Problem, ModelProblem, Eval

Public Objects

ObjectRole
ProblemDefine a static objective and optional constraint problem.
ModelProblemDefine a simulation problem with optional observations and masks.
ModelEvalContextRuntime context passed to ModelProblem objective, constraint, or evaluation callables.
EvalStandard return object from evaluate().
SpaceDefine a bounded input space with continuous, integer, or discrete variables.
SpaceBaseBase input-space interface.
ProblemBaseBase problem interface used by built-in benchmark problems.
ProblemABCAlias of ProblemBase.
singleFuncDecorator for adapting one-sample objective functions to batched input.
singleEvalDecorator for adapting one-sample Eval functions to batched input.

Problem

Use Problem when objectives and constraints are computed directly from input variables.

python
Problem(
    nInput=None,
    nObj=None,
    ub=None,
    lb=None,
    objFunc=None,
    conFunc=None,
    evaluate=None,
    conWgt=None,
    nCon=0,
    varType=None,
    varSet=None,
    optType="min",
    xLabels=None,
    name=None,
    space=None,
    objLabels=None,
    conLabels=None,
)
ParameterMeaning
nInputNumber of input variables. Required unless space is provided.
nObjNumber of objectives. Required.
ubUpper bounds. Scalar, list, or NumPy array.
lbLower bounds. Scalar, list, or NumPy array.
objFuncObjective callable. Receives batched X, returns shape (n_samples, n_obj).
conFuncConstraint callable. Receives batched X, returns shape (n_samples, n_con).
evaluateCombined evaluation callable. Must return Eval.
conWgtOptional constraint weights.
nConNumber of constraints. Defaults to 0.
varTypeVariable type list. 0 continuous, 1 integer, 2 discrete.
varSetDiscrete value mapping for variables with varType=2.
optTypeOptimization direction. "min", "max", or one value per objective.
xLabelsOptional input labels.
nameOptional problem name.
spaceOptional SpaceBase object. When provided, it defines the input space.
objLabelsOptional objective labels.
conLabelsOptional constraint labels.

Problem accepts exactly one callable configuration:

ConfigurationUse when
objFuncThe problem has objectives only.
objFunc + conFuncThe problem has objectives and constraints.
evaluateObjectives and constraints should be computed together.

Do not combine evaluate with objFunc or conFunc.

Methods

MethodReturnsMeaning
evaluate(X, target=None)EvalEvaluate objectives and constraints.
objFunc(X)np.ndarrayEvaluate objectives only.
conFunc(X)np.ndarray or NoneEvaluate constraints only.
validate(X)np.ndarrayConvert input to 2D and check dimension.
unit_to_space(X, IFlag=True, DFlag=True)np.ndarrayMap unit-space values to problem bounds.
apply_var_type(X, IFlag=True, DFlag=True)np.ndarrayApply integer and discrete variable transforms.
cast_int_vars(X)np.ndarrayRound integer variables.
map_discrete_vars(X)np.ndarrayMap discrete variables to values in varSet.
getOptimum()implementation-specificReturn a known optimum when implemented.

target can be:

ValueMeaning
NoneReturn all available outputs.
"objs"Return objectives only.
"cons"Return constraints only.

Example:

python
import numpy as np

from UQPyL.problem import Problem


def objFunc(X):
    X = np.atleast_2d(X)
    return np.sum(X**2, axis=1, keepdims=True)


problem = Problem(
    nInput=2,
    nObj=1,
    ub=1.0,
    lb=-1.0,
    objFunc=objFunc,
)

res = problem.evaluate([[0.2, 0.3]])
print(res.objs)

ModelProblem

Use ModelProblem when the primary callable is a simulation model.

python
ModelProblem(
    nInput=None,
    nObj=1,
    ub=None,
    lb=None,
    simFunc=None,
    objFunc=None,
    conFunc=None,
    evaluate=None,
    obs=None,
    mask=None,
    conWgt=None,
    nCon=0,
    varType=None,
    varSet=None,
    optType="min",
    xLabels=None,
    name=None,
    space=None,
    objLabels=None,
    conLabels=None,
    simLabels=None,
)
ParameterMeaning
simFuncRequired simulation callable. Receives batched X; returns numeric NumPy array whose first dimension is n_samples.
objFuncOptional objective callable. Receives (X, context).
conFuncOptional constraint callable. Receives (X, context).
evaluateOptional combined callable. Receives (X, context) and returns Eval.
obsOptional 2D observation array with shape (n_time, n_series).
maskOptional boolean mask with the same shape as obs.
simLabelsOptional labels for simulation series.

Other parameters are the same as Problem.

Methods

MethodReturnsMeaning
evaluate(X, target=None)EvalEvaluate simulation, objectives, and constraints.
simFunc(X)np.ndarrayRun the simulation callable and validate output.
buildContext(X)ModelEvalContextRun simulation and build the context object.
objFunc(X, context=None)np.ndarrayEvaluate objectives. Builds context when omitted.
conFunc(X, context=None)np.ndarray or NoneEvaluate constraints. Builds context when omitted.
flattenSim(sim)np.ndarrayFlatten simulation output to (n_samples, n_obs).
flattenObs()np.ndarrayFlatten observations to (n_obs,).
flattenMask()np.ndarrayFlatten mask to (n_obs,).

ModelProblem.evaluate() supports one extra target:

ValueMeaning
"sim"Return simulation output only in Eval.sim.

Example:

python
import numpy as np

from UQPyL.problem import ModelProblem


obs = np.array([[1.0], [2.0]])


def simFunc(X):
    X = np.atleast_2d(X)
    sim = np.zeros((X.shape[0], 2, 1))
    sim[:, 0, 0] = X[:, 0]
    sim[:, 1, 0] = X[:, 1]
    return sim


def objFunc(X, context):
    err = context.sim - context.obs
    return np.mean(err**2, axis=(1, 2)).reshape(-1, 1)


problem = ModelProblem(
    nInput=2,
    nObj=1,
    ub=3.0,
    lb=0.0,
    simFunc=simFunc,
    objFunc=objFunc,
    obs=obs,
)

res = problem.evaluate([[1.0, 2.2]])
print(res.objs)
print(res.sim)

Eval

Eval is the standard return object from evaluate().

python
Eval(objs=None, cons=None, sim=None)
FieldTypeMeaning
objsnp.ndarray or NoneObjective values.
consnp.ndarray or NoneConstraint values. Feasible constraints satisfy cons <= 0.
simnp.ndarray or NoneSimulation output for ModelProblem.
PropertyMeaning
hasObjsTrue when objs is not None.
hasConsTrue when cons is not None.
hasSimTrue when sim is not None.

Space

Space defines input dimension, bounds, labels, and variable type transformations.

python
Space(
    nInput,
    ub,
    lb,
    varType=None,
    varSet=None,
    xLabels=None,
)
ParameterMeaning
nInputNumber of input variables.
ubUpper bounds. Scalar, list, or NumPy array.
lbLower bounds. Scalar, list, or NumPy array.
varTypeOptional variable type list.
varSetRequired mapping for discrete variables.
xLabelsOptional input labels.

Variable types:

ValueMeaning
0Continuous
1Integer
2Discrete

Methods:

MethodReturnsMeaning
validate(X)np.ndarrayConvert input to 2D and check dimension.
transform(X)np.ndarrayValidate input and apply variable type transforms.
unit_to_space(X, IFlag=True, DFlag=True)np.ndarrayMap unit-space values to bounds.
apply_var_type(X, IFlag=True, DFlag=True)np.ndarrayApply integer and discrete transforms.
cast_int_vars(X)np.ndarrayRound integer variables.
map_discrete_vars(X)np.ndarrayMap discrete variables to varSet values.

Attributes:

AttributeMeaning
nInputNumber of input variables.
ubUpper bounds as shape (1, n_input).
lbLower bounds as shape (1, n_input).
varTypeVariable type array.
idxFContinuous variable indices.
idxIInteger variable indices.
idxDDiscrete variable indices.
varSetDiscrete variable value mapping.
xLabelsInput labels.
encoding"real" for continuous spaces, "mix" for mixed spaces.

Decorators

singleFunc

Use singleFunc when an objective function is easier to write for one sample at a time.

python
from UQPyL.problem import Problem, singleFunc


@singleFunc
def objFunc(x):
    return x[0] ** 2 + x[1] ** 2


problem = Problem(nInput=2, nObj=1, ub=1.0, lb=-1.0, objFunc=objFunc)

The wrapped function receives one 1D sample and the wrapper returns a 2D objective array.

singleEval

Use singleEval when a one-sample function returns Eval.

python
import numpy as np

from UQPyL.problem import Eval, Problem, singleEval


@singleEval
def evaluate(x):
    return Eval(
        objs=np.array([x[0] ** 2 + x[1] ** 2]),
        cons=np.array([x[0] + x[1] - 1.0]),
    )


problem = Problem(
    nInput=2,
    nObj=1,
    nCon=1,
    ub=1.0,
    lb=0.0,
    evaluate=evaluate,
)

Benchmark Problems

Built-in benchmark problems can be imported from UQPyL.problem.

Single-Objective Problems

Class
Sphere
Schwefel_2_22
Schwefel_1_22
Schwefel_2_21
Rosenbrock
Step
Quartic
Schwefel_2_26
Rastrigin
Ackley
Griewank
Trid
Bent_Cigar
Discus
Weierstrass
RosenbrockWithCon

Multi-Objective Problems

Class
ZDT1
ZDT2
ZDT3
ZDT4
ZDT6
DTLZ1
DTLZ2
DTLZ3
DTLZ4
DTLZ5
DTLZ6
DTLZ7

Example:

python
from UQPyL.problem import Sphere, ZDT1


sphere = Sphere(nInput=10)
zdt1 = ZDT1(nInput=30)

print(sphere.evaluate([[0.0] * 10]).objs)
print(zdt1.evaluate([[0.5] * 30]).objs)