Skip to content

Commit

Permalink
Merge 3ffec4b into aa81374
Browse files Browse the repository at this point in the history
  • Loading branch information
rht authored Jul 6, 2019
2 parents aa81374 + 3ffec4b commit eff593a
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 30 deletions.
4 changes: 4 additions & 0 deletions examples/Schelling/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ 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.
Expand Down
51 changes: 51 additions & 0 deletions examples/Schelling/run_ascii.py
Original file line number Diff line number Diff line change
@@ -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.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'


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
Original file line number Diff line number Diff line change
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 eff593a

Please sign in to comment.