Skip to content

Commit 70abb3f

Browse files
committed
Update simulating components, fix #154
1 parent 5b8165b commit 70abb3f

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

src/data_science/simulating_component_failure.jl

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
### A Pluto.jl notebook ###
2-
# v0.19.45
2+
# v0.20.24
33

44
#> [frontmatter]
55
#> chapter = 2
@@ -18,12 +18,14 @@ using InteractiveUtils
1818

1919
# This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error).
2020
macro bind(def, element)
21-
quote
21+
#! format: off
22+
return quote
2223
local iv = try Base.loaded_modules[Base.PkgId(Base.UUID("6e696c72-6542-2067-7265-42206c756150"), "AbstractPlutoDingetjes")].Bonds.initial_value catch; b -> missing; end
2324
local el = $(esc(element))
2425
global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : iv(el)
2526
el
2627
end
28+
#! format: on
2729
end
2830

2931
# ╔═╡ 9a0cec14-08db-11eb-3cfa-4d1c327c63f1
@@ -126,7 +128,7 @@ Then we convert the string to HTML with the `HTML(...)` constructor:
126128
@bind bernoulliwidth Slider(10:10:500, show_value=true)
127129

128130
# ╔═╡ f947a976-8cb6-11eb-2ae7-59eba4c6f40f
129-
url = "https://upload.wikimedia.org/wikipedia/commons/thumb/b/b3/ETH-BIB-Bernoulli%2C_Daniel_%281700-1782%29-Portrait-Portr_10971.tif_%28cropped%29.jpg/440px-ETH-BIB-Bernoulli%2C_Daniel_%281700-1782%29-Portrait-Portr_10971.tif_%28cropped%29.jpg"
131+
url = "https://upload.wikimedia.org/wikipedia/commons/thumb/4/45/ETH-BIB-Bernoulli%2C_Daniel_%281700-1782%29-Portrait-Portr_10971.jpg/500px-ETH-BIB-Bernoulli%2C_Daniel_%281700-1782%29-Portrait-Portr_10971.jpg"
130132

131133
# ╔═╡ 5a0b407e-8cb7-11eb-0c0d-c7767a6b0a1d
132134
s = "<img src=$(url) width=$(bernoulliwidth) >"
@@ -329,15 +331,15 @@ Note that does not require (or even allow) methods at first, as some other langu
329331
Base.rand(X::Binomial) = sum(rand(Bernoulli(X.p)) for i in 1:X.N)
330332

331333
# ╔═╡ 178631ec-8cac-11eb-1117-5d872ba7f66e
332-
function simulate(N, p)
333-
v = fill(0, N, N)
334+
function simulate(N, p; max_time=100)
335+
v = fill(-1, N, N) # -1 means that it did not fail before `max_time` steps.
334336
t = 0
335337

336-
while any( v .== 0 ) && t < 100
338+
while any( v .== -1 ) && t < max_time
337339
t += 1
338340

339341
for i= 1:N, j=1:N
340-
if rand() < p && v[i,j]==0
342+
if rand() < p && v[i,j] == -1
341343
v[i,j] = t
342344
end
343345
end
@@ -552,9 +554,6 @@ function plot_cumulative!(p, N, δ=1; kw...)
552554
scatter!([n*δ for n in 1:N], cumulative; kw...)
553555
end
554556

555-
# ╔═╡ f1f0529a-8c39-11eb-372b-95d591a573e2
556-
plotly()
557-
558557
# ╔═╡ 9572eda8-8c38-11eb-258c-739b511de833
559558
begin
560559
plot(size=(500, 300), leg=false)
@@ -563,7 +562,6 @@ begin
563562
plot_cumulative!(0.05, 60, 0.5; label="", ms=2, c=:lightgreen, alpha=1)
564563
plot_cumulative!(0.025, 120, 0.25; label="", ms=1, c=:lightgreen, alpha=1)
565564
plot_cumulative!(0.0125, 240, 0.125; label="", ms=1, c=:lightgreen, alpha=1)
566-
567565
end
568566

569567
# ╔═╡ 7850b114-8c3b-11eb-276a-df5c332bf6d3
@@ -575,8 +573,6 @@ begin
575573

