import random
import math
def sensor_location(n):
coordinates = []
for i in range(n):
x = random.uniform(0,100)
y = random.uniform(0,100)
coordinates.append((x,y))
return coordinates
def coverage_ratio(sample_count,sensor,radius):
count = 0
location = sensor_location(sensor)
for i in range(sample_count):
xi = random.uniform(0,100)
yi = random.uniform(0,100)
sensed = False
for j in location:
d = math.sqrt( math.pow((xi-j[0]),2)+ math.pow((yi-j[1]),2))
if d <= radius:
sensed = True
break
if sensed == False:
count += 1
'''
coverage indicates non-sensed area
'''
coverage = ((count/float(sample_count))*10000)
return 100-(coverage/100)
def simulation(sample, sensor, radius):
print "running for radius = %d" % radius
while True:
ratio = coverage_ratio(sample,sensor,radius)
if ratio >= 99.0:
print "number of required sensor: %d" % sensor
print "coverage: %f" % ratio
break
else:
sensor+=5
return ratio
'''perform simulation for
initial sensor = 10
and r = 5 m
'''
def main():
simulation(10000,10,5)
if __name__ == "__main__":
main()
After 15 times running the program, the simulation result shows the average necessary sensor number for 99% sensing coverage is 586.73.
No comments:
Post a Comment