-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathModel.js
216 lines (201 loc) · 6.39 KB
/
Model.js
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
import World from './World.js'
import GeoWorld from './GeoWorld.js'
import Patches from './Patches.js'
import Patch from './Patch.js'
import Turtles from './Turtles.js'
import Turtle from './Turtle.js'
import Links from './Links.js'
import Link from './Link.js'
/**
* Class Model is the primary interface for modelers, integrating
* the Patches/Patch Turtles/Turtle and Links/Link AgentSets .. i.e.:
*
* - model.Patches: an array {@link Patches} of {@link Patch} instances
* - model.Turtles: an array {@link Turtles} of {@link Turtle} instances
* - model.Links: an array {@link Links} of {@link Link} instances
* - model.breed: a sub-array of any of the three above.
* - All of which are subclasses of {@link AgentSet}.
*
* Convention: Three abstract methods are provided by the modeler
*
* - startup(): (Optional) Called once to import images, data etc
* - setup(): Called to initialize the model state.
* - step(): Step the model. Will advance ticks.
*/
class Model {
world
patches
turtles
links
ticks
// set to true by subclasses to let animator and others know we're done
done = false
/**
* Creates an instance of Model.
* The worldOptions define the coordinate system for this model
* The {@link World} options sets xMin, xMax, yMin, yMax
* and when using 3D: zMin, zMax
*
* Fine point: Can also be an instance of World or GeoWorld
*
* @constructor
* @param {Object} [worldOptions=World.defaultOptions()]
*/
constructor(worldOptions = World.defaultOptions()) {
this.initModel(worldOptions)
this.setAutoTick(true)
this.setGeometry('heading')
}
initAgentSet(name, AgentsetClass, AgentClass) {
this[name] = new AgentsetClass(this, AgentClass, name)
}
options2world(worldOptions) {
return worldOptions.bbox
? new GeoWorld(worldOptions)
: new World(worldOptions)
}
/**
* Initialize model to initial state w/ new Patches, Turtles, Links.
* The worldOptions will default to initial values but can be
* changed by modeler.
*
* @param {Object|World} [worldOptions=this.world] World object
*/
initModel(worldOptions) {
this.ticks = 0
this.world =
worldOptions.maxXcor === undefined // is already a world object
? this.options2world(worldOptions)
: worldOptions
// Base AgentSets setup here. Breeds handled by setup
this.initAgentSet('patches', Patches, Patch)
this.initAgentSet('turtles', Turtles, Turtle)
this.initAgentSet('links', Links, Link)
}
/**
* Reset the model by clearing the turtles, setting ID & ticks to 0
* and calling setup()
*
* @param {boolean} [callSetup=true]
*/
reset(callSetup = true) {
this.turtles.clear()
this.turtles.ID = 0
this.ticks = 0
this.done = false
if (callSetup) this.setup()
}
/**
* Increment the tick cound. Not needed if autoTick true, the default
*/
tick() {
this.ticks++
}
/**
* An abstract method to perform one-time initialization.
* Subclasses provide their versions of this to import data.
*
* @abstract
*/
async startup() {}
/**
* An abstract method for initializing the model
* Subclasses provide their version of this to initialice the model
*
* @abstract
*/
setup() {}
/**
* An abstract method to run the model one step.
*
* @abstract
*/
step() {}
// A trick to auto advance ticks every step using a Proxy
setAutoTick(autoTick = true) {
const isAutoTick = !!this.stepTarget
if (autoTick) {
if (isAutoTick) return
this.stepTarget = this.step
this.step = new Proxy(this.stepTarget, {
apply: (target, thisArg, args) => {
this.stepTarget()
this.tick()
// console.log('ticks', this.ticks)
},
})
} else {
this.step = this.stepTarget
delete this.stepTarget
}
}
/**
* Create breeds (sub-arrays) of Patches. Used in the Exit model:
* * this.patchBreeds('exits inside wall')
*
* @param {string} breedNames A string of space separated breeds names
*/
patchBreeds(breedNames) {
for (const breedName of breedNames.split(' ')) {
this[breedName] = this.patches.newBreed(breedName)
}
}
/**
* Create breeds (sub-arrays) of Turtles. Used in Wallfollower model:
* * this.turtleBreeds('lefty righty')
*
* @param {string} breedNames A string of space separated breeds names
*/
turtleBreeds(breedNames) {
for (const breedName of breedNames.split(' ')) {
this[breedName] = this.turtles.newBreed(breedName)
}
}
/**
* Create breeds (sub-arrays) of Links. Used in Roads model:
* * this.linkBreeds('trips')
*
* @param {string} breedNames A string of space separated breeds names
*/
linkBreeds(breedNames) {
for (const breedName of breedNames.split(' ')) {
this[breedName] = this.links.newBreed(breedName)
}
}
setGeometry(name = 'heading') {
const geometry = geometries[name]
if (!geometry) throw Error(`setGeometry: ${name} geometry not defined`)
Object.assign(this, geometry)
}
}
// Five geometry functions converting to the JavaScript
// radians: radians counter-clockwise from the x-axis.
// degrees: degrees counter-clockwise from the x-axis.
// heading: "clock geometry", clockwise degrees from y axis
// Add mod2pi & mod360?
const toDeg = 180 / Math.PI
const toRad = Math.PI / 180
const geometries = {
radians: {
toRads: rads => rads,
fromRads: rads => rads,
toAngleRads: rads => rads,
fromAngleRads: rads => rads,
toCCW: angle => angle,
},
degrees: {
toRads: deg => deg * toRad,
fromRads: rads => rads * toDeg,
toAngleRads: deg => deg * toRad,
fromAngleRads: rads => rads * toDeg,
toCCW: angle => angle,
},
heading: {
toRads: deg => (90 - deg) * toRad,
fromRads: rads => 90 - rads * toDeg,
toAngleRads: deg => deg * toRad,
fromAngleRads: rads => rads * toDeg,
toCCW: angle => -angle,
},
}
export default Model