Skip to content

Commit

Permalink
Merge branch 'master' into ADD-pre-commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Corvince authored Apr 7, 2020
2 parents 69afa97 + bb56278 commit 023e8ce
Show file tree
Hide file tree
Showing 11 changed files with 209 additions and 128 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ You can also use `pip` to install the github version:

.. code-block:: bash
$ pip install -e git+https://github.com/projectmesa/mesa
$ pip install -e git+https://github.com/projectmesa/mesa#egg=mesa
Take a look at the `examples <https://github.com/projectmesa/mesa/tree/master/examples>`_ folder for sample models demonstrating Mesa features.

Expand Down
21 changes: 9 additions & 12 deletions docs/tutorials/adv_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -257,9 +257,9 @@
" // Create the tag:\n",
" var canvas_tag = \"<canvas width='\" + canvas_width + \"' height='\" + canvas_height + \"' \";\n",
" canvas_tag += \"style='border:1px dotted'></canvas>\";\n",
" // Append it to body:\n",
" // Append it to #elements:\n",
" var canvas = $(canvas_tag)[0];\n",
" $(\"body\").append(canvas);\n",
" $(\"#elements\").append(canvas);\n",
" // Create the context and the drawing controller:\n",
" var context = canvas.getContext(\"2d\");\n",
"};\n",
Expand All @@ -269,14 +269,12 @@
"\n",
"```javascript\n",
"var HistogramModule = function(bins, canvas_width, canvas_height) {\n",
" // Create the elements\n",
"\n",
" // Create the tag:\n",
" var canvas_tag = \"<canvas width='\" + canvas_width + \"' height='\" + canvas_height + \"' \";\n",
" canvas_tag += \"style='border:1px dotted'></canvas>\";\n",
" // Append it to body:\n",
" // Append it to #elements:\n",
" var canvas = $(canvas_tag)[0];\n",
" $(\"body\").append(canvas);\n",
" $(\"#elements\").append(canvas);\n",
" // Create the context and the drawing controller:\n",
" var context = canvas.getContext(\"2d\");\n",
"\n",
Expand Down Expand Up @@ -304,7 +302,7 @@
" };\n",
"\n",
" // Create the chart object\n",
" var chart = new Chart(context).Bar(data, options);\n",
" var chart = new Chart(context, {type: 'bar', data: data, options: options});\n",
"\n",
" // Now what?\n",
"};\n",
Expand All @@ -322,14 +320,13 @@
"var HistogramModule = function(bins, canvas_width, canvas_height) {\n",
" // ...Everything from above...\n",
" this.render = function(data) {\n",
" for (var i in data)\n",
" chart.datasets[0].bars[i].value = data[i];\n",
" datasets[0].data = data;\n",
" chart.update();\n",
" };\n",
"\n",
" this.reset = function() {\n",
" chart.destroy();\n",
" chart = new Chart(context).Bar(data, options);\n",
" chart = new Chart(context, {type: 'bar', data: data, options: options});\n",
" };\n",
"};\n",
"```\n",
Expand Down Expand Up @@ -431,9 +428,9 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.1"
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 0
"nbformat_minor": 1
}
17 changes: 7 additions & 10 deletions docs/tutorials/adv_tutorial.rst
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,9 @@ context, which is required for doing anything with it.
// Create the tag:
var canvas_tag = "<canvas width='" + canvas_width + "' height='" + canvas_height + "' ";
canvas_tag += "style='border:1px dotted'></canvas>";
// Append it to body:
// Append it to #elements:
var canvas = $(canvas_tag)[0];
$("body").append(canvas);
$("#elements").append(canvas);
// Create the context and the drawing controller:
var context = canvas.getContext("2d");
};
Expand All @@ -330,14 +330,12 @@ created, we can create the chart object.
.. code:: javascript
var HistogramModule = function(bins, canvas_width, canvas_height) {
// Create the elements
// Create the tag:
var canvas_tag = "<canvas width='" + canvas_width + "' height='" + canvas_height + "' ";
canvas_tag += "style='border:1px dotted'></canvas>";
// Append it to body:
// Append it to #elements:
var canvas = $(canvas_tag)[0];
$("body").append(canvas);
$("#elements").append(canvas);
// Create the context and the drawing controller:
var context = canvas.getContext("2d");
Expand Down Expand Up @@ -365,7 +363,7 @@ created, we can create the chart object.
};
// Create the chart object
var chart = new Chart(context).Bar(data, options);
var chart = new Chart(context, {type: 'bar', data: data, options: options});
// Now what?
};
Expand All @@ -389,14 +387,13 @@ With that in mind, we can add these two methods to the class:
var HistogramModule = function(bins, canvas_width, canvas_height) {
// ...Everything from above...
this.render = function(data) {
for (var i in data)
chart.datasets[0].bars[i].value = data[i];
datasets[0].data = data;
chart.update();
};
this.reset = function() {
chart.destroy();
chart = new Chart(context).Bar(data, options);
chart = new Chart(context, {type: 'bar', data: data, options: options});
};
};
Expand Down
2 changes: 1 addition & 1 deletion examples/hex_snowflake/Readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Conway's Game Of "Life"
# Conway's Game Of "Life" on a hexagonal grid

