/
interhost_process.go
120 lines (112 loc) · 3.37 KB
/
interhost_process.go
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
package contagiongo
import (
"math"
"math/rand"
"sync"
rv "github.com/kentwait/randomvariate"
)
// TransmissionEvent is a struct for sending and receiving
// transmission event information.
type TransmissionEvent struct {
destination Host
pathogen GenotypeNode
}
// ExchangeEvent is a struct for sending and receiving
// exchange event information.
type ExchangeEvent struct {
source Host
destination Host
// pathogenIndex int
pathogen GenotypeNode
}
// TransmitPathogens transmits the pathogen to its neighboring host/s.
// If transmission occurs, sends transmitted node over the channel to
// be added to the recepient. Also sends node information in order to
// record the event.
func TransmitPathogens(i, t int, src, dst Host, numMigrants int, transmissionProb float64, count int, c chan<- TransmissionEvent, d chan<- TransmissionPackage, wg *sync.WaitGroup) {
defer wg.Done()
// Check if migration size if larger than the current population size
// If larger, make migrants equal to existing size
if count < 1 {
return
}
// numMigrants := src.GetTransmissionModel().TransmissionSize()
if numMigrants > count {
numMigrants = count
}
// Determine if tranmission occurs or not based on source's
// transmission probability
// transmissionProb := src.GetTransmissionModel().TransmissionProb()
if rv.Binomial(1, transmissionProb) == 1.0 {
// If transmission occurs, randomly pick pathogens to transmit
for _, p := range src.PickPathogens(numMigrants) {
if p != nil {
c <- TransmissionEvent{dst, p}
d <- TransmissionPackage{
instanceID: i,
genID: t,
fromHostID: src.ID(),
toHostID: dst.ID(),
nodeID: p.UID(),
}
}
}
}
}
// ExchangePathogens exchanges pathogens between neighboring hosts.
func ExchangePathogens(i, t int, h1, h2 Host, h1Count, h2Count int, c chan<- ExchangeEvent, d chan<- TransmissionPackage, wg *sync.WaitGroup) {
defer wg.Done()
// Check if migration size if larger than the current population size
// If larger, skip
if h1.GetTransmissionModel().TransmissionSize() > h1Count {
return
} else if h2.GetTransmissionModel().TransmissionSize() > h2Count {
return
}
// Assumes transmission size if equal in all hosts
numMigrants := h1.GetTransmissionModel().TransmissionSize()
// Determine if exchange occurs or not based on square of the source's
// transmission probability. This assumes that transmission prob is equal
// between any two hosts.
transmissionProb := math.Pow(h1.GetTransmissionModel().TransmissionProb(), 2)
if rv.Binomial(1, transmissionProb) == 1.0 {
// If exchange occurs, randomly pick pathogens in the h1 and h2 hosts
// h1 -> h2
for _, p := range h1.PickPathogens(numMigrants) {
if p != nil {
c <- ExchangeEvent{
source: h1,
destination: h2,
pathogen: p,
}
d <- TransmissionPackage{
instanceID: i,
genID: t,
fromHostID: h1.ID(),
toHostID: h2.ID(),
nodeID: p.UID(),
}
}
}
// h2 -> h1
for _, p := range h2.PickPathogens(numMigrants) {
if p != nil {
c <- ExchangeEvent{
source: h2,
destination: h1,
pathogen: p,
}
d <- TransmissionPackage{
instanceID: i,
genID: t,
fromHostID: h2.ID(),
toHostID: h1.ID(),
nodeID: p.UID(),
}
}
}
}
}
func pickPathogens(count, numMigrants int) []int {
return rand.Perm(count)[:numMigrants]
}