Skip to content

Commit

Permalink
Merge pull request #706 from rht/schelling-text
Browse files Browse the repository at this point in the history
Schelling: Separate text-only viz into run_ascii.py
  • Loading branch information
dmasad committed Jul 6, 2019
2 parents aa81374 + e86ce30 commit 7ca4b4e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 31 deletions.
2 changes: 1 addition & 1 deletion examples/Readme.md
Expand Up @@ -21,7 +21,7 @@ Simple cellular automata of a fire spreading through a forest of cells on a grid
### PD_Grid
Grid-based demographic prisoner's dilemma model, demonstrating how simple mimicing can lead to the emergence of widespread cooperation -- and how a model activation regime can change its outcome.

### Schelling
### Schelling (GUI and Text)
Mesa implementation of the classic [Schelling segregation model](http://nifty.stanford.edu/2014/mccown-schelling-model-segregation/).

### Shape Example
Expand Down
5 changes: 5 additions & 0 deletions examples/Schelling/README.md
Expand Up @@ -26,9 +26,14 @@ Then open your browser to [http://127.0.0.1:8521/](http://127.0.0.1:8521/) and p

To view and run some example model analyses, launch the IPython Notebook and open ``analysis.ipynb``. Visualizing the analysis also requires [matplotlib](http://matplotlib.org/).

## How to Run without the GUI

To run the model with the grid displayed as an ASCII text, run `python run_ascii.py` in this directory.

## Files

* ``run.py``: Launches a model visualization server.
* ``run_ascii.py``: Run the model in text mode.
* ``schelling.py``: Contains the agent class, and the overall model class.
* ``server.py``: Defines classes for visualizing the model in the browser via Mesa's modular server, and instantiates a visualization server.
* ``analysis.ipynb``: Notebook demonstrating how to run experiments and parameter sweeps on the model.
Expand Down
51 changes: 51 additions & 0 deletions examples/Schelling/run_ascii.py
@@ -0,0 +1,51 @@
from mesa.visualization.TextVisualization import (
TextData, TextGrid, TextVisualization
)

from model import Schelling


class SchellingTextVisualization(TextVisualization):
'''
ASCII visualization for schelling model
'''

def __init__(self, model):
'''
Create new Schelling ASCII visualization.
'''
self.model = model

grid_viz = TextGrid(self.model.grid, self.print_ascii_agent)
happy_viz = TextData(self.model, 'happy')
self.elements = [grid_viz, happy_viz]

@staticmethod
def print_ascii_agent(a):
'''
Minority agents are X, Majority are O.
'''
if a.type == 0:
return 'O'
if a.type == 1:
return 'X'


if __name__ == '__main__':
model_params = {
"height": 20,
"width": 20,
# Agent density, from 0.8 to 1.0
"density": 0.8,
# Fraction minority, from 0.2 to 1.0
"minority_pc": 0.2,
# Homophily, from 3 to 8
"homophily": 3
}

model = Schelling(**model_params)
viz = SchellingTextVisualization(model)
for i in range(10):
print("Step:", i)
viz.step()
print('---')
30 changes: 0 additions & 30 deletions examples/Schelling/server.py
Expand Up @@ -2,39 +2,9 @@
from mesa.visualization.modules import CanvasGrid, ChartModule, TextElement
from mesa.visualization.UserParam import UserSettableParameter

from mesa.visualization.TextVisualization import (
TextData, TextGrid, TextVisualization
)

from model import Schelling


class SchellingTextVisualization(TextVisualization):
'''
ASCII visualization for schelling model
'''

def __init__(self, model):
'''
Create new Schelling ASCII visualization.
'''
self.model = model

grid_viz = TextGrid(self.model.grid, self.ascii_agent)
happy_viz = TextData(self.model, 'happy')
self.elements = [grid_viz, happy_viz]

@staticmethod
def ascii_agent(a):
'''
Minority agents are X, Majority are O.
'''
if a.type == 0:
return 'O'
if a.type == 1:
return 'X'


class HappyElement(TextElement):
'''
Display a text count of how many happy agents there are.
Expand Down

0 comments on commit 7ca4b4e

Please sign in to comment.