## Summary

Expand Down
13 changes: 10 additions & 3 deletions mesa/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,27 @@
Core Objects: Agent
"""
# mypy
from .model import Model
from random import Random


class Agent:
""" Base class for a model agent. """

def __init__(self, unique_id, model):
def __init__(self, unique_id: int, model: Model) -> None:
""" Create a new agent. """
self.unique_id = unique_id
self.model = model
self.pos = None

def step(self):
def step(self) -> None:
""" A single step of the agent. """
pass

def advance(self) -> None:
pass

@property
def random(self):
def random(self) -> Random:
return self.model.random
2 changes: 1 addition & 1 deletion mesa/datacollection.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def get_agent_vars_dataframe(self):
rep_names = [rep_name for rep_name in self.agent_reporters]

df = pd.DataFrame.from_records(
data=all_records, columns=["Step", "AgentID"] + rep_names
data=all_records, columns=["Step", "AgentID"] + rep_names,
)
df = df.set_index(["Step", "AgentID"])
return df
Expand Down
26 changes: 12 additions & 14 deletions mesa/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,22 @@
Core Objects: Model
"""
import time
import random

# mypy
from typing import Any, Optional


class Model:
""" Base class for models. """

def __new__(cls, *args, **kwargs):
def __new__(cls, *args: Any, **kwargs: Any) -> Any:
"""Create a new model object and instantiate its RNG automatically."""
cls._seed = kwargs.get("seed", None)
cls.random = random.Random(cls._seed)
return object.__new__(cls)

model = object.__new__(cls) # This only works in Python 3.3 and above
model._seed = time.time()
if "seed" in kwargs and kwargs["seed"] is not None:
model._seed = kwargs["seed"]
model.random = random.Random(model._seed)
return model

def __init__(self, *args, **kwargs):
def __init__(self, *args: Any, **kwargs: Any) -> None:
""" Create a new model. Overload this method with the actual code to
start the model.
Expand All @@ -36,24 +34,24 @@ def __init__(self, *args, **kwargs):
self.schedule = None
self.current_id = 0

def run_model(self):
def run_model(self) -> None:
""" Run the model until the end condition is reached. Overload as
needed.
"""
while self.running:
self.step()

def step(self):
def step(self) -> None:
""" A single step. Fill in here. """
pass

def next_id(self):
def next_id(self) -> int:
""" Return the next unique ID for agents, increment current_id"""
self.current_id += 1
return self.current_id

def reset_randomizer(self, seed=None):
def reset_randomizer(self, seed: Optional[int] = None) -> None:
"""Reset the model random number generator.
Args:
Expand Down
Loading

0 comments on commit 023e8ce

Please sign in to comment.