def test_ReliabilityBenchmarkMetaAlgorithm(self): maximumEvaluationNumber = 1000 maximumAbsoluteError = 1.0e-3 maximumRelativeError = 1.0e-3 maximumResidualError = 1.0e-3 maximumConstraintError = 1.0e-3 nearestPointAlgorithm = ot.AbdoRackwitz() nearestPointAlgorithm.setMaximumEvaluationNumber( maximumEvaluationNumber) nearestPointAlgorithm.setMaximumAbsoluteError(maximumAbsoluteError) nearestPointAlgorithm.setMaximumRelativeError(maximumRelativeError) nearestPointAlgorithm.setMaximumResidualError(maximumResidualError) nearestPointAlgorithm.setMaximumConstraintError(maximumConstraintError) problem = otb.ReliabilityProblem8() metaAlgorithm = otb.ReliabilityBenchmarkMetaAlgorithm(problem) benchmarkResult = metaAlgorithm.runFORM(nearestPointAlgorithm) print(benchmarkResult.summary()) benchmarkResult = metaAlgorithm.runSORM(nearestPointAlgorithm) print(benchmarkResult.summary()) benchmarkResult = metaAlgorithm.runLHS(maximumOuterSampling=10000) print(benchmarkResult.summary()) benchmarkResult = metaAlgorithm.runMonteCarlo( maximumOuterSampling=10000) print(benchmarkResult.summary()) benchmarkResult = metaAlgorithm.runFORMImportanceSampling( nearestPointAlgorithm) print(benchmarkResult.summary()) benchmarkResult = metaAlgorithm.runSubsetSampling() print(benchmarkResult.summary())
def test_FORM(self): problem = otb.ReliabilityProblem14() nearestPointAlgorithm = ot.AbdoRackwitz() algo = otb.FORM(problem, nearestPointAlgorithm) algo.run() result = algo.getResult() pf = result.getEventProbability() exactPf = problem.getProbability() np.testing.assert_almost_equal(pf, exactPf, decimal=2)
def test_FORMIS(self): problem = otb.ReliabilityProblem14() nearestPointAlgorithm = ot.AbdoRackwitz() factory = otb.ProbabilitySimulationAlgorithmFactory() algo = factory.buildFORMIS(problem, nearestPointAlgorithm) algo.run() result = algo.getResult() pf = result.getProbabilityEstimate() exactPf = problem.getProbability() np.testing.assert_almost_equal(pf, exactPf, decimal=2)
#! /usr/bin/env python from __future__ import print_function import openturns as ot levelFunction = ot.NumericalMathFunction(["x1", "x2", "x3", "x4"], ["y1"], ["x1+2*x2-3*x3+4*x4"]) # Add a finite difference gradient to the function, as Abdo Rackwitz algorithm # needs it myGradient = ot.NonCenteredFiniteDifferenceGradient( 1e-7, levelFunction.getEvaluation()) print("myGradient = ", repr(myGradient)) # Substitute the gradient levelFunction.setGradient(ot.NonCenteredFiniteDifferenceGradient(myGradient)) startingPoint = [0.0] * 4 algo = ot.AbdoRackwitz(ot.OptimizationProblem(levelFunction, 3.0)) algo.setStartingPoint(startingPoint) algo.run() print("result = ", algo.getResult()) levelFunction = ot.NumericalMathFunction(["x1", "x2", "x3", "x4"], ["y1"], ["x1*cos(x1)+2*x2*x3-3*x3+4*x3*x4"]) # Add a finite difference gradient to the function, as Abdo Rackwitz algorithm # needs it myGradient = ot.NonCenteredFiniteDifferenceGradient( 1e-7, levelFunction.getEvaluation()) print("myGradient = ", repr(myGradient)) # Substitute the gradient levelFunction.setGradient(ot.NonCenteredFiniteDifferenceGradient(myGradient)) startingPoint = [0.0] * 4 algo = ot.AbdoRackwitz(ot.OptimizationProblem(levelFunction, -0.5))
# We consider a system event in disjunctive normal form (union of intersections): event = ot.UnionEvent( [ot.IntersectionEvent([e0, e3, e4]), ot.IntersectionEvent([e2, e3, e4])]) # %% # We can estimate the probability of the event with basic sampling. print("Probability of the event : %.4f" % event.getSample(10000).computeMean()[0]) # %% # We can also run a :class:`~openturns.systemFORM` algorithm to estimate the probability differently. # %% # We first set up a solver to find the design point. solver = ot.AbdoRackwitz() solver.setMaximumIterationNumber(1000) solver.setMaximumAbsoluteError(1.0e-3) solver.setMaximumRelativeError(1.0e-3) solver.setMaximumResidualError(1.0e-3) solver.setMaximumConstraintError(1.0e-3) # %% # We build the :class:`~openturns.SystemFORM` algorithm from the solver, the event and a starting point (here the mean) and then run the algorithm. algo = ot.SystemFORM(solver, event, mean) algo.run() # %% # We store the result and display the probability. result = algo.getResult() prbSystemFORM = result.getEventProbability()
from __future__ import print_function import openturns as ot ot.TESTPREAMBLE() levelFunction = ot.SymbolicFunction(["x1", "x2", "x3", "x4"], ["x1+2*x2-3*x3+4*x4"]) # Add a finite difference gradient to the function, as Abdo Rackwitz algorithm # needs it myGradient = ot.NonCenteredFiniteDifferenceGradient( 1e-7, levelFunction.getEvaluation()) print("myGradient = ", repr(myGradient)) # Substitute the gradient levelFunction.setGradient(ot.NonCenteredFiniteDifferenceGradient(myGradient)) startingPoint = [0.0] * 4 algo = ot.AbdoRackwitz(ot.NearestPointProblem(levelFunction, 3.0)) algo.setStartingPoint(startingPoint) algo.run() print("result = ", algo.getResult()) levelFunction = ot.SymbolicFunction(["x1", "x2", "x3", "x4"], ["x1*cos(x1)+2*x2*x3-3*x3+4*x3*x4"]) # Add a finite difference gradient to the function, as Abdo Rackwitz algorithm # needs it myGradient = ot.NonCenteredFiniteDifferenceGradient( 1e-7, levelFunction.getEvaluation()) print("myGradient = ", repr(myGradient)) # Substitute the gradient levelFunction.setGradient(ot.NonCenteredFiniteDifferenceGradient(myGradient)) startingPoint = [0.0] * 4 algo = ot.AbdoRackwitz(ot.NearestPointProblem(levelFunction, -0.5))
print('Number of evaluations: %d' % MCS_evaluation_number) # In[22]: confidence_level = .9 MCS_convergence_graph = MCS_algorithm.drawProbabilityConvergence( confidence_level) _ = View(MCS_convergence_graph).show() # # *Most-probable-failure-point*-based approaches # ## Search for the *most probable failure point* (MPFP) # In[23]: mpfp_search_algorithm = ot.AbdoRackwitz( ) # Alternatives: ot.AbdoRackwitz(), ot.Cobyla() mpfp_search_algorithm.setMaximumIterationNumber(int(1e3)) mpfp_search_algorithm.setMaximumAbsoluteError(1e-10) mpfp_search_algorithm.setMaximumRelativeError(1e-10) mpfp_search_algorithm.setMaximumResidualError(1e-10) mpfp_search_algorithm.setMaximumConstraintError(1e-10) print(mpfp_search_algorithm) # ## *First-order-reliability-method* (FORM) # In[24]: g.clearHistory() # In[25]:
def run_FORM_simple( event, inputDistribution, nearestPointAlgo="AbdoRackwitz", NmaxIteration=100, eps=[1e-5] * 4, physicalStartingPoint=None, seed=1234, verbose=False, ): """ Run a FORM approximation. Parameters ---------- event : openturns.Event The failure event. inputDistribution : openturns.distribution The distribution of the event. nearestPointAlgo : str Type of the optimization algorithm. It must be 'AbdoRackwitz', 'SQP' or 'Cobyla'. NmaxIteration : int The maximum number of iterations. eps = sequence of float The stopping criterion value of the optimization algorithm. Order is absolute error, relative error, residual error, constraint error. physicalStartingPoint : sequence of float The starting point of the algorithm. Default is the median values. seed : int Seed for the openturns random generator. logfile : bool Enable or not to write the log in FORM.log file. verbose : bool Enable or not the display of the result. activeCache : bool Enable or not the cache mechanism of the NumericalMathFunction. activeHistory : bool Enable or not the history mechanism of the NumericalMathFunction. """ # Initialize the random generator ot.RandomGenerator.SetSeed(seed) # Defintion of the nearest point algorithm if nearestPointAlgo == "AbdoRackwitz": solver = ot.AbdoRackwitz() # spec = algo.getSpecificParameters() # spec.setTau(0.5) # algo.setSpecificParameters(spec) elif nearestPointAlgo == "Cobyla": solver = ot.Cobyla() elif nearestPointAlgo == "SQP": solver = ot.SQP() else: raise NameError("Nearest point algorithm name must be \ 'AbdoRackwitz', 'Cobyla' or 'SQP'.") eps = np.array(eps) solver.setMaximumAbsoluteError(eps[0]) solver.setMaximumRelativeError(eps[1]) solver.setMaximumResidualError(eps[2]) solver.setMaximumConstraintError(eps[3]) solver.setMaximumIterationNumber(NmaxIteration) # Set the physical starting point of the Nearest point # algorithm to the mediane value if physicalStartingPoint is None: physicalStartingPoint = inputDistribution.getMean() # Run FORM method approximation = ot.FORM(solver, event, physicalStartingPoint) approximation.run() result = approximation.getResult() optimResult = result.getOptimizationResult() iter_number = optimResult.getIterationNumber() dfResult = pd.DataFrame() dfResult = dfResult.append( pd.DataFrame([result.getEventProbability()], index=["Probability of failure"])) dfResult = dfResult.append( pd.DataFrame( [result.getGeneralisedReliabilityIndex()], index=["Generalised reliability index"], )) dfResult = dfResult.append( pd.DataFrame([iter_number], index=["Number of iterations"])) dfResult = dfResult.append( pd.DataFrame( [result.getStandardSpaceDesignPoint()], index=["Standard space design point"], )) dfResult = dfResult.append( pd.DataFrame( [result.getPhysicalSpaceDesignPoint()], index=["Physical space design point"], )) dfResult = dfResult.reset_index() dfResult.columns = ["", "Results - FORM (" + nearestPointAlgo + ")"] pd.options.display.float_format = "{:,.2E}".format if verbose: print(dfResult, "\n") return approximation
analysis2.setBlockSize(100) myStudy.add(analysis2) print(analysis2) analysis2.run() print("result=", analysis2.getResult()) # IS ## X2 = persalys.Input('X2', 2) model.addInput(X2) model.setFormula('Y0', 'sin(X0) + 8*X1 + X2') analysis3 = persalys.FORMImportanceSamplingAnalysis('myIS3', limitState) analysis3.setMaximumCalls(1000) analysis3.setMaximumCoefficientOfVariation(-1.) analysis3.setPhysicalStartingPoint([1.08161, 2.38966]) analysis3.setOptimizationAlgorithm(ot.AbdoRackwitz()) myStudy.add(analysis3) print(analysis3) analysis3.run() result3 = analysis3.getResult() print("result=", result3) # script script = myStudy.getPythonScript() print(script) exec(script)
taylor.setInterestVariables(['y0', 'y1']) myStudy.add(taylor) # 2-c Taylor Expansion which generate an error taylor2 = persalys.TaylorExpansionMomentsAnalysis('Taylor2', model1) taylor2.setInterestVariables(['fake_var']) myStudy.add(taylor2) # 3- reliability ## # limit state ## limitState = persalys.LimitState('aLimitState', model1, 'y1', ot.Greater(), 0.5) myStudy.add(limitState) optimAlgo = ot.AbdoRackwitz() optimAlgo.setMaximumIterationNumber(150) optimAlgo.setMaximumAbsoluteError(1e-3) # 3-a Monte Carlo ## monteCarloReliability = persalys.MonteCarloReliabilityAnalysis( 'MonteCarloReliability', limitState) monteCarloReliability.setMaximumCoefficientOfVariation(-1.) monteCarloReliability.setMaximumElapsedTime(1000) monteCarloReliability.setMaximumCalls(20) monteCarloReliability.setSeed(2) myStudy.add(monteCarloReliability) # 3-b FORM IS ## form_is = persalys.FORMImportanceSamplingAnalysis('FORM_IS', limitState) form_is.setOptimizationAlgorithm(optimAlgo)