Skip to content

Instantly share code, notes, and snippets.

@wybo
Last active May 1, 2017 23:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wybo/96c36a9b3a1760f3c55f to your computer and use it in GitHub Desktop.
Save wybo/96c36a9b3a1760f3c55f to your computer and use it in GitHub Desktop.
Link Travel
# AgentBase is Free Software, available under GPL v3 or any later version.
# Original AgentScript code @ 2013, 2014 Owen Densmore and RedfishGroup LLC.
# AgentBase (c) 2014, Wybo Wiersma.
# Link Travel has agents traversing a graph of nodes and links.
u = ABM.util
class ABM.LinkTravelModel extends ABM.Model
setup: ->
@agentBreeds ["nodes", "drivers"]
@nodes.setDefault "shape", "circle"
@nodes.setDefault "size", .3
@drivers.setDefault "size", 1.5
# 40-44fps -> 58+fps
@refreshPatches = false # 56fps
@refreshLinks = false # 40-49fps
# @patches.usePixels() # 58+fps same as refresh off
@agents.setUseSprites() # 24 -> 36
# w/ refreshs off and 1 optimization 59/max-fps
@animator.setRate 30, true # almost 3:1 steps/draws!
# @animator.setRate 60, false # for optimization studies & profiling
# globals
@numNodes = 30
@numDrivers = 100
@layoutCircle = true
@baseVelocity = 0.1 # patchs
@velocityDelta = 0.1 # patches
@links.setDefault "labelColor", u.color.red
for patch in @patches.create()
patch.color = u.color.random type: "gray", min: 10, max: 50
for patch in @patches.sample @numNodes
patch.sprout 1, @nodes, (agent) =>
if @nodes.length > 1 # if @nodes().length > 1
@links.create agent, @nodes.other(agent).sample()
if @layoutCircle
@nodes.formCircle @patches.max.x - 1
# Test labels
# l.label = l.length().toFixed(0) for l in @links
# no defaults, hatch copies its values to new agent
for i in [1..@numDrivers]
node = @nodes.sample() # n = @nodes().sample()
node.hatch 1, @drivers, (agent) =>
agent.fromNode = agent.toNode = node.linkNeighbors().sample()
agent.face agent.toNode.position
agent.v = @baseVelocity + u.randomFloat @velocityDelta
null # avoid returning "for" results above
step: ->
console.log @animator.toString() if @animator.ticks % 100 is 0
for driver in @drivers
driver.face driver.toNode.position
driver.forward(
Math.min driver.v, driver.distance(driver.toNode.position))
if .01 > driver.distance driver.toNode.position
driver.fromNode = driver.toNode
driver.toNode = driver.toNode.linkNeighbors().sample()
null # avoid returning "for" results above
window.model = new ABM.LinkTravelModel {
div: "world",
patchSize: 13
}
window.model.start()
# Uses the AgentBase library (03-02-2017 release)
# https://github.com/wybo/agentbase/commit/3501302567c018da82601cd858be2ea6d0b9c74d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment