Repository navigation
Expand file tree
/
Copy pathants.html
More file actions
154 lines (136 loc) · 5.51 KB
/
Copy pathants.html
File metadata and controls
154 lines (136 loc) · 5.51 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
<html>
<head>
<title>Ants Model</title>
<script src="../lib/agentscript.js"></script>
<script src="../tools/coffee-script.js"></script>
<script type="text/coffeescript">
u = ABM.Util # aliases
Shapes = ABM.Shapes
ColorMaps = ABM.ColorMaps
class MyModel extends ABM.Model
setup: ->
# No optimizations: 10fps
@patches.usePixels() # 24fps
# @patches.cacheTurtlesHere() # 9-10fps, not needed & modest overhead
# two: 27fps, now static colors larger percent of faster patches.
# Deprecated: @turtles.setUseSprites() # 24->46-48fps!
@turtles.setDefault "useSprites", true
# @anim.setRate 25, true # normal: let turtles step faster than drawing
@anim.setRate 60, false # speed test
# globals
@nestColor = "yellow" # [255, 255, 0] # yellow
@foodColor = "blue" # [0, 0, 255] # blue
# New color model: use maps for diffusion colors
@nestColorMap = ABM.ColorMaps.NetLogo.yellow
@foodColorMap = ABM.ColorMaps.NetLogo.blue
# defaults
@turtles.setDefault "shape", "bug"
@turtles.setDefault "color", @foodColor # so default sprite not random color
# bugSize = 3
@turtles.setDefault "size", 3 # bugSize
# UI globals:
@population = 255
@maxPheromone = 35
@diffusionRate = .30
@evaporationRate = .01
@wiggleAngle = u.degToRad(30) # radians
if @turtles.getDefault "useSprites"
bitSize = @patches.toBits @turtles.getDefault("size") #bugSize
@nestSprite = Shapes.shapeToSprite "bug", @nestColor, bitSize, "red"
@foodSprite = Shapes.shapeToSprite "bug", @foodColor, bitSize, "blue"
@setupPatches()
@setupTurtles()
step: ->
@updateTurtles()
@updatePatches()
setupPatches: ->
for p in @patches
p.isNest = false ; p.isFood = false
# REMIND: turtlesOwn etc; NL defaults to 0
p.foodPheromone = 0; p.nestPheromone = 0
for p in @patchRect(@patches.maxX-6, 0, 3, 3)
p.isNest=true; p.color=@nestColor
for p in @patchRect(@patches.minX+6, 0, 3, 3)
p.isFood=true; p.color=@foodColor
null
setupTurtles: -> # a is turtle
@turtles.create @population, (a) => # fat arrow for @patches etc
#a.shape = "bug"
#a.size = 3
a.setXY @patches.maxX-6, 0
@resetTurtle(a, false) # sets a.pheromone to max
resetTurtle: (a, withFood) ->
a.carryingFood = withFood
if a.sprite?
a.sprite = if withFood then @nestSprite else @foodSprite
# a.setSprite (if withFood then @nestSprite else @foodSprite)
else
a.color = if withFood then @nestColor else @foodColor
# a.colorStr = u.colorStr a.color if @turtles.staticColors
a.pheromone = @maxPheromone
updateTurtles: ->
for a in @turtles when a.id < @anim.ticks # gradually leave nest
@wiggleUphill a
@dropPheromone a
null # needed to avoid returning an absurd comprehension
wiggleUphill: (a) ->
if a.p.isOnEdge()
# a.rotate Math.PI
a.rotate u.degToRad(180)
else
# nAhead = a.inCone a.p.n, Math.PI, 2
nAhead = a.inCone a.p.n, u.degToRad(180), 2
# nAhead = a.p.n.inCone a.p, a.heading, u.degToRad(180), 2
# nAhead = @patches.inCone a.p, a.heading, u.degToRad(180), 2
[p, max] = nAhead.maxOneOf ((n) => @targetPheromone(a, n)), true # => for @
a.face p if max > .001/@maxPheromone
# p = nAhead.maxOneOf ((n) => @targetPheromone(a, n)) # => for @
# max = @targetPheromone(a, p)
# a.face p if max > .001/@maxPheromone
a.rotate u.randomCentered(@wiggleAngle)
a.forward 1
targetPheromone: (a, p) ->
if a.carryingFood then p.nestPheromone else p.foodPheromone
dropPheromone: (a) ->
if (not a.carryingFood and a.p.isFood) or (a.carryingFood and a.p.isNest)
@resetTurtle a, not a.carryingFood
if a.carryingFood
then a.p.foodPheromone += 0.1*a.pheromone
else a.p.nestPheromone += 0.1*a.pheromone
a.pheromone *= 0.9
updatePatches: ->
@patches.diffuse "nestPheromone", @diffusionRate
@patches.diffuse "foodPheromone", @diffusionRate
for p in @patches
p.foodPheromone *= (1 - @evaporationRate)
p.nestPheromone *= (1 - @evaporationRate)
p.foodPheromone = p.nestPheromone = 0 if p.isOnEdge()
unless (p.isNest or p.isFood) # or p.isOnEdge()
if p.foodPheromone > p.nestPheromone
then p.color = @foodColorMap.scaleColor p.foodPheromone, 0, 1
else p.color = @nestColorMap.scaleColor p.nestPheromone, 0, 1
# Old:
# then p.scaleColor @foodColor, p.foodPheromone
# else p.scaleColor @nestColor, p.nestPheromone
null
# utilities
patchRect: (x, y, dw, dh) ->
@patches.patchRect @patches.patch(x,y), dw, dh, true
# div, patchSize, minX, maxX, minY, maxY, isTorus, hasNeighbors
# Defaults: 13, -16, 16, -16, 16, false, true
window.model = new MyModel {
div: "layers",
size: 6,
minX: -40,
maxX: 40,
minY: -40,
maxY: 40
}
model.debug() # Debug: Put Model vars in global name space
model.start() # Run model immediately after startup initialization
</script>
</head>
<body>
<div id="layers"></div>
</body>
</html>