forked from mtsatsev/NaturalComputingProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboid.js
More file actions
262 lines (207 loc) · 8.03 KB
/
Copy pathboid.js
File metadata and controls
262 lines (207 loc) · 8.03 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
class Boid {
constructor(id, pos_x, pos_y, rule_weights) {
// Boid ID, for keeping track of this boid's position in the distance matrix
this.id = id
// Position
this.pos = createVector(pos_x, pos_y)
// Velocity
this.vel = p5.Vector.random2D().mult(2)
// Acceleration
this.acc = createVector(0, 0)
// Set maximum velocity and acceleration (Same as in previous assignment)
this.max_vel = 2
this.max_acc = 0.1
// Set vision range
this.vision_range = 50
// Keep track of neighbours (within vision)
this.max_neighbours = 5
this.neighbours = [] // Neigbour positions and velocities
// Food consumption
this.cd = 0
this.consumed_food = 0
this.rw = rule_weights
// Set movement rule strengths (rw = rule weight)
// Positional rules
this.rw_mean_pos = rule_weights[0]
this.rw_near_pos = rule_weights[1]
this.rw_far_pos = rule_weights[2]
// Velocity rules
this.rw_mean_vel = rule_weights[3]
this.rw_near_vel = rule_weights[4]
this.rw_far_vel = rule_weights[5]
// Allignment rules
this.rw_mean_all = rule_weights[6]
this.rw_near_all = rule_weights[7]
this.rw_far_all = rule_weights[8]
// Seperation rules
this.rw_mean_sep = rule_weights[9]
this.rw_near_sep = rule_weights[10]
this.rw_far_sep = rule_weights[11]
// Food rules
this.rw_near_food = rule_weights[12]
}
// Boid_distances = [#Boid x #Boid] matrix
collect_neighbours(Boid_distances) {
// Copy by value
let neighbour_distances = Boid_distances[this.id];
// Sort by numerical value
neighbour_distances.sort(function (a, b) { return a[0] - b[0] })
// get the first max_neighbour + 1 (self is included) boid
neighbour_distances = neighbour_distances.slice(1, this.max_neighbours + 1)
for (let i = 0; i < neighbour_distances.length; i++) {
if (neighbour_distances[i][0] > this.vision_range) {
return neighbour_distances.slice(0, i)
}
}
return neighbour_distances // Array[Tuple(float, vector, Boid)]
}
// Calculate movement vector based on the movement rules.
move(Boid_distances) {
// Get neighbour_distances
var neighbour_distances = this.collect_neighbours(Boid_distances)
var acc_hat = createVector()
// Go over all movement rules
if (neighbour_distances.length > 0) {
// Positional rules
acc_hat = this.positional(acc_hat, neighbour_distances)
// Velocity rules
acc_hat = this.velocity(acc_hat, neighbour_distances)
// Allignment rules
acc_hat = this.allignment(acc_hat, neighbour_distances)
// Seperation rules
acc_hat = this.seperation(acc_hat, neighbour_distances)
}
// Food rules
acc_hat = this.food(acc_hat)
// Update Velocity
acc_hat.limit(this.max_acc)
this.vel.add(acc_hat)
this.vel.limit(this.max_vel)
// Update Position
this.pos.add(this.vel)
if (this.pos.x > width) {
this.pos.x = 0;
}
if (this.pos.y > height) {
this.pos.y = 0;
}
if (this.pos.x < 0) {
this.pos.x = width;
}
if (this.pos.y < 0) {
this.pos.y = height;
}
}
positional(acc_hat, neighbour_distances) {
var mean_pos = createVector() // Mean position
let j = 0
for (let i = 0; i < this.max_neighbours && i < neighbour_distances.length; i++) {
j += 1
mean_pos.add(neighbour_distances[i][1])
}
acc_hat.add(mean_pos.mult(-this.rw_mean_pos / j))
// Nearest neighbour
var near_pos = createVector()
near_pos.add(neighbour_distances[0][1])
acc_hat.add(near_pos.mult(-this.rw_near_pos))
// Furthest neighbour
var far_pos = createVector()
far_pos.add(neighbour_distances[neighbour_distances.length - 1][1])
acc_hat.add(far_pos.mult(-this.rw_far_pos))
return acc_hat
}
velocity(acc_hat, neighbour_distances) {
var mean_vel = this.vel.copy()
mean_vel.setMag(1) // Mean position
let magnitude = 0
let j = 0
for (let i = 0; i < this.max_neighbours && i < neighbour_distances.length; i++) {
j += 1
magnitude += neighbour_distances[i][2].vel.mag() //*this.rw_mean_vel
}
magnitude /= j
mean_vel.setMag(magnitude)
acc_hat.add(mean_vel.mult(this.rw_mean_vel))
// Nearest neighbour
var near_vel = this.vel.copy()
near_vel.setMag(neighbour_distances[0][2].vel.mag())
acc_hat.add(near_vel.mult(this.rw_near_vel))
// Furthest neighbour
var far_vel = this.vel.copy()
far_vel.setMag(neighbour_distances[neighbour_distances.length - 1][2].vel.mag())
acc_hat.add(far_vel.mult(this.rw_far_vel))
return acc_hat
}
allignment(acc_hat, neighbour_distances) {
var mean_allign = createVector(1, 0) // Mean position
let rads = 0
let j = 0
for (let i = 0; i < this.max_neighbours && i < neighbour_distances.length; i++) {
j += 1
rads += neighbour_distances[i][2].vel.heading()
//*this.rw_mean_pos
}
rads /= j
mean_allign.setHeading(rads)
acc_hat.add(mean_allign.mult(this.rw_mean_all))
// Nearest neighbour
var near_allign = createVector(1, 0)
near_allign.setHeading(neighbour_distances[0][2].vel.heading())
acc_hat.add(near_allign.mult(this.rw_near_all))
// Furthest neighbour
var far_allign = createVector(1, 0)
far_allign.setHeading(neighbour_distances[neighbour_distances.length - 1][2].vel.heading())
acc_hat.add(far_allign.mult(this.rw_far_all))
return acc_hat
}
seperation(acc_hat, neighbour_distances) {
var mean_sep = createVector() // Mean position
let j = 0
for (let i = 0; i < this.max_neighbours && i < neighbour_distances.length; i++) {
j += 1
mean_sep.add(neighbour_distances[i][1])
mean_sep.mult(1 - (neighbour_distances[i][0] / this.vision_range))//*this.rw_mean_pos
}
acc_hat.add(mean_sep.mult(this.rw_mean_sep / j))
// Nearest neighbour
var near_sep = createVector()
near_sep.add(neighbour_distances[0][1])
near_sep.mult(1 - (neighbour_distances[0][0] / this.vision_range))
acc_hat.add(near_sep.mult(this.rw_near_sep))
// Furthest neighbour
var far_sep = createVector()
far_sep.add(neighbour_distances[neighbour_distances.length - 1][1])
far_sep.mult(1 - (neighbour_distances[neighbour_distances.length - 1][0] / this.vision_range))
acc_hat.add(far_sep.mult(this.rw_far_sep))
return acc_hat
}
food(acc_hat) {
var food_distances = []
// Calculate distances
for (let food of Foods) {
if (food.weight > 0) {
let vector = p5.Vector.sub(this.pos, food.pos)
let distance = vector.mag()
food_distances.push([distance, vector, food])
}
}
// sort to get closest
food_distances.sort(function (a, b) { return a[0] - b[0] })
// Go to closest (if inside of vision)
if (food_distances[0][0] <= this.vision_range) {
var near_food = createVector()
near_food.add(food_distances[0][1])
acc_hat.add(near_food.mult(-this.rw_near_food))
}
// Food eating if available
if (this.cd > 0) {
this.cd--
}
if (this.cd == 0 && food_distances[0][0] <= 5) {
this.cd = FoodCoolDown;
this.consumed_food++;
food_distances[0][2].weight--;
}
return acc_hat
}
}