Create a gist now

Instantly share code, notes, and snippets.

@wybo /model.coffee
Last active Mar 5, 2016

What would you like to do?
Template
# 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.
# This model shows the basic structure of a model and is a good
# place to get started when you want to try building your own.
#
# To build your own model, extend class ABM.Model supplying the
# two built in methods `setup` and `step`.
#
#`@foo` signifies a variable or method that is part of the Model
# class.
#
# Note how this example uses CoffeeScript directly within the
# browser via [text/coffeescript tags](http://coffeescript.org/#scripts):
u = ABM.util # shortcut for ABM.util, some tools
log = (arg) -> console.log arg # log to the browser console
class ABM.TemplateModel extends ABM.Model
# Initialize our model via the `setup` method. This model simply
# creates a population of agents with arbitrary shapes on a grid
# of patches colored in random shades of gray.
#
setup: -> # called by Model constructor
# When agents move, they will wiggle. This sets the
# degrees/radians to wiggle, which is used further below.
#
@wiggle = u.degreesToRadians(30)
for patch in @patches.create()
patch.color = u.color.random "gray"
for agent in @agents.create 25
agent.size = 3
agent.shape = u.shapes.names().sample()
# Update, or run our model for one step, via the second built in
# method, `step`.
#
# The agents wiggle, and move forward.
#
step: -> # called by Model.animate
for agent in @agents
agent.rotate u.randomCentered @wiggle
agent.forward 1
# Now that we've build our class, we call it with Model's
# constructor arguments:
#
# div: name of the div
# isTorus: map curls around the edges = has torus topology.
#
window.model = new ABM.TemplateModel({
div: "world",
isTorus: true
})
window.model.start()
# Uses the AgentBase library (05-03-2016 release)
# https://github.com/wybo/agentbase/commit/7b753b12622a88d4a1df8bf0ff1efa1f6b663973
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment