-
Notifications
You must be signed in to change notification settings - Fork 2
/
monte.py
74 lines (61 loc) · 2.38 KB
/
monte.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#comment
import random
class MonteCarlo(object):
def __init__(self,network):
self.network = network
self.data = list() # list of dictionaries <node, int state> to hold simulation data of each timestep
def getTimesteps(self):
"""
Returns the number of timesteps in the current simulation.
"""
return len(self.data)
##### Initial Start States #####
def startEmpty(self):
"""
Initializes the network by having the entire network
with their nodes empty.
"""
if len(self.data) == 0:
self.network.setFeature("state",{node:0 for node in self.network})
self.data.append(self.network.getFeature("state"))
def startFull(self):
"""
Initializes the network by having the entire network
with their nodes full.
"""
if len(self.data) == 0:
self.network.setFeature("state",{node:1 for node in self.network})
self.data.append(self.network.getFeature("state"))
def startRandom(self,concentration):
"""
Initializes the network by having the entire network
with a certain biased percentage of the network filled
based the concentration of the nanoparticles initially
put in.
"""
if len(self.data) == 0:
self.network.setFeature("state",{node: 1 if random.uniform(0,1) < concentration else 0 for node in self.network})
self.data.append(self.network.getFeature("state"))
#### State Counter Methods ####
def countEmpty(self,timestep):
"""
Returns the number of empty nodes in the network.
"""
return list(self.data[timestep].values()).count(0)
def countFull(self,timestep):
"""
Returns the number of full nodes in the network.
"""
return list(self.data[timestep].values()).count(1)
def stateDegree(self,node,timestep):
"""
Returns the state value of the node for its neighbors
of its degree.
"""
return sum([self.data[timestep][node] for node in self.network.getNeighbors(node)])
#### Density Methods ####
def density(self,timestep):
"""
Returns the density of the network at given timestep.
"""
return self.countFull(timestep) / len(self.network)