def getXEvent(b, t, mu_S, covariance, R, delta_t): full = buildCrossing(b, t, mu_S, covariance, R, delta_t) X = ot.RandomVector(full) f1 = ot.SymbolicFunction(["R", "X1", "X2"], ["X1 - R"]) e1 = ot.ThresholdEvent(ot.CompositeRandomVector(f1, X), ot.Less(), 0.0) f2 = ot.SymbolicFunction(["R", "X1", "X2"], ["X2 - R"]) e2 = ot.ThresholdEvent(ot.CompositeRandomVector(f2, X), ot.GreaterOrEqual(), 0.0) event = ot.IntersectionEvent([e1, e2]) return X, event
def create_monte_carlo(model, inputRandomVector, coefficient_variation): """Create a Monte Carlo algorithm. Parameters ---------- model : OpenTURNS Function. inputRandomVector : OpenTURNS RandomVector, vector of random inputs. coefficient_variation : Float, target for the coefficient of variation of the estimator. """ outputVariableOfInterest = ot.CompositeRandomVector(model, inputRandomVector) # Create an Event from this RandomVector threshold = 30 myEvent = ot.ThresholdEvent(outputVariableOfInterest, ot.Greater(), threshold) myEvent.setName("Deviation > %g cm" % threshold) # Create a Monte Carlo algorithm experiment = ot.MonteCarloExperiment() myAlgoMonteCarlo = ot.ProbabilitySimulationAlgorithm(myEvent, experiment) myAlgoMonteCarlo.setBlockSize(100) myAlgoMonteCarlo.setMaximumCoefficientOfVariation(coefficient_variation) return myAlgoMonteCarlo
def test_DrawFilledEvent(self): # Distribution R = ot.Normal(4.0, 1.0) R.setDescription("R") S = ot.Normal(2.0, 1.0) S.setDescription("S") g = ot.SymbolicFunction(["R", "S"], ["R - S"]) # Event distribution = ot.ComposedDistribution([R, S]) inputRV = ot.RandomVector(distribution) outputRV = ot.CompositeRandomVector(g, inputRV) eventF = ot.ThresholdEvent(outputRV, ot.GreaterOrEqual(), 0) alpha = 1.0 - 1.0e-2 ( bounds, marginalProb, ) = distribution.computeMinimumVolumeIntervalWithMarginalProbability( alpha) # drawEvent = otbenchmark.DrawEvent(eventF) # Avoid failing on CircleCi # _tkinter.TclError: no display name and no $DISPLAY environment variable try: _ = drawEvent.fillEvent(bounds) except Exception as e: print(e)
def test_DrawFilledEvent(self): # Distribution R = ot.Normal(4.0, 1.0) R.setDescription("R") S = ot.Normal(2.0, 1.0) S.setDescription("S") g = ot.SymbolicFunction(["R", "S"], ["R - S"]) # Event inputvector = ot.ComposedDistribution([R, S]) inputRV = ot.RandomVector(inputvector) outputRV = ot.CompositeRandomVector(g, inputRV) eventF = ot.Event(outputRV, ot.GreaterOrEqual(), 0) # # Bounds alphaMin = 0.01 alphaMax = 1 - alphaMin lowerBound = ot.Point( [R.computeQuantile(alphaMin)[0], S.computeQuantile(alphaMin)[0]]) upperBound = ot.Point( [R.computeQuantile(alphaMax)[0], S.computeQuantile(alphaMax)[0]]) bounds = ot.Interval(lowerBound, upperBound) # drawEvent = otbenchmark.DrawEvent(eventF) graph = drawEvent.fillEvent(bounds) assert type(graph) is ot.Graph
def test_ReliabilityBenchmarkProblem(self): limitStateFunction = ot.SymbolicFunction(["R", "S"], ["R - S"]) muR = 4.0 sigmaR = 1.0 muS = 2.0 sigmaS = 1.0 threshold = 0.0 R = ot.Normal(muR, sigmaR) S = ot.Normal(muS, sigmaS) myDistribution = ot.ComposedDistribution([R, S]) inputRandomVector = ot.RandomVector(myDistribution) outputRandomVector = ot.CompositeRandomVector(limitStateFunction, inputRandomVector) thresholdEvent = ot.ThresholdEvent(outputRandomVector, ot.Less(), threshold) name = "R-S" probability = 0.123456789 problem = otb.ReliabilityBenchmarkProblem(name, thresholdEvent, probability) # print(problem) print(problem.toFullString()) p = problem.getProbability() assert p == probability s = problem.getName() assert s == name
def __init__(self, threshold=0.0, expo=1): """ Creates a reliability problem RP54. The event is {g(X) < threshold} where X = (x1, x2, ...., x20) g(X) = (x1 + x2 + ... + x20) - 8.951 Parameters ---------- threshold : float The threshold. expo : float Rate parameter of the Xi Exponential distribution for i in {1, 2, ..., 20}. """ formula = "x1+x2+x3+x4+x5+x6+x7+x8+x9+x10" formula = formula + " + x11+x12+x13+x14+x15+x16+x17+x18+x19+x20-8.951" print(formula) limitStateFunction = ot.SymbolicFunction( [ "x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10", "x11", "x12", "x13", "x14", "x15", "x16", "x17", "x18", "x19", "x20", ], [formula], ) X = [ot.Exponential(expo) for i in range(20)] myDistribution = ot.ComposedDistribution(X) inputRandomVector = ot.RandomVector(myDistribution) outputRandomVector = ot.CompositeRandomVector( limitStateFunction, inputRandomVector ) thresholdEvent = ot.ThresholdEvent(outputRandomVector, ot.Less(), threshold) name = "RP54" probability = 0.000998 super(ReliabilityProblem54, self).__init__(name, thresholdEvent, probability) return None
def __init__(self, threshold=0.0, muR=4.0, sigmaR=1.0, muS=2.0, sigmaS=1.0): """ Create a R-S reliability problem. The event is {g(X) < threshold} where g(R, S) = R - S We have R ~ Normal(muR, sigmaR) and S ~ Normal(muS, sigmaS). Waarts (2000) uses muR = 7.0 and muS = 2.0 leading to beta = 3.54. Parameters ---------- threshold : float The threshold. muR : float The mean of the R gaussian distribution. sigmaR : float The standard deviation of the R gaussian distribution. muS : float The mean of the S gaussian distribution. sigmaS : float The standard deviation of the S gaussian distribution. Example ------- problem = RminusSReliabilityBenchmarkProblem() """ limitStateFunction = ot.SymbolicFunction(["R", "S"], ["R - S"]) R = ot.Normal(muR, sigmaR) R.setDescription("R") S = ot.Normal(muS, sigmaS) S.setDescription("S") myDistribution = ot.ComposedDistribution([R, S]) inputRandomVector = ot.RandomVector(myDistribution) outputRandomVector = ot.CompositeRandomVector( limitStateFunction, inputRandomVector ) thresholdEvent = ot.ThresholdEvent(outputRandomVector, ot.Less(), threshold) name = "R-S" diff = R - S probability = diff.computeCDF(threshold) super(RminusSReliabilityBenchmarkProblem, self).__init__( name, thresholdEvent, probability ) return None
def __init__(self, threshold=0.0, mu=[0.0] * 2, sigma=[1.0] * 2): """ Creates a reliability problem RP35. The event is {g(X) < threshold} where g(x1, x2) = min(g1, g2) with g1 = 2 - x2 + exp(-0.1 * x1^2) + (0.2 * x1) ^ 4 g2 = 4.5 - x1 * x2 We have x1 ~ Normal(mu[0], sigma[0]) and x2 ~ Normal(mu[1], sigma[1]). Parameters ---------- threshold : float The threshold. mu : sequence of floats The list of two items representing the means of the gaussian distributions. sigma : float The list of two items representing the standard deviations of the gaussian distributions. """ equations = ["var g1 := 2 - x2 + exp(-0.1 * x1^2) + (0.2 * x1) ^ 4"] equations.append("var g2 := 4.5 - x1 * x2") equations.append("gsys := min(g1, g2)") formula = ";".join(equations) limitStateFunction = ot.SymbolicFunction(["x1", "x2"], ["gsys"], formula) inputDimension = len(mu) if inputDimension != 2: raise Exception( "The dimension of mu is %d, but the expected dimension is 2." % (inputDimension)) inputDimension = len(sigma) if inputDimension != 2: raise Exception( "The dimension of sigma is %d, but the expected dimension is 2." % (inputDimension)) X1 = ot.Normal(mu[0], sigma[0]) X1.setDescription(["X1"]) X2 = ot.Normal(mu[1], sigma[1]) X2.setDescription(["X2"]) myDistribution = ot.ComposedDistribution([X1, X2]) inputRandomVector = ot.RandomVector(myDistribution) outputRandomVector = ot.CompositeRandomVector(limitStateFunction, inputRandomVector) thresholdEvent = ot.ThresholdEvent(outputRandomVector, ot.Less(), threshold) name = "RP35" probability = 0.00354 super(ReliabilityProblem35, self).__init__(name, thresholdEvent, probability) return None
def __init__(self, threshold=0.0, mu1=1.5, sigma1=1.0, mu2=2.5, sigma2=1.0): """ Creates a reliability problem RP53. The event is {g(X) < threshold} where g(X1, X2) = sin(5 * X1 / 2) + 2 - (X1^2 + 4) * (X2 - 1) / 20 We have X1 ~ Normal(mu1, sigma1) and X2 ~ Normal(mu2, sigma2). Parameters ---------- threshold : float The threshold. mu1 : float The mean of the X1 gaussian distribution. sigma1 : float The standard deviation of the X1 gaussian distribution. mu2 : float The mean of the X2 gaussian distribution. sigma2 : float The standard deviation of the X2 gaussian distribution. """ formula = "sin(5 * x1 / 2) + 2 - ( x1 * x1 + 4 ) * ( x2 - 1 ) / 20" limitStateFunction = ot.SymbolicFunction(["x1", "x2"], [formula]) print("sin(5 * x1 / 2) + 2 - ( x1 * x1 + 4 ) * ( x2 - 1 ) / 20") X1 = ot.Normal(mu1, sigma1) X1.setDescription(["X1"]) X2 = ot.Normal(mu2, sigma2) X2.setDescription(["X2"]) myDistribution = ot.ComposedDistribution([X1, X2]) inputRandomVector = ot.RandomVector(myDistribution) outputRandomVector = ot.CompositeRandomVector(limitStateFunction, inputRandomVector) thresholdEvent = ot.ThresholdEvent(outputRandomVector, ot.Less(), threshold) name = "RP53" probability = 0.0313 super(ReliabilityProblem53, self).__init__(name, thresholdEvent, probability) return None
def __init__(self, threshold=0.0, mu1=0.0, sigma1=1.0, mu2=0.0, sigma2=1.0): """ Creates a reliability problem RP25. The event is {g(X) < threshold} where --- g(x1, x2) = max(x1^2 -8 * x2 + 16, -16 * x1 + x2 + 32) --- We have x1 ~ Normal(mu1, sigma1) and x2 ~ Normal(mu2, sigma2). --- Parameters ---------- threshold : float The threshold. *** mu1 : float The mean of the X1 gaussian distribution. *** sigma1 : float The standard deviation of the X1 gaussian distribution. *** mu2 : float The mean of the X2 gaussian distribution. *** sigma2 : float The standard deviation of the X2 gaussian distribution. """ equations = ["var g1 := x1^2 -8 * x2 + 16"] equations.append("var g2 := -16 * x1 + x2 + 32") equations.append("gsys := max(g1, g2)") formula = ";".join(equations) limitStateFunction = ot.SymbolicFunction(["x1", "x2"], ["gsys"], formula) print(formula) X1 = ot.Normal(mu1, sigma1) X1.setDescription(["X1"]) X2 = ot.Normal(mu2, sigma2) X2.setDescription(["X2"]) myDistribution = ot.ComposedDistribution([X1, X2]) inputRandomVector = ot.RandomVector(myDistribution) outputRandomVector = ot.CompositeRandomVector(limitStateFunction, inputRandomVector) thresholdEvent = ot.ThresholdEvent(outputRandomVector, ot.Less(), threshold) name = "RP25" probability = 0.00000614 super(ReliabilityProblem25, self).__init__(name, thresholdEvent, probability) return None
def __init__(self, threshold=0.0, mu1=0.0, sigma1=1.0, mu2=0.0, sigma2=1.0): """ Creates a reliability problem RP57. The event is {g(X) < threshold} where g(x1, x2) = min(max(g1, g2), g3) with g1 = -x1^2 + x2^3 + 3 g2 = 2 - x1 - 8 * x2 g3 = (x1 + 3)^2 + (x2 + 3)^2 - 4 We have x1 ~ Normal(mu1, sigma1) and x2 ~ Normal(mu2, sigma2). *** Parameters ---------- threshold : float The threshold. mu1 : float The mean of the X1 gaussian distribution. sigma1 : float The standard deviation of the X1 gaussian distribution. mu2 : float The mean of the X2 gaussian distribution. sigma2 : float The standard deviation of the X2 gaussian distribution. """ equations = ["var g1 := -x1^2 + x2^3 + 3"] equations.append("var g2 := 2 - x1 - 8 * x2") equations.append("var g3 := (x1 + 3)^2 + (x2 + 3)^2 - 4") equations.append("gsys := min(max(g1, g2), g3) ") formula = ";".join(equations) limitStateFunction = ot.SymbolicFunction(["x1", "x2"], ["gsys"], formula) print(formula) X1 = ot.Normal(mu1, sigma1) X1.setDescription(["X1"]) X2 = ot.Normal(mu2, sigma2) X2.setDescription(["X2"]) myDistribution = ot.ComposedDistribution([X1, X2]) inputRandomVector = ot.RandomVector(myDistribution) outputRandomVector = ot.CompositeRandomVector(limitStateFunction, inputRandomVector) thresholdEvent = ot.ThresholdEvent(outputRandomVector, ot.Less(), threshold) name = "RP57" probability = 0.0284 super(ReliabilityProblem57, self).__init__(name, thresholdEvent, probability) return None
def __init__(self, threshold=0.0, mu=[10.0] * 2, sigma=[3.0] * 2): """ Creates a reliability problem RP24. The event is {g(X) < threshold} where g(x1, x2) = 2.5 - 0.2357 * (x1 - x2) + 0.00463 * (x1 + x2 - 20)^4 We have x1 ~ Normal(mu[0], sigma[0]) and x2 ~ Normal(mu[1], sigma[1]). Parameters ---------- threshold : float The threshold. mu : sequence of floats The list of two items representing the means of the gaussian distributions. sigma : float The list of two items representing the standard deviations of the gaussian distributions. """ formula = "2.5 - 0.2357 * (x1 - x2) + 0.00463 * (x1 + x2 - 20)^4" limitStateFunction = ot.SymbolicFunction(["x1", "x2"], [formula]) inputDimension = len(mu) if inputDimension != 2: raise Exception( "The dimension of mu is %d, but the expected dimension is 2." % (inputDimension)) inputDimension = len(sigma) if inputDimension != 2: raise Exception( "The dimension of sigma is %d, but the expected dimension is 2." % (inputDimension)) X1 = ot.Normal(mu[0], sigma[0]) X1.setDescription(["X1"]) X2 = ot.Normal(mu[1], sigma[1]) X2.setDescription(["X2"]) myDistribution = ot.ComposedDistribution([X1, X2]) inputRandomVector = ot.RandomVector(myDistribution) outputRandomVector = ot.CompositeRandomVector(limitStateFunction, inputRandomVector) thresholdEvent = ot.ThresholdEvent(outputRandomVector, ot.Less(), threshold) name = "RP24" probability = 0.00286 super(ReliabilityProblem24, self).__init__(name, thresholdEvent, probability) return None
def __init__(self, threshold=146.14, mu1=78064.0, sigma1=11710.0, mu2=0.0104, sigma2=0.00156): """ Creates a reliability problem RP28. The event is {g(X) < threshold} where g(x1, x2) = x1 * x2 - threshold with threshold = 146.14. We have x1 ~ Normal(mu1, sigma1) and x2 ~ Normal(mu2, sigma2). Parameters ---------- threshold : float The threshold. mu1 : float The mean of the X1 gaussian distribution. sigma1 : float The standard deviation of the X1 gaussian distribution. mu2 : float The mean of the X2 gaussian distribution. sigma2 : float The standard deviation of the X2 gaussian distribution. """ formula = "x1 * x2" limitStateFunction = ot.SymbolicFunction(["x1", "x2"], [formula]) X1 = ot.Normal(mu1, sigma1) X1.setDescription(["X1"]) X2 = ot.Normal(mu2, sigma2) X2.setDescription(["X2"]) myDistribution = ot.ComposedDistribution([X1, X2]) inputRandomVector = ot.RandomVector(myDistribution) outputRandomVector = ot.CompositeRandomVector(limitStateFunction, inputRandomVector) thresholdEvent = ot.ThresholdEvent(outputRandomVector, ot.Less(), threshold) name = "RP28" Y = X1 * X2 probability = Y.computeCDF(threshold) super(ReliabilityProblem28, self).__init__(name, thresholdEvent, probability) return None
def __init__(self): """ Creates the four-branch serial system from Waarts. References ---------- Waarts, P.-H. (2000). Structural reliability using finite element methods: an appraisal of DARS: Directional Adaptive Response Surface Sampling. Ph. D. thesis, Technical University of Delft, The Netherlands. Pages 58, 69, 160. Thèse Vincent Dubourg 2011, Méta-modèles adaptatifs pour l’analyse de fiabilité et l’optimisation sous contrainte fiabiliste, section "A two-dimensional four-branch serial system", page 182 Parameters ---------- None. Example ------- problem = FourBranchSerialSystemReliabilityBenchmarkProblem() """ formulaList = [ "var y0 := 3 + 0.1 * (x0 - x1)^2 - (x0 + x1) / sqrt(2)", "var y1 := 3 + 0.1 * (x0 - x1)^2 + (x0 + x1) / sqrt(2)", "var y2 := x0 - x1 + 7 / sqrt(2)", "var y3 := x1 - x0 + 7 / sqrt(2)", "y := min(y0,y1,y2,y3)", ] formula = ";".join(formulaList) limitStateFunction = ot.SymbolicFunction(["x0", "x1"], ["y"], formula) x0 = ot.Normal(0.0, 1.0) x1 = ot.Normal(0.0, 1.0) inputDistribution = ot.ComposedDistribution((x0, x1)) inputRandomVector = ot.RandomVector(inputDistribution) outputRandomVector = ot.CompositeRandomVector( limitStateFunction, inputRandomVector ) thresholdEvent = ot.ThresholdEvent(outputRandomVector, ot.Less(), 0.0) name = "Four-branch serial system (Waarts, 2000)" beta = 2.85 probability = ot.Normal().computeComplementaryCDF(beta) super(FourBranchSerialSystemReliabilityBenchmarkProblem, self).__init__( name, thresholdEvent, probability ) return None
def __init__(self, threshold=0.0, mu1=10.0, sigma1=3.0, mu2=10.0, sigma2=3.0): """ Creates a reliability problem RP24. The event is {g(X) < threshold} where g(x1, x2) = 2.5 - 0.2357 * (x1 - x2) + 0.00463 * (x1 + x2 - 20)^4 We have x1 ~ Normal(mu1, sigma1) and x2 ~ Normal(mu2, sigma2). *** Parameters ---------- threshold : float The threshold. mu1 : float The mean of the X1 gaussian distribution. sigma1 : float The standard deviation of the X1 gaussian distribution. mu2 : float The mean of the X2 gaussian distribution. sigma2 : float The standard deviation of the X2 gaussian distribution. """ formula = "2.5 - 0.2357 * (x1 - x2) + 0.00463 * (x1 + x2 - 20)^4" limitStateFunction = ot.SymbolicFunction(["x1", "x2"], [formula]) print(formula) X1 = ot.Normal(mu1, sigma1) X1.setDescription(["X1"]) X2 = ot.Normal(mu2, sigma2) X2.setDescription(["X2"]) myDistribution = ot.ComposedDistribution([X1, X2]) inputRandomVector = ot.RandomVector(myDistribution) outputRandomVector = ot.CompositeRandomVector(limitStateFunction, inputRandomVector) thresholdEvent = ot.ThresholdEvent(outputRandomVector, ot.Less(), threshold) name = "RP24" probability = 0.00286 super(ReliabilityProblem24, self).__init__(name, thresholdEvent, probability) return None
def __init__(self, threshold=0.0): """ Create a axial stressed beam reliability problem. The inputs are R, the Yield strength, and F, the traction load. The event is {g(X) < threshold} where g(R, F) = R - F/(pi_ * 100.0) We have R ~ LogNormalMuSigma() and F ~ Normal(). Parameters ---------- threshold : float The threshold. Example ------- problem = AxialStressedBeamReliability() """ limitStateFunction = ot.SymbolicFunction(["R", "F"], ["R - F/(pi_ * 100.0)"]) R_dist = ot.LogNormalMuSigma(300.0, 30.0, 0.0).getDistribution() R_dist.setName("Yield strength") R_dist.setDescription("R") F_dist = ot.Normal(75000.0, 5000.0) F_dist.setName("Traction load") F_dist.setDescription("F") myDistribution = ot.ComposedDistribution([R_dist, F_dist]) inputRandomVector = ot.RandomVector(myDistribution) outputRandomVector = ot.CompositeRandomVector( limitStateFunction, inputRandomVector ) thresholdEvent = ot.ThresholdEvent(outputRandomVector, ot.Less(), 0.0) name = "Axial stressed beam" diff = R_dist - F_dist / (np.pi * 100.0) probability = diff.computeCDF(threshold) super(AxialStressedBeamReliability, self).__init__( name, thresholdEvent, probability ) return None
def run_demo(n_simulation): """Run the demonstration. Parameters ---------- n_simulation : Integer, number of simulation to perform. """ print("\nWith the high level object 'FMUFunction', the number" " of cores is selected at instantiation.") n_cpus_highlevel = ask_n_cpus() highlevel = instantiate_highlevel(n_cpus=n_cpus_highlevel) print(("Instantiated an 'FMUFunction' with %d cores for" " multiprocessing." % n_cpus_highlevel)) outputVariableOfInterest = ot.CompositeRandomVector( highlevel, inputRandomVector) print(("Running %d simulations with %d cores." % (n_simulation, n_cpus_highlevel))) pause() title = "Simulation results:" print(title) print(("-" * len(title))) start = time.time() print((outputVariableOfInterest.getSample(n_simulation))) elapsed_highlevel = time.time() - start print("\nWith the lower level object 'OpenTURNSFMUFunction', the number" " of cores can be selected at runtime.") lowlevel = instantiate_lowlevel() print("Instantiated an 'OpenTURNSFMUFunction'") n_cpus_lowlevel = ask_n_cpus() print(("Running %d simulations with %d cores." % (n_simulation, n_cpus_lowlevel))) pause() title = "Simulation results:" print(title) print(("-" * len(title))) import numpy as np start = time.time() print((lowlevel(np.array(inputRandomVector.getSample(n_simulation)), n_cpus=n_cpus_lowlevel))) elapsed_lowlevel = time.time() - start return (n_simulation, n_cpus_highlevel, elapsed_highlevel, n_cpus_lowlevel, elapsed_lowlevel)
def functionCrue4VarsStochastic(X): Q, Ks, Zv, Zm = X # 1. Creation of the problem function f8v = ot.PythonFunction(8, 1, functionCrue8vars) g = ot.ParametricFunction(f8v, [0, 1, 2, 3], X) # 2. Random vector definition Hd = ot.Uniform(4.,14.) Zb = ot.Uniform(50.,60.) L = ot.Uniform(1000.,10000.) B = ot.Uniform(50.,500.) inputvector = ot.ComposedDistribution([Hd, Zb, L, B]) inputRV = ot.RandomVector(inputvector) S = ot.CompositeRandomVector(g, inputRV) # 3. Sample output sampleSize = 10 outputSample = S.getSample(sampleSize) Smean = outputSample.computeMean()[0] return [Smean]
def _runMonteCarlo(self, defect): # set a parametric function where the first parameter = given defect g = ot.ParametricFunction(self._metamodel, [0], [defect]) g = ot.MemoizeFunction(g) g.enableHistory() g.clearHistory() g.clearCache() output = ot.CompositeRandomVector(g, ot.RandomVector(self._distribution)) event = ot.ThresholdEvent(output, ot.Greater(), self._detectionBoxCox) ##### Monte Carlo ######## algo_MC = ot.ProbabilitySimulationAlgorithm(event) algo_MC.setMaximumOuterSampling(self._samplingSize) # set negative coef of variation to be sure the stopping criterion is the sampling size algo_MC.setMaximumCoefficientOfVariation(-1) algo_MC.run() return algo_MC.getResult()
def __init__(self, threshold=0.0, a1=-1.0, b1=1.0, a2=-1.0, b2=1.0): """ Creates a reliability problem RP55. The event is {g(X) < threshold} where We have x1 ~ Uniform(a1, b1) and x2 ~ Uniform(a2, b2). *** Parameters ---------- threshold : float The threshold. a1 , b1 : float Parameters of the X1 uniform distribution. a2 , b2 : float Parameters of the X2 uniform distribution. """ equations = ["var g1 := 0.2 + 0.6 * (x0 - x1)^4 - (x0 - x1) / sqrt(2)"] equations.append( "var g2 := 0.2 + 0.6 * (x0 - x1)^4 + (x0 - x1) / sqrt(2)") equations.append("var g3 := (x0 - x1) + 5 / sqrt(2) - 2.2") equations.append("var g4 := (x1 - x0) + 5 / sqrt(2) - 2.2") equations.append("gsys := min(g1, g2, g3, g4)") formula = ";".join(equations) limitStateFunction = ot.SymbolicFunction(["x0", "x1"], ["gsys"], formula) print(formula) X1 = ot.Uniform(a1, b1) X1.setDescription(["X1"]) X2 = ot.Uniform(a2, b2) X2.setDescription(["X2"]) myDistribution = ot.ComposedDistribution([X1, X2]) inputRandomVector = ot.RandomVector(myDistribution) outputRandomVector = ot.CompositeRandomVector(limitStateFunction, inputRandomVector) thresholdEvent = ot.ThresholdEvent(outputRandomVector, ot.Less(), threshold) name = "RP55" probability = 0.36 super(ReliabilityProblem55, self).__init__(name, thresholdEvent, probability) return None
def __init__(self, threshold=0.0, mu=0, sigma=1): """ Creates a reliability problem RP107. The event is {g(X) < threshold} where X = (x1, x2, ...., x10) g(X) = 5*sqrt(10) -(x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10). We have xi ~ Normal(0, 1) for i in {1, 2, ...,10} Parameters ---------- threshold : float The threshold. mu : float The mean of the Xi Normal distribution for i in {1, 2, ..., 10}. sigma : float The standard deviation of the Xi Normal distribution for i in {1, 2, ..., 10}. """ formula = "5*sqrt(10)-(x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8 + x9 + x10)" limitStateFunction = ot.SymbolicFunction( ["x1", "x2", "x3", "x4", "x5", "x6", "x7", "x8", "x9", "x10"], [formula]) X = [ot.Normal(mu, sigma) for i in range(10)] myDistribution = ot.ComposedDistribution(X) inputRandomVector = ot.RandomVector(myDistribution) outputRandomVector = ot.CompositeRandomVector(limitStateFunction, inputRandomVector) thresholdEvent = ot.ThresholdEvent(outputRandomVector, ot.Less(), threshold) name = "RP107" probability = 0.000000292 super(ReliabilityProblem107, self).__init__(name, thresholdEvent, probability) return None
def test_DrawSample(self): # Distribution R = ot.Normal(4.0, 1.0) R.setDescription("R") S = ot.Normal(2.0, 1.0) S.setDescription("S") g = ot.SymbolicFunction(["R", "S"], ["R - S"]) # Event inputvector = ot.ComposedDistribution([R, S]) inputRV = ot.RandomVector(inputvector) outputRV = ot.CompositeRandomVector(g, inputRV) eventF = ot.Event(outputRV, ot.GreaterOrEqual(), 0) # sampleSize = 500 inputSample = inputvector.getSample(sampleSize) outputSample = g(inputSample) # drawEvent = otbenchmark.DrawEvent(eventF) graph = drawEvent.drawSample(inputSample, outputSample) assert type(graph) is ot.Graph
def test_DrawSample(self): # Distribution R = ot.Normal(4.0, 1.0) R.setDescription("R") S = ot.Normal(2.0, 1.0) S.setDescription("S") g = ot.SymbolicFunction(["R", "S"], ["R - S"]) # Event distribution = ot.ComposedDistribution([R, S]) inputRV = ot.RandomVector(distribution) outputRV = ot.CompositeRandomVector(g, inputRV) eventF = ot.ThresholdEvent(outputRV, ot.GreaterOrEqual(), 0) # sampleSize = 500 drawEvent = otbenchmark.DrawEvent(eventF) _ = drawEvent.drawSampleCrossCut(sampleSize, 0, 1) # Avoid failing on CircleCi # _tkinter.TclError: no display name and no $DISPLAY environment variable try: _ = drawEvent.drawSample(sampleSize) except Exception as e: print(e)
def test_GetSample(self): """ Teste l'utilisation de l'objet ModelePensionProbabiliste. """ simulateur = SimulateurRetraites() S = 0.0 D = 0.14 annee = 2050 modele = ModelePensionProbabiliste(simulateur, annee, S, D) fonction = modele.getFonction() inputDistribution = modele.getInputDistribution() # Crée un vecteur aléatoire inputRandomVector = ot.RandomVector(inputDistribution) outputRandomVector = ot.CompositeRandomVector(fonction, inputRandomVector) sampleSize = 100 sample = outputRandomVector.getSample(sampleSize) # Vérifie l'échantillon dim = sample.getDimension() self.assertEqual(dim, 1) size = sample.getSize() self.assertEqual(size, sampleSize) return None
import openturns.viewer as viewer from matplotlib import pylab as plt import math as m import time ot.Log.Show(ot.Log.NONE) # %% # Define an event to compute a probability myFunction = ot.SymbolicFunction(['E', 'F', 'L', 'I'], ['-F*L^3/(3.0*E*I)']) dim = myFunction.getInputDimension() mean = [50.0, 1.0, 10.0, 5.0] sigma = [1.0] * dim R = ot.IdentityMatrix(dim) myDistribution = ot.Normal(mean, sigma, R) vect = ot.RandomVector(myDistribution) output = ot.CompositeRandomVector(myFunction, vect) myEvent = ot.ThresholdEvent(output, ot.Less(), -3.0) # %% # **Stop a FORM algorithm using a calls number limit** # # A FORM algorithm termination can be controlled by the maximum number of iterations # # of its underlying optimization solver, but not directly by a maximum number of evaluations. # %% # Create the optimization algorithm myCobyla = ot.Cobyla() myCobyla.setMaximumEvaluationNumber(400) myCobyla.setMaximumAbsoluteError(1.0e-10) myCobyla.setMaximumRelativeError(1.0e-10)
# %% # We use the input parameters distribution from the data class : distribution = cb.distribution distribution.setDescription(['E', 'F', 'L', 'I']) # %% # We define the model model = cb.model # %% # Create the event whose probability we want to estimate. # %% vect = ot.RandomVector(distribution) G = ot.CompositeRandomVector(model, vect) event = ot.ThresholdEvent(G, ot.Greater(), 0.3) event.setName("deviation") # %% # Define a solver optimAlgo = ot.Cobyla() optimAlgo.setMaximumEvaluationNumber(1000) optimAlgo.setMaximumAbsoluteError(1.0e-10) optimAlgo.setMaximumRelativeError(1.0e-10) optimAlgo.setMaximumResidualError(1.0e-10) optimAlgo.setMaximumConstraintError(1.0e-10) # %% # Run FORM algo = ot.FORM(optimAlgo, event, distribution.getMean())
from __future__ import print_function import openturns as ot ot.TESTPREAMBLE() # create a function dim = 4 function = ot.SymbolicFunction( ['E', 'F', 'L', 'I'], ['F*L^3/(3.*E*I)']) # create a distribution distribution = ot.Normal( [50., 1.0, 10.0, 5.0], [1.0] * dim, ot.IdentityMatrix(dim)) vect = ot.RandomVector(distribution) composite = ot.CompositeRandomVector(function, vect) event = ot.Event(composite, ot.Less(), -3.0) # create an ADS algorithm n = int(1e4) algo = ot.AdaptiveDirectionalSampling(event) algo.setMaximumOuterSampling(n) algo.setGamma([0.6, 0.4]) algo.run() result = algo.getResult() print(result) # ADS-2+ algo2 = algo algo2.setPartialStratification(True)
# %% F_dist = sm.distribution_F graph = F_dist.drawPDF() view = viewer.View(graph) # %% # These independent marginals define the joint distribution of the input parameters : myDistribution = sm.distribution # %% # We create a `RandomVector` from the `Distribution`, then a composite random vector. Finally, we create a `ThresholdEvent` from this `RandomVector`. # %% inputRandomVector = ot.RandomVector(myDistribution) outputRandomVector = ot.CompositeRandomVector( limitStateFunction, inputRandomVector) myEvent = ot.ThresholdEvent(outputRandomVector, ot.Less(), 0.0) # %% # Using Monte Carlo simulations # ----------------------------- # %% cv = 0.05 NbSim = 100000 experiment = ot.MonteCarloExperiment() algoMC = ot.ProbabilitySimulationAlgorithm(myEvent, experiment) algoMC.setMaximumOuterSampling(NbSim) algoMC.setBlockSize(1) algoMC.setMaximumCoefficientOfVariation(cv)
#create joint probability distribution heave distribution_h = ot.ComposedDistribution(marginals_h, copula_h) distribution_h.setDescription(['h_exit', 'D_cover', 'i_ch']) #create joint probability distribution distribution_p = ot.ComposedDistribution(marginals_p, copula_p) distribution_p.setDescription(['h_exit', 'D_cover', 'L', 'D', 'm_p', 'k', 'd70']) #create joint probability distribution distribution = ot.ComposedDistribution(marginals, copula) distribution.setDescription(['h_exit', 'D_cover', 'm_u', 'i_ch', 'L', 'D', 'm_p', 'k', 'd70']) #create the event we want to estimate the probability uplift vect_u = ot.RandomVector(distribution_u) G_u = ot.CompositeRandomVector(dijk.Z_u_function, vect_u) event_u = ot.Event(G_u, ot.Less(), 0.0) event_u.setName('uplift failure') #create the event we want to estimate the probability heave vect_h = ot.RandomVector(distribution_h) G_h = ot.CompositeRandomVector(dijk.Z_h_function, vect_h) event_h = ot.Event(G_h, ot.Less(), 0.0) event_h.setName('heave failure') #create the event we want to estimate the probability piping vect_p = ot.RandomVector(distribution_p) G_p = ot.CompositeRandomVector(dijk.Z_p_function, vect_p) event_p = ot.Event(G_p, ot.Less(), 0.0) event_p.setName('piping failure')
ot.Interval([low] * 2, [up] * 2, [False, True], [True, True]), ot.Interval([low] * 2, [up] * 2, [False, True], [True, False]), ot.Interval([low] * 2, [up] * 2, [False, True], [False, True]), ot.Interval([low] * 2, [up] * 2, [False, True], [False, False]), ot.Interval([low] * 2, [up] * 2, [False, False], [True, True]), ot.Interval([low] * 2, [up] * 2, [False, False], [True, False]), ot.Interval([low] * 2, [up] * 2, [False, False], [False, True]), ot.Interval([low] * 2, [up] * 2, [False, False], [False, False]) ] for domain in intervals: print('#' * 50) print('domain=\n', domain) outDim = domain.getDimension() f = ot.SymbolicFunction(inVars, inVars[0:outDim]) Y = ot.CompositeRandomVector(f, X) event = ot.ThresholdEvent(Y, domain) ot.RandomGenerator.SetSeed(0) # algo = getattr(openturns, algoName)(event) algo = ot.ProbabilitySimulationAlgorithm(event, ot.MonteCarloExperiment()) algo.run() res = algo.getResult().getProbabilityEstimate() print('MC p=%.6g' % res) ot.RandomGenerator.SetSeed(0) # algo = getattr(openturns, algoName)(event) algo = ot.FORM(ot.Cobyla(), event, X.getMean()) algo.run() res = algo.getResult().getEventProbability() print('FORM p=%.2f' % res)