File: t_LineSampling_std.py

package info (click to toggle)
openturns 1.26-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 67,708 kB
  • sloc: cpp: 261,605; python: 67,030; ansic: 4,378; javascript: 406; sh: 185; xml: 164; makefile: 101
file content (243 lines) | stat: -rwxr-xr-x 7,912 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#!/usr/bin/env python

import openturns as ot
import openturns.experimental as otexp
import openturns.testing as ott
from openturns.usecases import cantilever_beam
from openturns.usecases import stressed_beam
import math

# stressed_beam ##############################################

print(f"-- stressed_beam {'-' * 50}")
threshold = -1e6
sm = stressed_beam.AxialStressedBeam()
limitStateFunction = sm.model
R_dist = sm.distribution_R
F_dist = sm.distribution_F
distribution = sm.distribution
inputRandomVector = ot.RandomVector(distribution)
outputRandomVector = ot.CompositeRandomVector(limitStateFunction, inputRandomVector)
event = ot.ThresholdEvent(outputRandomVector, ot.Less(), threshold)
D = 0.02
G = R_dist - F_dist / (D**2 / 4 * math.pi)
pf_ref = G.computeCDF(threshold)
print(f"exact probability={pf_ref}")

# FORM analysis
optimAlgo = ot.Cobyla()
optimAlgo.setStartingPoint(distribution.getMean())
algoFORM = ot.FORM(optimAlgo, event)
algoFORM.run()
resultFORM = algoFORM.getResult()
alpha = resultFORM.getStandardSpaceDesignPoint()
print(f"alpha={alpha}")

# LineSampling
ot.RandomGenerator.SetSeed(0)
solver = ot.Brent(1e-3, 1e-3, 1e-3, 5)
rootStrategy = ot.MediumSafe(solver)
algo = otexp.LineSampling(event, alpha, rootStrategy)
algo.setMaximumCoefficientOfVariation(1e-3)
algo.setMaximumOuterSampling(1000)
algo.setStoreHistory(True)
algo.run()
result = algo.getResult()
print(str(algo)[:100])
print(result)
ott.assert_almost_equal(result.getProbabilityEstimate(), pf_ref)
assert algo.getStoreHistory()
assert len(algo.getAlphaHistory()) == algo.getMaximumOuterSampling(), "empty alphas"
assert len(algo.getRootValuesHistory()) > 0, "empty roots"
assert len(algo.getRootPointsHistory()) > 0, "empty roots"
assert algo.getAdaptiveImportantDirection(), "adaptive?"
assert algo.getSearchOppositeDirection(), "opposite?"
assert algo.getInitialAlpha() == alpha, "alpha?"
rootStrategy2 = algo.getRootStrategy()
algo.setRootStrategy(rootStrategy2)

# cantilever_beam ##############################################

print(f"-- cantilever_beam {'-' * 50}")

# model
cb = cantilever_beam.CantileverBeam()
distribution = cb.distribution
model = cb.model
vect = ot.RandomVector(distribution)
G = ot.CompositeRandomVector(model, vect)
event = ot.ThresholdEvent(G, ot.Greater(), 0.30)

# FORM for alpha
optimAlgo = ot.AbdoRackwitz()
optimAlgo.setStartingPoint(distribution.getMean())
optimAlgo.setMaximumCallsNumber(1000)
optimAlgo.setMaximumAbsoluteError(1.0e-10)
optimAlgo.setMaximumRelativeError(1.0e-10)
optimAlgo.setMaximumResidualError(1.0e-10)
optimAlgo.setMaximumConstraintError(1.0e-10)
algo = ot.FORM(optimAlgo, event)
algo.run()
result = algo.getResult()
alpha = result.getStandardSpaceDesignPoint()
print(f"alpha={alpha}")

# LineSampling
ot.RandomGenerator.SetSeed(0)
solver = ot.Brent(1e-3, 1e-3, 1e-3, 5)
rootStrategy = ot.MediumSafe(solver)
algo = otexp.LineSampling(event, alpha, rootStrategy)
algo.setMaximumOuterSampling(1000)
algo.setMaximumCoefficientOfVariation(5e-2)
algo.run()
result = algo.getResult()
print(result)
ott.assert_almost_equal(result.getProbabilityEstimate(), 4.57807e-07)

# two branch model ##############################################

print(f"-- 2 branch {'-' * 50}")

X = ot.RandomVector(ot.Normal(2))
g_twoBranch = ot.SymbolicFunction(
    ["x1", "x2"],
    [
        "min(5 + 0.1 * (x1 - x2)^2 - (x1 + x2) / sqrt(2), 5 + 0.1 * (x1 - x2)^2 + (x1 + x2) / sqrt(2))"
    ],
)
Y_twoBranch = ot.CompositeRandomVector(g_twoBranch, X)
threshold = 1.5
event_twoBranch = ot.ThresholdEvent(Y_twoBranch, ot.Less(), threshold)

g1 = ot.SymbolicFunction(["x1", "x2"], ["5 + 0.1 * (x1 - x2)^2 - (x1 + x2) / sqrt(2)"])
Y1 = ot.CompositeRandomVector(g1, X)
event1 = ot.ThresholdEvent(Y1, ot.Less(), threshold)

g2 = ot.SymbolicFunction(["x1", "x2"], ["5 + 0.1 * (x1 - x2)^2 + (x1 + x2) / sqrt(2)"])
Y2 = ot.CompositeRandomVector(g2, X)
event2 = ot.ThresholdEvent(Y2, ot.Less(), threshold)
unionEvent = ot.UnionEvent([event1, event2])

