-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclassCell.py
More file actions
101 lines (91 loc) · 3.76 KB
/
classCell.py
File metadata and controls
101 lines (91 loc) · 3.76 KB
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
import numpy as np
import math
class Cell():
def __init__(self, payoff, location, N, D, p):
self.payoff = payoff
self.location = location
self.N = N
self.D = D
self.p = p
self.numHits = 0
self.lowerx, self.upperx, self.yList = self.logiFunc(N, D)
self.stepSize, self.slopeVals = self.setStepSize(D, p)
self.numSciHits = 0
self.funds = 0
self.totalFunds = 0
def __repr__(self):
"""string representation of Cell"""
return str(self.payoff)
def logiFunc(self, N, D):
"""
model payoff extraction for each cell as a randomized logistic function
"""
# boolean for whether we have found the lowerx and upperx of the logistic function
lowSuccess = False
highSuccess = False
while lowSuccess == False or highSuccess == False:
# set steepness and center parameters for logistic function
steepOptions = [x for x in np.arange(0.3, 7, 0.1)]
steepness = np.random.choice(steepOptions)
center = np.random.randint(0, 15)
lowerx = 0
upperx = []
lowSuccess = False
highSuccess = False
# finds upperx and lowerx of the lgoistic function
for x in np.arange(0, 50, 0.01):
y = float(1/(1 + math.pow(math.e, -steepness*(x-center))))
# creates a boundary 0.1 above 0 and below 1 for y-val to set as upperx and lowerx
if ((y - .01) <= 0.1) and math.floor(x) > 0:
lowerx = math.floor(x)
lowSuccess = True
elif (.99 - y) <= 0.1:
upperx.append(math.ceil(x))
highSuccess = True
xmax = min(upperx)
#finds the y value (proportion) at given x in the logistic function
self.incHit = float(1/(1 + math.pow(math.e, -steepness*(xmax-center))))
self.breakHit = float(1/(1 + math.pow(math.e, -steepness*(lowerx-center))))
# Creates a list of coordinates by D microsteps up to xmax
yList = []
interval = xmax/N
for x in np.arange(0, xmax, interval/D):
yval = float(1/(1 + math.pow(math.e, -steepness*(x-center))))
yList += [yval]
return lowerx, xmax, yList
def setStepSize(self, D, p):
"""
split the steps along the logistic function into randomized microsteps
"""
slopeVals = {}
# randomly generates the microstep interval
stepSize = np.random.binomial(D*2, p, size=None)
# calculates slopes according to stepSize
prevY = 0
i = 0
while i <= self.upperx:
if stepSize == 0:
slope = 0
else:
slope = (self.yList[i+stepSize] - prevY)/stepSize
slopeVals[i] = slope
prevY = self.yList[i+stepSize]
i += 1
return stepSize, slopeVals
def cellQuery(self, board):
"""returns payoff for each scientist based on conditions of a cell"""
original = self.payoff
sciPayoff = 0.0
self.setStepSize(self.D, self.p)
originalPay = board.originalPays[self.location[1]][self.location[0]]
# if we enter incremental phase, taken payoff becomes random
if originalPay == 0:
frac = 0
elif self.payoff/originalPay >= self.incHit-1:
frac = np.random.random()
else:
frac = self.slopeVals[self.numHits]
self.payoff = (1-frac) * self.payoff
sciPayoff = frac * original
self.numHits += 1
return sciPayoff