예제 #1
0
def test_rss_runmodel_object():
    """
        Check 'runmodel_object' should be a UQpy.RunModel class object.
    """
    marginals = [Uniform(loc=0., scale=2.), Uniform(loc=0., scale=1.)]
    strata = RectangularStrata(strata_number=[2, 2])
    x = TrueStratifiedSampling(distributions=marginals,
                               strata_object=strata,
                               nsamples_per_stratum=1,
                               random_state=1)
    from UQpy.surrogates.kriging.regression_models import LinearRegression
    from UQpy.surrogates.kriging.correlation_models import ExponentialCorrelation

    K = Kriging(
        regression_model=LinearRegression(),
        correlation_model=ExponentialCorrelation(),
        optimizations_number=20,
        correlation_model_parameters=[1, 1],
        optimizer=MinimizeOptimizer('l-bfgs-b'),
    )
    model = PythonModel(model_script='python_model_function.py',
                        model_object_name="y_func")
    rmodel = RunModel(model=model)
    K.fit(samples=x.samples, values=rmodel.qoi_list)
    with pytest.raises(BeartypeCallHintPepParamException):
        refinement = GradientEnhancedRefinement(strata=x.strata_object,
                                                runmodel_object='abc',
                                                surrogate=K)
        RefinedStratifiedSampling(stratified_sampling=x,
                                  samples_number=6,
                                  samples_per_iteration=2,
                                  refinement_algorithm=refinement)
예제 #2
0
def test_random_state():
    """
        Raises an error if type of random_state is not correct.
    """
    with pytest.raises(BeartypeCallHintPepParamException):
        Kriging(regression_model=LinearRegression(), correlation_model=GaussianCorrelation(),
                correlation_model_parameters=[1], random_state='A')
예제 #3
0
def test_optimizer():
    """
        Raises an error if corr_model_params is not defined.
    """
    with pytest.raises(ValueError):
        Kriging(regression_model=LinearRegression(), correlation_model=GaussianCorrelation(),
                correlation_model_parameters=[1], optimizer='A')
예제 #4
0
def test_missing_correlation_model_parameters():
    """
        Raises an error if corr_model_params is not defined.
    """
    with pytest.raises(TypeError):
        Kriging(regression_model=LinearRegression(), correlation_model=GaussianCorrelation(), bounds=[[0.01, 5]],
                optimizations_number=100, random_state=1)
예제 #5
0
def test_akmcs_expected_feasibility():
    from UQpy.surrogates.kriging.regression_models.LinearRegression import LinearRegression
    from UQpy.surrogates.kriging.correlation_models.ExponentialCorrelation import ExponentialCorrelation

    marginals = [Normal(loc=0., scale=4.), Normal(loc=0., scale=4.)]
    x = MonteCarloSampling(distributions=marginals,
                           nsamples=20,
                           random_state=1)
    model = PythonModel(model_script='series.py', model_object_name="series")
    rmodel = RunModel(model=model)
    regression_model = LinearRegression()
    correlation_model = ExponentialCorrelation()
    K = Kriging(
        regression_model=regression_model,
        correlation_model=correlation_model,
        optimizations_number=10,
        correlation_model_parameters=[1, 1],
        optimizer=MinimizeOptimizer('l-bfgs-b'),
    )
    # OPTIONS: 'U', 'EFF', 'Weighted-U'
    learning_function = ExpectedFeasibility(eff_a=0,
                                            eff_epsilon=2,
                                            eff_stop=0.001)
    a = AdaptiveKriging(distributions=marginals,
                        runmodel_object=rmodel,
                        surrogate=K,
                        learning_nsamples=10**3,
                        n_add=1,
                        learning_function=learning_function,
                        random_state=2)
    a.run(nsamples=25, samples=x.samples)

    assert a.samples[23, 0] == 1.366058523912817
    assert a.samples[20, 1] == -12.914668932772358
예제 #6
0
def test_akmcs_u():
    from UQpy.surrogates.kriging.regression_models.LinearRegression import LinearRegression
    from UQpy.surrogates.kriging.correlation_models.ExponentialCorrelation import ExponentialCorrelation

    marginals = [Normal(loc=0., scale=4.), Normal(loc=0., scale=4.)]
    x = MonteCarloSampling(distributions=marginals,
                           nsamples=20,
                           random_state=1)
    model = PythonModel(model_script='series.py', model_object_name="series")
    rmodel = RunModel(model=model)
    regression_model = LinearRegression()
    correlation_model = ExponentialCorrelation()
    K = Kriging(regression_model=regression_model,
                correlation_model=correlation_model,
                optimizer=MinimizeOptimizer('l-bfgs-b'),
                optimizations_number=10,
                correlation_model_parameters=[1, 1],
                random_state=0)
    # OPTIONS: 'U', 'EFF', 'Weighted-U'
    learning_function = UFunction(u_stop=2)
    a = AdaptiveKriging(distributions=marginals,
                        runmodel_object=rmodel,
                        surrogate=K,
                        learning_nsamples=10**3,
                        n_add=1,
                        learning_function=learning_function,
                        random_state=2)
    a.run(nsamples=25, samples=x.samples)

    assert a.samples[23, 0] == -4.141979058326188
    assert a.samples[20, 1] == -1.6476534435429009
