-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactions.py
55 lines (41 loc) · 1.81 KB
/
interactions.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
import numpy as np
from numpy import int8
# Representing rock, paper, and scissors with simple 8-bit integers.
ROCK, PAPER, SCISSORS = int8(1), int8(2), int8(3)
# Colors to use for plotting rock, paper, and scissors particles.
ROCK_COLOR = "red"
PAPER_COLOR = "limegreen"
SCISSORS_COLOR = "blue"
def rock_paper_scissors_interaction(parameters, microbe_properties, p1, p2):
pRS, pPR, pSP = parameters["pRS"], parameters["pPR"], parameters["pSP"]
species = microbe_properties["species"]
if species[p1] != species[p2]:
s1, s2 = species[p1], species[p2]
r = np.random.rand() # Random float from Uniform[0,1)
winner = None
if s1 == ROCK and s2 == SCISSORS:
winner = p1 if r < pRS else p2
elif s1 == ROCK and s2 == PAPER:
winner = p2 if r < pPR else p1
elif s1 == PAPER and s2 == ROCK:
winner = p1 if r < pPR else p2
elif s1 == PAPER and s2 == SCISSORS:
winner = p2 if r < pSP else p1
elif s1 == SCISSORS and s2 == ROCK:
winner = p2 if r < pRS else p1
elif s1 == SCISSORS and s2 == PAPER:
winner = p1 if r < pSP else p2
if winner == p1:
species[p2] = species[p1]
elif winner == p2:
species[p1] = species[p2]
def rock_paper_scissors(N_microbes, pRS, pPR, pSP):
microbe_properties = {
"species": np.random.choice([ROCK, PAPER, SCISSORS], N_microbes)
}
interaction_parameters = {
"pRS": pRS, # Forward probability that rock beats scissors.
"pPR": pPR, # Forward probability that paper beats rock.
"pSP": pSP # Forward probability that scissors beats paper.
}
return rock_paper_scissors_interaction, interaction_parameters, microbe_properties