Skip to content

Instantly share code, notes, and snippets.

View bfbraum's full-sized avatar

Bear Braumoeller bfbraum

View GitHub Profile
@bfbraum
bfbraum / Simple ABM template.R
Created May 29, 2017 15:44
A template for running and plotting a very simple agent-based model in R
# Template for running and plotting a very simple agent-based model in R
# Professor Bear Braumoeller, Department of Political Science, Ohio State
# This code creates a 20x20 grid of 0s and 1s, which represent values of some
# variable held by agents in those cells. It then chooses two adjacent cells,
# the first at random and the second at random from among the first cell's
# neighbors, and applies a simple rule -- the first cell takes on the value
# of the second. It iterates this cell selection and rule application 1,000
# times, displays the result, and tracks the fraction of 1s in the matrix
# over time.
# Code to create new matrix based on some function of neighbors of old matrix.
# Professor Bear Braumoeller, Department of Political Science, Ohio State
# This code sets up a 10x10 matrix and then demonstrates how to generate a second
# 10x10 matrix whose entries at (r,c) are a function of the adjacent cells of the
# original matrix at (r,c). In this case, it's used to answer the question,
# "Which cells in this matrix have no adjacent cells with values equal to 5?"
# It can also be used for summing adjacent cells, etc., etc.
# In an agent-based model, this is one way that an agent can "look" for other
# cells that meet certain criteria.
@bfbraum
bfbraum / Axelrod Continuous v2.R
Last active June 21, 2017 07:36
Axelrod's Dissemination of Culture model in R
# Axelrod's Dissemination of Culture model in R
# From Axelrod, Robert. “The Dissemination of Culture: A Model with Local Convergence
# and Global Polarization.” Journal of Conflict Resolution 41, no. 2 (1997): 203–226.
# Professor Bear Braumoeller, Department of Political Science, Ohio State
# This code creates a 16x16x3 array of 0s and 1s, which represent values of
# 3 traits held by agents in those cells. It then chooses two adjacent cells,
# the first at random and the second at random from among the first cell's
# neighbors, compares the overall similarity of the two, attempts to communicate
# with p(success) based on degree of similarity, and if successful changes one
# A very basic implementation of the standing ovation problem
# Miller, John H., and Scott E. Page. “The Standing Ovation Problem.” Complexity 9, no. 5 (2004): 8–16.
# Professor Bear Braumoeller, Department of Political Science, Ohio State
# The model implements two rules. In the 0th round, people stand if their estimate
# of the quality of the performance is greater than their threshold for standing.
# Both are measured on a 0-1 interval, so if estimate = 0.9 and threshold = 0.8, for
# example, the person stands. Ranges of estimates and thresholds can be manipulated.
# In the first and subsequent rounds, people stand up if the fraction of people they
# Code to specify neighborhoods and apply functions to them.
# Professor Bear Braumoeller, Department of Political Science, Ohio State
# This code sets up a 100x100 grid, implemented as a matrix,
# and creates functions to do three things:
# 1. return the values from a neighborhood of cells around cell (row,col)
# within a specified radius r
# 2. return the coordinates of the cells with the highest value in that
# neighborhood
# 3. return the coordinates of the closest cell with the highest value in