.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_numerical_methods/optimization/plot_control_termination.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_numerical_methods_optimization_plot_control_termination.py: Control algorithm termination ============================= .. GENERATED FROM PYTHON SOURCE LINES 6-8 In this examples we are going to expose ways to control the termination of optimization and simulation algorithms using callbacks. .. GENERATED FROM PYTHON SOURCE LINES 10-14 .. code-block:: Python import openturns as ot ot.Log.Show(ot.Log.NONE) .. GENERATED FROM PYTHON SOURCE LINES 15-16 Define an event to compute a probability .. GENERATED FROM PYTHON SOURCE LINES 16-26 .. code-block:: Python 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) .. GENERATED FROM PYTHON SOURCE LINES 27-32 **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. .. GENERATED FROM PYTHON SOURCE LINES 34-35 Create the optimization algorithm .. GENERATED FROM PYTHON SOURCE LINES 35-43 .. code-block:: Python myCobyla = ot.Cobyla() myCobyla.setMaximumEvaluationNumber(400) myCobyla.setMaximumAbsoluteError(1.0e-10) myCobyla.setMaximumRelativeError(1.0e-10) myCobyla.setMaximumResidualError(1.0e-10) myCobyla.setMaximumConstraintError(1.0e-10) .. GENERATED FROM PYTHON SOURCE LINES 44-45 Define the stopping criterion .. GENERATED FROM PYTHON SOURCE LINES 45-51 .. code-block:: Python def stop(): return myFunction.getCallsNumber() > 100 myCobyla.setStopCallback(stop) .. GENERATED FROM PYTHON SOURCE LINES 52-53 Run FORM .. GENERATED FROM PYTHON SOURCE LINES 53-59 .. code-block:: Python myAlgo = ot.FORM(myCobyla, myEvent, mean) myAlgo.run() result = myAlgo.getResult() print("event probability:", result.getEventProbability()) print("calls number:", myFunction.getCallsNumber()) .. rst-class:: sphx-glr-script-out .. code-block:: none event probability: 0.15642619193819485 calls number: 102 .. GENERATED FROM PYTHON SOURCE LINES 60-63 **Stop a simulation algorithm using a time limit** Here we will create a callback to not exceed a specified simulation time. .. GENERATED FROM PYTHON SOURCE LINES 65-66 Create simulation .. GENERATED FROM PYTHON SOURCE LINES 66-71 .. code-block:: Python experiment = ot.MonteCarloExperiment() myAlgo = ot.ProbabilitySimulationAlgorithm(myEvent, experiment) myAlgo.setMaximumOuterSampling(1000000) myAlgo.setMaximumCoefficientOfVariation(-1.0) .. GENERATED FROM PYTHON SOURCE LINES 72-73 Define the stopping criterion .. GENERATED FROM PYTHON SOURCE LINES 73-76 .. code-block:: Python timer = ot.TimerCallback(0.01) myAlgo.setStopCallback(timer) .. GENERATED FROM PYTHON SOURCE LINES 77-78 Run algorithm .. GENERATED FROM PYTHON SOURCE LINES 78-82 .. code-block:: Python myAlgo.run() result = myAlgo.getResult() print("event probability:", result.getProbabilityEstimate()) print("calls number:", myFunction.getCallsNumber()) .. rst-class:: sphx-glr-script-out .. code-block:: none event probability: 0.14196998447472833 calls number: 5899 .. _sphx_glr_download_auto_numerical_methods_optimization_plot_control_termination.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_control_termination.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_control_termination.py `