Skip to content

Instantly share code, notes, and snippets.

@Mohamedemad4
Created July 4, 2019 12:34
Show Gist options
  • Save Mohamedemad4/c900e4032449943c637bba58e8bdb9d8 to your computer and use it in GitHub Desktop.
Save Mohamedemad4/c900e4032449943c637bba58e8bdb9d8 to your computer and use it in GitHub Desktop.
a Monto Carlo Simulation to determine the needed time of motion for an MRS supervisor in minesweepers competition see: landminefree.org
import random
import numpy as np
import math
from pprint import pprint as p
LinearSpeed=0.5#linear speed of the Supervisor in m/s
class Arena:
def __init__(self):
self.mine_locs=np.zeros(shape=(20,20))
self.d_loc=[0,0] # x,y ,right hand vector system z is towards you x to the left y to the top of the screen
self.s_loc=[1,0]
self.dSteps=0
self.markings=0
self.sSteps=0
self.last_dist=0
def place_mines(self,nM):
for i in range(0,nM):
self.mine_locs[random.randint(0,19)][random.randint(0,19)]=1
def traverse(self):
for i in range(400):
if self.mine_locs[self.d_loc[0]][self.d_loc[1]]==1:
# import pdb;pdb.set_trace()
self.sSteps+=self._dist(self.d_loc,self.s_loc)
self.s_loc[0],self.s_loc[1]=self.d_loc[0],self.d_loc[1]
self.markings+=1
if self.d_loc[1]==19:
self.d_loc[1]=-1
self.d_loc[0]=self.d_loc[0]+1
self.d_loc[1]+=1
self.dSteps+=1
def _dist(self,p0,p1):
return int(math.sqrt((p0[0] - p1[0])**2 + (p0[1] - p1[1])**2))
things=[]
for i in range(0,1000):
a=Arena()
a.place_mines(16)
a.traverse()
things.append((a.sSteps/LinearSpeed)+a.markings*10)
print(np.average(things)//60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment