File: t_FORM_interval.py

package info (click to toggle)
openturns 1.24-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 66,204 kB
  • sloc: cpp: 256,662; python: 63,381; ansic: 4,414; javascript: 406; sh: 180; xml: 164; yacc: 123; makefile: 98; lex: 55
file content (70 lines) | stat: -rwxr-xr-x 2,778 bytes parent folder | download
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
#! /usr/bin/env python

import openturns as ot

# Tests
ot.ResourceMap.SetAsUnsignedInteger("SimulationAlgorithm-DefaultBlockSize", 100)
ot.ResourceMap.SetAsUnsignedInteger(
    "SimulationAlgorithm-DefaultMaximumOuterSampling", 100
)
ot.ResourceMap.SetAsScalar(
    "SimulationAlgorithm-DefaultMaximumCoefficientOfVariation", 0.0
)
ot.ResourceMap.SetAsScalar("SimulationAlgorithm-DefaultMaximumStandardDeviation", 0.0)
ot.ResourceMap.SetAsScalar("RootStrategy-DefaultStepSize", 0.1)

algorithms = ["MonteCarlo", "LHS", "QuasiMonteCarlo", "DirectionalSampling"]

inDim = 4
X = ot.RandomVector(ot.Normal(inDim))
inVars = ot.Description.BuildDefault(inDim, "x")

low = 1.0
up = 2.0
intervals = [
    ot.Interval([low], [up], [True], [False]),
    ot.Interval([low], [up], [False], [True]),
    ot.Interval([low], [up], [True], [True]),
    ot.Interval([low], [up], [False], [False]),
    ot.Interval([low] * 2, [up] * 2, [True, True], [True, True]),
    ot.Interval([low] * 2, [up] * 2, [True, True], [True, False]),
    ot.Interval([low] * 2, [up] * 2, [True, True], [False, True]),
    ot.Interval([low] * 2, [up] * 2, [True, True], [False, False]),
    ot.Interval([low] * 2, [up] * 2, [True, False], [True, True]),
    ot.Interval([low] * 2, [up] * 2, [True, False], [True, False]),
    ot.Interval([low] * 2, [up] * 2, [True, False], [False, True]),
    ot.Interval([low] * 2, [up] * 2, [True, False], [False, False]),
    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)
    algoOptim = ot.Cobyla()
    algoOptim.setMaximumConstraintError(1)
    algo = ot.FORM(algoOptim, event, X.getMean())

    algo.run()
    res = algo.getResult().getEventProbability()
    print("FORM p=%.2f" % res)