-
Notifications
You must be signed in to change notification settings - Fork 0
/
individual.go
190 lines (164 loc) · 5.16 KB
/
individual.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"image/color"
"math"
"math/rand"
"time"
"github.com/real-time-footfall-analysis/rtfa-simulation/geometry"
"github.com/real-time-footfall-analysis/rtfa-simulation/utils"
)
type Likelihood struct {
Destination DestinationID // If this likelihood is picked - where should we go
ProbabilityFunctions []func(time.Time) bool // Array of mutually exclusive functions to return "true" when we should use the corresponding probability
Probabilities []float64 // Array of probabilities. MUST be same cardinality as the above.
}
type Individual struct {
Loc geometry.Point // The point where this person current is stood
Tick int // The current tick, from a base of 0, to measure time
Likelihoods []Likelihood // The array of likelihoods for their preferences
StepSize float64 // The average size step that the person will walk at (picked from a normal distribution)
UpdateSender bool // set if this AI is to send updates
RegionIds map[int32]bool // map (set) containing keys of all regions the actor is in
UUID string // UUID of this actor for sending updates
Colour color.Color // colour to render this individual
LastMoveDist float64
MoveDistAvg float64 /// running avg of movements
CurrentOri float64 // Current orientation
CurrentSway float64 // Current sway
target *Destination // current target Destination
leaveTime time.Time // time to leave current place
UpdateChan *UpdateChan
}
const (
ORIENTATION_THRESHOLD = 0.1
)
func (l *Likelihood) ProbabilityAtTick(time time.Time) float64 {
bestProb := 0.0
for i, useProb := range l.ProbabilityFunctions {
if useProb(time) && l.Probabilities[i] > bestProb {
bestProb = l.Probabilities[i]
}
}
return bestProb
}
func (i *Individual) Next(w *State) DestinationID {
if i.target == nil || i.target.isClosed() {
i.leaveTime = time.Time{}
destID := i.requestedDestination(w)
i.target = w.scenario.GetDestination(destID)
return destID
}
dest := w.scenario.GetDestination(i.target.ID)
x, y := i.Loc.GetXY()
if dest.Contains(int(x), int(y)) {
// inside target
if i.leaveTime.IsZero() {
dest := w.scenario.GetDestination(i.target.ID)
if dest.MeanTime == 0 {
event := dest.NextEventToEnd(w.time)
if event == nil {
i.leaveTime = w.time
} else {
i.leaveTime = event.End
}
} else {
seconds := time.Duration((rand.NormFloat64()*dest.VarTime)+dest.MeanTime) * time.Second
i.leaveTime = w.time.Add(seconds)
}
}
if w.time.Before(i.leaveTime) {
return i.target.ID
} else {
i.leaveTime = time.Time{}
destID := i.requestedDestination(w)
i.target = w.scenario.GetDestination(destID)
return destID
}
} else {
// outside target
return i.target.ID
}
}
type ProbabilityPair struct {
prob float64
dest DestinationID
}
func (a *Individual) requestedDestination(w *State) DestinationID {
// Get all of the likelihood probabilities
var sum float64 = 0
probs := make([]ProbabilityPair, 0)
for _, likelihood := range a.Likelihoods {
dest := w.scenario.GetDestination(likelihood.Destination)
if !dest.isClosed() {
prob := likelihood.ProbabilityAtTick(w.time)
prob /= dest.density
sum += prob
probs = append(probs, ProbabilityPair{
prob: prob,
dest: likelihood.Destination,
})
}
}
// Normalise them
for i, prob := range probs {
probs[i].prob = prob.prob / sum
}
// Take a sample from them
randPick := rand.Float64()
var sumSoFar float64 = 0
for _, prob := range probs {
sumSoFar += prob.prob
if randPick < sumSoFar {
// Return the Destination
return prob.dest
}
}
return probs[0].dest
}
func (i *Individual) DirectionForDestination(dest DestinationID, w *State) utils.OptionalFloat64 {
x, y := i.Loc.GetXY()
if i.target.ContainsR(int(x), int(y), 0.7) {
// inside the area
return utils.OptionalFloat64WithValue((rand.Float64() - 0.5) * 2 * math.Pi)
}
tile := w.GetTileHighRes(i.Loc.GetXY())
if tile == nil {
return utils.OptionalFloat64WithEmptyValue()
}
var newOri float64
// Pick a random "sway" so they dont walk just in ordinal directions - more realistic
// TODO: Look for people in their ordinal direction and follow them
v, ok := tile.Directions[dest]
if !ok {
newOri = i.dumbDirection(w, dest)
} else {
Ori, present := v.Value()
if !present {
newOri = i.dumbDirection(w, dest)
} else {
newOri = Ori
}
}
newSway := (rand.Float64() - 0.5) * math.Pi / 2
if i.LastMoveDist < 0.05 {
newSway *= 2.5
}
// Only change direction if we are going sufficiently against the flow field
if math.Abs(i.CurrentOri-newOri) >= ORIENTATION_THRESHOLD ||
i.LastMoveDist < 0.05 {
// Set the new orientation and sway
i.CurrentOri = newOri
i.CurrentSway = newSway
}
return utils.OptionalFloat64WithValue(i.CurrentOri + i.CurrentSway)
}
func (i *Individual) dumbDirection(w *State, dest DestinationID) float64 {
x, y := i.Loc.GetXY()
destination := w.scenario.GetDestination(dest)
r := rand.Intn(len(destination.Coords))
coord := destination.Coords[r]
dx := float64(coord.X) - x
dy := float64(coord.Y) - y
theta := math.Atan2(dy, dx)
return theta
}