예제 #7
0
def test_akmcs_samples_error():
    from UQpy.surrogates.kriging.regression_models.LinearRegression import LinearRegression
    from UQpy.surrogates.kriging.correlation_models.ExponentialCorrelation import ExponentialCorrelation

    marginals = [Normal(loc=0., scale=4.), Normal(loc=0., scale=4.)]
    x = MonteCarloSampling(distributions=marginals,
                           nsamples=20,
                           random_state=0)
    model = PythonModel(model_script='series.py', model_object_name="series")
    rmodel = RunModel(model=model)
    regression_model = LinearRegression()
    correlation_model = ExponentialCorrelation()
    K = Kriging(regression_model=regression_model,
                correlation_model=correlation_model,
                optimizer=MinimizeOptimizer('l-bfgs-b'),
                optimizations_number=10,
                correlation_model_parameters=[1, 1],
                random_state=1)
    # OPTIONS: 'U', 'EFF', 'Weighted-U'
    learning_function = WeightedUFunction(weighted_u_stop=2)
    with pytest.raises(NotImplementedError):
        a = AdaptiveKriging(distributions=[Normal(loc=0., scale=4.)] * 3,
                            runmodel_object=rmodel,
                            surrogate=K,
                            learning_nsamples=10**3,
                            n_add=1,
                            learning_function=learning_function,
                            random_state=2,
                            samples=x.samples)
예제 #8
0
def test_akmcs_expected_improvement_global_fit():
    from UQpy.surrogates.kriging.regression_models.LinearRegression import LinearRegression
    from UQpy.surrogates.kriging.correlation_models.ExponentialCorrelation import ExponentialCorrelation

    marginals = [Normal(loc=0., scale=4.), Normal(loc=0., scale=4.)]
    x = MonteCarloSampling(distributions=marginals,
                           nsamples=20,
                           random_state=1)
    model = PythonModel(model_script='series.py', model_object_name="series")
    rmodel = RunModel(model=model)
    regression_model = LinearRegression()
    correlation_model = ExponentialCorrelation()
    K = Kriging(
        regression_model=regression_model,
        correlation_model=correlation_model,
        optimizations_number=10,
        correlation_model_parameters=[1, 1],
        optimizer=MinimizeOptimizer('l-bfgs-b'),
    )
    # OPTIONS: 'U', 'EFF', 'Weighted-U'
    learning_function = ExpectedImprovementGlobalFit()
    a = AdaptiveKriging(distributions=marginals,
                        runmodel_object=rmodel,
                        surrogate=K,
                        learning_nsamples=10**3,
                        n_add=1,
                        learning_function=learning_function,
                        random_state=2)
    a.run(nsamples=25, samples=x.samples)

    assert a.samples[23, 0] == 11.939859785098493
    assert a.samples[20, 1] == -8.429899469300118
예제 #9
0
def test_vor_gerss():
    """
    Test the 6 samples generated by GE-RSS using voronoi stratification
    """
    marginals = [Uniform(loc=0., scale=2.), Uniform(loc=0., scale=1.)]
    strata_vor = VoronoiStrata(seeds_number=4, dimension=2, random_state=10)
    x_vor = TrueStratifiedSampling(
        distributions=marginals,
        strata_object=strata_vor,
        nsamples_per_stratum=1,
    )
    from UQpy.surrogates.kriging.regression_models.LinearRegression import LinearRegression
    from UQpy.surrogates.kriging.correlation_models.ExponentialCorrelation import ExponentialCorrelation
    model = PythonModel(model_script='python_model_function.py',
                        model_object_name="y_func")
    rmodel = RunModel(model=model)
    K_ = Kriging(regression_model=LinearRegression(),
                 correlation_model=ExponentialCorrelation(),
                 optimizations_number=20,
                 optimizer=MinimizeOptimizer('l-bfgs-b'),
                 random_state=0,
                 correlation_model_parameters=[1, 1])

    K_.fit(samples=x_vor.samples, values=rmodel.qoi_list)
    z_vor = RefinedStratifiedSampling(
        stratified_sampling=x_vor,
        nsamples=6,
        random_state=x_vor.random_state,
        refinement_algorithm=GradientEnhancedRefinement(
            strata=x_vor.strata_object,
            runmodel_object=rmodel,
            surrogate=K_,
            nearest_points_number=4))
    assert np.allclose(
        z_vor.samples,
        np.array([[1.78345908, 0.01640854], [1.46201137, 0.70862104],
                  [0.4021338, 0.05290083], [0.1062376, 0.88958226],
                  [0.61246269, 0.47160095], [1.16609055, 0.30832536]]))
    assert np.allclose(
        z_vor.samplesU01,
        np.array([[0.89172954, 0.01640854], [0.73100569, 0.70862104],
                  [0.2010669, 0.05290083], [0.0531188, 0.88958226],
                  [0.30623134, 0.47160095], [0.58304527, 0.30832536]]))
