Skip to content

SMTSurrogate

mdo_framework.core.surrogates.SMTSurrogate

Wrapper for SMT surrogate models, supporting single and multi-fidelity.

Source code in src/mdo_framework/core/surrogates.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
class SMTSurrogate:
    """Wrapper for SMT surrogate models, supporting single and multi-fidelity."""

    def __init__(self, model_type="KRG", multi_fidelity=False):
        self.model_type = model_type
        self.multi_fidelity = multi_fidelity
        self.model = None

    def train(
        self,
        xt: np.ndarray,
        yt: np.ndarray,
        x_lf: np.ndarray = None,
        y_lf: np.ndarray = None,
    ):
        """Trains the surrogate model.

        Args:
            xt: High-fidelity training inputs.
            yt: High-fidelity training outputs.
            x_lf: Low-fidelity training inputs (for multi-fidelity).
            y_lf: Low-fidelity training outputs (for multi-fidelity).

        """
        n_dims = xt.shape[1]

        if self.multi_fidelity:
            self.model = MFK(theta0=[1e-2] * n_dims, print_global=False)

            if x_lf is not None and y_lf is not None:
                self.model.set_training_values(x_lf, y_lf, name=0)

            self.model.set_training_values(xt, yt)

        else:
            if self.model_type == "KRG":
                self.model = KRG(theta0=[1e-2] * n_dims, print_global=False)
            elif self.model_type == "KPLS":
                # KPLS reduces dimensions. n_comp defaults to 1.
                # theta0 must match n_comp, NOT n_dims.
                # Default n_comp=1 in SMT KPLS.
                n_comp = 1
                self.model = KPLS(
                    theta0=[1e-2] * n_comp,
                    n_comp=n_comp,
                    print_global=False,
                )
            else:
                raise ValueError(f"Unknown model type: {self.model_type}")

            self.model.set_training_values(xt, yt)

        self.model.train()

    def predict(self, x: np.ndarray) -> np.ndarray:
        """Predicts values at x."""
        return self.model.predict_values(x)

    def predict_variances(self, x: np.ndarray) -> np.ndarray:
        """Predicts variances at x."""
        return self.model.predict_variances(x)

predict(x)

Predicts values at x.

Source code in src/mdo_framework/core/surrogates.py
66
67
68
def predict(self, x: np.ndarray) -> np.ndarray:
    """Predicts values at x."""
    return self.model.predict_values(x)

predict_variances(x)

Predicts variances at x.

Source code in src/mdo_framework/core/surrogates.py
70
71
72
def predict_variances(self, x: np.ndarray) -> np.ndarray:
    """Predicts variances at x."""
    return self.model.predict_variances(x)

train(xt, yt, x_lf=None, y_lf=None)

Trains the surrogate model.

Parameters:

Name Type Description Default
xt ndarray

High-fidelity training inputs.

required
yt ndarray

High-fidelity training outputs.

required
x_lf ndarray

Low-fidelity training inputs (for multi-fidelity).

None
y_lf ndarray

Low-fidelity training outputs (for multi-fidelity).

None
Source code in src/mdo_framework/core/surrogates.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
def train(
    self,
    xt: np.ndarray,
    yt: np.ndarray,
    x_lf: np.ndarray = None,
    y_lf: np.ndarray = None,
):
    """Trains the surrogate model.

    Args:
        xt: High-fidelity training inputs.
        yt: High-fidelity training outputs.
        x_lf: Low-fidelity training inputs (for multi-fidelity).
        y_lf: Low-fidelity training outputs (for multi-fidelity).

    """
    n_dims = xt.shape[1]

    if self.multi_fidelity:
        self.model = MFK(theta0=[1e-2] * n_dims, print_global=False)

        if x_lf is not None and y_lf is not None:
            self.model.set_training_values(x_lf, y_lf, name=0)

        self.model.set_training_values(xt, yt)

    else:
        if self.model_type == "KRG":
            self.model = KRG(theta0=[1e-2] * n_dims, print_global=False)
        elif self.model_type == "KPLS":
            # KPLS reduces dimensions. n_comp defaults to 1.
            # theta0 must match n_comp, NOT n_dims.
            # Default n_comp=1 in SMT KPLS.
            n_comp = 1
            self.model = KPLS(
                theta0=[1e-2] * n_comp,
                n_comp=n_comp,
                print_global=False,
            )
        else:
            raise ValueError(f"Unknown model type: {self.model_type}")

        self.model.set_training_values(xt, yt)

    self.model.train()