示例#1
0
import openturns as ot

ot.PlatformInfo.SetNumericalPrecision(4)

# linear
levelFunction = ot.NumericalMathFunction(
    ["x1", "x2", "x3", "x4"], ["y1"], ["x1+2*x2-3*x3+4*x4"])
# Add a finite difference gradient to the function
myGradient = ot.NonCenteredFiniteDifferenceGradient(
    1e-7, levelFunction.getEvaluation())
print("myGradient = ", repr(myGradient))
# Substitute the gradient
levelFunction.setGradient(
    ot.NonCenteredFiniteDifferenceGradient(myGradient))
startingPoint = ot.NumericalPoint(4, 0.0)
algo = ot.SQP(ot.OptimizationProblem(levelFunction, 3.0))
algo.setStartingPoint(startingPoint)
print('algo=', algo)
algo.run()
result = algo.getResult()
print('result=', result)


# non-linear
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,
# needs it
myGradient = ot.NonCenteredFiniteDifferenceGradient(
    1e-7, levelFunction.getEvaluation())
# Substitute the gradient
示例#2
0
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
示例#3
0
import openturns as ot

ot.TESTPREAMBLE()
ot.PlatformInfo.SetNumericalPrecision(3)

# linear
levelFunction = ot.SymbolicFunction(["x1", "x2", "x3", "x4"],
                                    ["x1+2*x2-3*x3+4*x4"])
# Add a finite difference gradient to the function
myGradient = ot.NonCenteredFiniteDifferenceGradient(
    1e-7, levelFunction.getEvaluation())
print("myGradient = ", repr(myGradient))
# Substitute the gradient
levelFunction.setGradient(ot.NonCenteredFiniteDifferenceGradient(myGradient))
startingPoint = ot.Point(4, 0.0)
algo = ot.SQP(ot.NearestPointProblem(levelFunction, 3.0))
algo.setStartingPoint(startingPoint)
print('algo=', algo)
algo.run()
result = algo.getResult()
print('result=', result)

# non-linear
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,
# needs it
myGradient = ot.NonCenteredFiniteDifferenceGradient(
    1e-7, levelFunction.getEvaluation())
# Substitute the gradient
levelFunction.setGradient(ot.NonCenteredFiniteDifferenceGradient(myGradient))
def computeCrossingProbability_FORM(b, t, mu_S, covariance, R, delta_t):
    X, event = getXEvent(b, t, mu_S, covariance, R, delta_t)
    algo = ot.SystemFORM(ot.SQP(), event, X.getMean())
    algo.run()
    return algo.getResult().getEventProbability() / delta_t