576574
plot!(0:0.01:20, t -> 1 - exp(-λ*t), lw=1)
577575
plot!(0:0.01:20, t -> 1 - exp(-0.1*t), lw=1)
578-
579-
580576
end
581577

582578
# ╔═╡ 148f486c-8c3d-11eb-069f-cd595c5f7177
@@ -763,7 +759,16 @@ simulation = simulate(M, prob)
763759
begin
764760
w = .9
765761
h = .9
766-
c = [RGB(0,1,0), RGB(1,0,0), :purple][1 .+ (simulation .< tt) .+ (simulation .< (tt.-1))]
762+
c = map(simulation) do s
763+
if s == tt
764+
RGB(1,0,0)
765+
elseif s == -1 || s > tt
766+
RGB(0,1,0)
767+
else
768+
RGB(.5,0,.5)
769+
end
770+
end
771+
767772

768773
plot(ratio=1, legend=false, axis=false, ticks=false)
769774

@@ -773,7 +778,7 @@ begin
773778
end
774779

775780
for i=1:M, j=1:M
776-
if simulation[i,j] < tt
781+
if simulation[i,j] < tt && simulation[i,j] != -1
777782
annotate!(i+.45, j+.5, text("$(simulation[i,j])", font(7), :white))
778783
end
779784
end
@@ -1922,12 +1927,12 @@ version = "1.4.1+1"
19221927
# ╟─f9a75ac4-08d9-11eb-3167-011eb698a32c
19231928
# ╟─17812c7c-8cac-11eb-1d0a-6512415f6938
19241929
# ╠═178631ec-8cac-11eb-1117-5d872ba7f66e
1925-
# ╠═179a4db2-8cac-11eb-374f-0f24dc81ebeb
1930+
# ╟─179a4db2-8cac-11eb-374f-0f24dc81ebeb
19261931
# ╠═17bbf532-8cac-11eb-1e3f-c54072021208
1927-
# ╠═8c8b5681-eeaa-4087-8b6b-1c72c99ae36b
1928-
# ╠═3bfed362-9732-4cb5-86a6-ec50b8429ad5
1929-
# ╠═a38fe2b2-8cae-11eb-19e8-d563e82855d3
1930-
# ╠═17e0d142-8cac-11eb-2d6a-fdf175f5d419
1932+
# ╟─8c8b5681-eeaa-4087-8b6b-1c72c99ae36b
1933+
# ╟─3bfed362-9732-4cb5-86a6-ec50b8429ad5
1934+
# ╟─a38fe2b2-8cae-11eb-19e8-d563e82855d3
1935+
# ╟─17e0d142-8cac-11eb-2d6a-fdf175f5d419
19311936
# ╠═18da7920-8cac-11eb-07f4-e109298fd5f1
19321937
# ╟─17fe87a0-8cac-11eb-2938-2d9cd19ecc0f
19331938
# ╟─1829091c-8cac-11eb-1b77-c5ed7dd1261b
@@ -1997,10 +2002,9 @@ version = "1.4.1+1"
19972002
# ╟─cb99fe22-0848-11eb-1f61-5953be879f92
19982003
# ╟─8d2858a4-8c38-11eb-0b3b-61a913eed928
19992004
# ╠═93da8b36-8c38-11eb-122a-85314d6e1921
2000-
# ╟─f1f0529a-8c39-11eb-372b-95d591a573e2
2001-
# ╟─9572eda8-8c38-11eb-258c-739b511de833
2005+
# ╠═9572eda8-8c38-11eb-258c-739b511de833
20022006
# ╟─7850b114-8c3b-11eb-276a-df5c332bf6d3
2003-
# ╟─9f41d4f2-8c38-11eb-3eae-a1ec0d86d64c
2007+
# ╠═9f41d4f2-8c38-11eb-3eae-a1ec0d86d64c
20042008
# ╟─148f486c-8c3d-11eb-069f-cd595c5f7177
20052009
# ╟─4d61636e-8c3d-11eb-2726-6dc51e8a4f84
20062010
# ╟─3ae9fc0a-8c3d-11eb-09d5-13cefa2d9da5

0 commit comments

Comments
 (0)