1 2 3 4 5 6 7 8 9 10 11 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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
// -*- C++ -*-
/**
* @brief The test file of FunctionalChaosAlgoritm class
*
* Copyright 2005-2025 Airbus-EDF-IMACS-ONERA-Phimeca
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "openturns/OT.hxx"
#include "openturns/OTtestcode.hxx"
using namespace OT;
using namespace OT::Test;
int main(int, char *[])
{
TESTPREAMBLE;
OStream fullprint(std::cout);
try
{
PlatformInfo::SetNumericalPrecision(3);
// Problem parameters
UnsignedInteger dimension = 3;
Scalar a = 7.0;
Scalar b = 0.1;
// Create the Ishigami function
Description inputVariables(dimension);
inputVariables[0] = "xi1";
inputVariables[1] = "xi2";
inputVariables[2] = "xi3";
Description formula(1);
formula[0] = (OSS() << "sin(xi1) + (" << a << ") * (sin(xi2)) ^ 2 + (" << b << ") * xi3^4 * sin(xi1)");
SymbolicFunction model(inputVariables, formula);
// Create the input distribution
Collection<Distribution> marginals(dimension);
marginals[0] = Uniform(-M_PI, M_PI);
marginals[1] = Uniform(-M_PI, M_PI);
marginals[2] = Uniform(-M_PI, M_PI);
JointDistribution distribution(marginals);
// Fix sampling size
const UnsignedInteger samplingSize = 100;
// Get input & output sample
LHSExperiment lhs(distribution, samplingSize);
Sample inputSample = lhs.generate();
Sample outputSample = model(inputSample);
// Validation of results on independent samples
const UnsignedInteger validationSize = 10;
Sample inputValidation = distribution.getSample(validationSize);
Sample outputValidation = model(inputValidation);
// 1) SPC algorithm
// Create the orthogonal basis
Collection<OrthogonalUniVariatePolynomialFamily> polynomialCollection(dimension, LegendreFactory());
LinearEnumerateFunction enumerateFunction(dimension);
OrthogonalProductPolynomialFactory productBasis(polynomialCollection, enumerateFunction);
// Create the adaptive strategy
UnsignedInteger degree = 8;
UnsignedInteger basisSize = enumerateFunction.getStrataCumulatedCardinal(degree);
AdaptiveStrategy adaptiveStrategy = FixedStrategy(productBasis, basisSize);
// Select the fitting algorithm
FittingAlgorithm fittingAlgorithm = KFold();
// LSMSF
LeastSquaresMetaModelSelectionFactory leastSquaresFactory(LARS(), fittingAlgorithm);
// Projection strategy
LeastSquaresStrategy projectionStrategy(inputSample, outputSample, leastSquaresFactory);
FunctionalChaosAlgorithm algo(inputSample, outputSample, distribution, adaptiveStrategy, projectionStrategy);
// Reinitialize the RandomGenerator to see the effect of the sampling method only
RandomGenerator::SetSeed(0);
algo.run();
// Get the results
FunctionalChaosResult result = algo.getResult();
// MetaModelValidation - SPC
Function metamodel(result.getMetaModel());
Sample metamodelPredictions(metamodel(inputValidation));
MetaModelValidation metaModelValidationSPC(outputValidation, metamodelPredictions);
fullprint << "Sparse chaos scoring" << std::endl;
fullprint << "R2 = " << std::setprecision(PlatformInfo::GetNumericalPrecision()) << std::fixed << metaModelValidationSPC.computeR2Score() << std::endl;
fullprint << "Residual sample = " << metaModelValidationSPC.getResidualSample() << std::endl;
// 2) Kriging algorithm
// KrigingAlgorithm
Basis basis(QuadraticBasisFactory(dimension).build());
// model computed
Point scale(3);
scale[0] = 3.52;
scale[1] = 2.15;
scale[2] = 2.99;
Point amplitude(1, 11.41);
CovarianceModel covarianceModel = GeneralizedExponential(scale, amplitude, 2.0);
KrigingAlgorithm algo2(inputSample, outputSample, covarianceModel, basis);
algo2.setOptimizeParameters(false);
algo2.run();
KrigingResult result2 = algo2.getResult();
// MetaModelValidation - KG
Function metamodel2(result2.getMetaModel());
Sample metamodelPredictions2(metamodel2(inputValidation));
MetaModelValidation metaModelValidationKG(outputValidation, metamodelPredictions2);
fullprint << "Kriging scoring" << std::endl;
fullprint << "R2 = " << std::setprecision(PlatformInfo::GetNumericalPrecision()) << std::fixed << metaModelValidationKG.computeR2Score() << std::endl;
PlatformInfo::SetNumericalPrecision(2);
fullprint << "Residual sample = " << metaModelValidationKG.getResidualSample() << std::endl;
}
catch (TestFailed & ex)
{
std::cerr << ex << std::endl;
return ExitCode::Error;
}
return ExitCode::Success;
}
|