-
Notifications
You must be signed in to change notification settings - Fork 0
/
containment_measure.go
94 lines (82 loc) · 2.98 KB
/
containment_measure.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
package main
import (
"log"
"math/rand"
"time"
)
// muskMeasure has 2 params:
// - Psucc: probability of success on prevention in case of contact
// - Active: containment measure applyed
type muskMeasure struct {
Active bool
FromEpoch int
Psucc float64
}
// socialDistancingMeasure is the social distance policy
// AllowContacts is the map of allowed contact relations
type socialDistancingMeasure struct {
Active bool
FromEpoch int
AllowContacts map[string]bool
}
// middlewareContainmentMeasure apply the requested measure on spreading it return true if there is an infection
func middlewareContainmentMeasure(personPointer *person, muskPointer *muskMeasure, socialDistancePointer *socialDistancingMeasure, epoch int) (bool, int) {
// generati a valid ID regarding socialDistancingMeasure if Active
if (*socialDistancePointer).Active && epoch >= (*socialDistancePointer).FromEpoch && (*socialDistancePointer).FromEpoch != -1 {
infected, infectedID := generateRandomId(personPointer, &(*socialDistancePointer).AllowContacts)
if infected && (*muskPointer).Active && epoch >= (*muskPointer).FromEpoch && (*muskPointer).FromEpoch != -1 {
prevent := bernoulli((*muskPointer).Psucc)
if ISDEBUG {
log.Println("SOCIAL DISTACING + MUSK POLICY", !prevent, infectedID)
}
return !prevent, infectedID
}
if ISDEBUG {
log.Println("SOCIAL DISTACING POLICY", infected, infectedID)
}
return infected, infectedID
} else if (*muskPointer).Active && epoch >= (*muskPointer).FromEpoch && (*muskPointer).FromEpoch != -1 {
// go into this branch only if musk policy
randomInfect := rand.Intn(len((*personPointer).Edges))
infectedID := int((*personPointer).Edges[randomInfect])
prevent := bernoulli((*muskPointer).Psucc)
if ISDEBUG {
log.Println("MUSK POLICY ACTIVE", !prevent, infectedID)
}
return !prevent, infectedID
} else {
randomInfect := rand.Intn(len((*personPointer).Edges))
infectedID := int((*personPointer).Edges[randomInfect])
if ISDEBUG {
log.Println("NO POLICY ACTIVE", infectedID)
}
return true, infectedID
}
}
// generateRandomId generate a random edge on the social distance policy
func generateRandomId(personPointer *person, allowContacts *map[string]bool) (bool, int) {
generationBaseAllowedId := make([]int, 1)
edgeLen := len((*personPointer).Edges)
// loop all edjes to found allowed ones
for edge := 0; edge < edgeLen; edge++ {
// check that relation type is allowed on the map
if (*allowContacts)[(*personPointer).RelationType[edge]] {
generationBaseAllowedId = append(generationBaseAllowedId, edge)
}
/*
// loop over allowed edge
for _, a := range *allowContacts {
if a == (*personPointer).RelationType[edge] {
generationBaseAllowedId = append(generationBaseAllowedId, edge)
break
}
}
*/
}
if len(generationBaseAllowedId) > 0 {
rand.Seed(time.Now().UnixNano())
randomInfect := int((*personPointer).Edges[rand.Intn(len(generationBaseAllowedId))])
return true, randomInfect
}
return false, 0
}