-
Notifications
You must be signed in to change notification settings - Fork 0
/
world.py
65 lines (52 loc) · 1.77 KB
/
world.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
import math
import networkx as nx
import random
from agent import Agent
from story import Story
def sigmoid(x):
return 1 / (1 + math.exp(-x))
class World:
"""a World contains Towns"""
def __init__(self, n, k, p, log=False, catchRumors=True):
# self.G = None
# self.instantiateTowns(n, k, p)
self.instantiateAgents(n, k, p)
self.stories = dict()
self.printLog = log
self.catchRumors = catchRumors
def instantiateAgents(self, n, k, p):
self.G = nx.newman_watts_strogatz_graph(n, k, p)
for node in self.G.nodes():
agent = Agent(node)
self.G.nodes[node]['node'] = agent
def getRandomNode(self):
node = random.choice(self.G.nodes())
return node
def getRandomAgent(self):
node = random.choice(self.G.nodes())
agent = self.G.nodes[node]['node']
return agent
def step(self):
# Create an event in a random node
# Allow nearby agents to perceive the event (distance = 0, 1, 2?)
# We want to set up the frequency of world.step() so that the world is
# not swarmed with new stories ... relative frequencey of world.step() to agent.step()
# needs to be tweaked
newStory = Story(self, self.getRandomNode(), None)
self.stories[story.id()] = newStory
def recursivePrint(self, story, indent=0):
if len(story.downstream) > 0 or (story.upstream is not None and story.isCaughtRumor is not True): #suppress storys that did not spread for now
print("{} {}{}".format("\t"*indent,
"(Caught Rumor!) " if story.isCaughtRumor else "", story.text()))
for e in story.downstream:
self.recursivePrint(e, indent=indent+1)
def doPrintLog(self):
for story in self.stories.values():
if story.upstream is None:
self.recursivePrint(story)
def run(self, n):
for i in range(n):
self.step()
if self.printLog:
self.doPrintLog()
# print("World ran!")