7(1/9)
4(1/9)
1(1/9)
8(1/9)
5(1/9)
2(1/9)
9(1/9)
6(1/9)
3(1/9)
P(7->8) = 1/4
P(8->7) = 1/4
P(4->5) = 1/4
P(5->4) = 1/4
P(1->2) = 1/4
P(2->1) = 1/4
P(9->8) = 1/4
P(8->9) = 1/4
P(6->5) = 1/4
P(5->6) = 1/4
P(3->2) = 1/4
P(2->3) = 1/4
P(4->7) = 1/4
P(7->4) = 1/4
P(1->4) = 1/4
P(4->1) = 1/4
P(5->8) = 1/4
P(8->5) = 1/4
P(6->9) = 1/4
P(9->6) = 1/4
P(2->5) = 1/4
P(5->2) = 1/4
P(3->6) = 1/4
P(6->3) = 1/4
7(3/18)
4(2/18)
1(1/18)
8(3/18)
5(2/18)
2(1/18)
9(3/18)
6(2/18)
3(1/18)
P(7->8) = 1/4
P(8->7) = 1/4
P(4->5) = 1/4
P(5->4) = 1/4
P(1->2) = 1/4
P(2->1) = 1/4
P(9->8) = 1/4
P(8->9) = 1/4
P(6->5) = 1/4
P(5->6) = 1/4
P(3->2) = 1/4
P(2->3) = 1/4
P(4->7) = 1/4
P(7->4) = 1/6
P(1->4) = 1/4
P(4->1) = 1/8
P(5->8) = 1/4
P(8->5) = 1/6
P(6->9) = 1/4
P(9->6) = 1/6
P(2->5) = 1/4
P(5->2) = 1/8
P(3->6) = 1/4
P(6->3) = 1/8
| Cell 1 | Cell 3 | Cell 9 |
|---|---|---|
| 0.246 ± 0.071 | 0.080 ± 0.023 | 0.000 ± 0.000 |
| Cell 1 | Cell 3 | Cell 9 |
|---|---|---|
| 0.054 ± 0.001 | 0.055 ± 0.001 | 0.167 ± 0.001 |
import random
import math
from pprint import pprint
CYCLE = 10000
def resetGrid(grid):
for key, value in grid.items():
value[0] = 0
def selectRoute(routes):
'''
Generates a random number then decides which way to go
according to the probabilities
'''
prob = random.uniform(0, 1)
routesProb = 0
for i, route in enumerate(routes):
routesProb += route[1]
if prob < routesProb:
return i
return None
def displayResults(grid, opt, reps, steps):
''' display function '''
print("RUN WITH {} REPETITIONS AND {} STEPS".format(reps, steps))
if opt == 3:
for key, value in grid.items():
print("CELL %d, turtle crosses it %d times, which probability is : %.3f" % (key, value[0], value[0] / (reps)))
elif opt == 4:
for key, value in grid.items():
print("CELL %d, turtle crosses it %d times, which probability is : %.3f" % (key, value[0], value[0] / (reps * steps)))
def standardDeviation(samples):
# compute standard deviation, by calculing the average of samples, the variance and standard deviation
stdDev = {}
for key, sample in samples.items():
if len(sample) > 0:
average = sum(sample) / len(sample)
variance = sum([(value - average) ** 2 for value in sample]) / len(sample)
stdDev[key] = math.sqrt(variance)
else:
stdDev[key] = 0
print("standard deviations : ")
for key, value in stdDev.items():
print("CELL %d, standard deviation : %.3f" % (key, round(value, 3)))
def simulation(grid, samples, steps, opt):
'''
one repetition process, choose a route
then increments cell traffic at the end of steps (for task 3)
'''
pos = 1
for i, step in enumerate(range(steps)):
route = selectRoute(grid[pos][1])
if route != None:
pos = grid[pos][1][route][0]
if opt == 4:
grid[pos][0] += 1
if i != 0 and i % CYCLE == 0:
for key in samples.keys():
samples[key].append(grid[key][0] / step)
if opt == 3:
grid[pos][0] += 1
return pos
def taskThree(grid):
REPETITIONS = 10000
STEPS = 3
OPTION = 3
# dict to store probabilities states
resetGrid(grid)
samples = {1: [], 3: [], 9: []}
for rep in range(REPETITIONS):
pos = simulation(grid, samples, STEPS, OPTION)
if pos in samples.keys():
samples[pos].append((grid[pos][0]) / (REPETITIONS))
displayResults(grid, OPTION, REPETITIONS, STEPS)
return samples
def taskFour(grid):
REPETITIONS = 1
STEPS = 1000000
OPTION = 4
# dict to store probabilities states
resetGrid(grid)
samples = {1: [], 3: [], 9: []}
for rep in range(REPETITIONS):
pos = simulation(grid, samples, STEPS, OPTION)
displayResults(grid, OPTION, REPETITIONS, STEPS)
return samples
def main():
'''
entry point, defines grid with an index for each node,
a number of crossing on the cell,
a list of nodes with their transition probabilities
'''
grid = {1: [0, [(2, 1/4), (4, 1/4)]],
2: [0, [(1, 1/4), (5, 1/4), (3, 1/4)]],
3: [0, [(2, 1/4), (6, 1/4)]],
4: [0, [(1, 1/8), (5, 1/4), (7, 1/4)]],
5: [0, [(4, 1/4), (8, 1/4), (6, 1/4), (2, 1/8)]],
6: [0, [(3, 1/8), (5, 1/4), (9, 1/4)]],
7: [0, [(4, 1/6), (8, 1/4)]],
8: [0, [(7, 1/4), (5, 1/6), (9, 1/4)]],
9: [0, [(8, 1/4), (6, 1/6)]]}
standardDeviation(taskThree(grid))
standardDeviation(taskFour(grid))
if __name__ == '__main__':
main()
NaturalComputationSlides
By mb1475963
NaturalComputationSlides
- 506