# Autonomous vehicles: Markov Chain Monte Carlo to save computational time

**URL:** <https://community.wolfram.com/t/autonomous-vehicles-markov-chain-monte-carlo-to-save-computational-time/9852>\
**Category:** Staff Picks\
**Tags:** computer-science, data-science, game-development, robotics, software-development, graphics-and-visualization, machine-learning, staff-pick, robotics-and-autonomy\
**Created:** [January 10, 2017, 10:57pm UTC](https://community.wolfram.com/t/autonomous-vehicles-markov-chain-monte-carlo-to-save-computational-time/9852 "2017-01-10T22:57:25Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubenszmm](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/rubenszmm/32/46204_2.png) [@rubenszmm](https://community.wolfram.com/u/rubenszmm)\
**Post date:** [January 10, 2017, 10:57pm UTC](https://community.wolfram.com/t/autonomous-vehicles-markov-chain-monte-carlo-to-save-computational-time/9852/1 "2017-01-10T22:57:25Z")

</div>

![dsfare4q5ywhtrgeaf.gif](https://global.discourse-cdn.com/wolfram/original/2X/8/875841f8d78c5f871c4b9c75f8a47b2c0e60350d.gif)

This is a code I developed last year, while trying to solve an Atari game. The main idea is to go from the initial part of the lattice (A first row) to the last row (B) deviating from obstacles (in this case, cars, black squares). The goal of the model is to find the shortest path between A and B, given the cars in the road. Initially I thought I could use tuples of paths in the 6 x 15 lattice, but the possible options were computationally expensive.

```
a=Table[Insert[Table[0,{5}],1,RandomInteger[{1,5}]],{15}];

{ArrayPlot[a,Mesh->True,ImageSize->180,Frame->True]}

```

Green square is car moving:

 ![ScreenShot2017-01-10at8.38.03PM.png](https://global.discourse-cdn.com/wolfram/original/2X/4/48c97af3c83b361f559ecc6954a54fd237caf1c8.png)

So I used a Markov Chain Monte Carlo model, using Markov Chains to generate possible paths given spatial restrictions.

```
b=Partition[Flatten[Position[a[[#]],0]&/@Table[k,{k,1,Dimensions[a][[1]],1}]],5]
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->'blank',1->'black car',2->'orange car'}]}]}

```

 ![ScreenShot2017-01-10at8.44.12PM.png](https://global.discourse-cdn.com/wolfram/original/2X/c/c47f25908ce0bc95d50d0571eda8d6948307315a.png)

Generating this dynamic output:

 ![ScreenShot2017-01-10at8.41.25PM.png](https://global.discourse-cdn.com/wolfram/original/3X/7/6/76bc02a60de7325ffd991dce3a0956750d8ea699.png)

Then the possible paths are shown:

```
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}]
q=LayeredGraphPlot[g12[[#]],VertexLabeling->True,ImageSize->180]&/@Table[k,{k,1,5,1}]

```

 ![ScreenShot2017-01-10at8.47.37PM.png](https://global.discourse-cdn.com/wolfram/original/2X/4/4f5e8dc0dc5e753adea85d1e06e156462da128a9.png)

And a Manipulate command is inserted to show car movements in each path:

```
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}]
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->"Open"}]

```

Then the shortest path is chosen:

```
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}]}

```

 ![ScreenShot2017-01-10at8.52.40PM.png](https://global.discourse-cdn.com/wolfram/original/2X/0/0fbead21dd504068f835d076cd1bccde1761f775.png)

For a video of the output, please access my YouTube video on MCMC: [Markov Chain Monte Carlo](https://www.youtube.com/watch?v=_2l6DgUgGFw)

 ![ScreenShot2017-01-11at12.31.08PM.png](https://global.discourse-cdn.com/wolfram/original/3X/7/2/72be54890ac206edb3edec42c386851b7eff6327.png)

---

<div class="post-metadata">

**Author:** ![wolframcommunity](https://yyz1.discourse-cdn.com/wolfram/user_avatar/community.wolfram.com/wolframcommunity/32/8335_2.png) [@wolframcommunity](https://community.wolfram.com/u/wolframcommunity)\
**Post date:** [January 11, 2017, 6:53pm UTC](https://community.wolfram.com/t/autonomous-vehicles-markov-chain-monte-carlo-to-save-computational-time/9852/2 "2017-01-11T18:53:29Z")

</div>

![2015-11-02_10-07-11.png](https://global.discourse-cdn.com/wolfram/original/3X/5/0/50e59c9ead62f729e7130d3ea1482555a75602b6.png)

- you have earned “Featured Contributor” badge, congratulations !

This is a great post and it has been selected for the curated [Staff Picks](http://community.wolfram.com/content?curTag=staff+picks) group. [Your profile](http://community.wolfram.com/web/rubenszmm) is now distinguished by a “Featured Contributor” badge and displayed on the [“Featured Contributor” board](http://wolfr.am/featured).