예제 #10
0
def test_rect_gerss():
    """
    Test the 6 samples generated by GE-RSS using rectangular stratification
    """
    marginals = [Uniform(loc=0., scale=2.), Uniform(loc=0., scale=1.)]
    strata = RectangularStrata(strata_number=[2, 2], random_state=1)
    x = TrueStratifiedSampling(distributions=marginals,
                               strata_object=strata,
                               nsamples_per_stratum=1)
    model = PythonModel(model_script='python_model_function.py',
                        model_object_name="y_func")
    rmodel = RunModel(model=model)
    from UQpy.surrogates.kriging.regression_models import LinearRegression
    from UQpy.surrogates.kriging.correlation_models import ExponentialCorrelation

    K = Kriging(
        regression_model=LinearRegression(),
        correlation_model=ExponentialCorrelation(),
        optimizations_number=20,
        random_state=0,
        correlation_model_parameters=[1, 1],
        optimizer=MinimizeOptimizer('l-bfgs-b'),
    )
    K.fit(samples=x.samples, values=rmodel.qoi_list)
    refinement = GradientEnhancedRefinement(strata=x.strata_object,
                                            runmodel_object=rmodel,
                                            surrogate=K,
                                            nearest_points_number=4)
    z = RefinedStratifiedSampling(stratified_sampling=x,
                                  random_state=2,
                                  refinement_algorithm=refinement)
    z.run(nsamples=6)
    assert np.allclose(
        z.samples,
        np.array([[0.417022, 0.36016225], [1.00011437, 0.15116629],
                  [0.14675589, 0.5461693], [1.18626021, 0.67278036],
                  [1.51296312, 0.77483124], [0.74237455, 0.66026822]]))
예제 #11
0
def test_wrong_correlation_model():
    """
        Raises an error if corr_model is not callable or a string of an in-built model.
    """
    with pytest.raises(BeartypeCallHintPepParamException):
        Kriging(regression_model=LinearRegression(), correlation_model='A', correlation_model_parameters=[1])
예제 #12
0
import pytest
from UQpy.utilities.MinimizeOptimizer import MinimizeOptimizer
from beartype.roar import BeartypeCallHintPepParamException

from UQpy.surrogates.kriging.Kriging import Kriging
import numpy as np
from UQpy.surrogates.kriging.regression_models import LinearRegression, ConstantRegression
from UQpy.surrogates.kriging.correlation_models import GaussianCorrelation


samples = np.linspace(0, 5, 20).reshape(-1, 1)
values = np.cos(samples)
optimizer = MinimizeOptimizer(method="L-BFGS-B")
krig = Kriging(regression_model=LinearRegression(), correlation_model=GaussianCorrelation(), optimizer=optimizer,
               correlation_model_parameters=[0.14], optimize=False, random_state=1)
krig.fit(samples=samples, values=values, correlation_model_parameters=[0.3])

optimizer = MinimizeOptimizer(method="L-BFGS-B")
krig2 = Kriging(regression_model=ConstantRegression(), correlation_model=GaussianCorrelation(), optimizer=optimizer,
                correlation_model_parameters=[0.3], bounds=[[0.01, 5]],
                optimize=False, optimizations_number=100, normalize=False,
                random_state=2)
krig2.fit(samples=samples, values=values)


# Using the in-built linear regression model as a function
linear_regression_model = Kriging(regression_model=LinearRegression(), correlation_model=GaussianCorrelation(), optimizer=optimizer,
                                  correlation_model_parameters=[1]).regression_model
optimizer = MinimizeOptimizer(method="L-BFGS-B")
gaussian_corrleation_model = Kriging(regression_model=LinearRegression(), correlation_model=GaussianCorrelation(), optimizer=optimizer,
                                     correlation_model_parameters=[1]).correlation_model