optimAlgo = ot.Cobyla()
optimAlgo.setStartingPoint(X.getMean())
algo = ot.FORM(optimAlgo, event_twoBranch)
algo.run()
result = algo.getResult()
alpha_twoBranch = result.getStandardSpaceDesignPoint()

# LineSampling / only one branch
ot.RandomGenerator.SetSeed(0)
solver = ot.Brent(1e-3, 1e-3, 1e-3, 5)
rootStrategy = ot.MediumSafe(solver)
algo = otexp.LineSampling(event_twoBranch, alpha_twoBranch, rootStrategy)
algo.setSearchOppositeDirection(False)
algo.setMaximumOuterSampling(1000)
algo.setMaximumCoefficientOfVariation(5e-2)
algo.run()
result = algo.getResult()
print(result)
ott.assert_almost_equal(result.getProbabilityEstimate(), 1.5e-4, 0.0, 1e-5)

# LineSampling
ot.RandomGenerator.SetSeed(0)
algo.setSearchOppositeDirection(True)
algo.setMaximumOuterSampling(1000)
algo.run()
result = algo.getResult()
print(result)
ott.assert_almost_equal(result.getProbabilityEstimate(), 3e-4, 0.0, 1e-5)

# LineSampling / system event / no adaptive alpha
algo = otexp.LineSampling(unionEvent, alpha_twoBranch, rootStrategy)
ot.RandomGenerator.SetSeed(0)
algo.setMaximumOuterSampling(1000)
algo.setAdaptiveImportantDirection(False)
algo.run()
result = algo.getResult()
print(result)
ott.assert_almost_equal(result.getProbabilityEstimate(), 3e-4, 0.0, 1e-5)
ot.ResourceMap.SetAsBool("LineSampling-DefaultAdaptiveImportantDirection", True)


# rosenbrock ##############################################

print(f"-- rosenbrock {'-' * 50}")
g = ot.SymbolicFunction(["x1", "x2"], ["100 * (x2 - x1^2)^2 + (x1 - 1)^2"])
X = ot.RandomVector(ot.Normal(2))
Y = ot.CompositeRandomVector(g, X)
threshold = 0.1
event = ot.ThresholdEvent(Y, ot.Less(), threshold)
###################################################

# FORM for  alpha
optimAlgo = ot.Cobyla()
optimAlgo.setStartingPoint(X.getMean())
algo = ot.FORM(optimAlgo, event)
algo.run()
result = algo.getResult()
alpha = result.getStandardSpaceDesignPoint()

# LineSampling
rootStrategy = ot.SafeAndSlow(ot.Brent(1e-3, 1e-3, 1e-3, 5), 3, 0.1)
ot.RandomGenerator.SetSeed(0)
algo = otexp.LineSampling(event, alpha, rootStrategy)
algo.setMaximumOuterSampling(3000)
algo.setMaximumCoefficientOfVariation(5e-2)
algo.run()
result = algo.getResult()
print(result)
ott.assert_almost_equal(result.getProbabilityEstimate(), 0.00110258, 0.0, 1e-4)

# complementary
event = ot.ThresholdEvent(Y, ot.Greater(), threshold)
ot.RandomGenerator.SetSeed(0)
algo = otexp.LineSampling(event, alpha, rootStrategy)
algo.setMaximumOuterSampling(3000)
algo.setMaximumCoefficientOfVariation(5e-2)
algo.run()
result = algo.getResult()
print(result)
ott.assert_almost_equal(result.getProbabilityEstimate(), 0.997749, 0.0, 1e-4)

# complementary + opposite
ot.RandomGenerator.SetSeed(0)
algo = otexp.LineSampling(event, alpha, rootStrategy)
algo.setMaximumOuterSampling(3000)
algo.setMaximumCoefficientOfVariation(5e-2)
algo.setSearchOppositeDirection(True)
algo.run()
result = algo.getResult()
print(result)
ott.assert_almost_equal(result.getProbabilityEstimate(), 0.997749, 0.0, 1e-4)

# paraboloid ##############################################

print(f"-- paraboloid {'-' * 50}")

# model
paraboloid = ot.SymbolicFunction(
    ["u1", "u2", "u3", "u4", "u5"], ["- u5 + u1^2 + u2^2 + u3^2 + u4^2"]
)
b = 3.5
db = 0.02
t = 0.0
dim = 5
U = ot.Normal(dim)
X = ot.RandomVector(U)
Y = ot.CompositeRandomVector(paraboloid, X)
E1 = ot.ThresholdEvent(Y, ot.Less(), -b)
E2 = ot.ThresholdEvent(Y, ot.Less(), -(b + db))
E3 = ot.ThresholdEvent(Y, ot.Greater(), -(b + db))
E4 = ot.IntersectionEvent([E1, E3])

# LineSampling, with step < db to find the roots
alpha = [0.0, 0.0, 0.0, 0.0, 1.0]
rootStrategy = ot.SafeAndSlow(ot.Brent(1e-3, 1e-3, 1e-3, 5), 8, 0.01)
ot.RandomGenerator.SetSeed(0)
algo = otexp.LineSampling(E4, alpha, rootStrategy)
algo.setMaximumOuterSampling(2000)
algo.setMaximumCoefficientOfVariation(5e-2)
algo.run()
result = algo.getResult()
print(result)
ott.assert_almost_equal(result.getProbabilityEstimate(), 2.103779e-07)