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/beba752ebfce2daaaa0e to your computer and use it in GitHub Desktop.
Save wybo/beba752ebfce2daaaa0e to your computer and use it in GitHub Desktop.
Preferential Attachment
# 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.
# Preferential Attachment models a dynamic graph where new links
# preferentially attach to the nodes that have the most links. This
# results in a power-law distribution.
u = ABM.util
class ABM.PreferentialAttachmentModel extends ABM.Model
setup: ->
@refreshPatches = false # for static patches
# globals
@circleLayout = true
@histogram = []
@stopTick = 400
# defaults
@agents.setDefault "shape", "circle"
@links.setDefault "thickness", .75 # Easier to see thiner link
@patches.create()
@agents.create 1
@makeNode @agents[0]
step: ->
@makeNode @findPartner()
for agent in @agents
agent.size = Math.sqrt agent.linkNeighbors().length
@agents.formCircle @patches.max.x * 0.9
@histogram = @agents.histogram 1, (agent) ->
agent.linkNeighbors().length
if @animator.ticks % 25 is 0
console.log "Tick: #{@animator.ticks} Histogram: [#{@histogram}]"
if @animator.ticks is @stopTick
(console.log "Stopping at stopTick: #{@animator.ticks}"; @stop())
makeNode: (toNode) ->
@agents.create 1, (agent) =>
@links.create agent, toNode
agent.moveTo toNode.position
agent.forward 8
findPartner: ->
@links.sample().bothEnds().sample()
window.model = new ABM.PreferentialAttachmentModel {
div: "world",
patchSize: 5,
mapSize: 90
}
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