-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathLinkTravelModel.js
More file actions
58 lines (50 loc) · 1.82 KB
/
LinkTravelModel.js
File metadata and controls
58 lines (50 loc) · 1.82 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
import Model from 'https://agentscript.org/src/Model.js'
import * as util from 'https://agentscript.org/src/utils.js'
export default class LinkTravelModel extends Model {
layoutCircle = true
numNodes = 30
numDrivers = 100
speed = 0.1
speedDelta = 0.1
// ======================
// We can use Model's constructor, due to using Model's default World.
// If you pass in world options, Model will use them
// constructor() {
// super() // use default world options.
// }
setup() {
this.turtleBreeds('nodes drivers')
this.nodes.create(this.numNodes)
// create two links per node to random other nodes.
// two so that there is always an outgoing link other than the incomming one.
this.nodes.ask(n => this.links.create(n, this.nodes.otherNOf(2, n)))
if (this.layoutCircle) {
this.nodes.layoutCircle(this.world.maxX - 1)
} else {
this.nodes.ask(n => n.moveTo(this.patches.oneOf()))
}
util.repeat(this.numDrivers, () => {
const node = this.nodes.oneOf()
node.hatch(1, this.drivers, driver => {
driver.fromNode = node
driver.toNode = node.linkNeighbors().oneOf()
driver.face(driver.toNode)
driver.speed = this.speed + util.randomFloat(this.speedDelta)
})
})
}
step() {
this.drivers.ask(driver => {
const moveBy = Math.min(
driver.speed,
driver.distance(driver.toNode)
)
driver.face(driver.toNode)
driver.forward(moveBy)
if (moveBy < driver.speed) {
driver.fromNode = driver.toNode
driver.toNode = util.oneOf(driver.toNode.linkNeighbors())
}
})
}
}