-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstag_hunt.py
128 lines (95 loc) · 3.42 KB
/
stag_hunt.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# coding: utf-8
# In[1]:
import matplotlib
matplotlib.use('TkAgg')
from pylab import *
import pycxsimulator
import pprint
import random
# set up animial classes
class Fox:
def __init__(self, label):
self.score = 0
self.trust_level = 0.5
self.label = str(label)
def __str__(self):
return self.label
def __repr__(self):
return self.label
def eat(self, points):
self.score = self.score+2
class Rabbit:
def __init__(self, label):
self.points = 1
self.label = str(label)
def __str__(self):
return self.label
def __repr__(self):
return self.label
class Stag:
def __init__(self, label):
self.points = 2
self.label = str(label)
def __str__(self):
return self.label
def __repr__(self):
return self.label
# size of game board
space_size = 10
# animals in ecosystem and probability of seed
ecosystem = {"r": 0.5, "s": 0.5, "f": 0.5}
def initialize():
global grid, next_grid, foxes, rabbits, stags
grid = []
# seed gameboard
for x in range(space_size):
row = []
for y in range(space_size):
# select a random species from ecosystem
species = random.choice(ecosystem.keys())
# flip coin to see if species is seeded or cell is empty
if ecosystem[species] > random.random():
if species == 'r':
row.append(Rabbit('r'+str(x)+str(y)))
elif species == 's':
row.append(Stag('s'+str(x)+str(y)))
elif species == 'f':
row.append(Fox('f'+str(x)+str(y)))
else:
row.append(0)
grid.append(row)
# print grid
pprint.pprint(grid)
def observe():
imshow(grid, cmap=cm.binary)
show()
def update():
global grid
global next_grid
for x in range(space_size):
for y in range(space_size):
fox_neighbors = 0
stag_neighbors = 0
rabbit_neighbors = 0
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
current_species = grid[((x+dx) % space_size)][((y+dy) % space_size)]
if(dx, dy) != (0, 0) and type(current_species) != int:
if grid[((x+dx) % space_size)][((y+dy) % space_size)].label.find('f') > -1:
fox_neighbors += 1
if grid[((x+dx) % space_size)][((y+dy) % space_size)].label.find('r') > -1:
rabbit_neighbors += 1
if grid[((x+dx) % space_size)][((y+dy) % space_size)].label.find('s') > -1:
stag_neighbors += 1
print str(x) + ", " + str(y) + " is " +str(grid[x][y]) + " with these neighbors: foxes: " + str(fox_neighbors) + " rabbits: "+ str(rabbit_neighbors) + " stags: "+ str(stag_neighbors)
# if current_state == 1:
# next_grid[x, y] = 1 if random() < sick_neighbors/8. else 0
# elif current_state == 2 :
# next_grid[x, y] = 1 if random() < 0.5 else 2
# else:
# next_grid[x, y] = 2 if random() > 0.5 else 3
# grid, next_grid = next_grid, grid
initialize()
update()
#just calling initial game board for now
#pycxsimulator.GUI(title='My Simulator', interval=0,parameterSetters=[]).start(func=[initialize, observe, update])