-
Notifications
You must be signed in to change notification settings - Fork 113
/
Mathematica - Monte Carlo Simulation
49 lines (33 loc) · 2.97 KB
/
Mathematica - Monte Carlo Simulation
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
MARKOV CHAIN MONTE CARLO SIMULATION - AUTONOMOUS DRIVING
VIDEO OF THE OUTPUT:
https://www.youtube.com/watch?v=_2l6DgUgGFw
a=Table[Insert[Table[0,{5}],1,RandomInteger[{1,5}]],{15}];
b=Partition[Flatten[Position[a[[#]],0]&/@Table[k,{k,1,Dimensions[a][[1]],1}]],5]
MONTE CARLO TREE SEARCH: RANDOM EXAMPLE;CLOCK;
h=b[[#]][[RandomInteger[{1,5}]]]&/@Table[k,{k,1,Dimensions[a][[1]],1}]
g1=Flatten[{h[[#]]->h[[#+1]]}&/@Table[k,{k,1,14,1}]]
hh=Insert[a[[#]],2,h[[#]]]&/@Table[k,{k,1,Dimensions[a][[1]],1}]
f1=Delete[hh[[#]],h[[#]]+1]&/@Table[k,{k,1,Dimensions[a][[1]],1}]
{LayeredGraphPlot[g1,VertexLabeling->True,ImageSize->360],Dynamic[j=Clock[{1,15,1},5];{ArrayPlot[bb=Insert[Delete[a,j],f1[[j]],j],Mesh->True,ImageSize->170,ColorRules->{0->White,1->Black,2->Green}],Grid[Insert[Delete[a,j],f1[[j]],j]/.{0->,1->,2->}]}]}
{,{ArrayPlot[Insert[Delete[a,8],f1[[8]],8],Mesh->True,ImageSize->170,ColorRules->{0->,1->,2->}],Grid[Insert[Delete[a,8],f1[[8]],8]]}}
r={b[[1]][[1]],b[[2]][[#]],b[[3]][[#]],b[[4]][[#]],b[[5]][[#]],b[[6]][[#]],b[[7]][[#]],b[[8]][[#]],b[[9]][[#]],b[[10]][[#]],b[[11]][[#]],b[[12]][[#]],b[[13]][[#]],b[[14]][[#]],b[[15]][[#]]}&/@Table[k,{k,1,5,1}]
g12={r[[#]][[1]]->r[[#]][[2]],r[[#]][[2]]->r[[#]][[3]],r[[#]][[3]]->r[[#]][[4]],r[[#]][[4]]->r[[#]][[5]],r[[#]][[5]]->r[[#]][[6]],r[[#]][[6]]->r[[#]][[7]],r[[#]][[7]]->r[[#]][[8]],r[[#]][[8]]->r[[#]][[9]],r[[#]][[9]]->r[[#]][[10]],r[[#]][[10]]->r[[#]][[11]],r[[#]][[11]]->r[[#]][[12]],r[[#]][[12]]->r[[#]][[13]],r[[#]][[13]]->r[[#]][[14]],r[[#]][[14]]->r[[#]][[15]]}&/@Table[k,{k,1,5,1}]
MARKOV CHAINS TO GENERATE PATHS AND SAVE COMPUTATIONAL TIME;
q=LayeredGraphPlot[g12[[#]],VertexLabeling->True,ImageSize->180]&/@Table[k,{k,1,5,1}]
ft[t_]:=Insert[a[[#]],2,r[[t]][[#]]]&/@Table[k,{k,1,Dimensions[a][[1]],1}]
hh2=ft/@Table[k,{k,1,Dimensions[r][[1]],1}]
gh[ty_]:=Delete[hh2[[ty]][[#]],r[[ty]][[#]]+1]&/@Table[k,{k,1,Dimensions[a][[1]],1}]
f13=gh/@Table[k,{k,1,Dimensions[hh2][[1]],1}]
LOGISTIC PATH ANIMATION;
Manipulate[Dynamic[j=Clock[{1,15,1},5];{ArrayPlot[Insert[Delete[a,j],f13[[ss]][[j]],j],Mesh->True,ImageSize->230,ColorRules->{0->White,1->Black,2->Green}],Grid[Insert[Delete[a,j],f13[[ss]][[j]],j]/.{0->,1->,2->}]}],{ss,1,5,1,Appearance->"\.1dOpen"}]
Manipulate[Dynamic[j = Clock[{1, 15, 1}, 5];
{ArrayPlot[Insert[Delete[a, j], f13[[ss]][[j]], j], Mesh -> True,
ImageSize -> 230, ColorRules -> {0 -> White, 1 -> Black, 2 -> Green}],
Grid[Insert[Delete[a, j], f13[[ss]][[j]], j] /.
{0 -> Image[X], 1 -> Image[Y]],
ImageSize -> {21.82421875, Automatic}, Interleaving -> True]}]}],
{ss, 1, 5, 1, Appearance -> "\.1dOpen"}]
MOST EFFICIENT PATH; SMALLEST DISPLACEMENT OF GREEN SQUARE;
dg=Total[Abs[{r[[#]][[1]]-r[[#]][[2]],r[[#]][[2]]-r[[#]][[3]],r[[#]][[3]]-r[[#]][[4]],r[[#]][[4]]-r[[#]][[5]]}]]&/@Table[k,{k,1,Dimensions[r][[1]],1}]
Insert[Delete[a,#],f13[[1]][[#]],#]&/@Table[k,{k,1,5,1}]
{q[[Flatten[Position[dg,Min[dg]]][[1]]]],ArrayPlot[hh2[[1]],Mesh->True,ImageSize->220,ColorRules->{0->White,1->Black,2->Green}]}