-
Notifications
You must be signed in to change notification settings - Fork 1
Home
Welcome to the Group8 wiki! Here is how to run the mesa model in our repo: Chinese Ver.
First, you need to install Git to get the repository code. Follow these steps to install:
- Go to the Git official website and download the Git installer for your operating system.
- After installation, open the command line tool (Windows: CMD/PowerShell; Mac/Linux: Terminal).
- Run the following command to clone the repository:
git clone https://github.com/EECS4461/Group8.git- Install GitHub Desktop.
- Open it and press
[Ctrl] + [Shift] + [O]to Clone a repo, enter the repository URL:https://github.com/EECS4461/Group8.git. - Choose the local path and click Clone.
Check if the local Python environment version is >= 3.11 in the terminal:
python -V # or python3 -V- If the output is
Python 3.11.xor higher, skip the installation steps. - If the version is lower than 3.11, install it as follows:
https://www.python.org/downloads/ Download the installer for your platform.
- Go to the Anaconda official website to download and install Anaconda (which includes Conda). Or install Miniconda (lightweight version).
- After installation, open the command line tool, create a new Conda environment and activate it:
conda create --name mesa-env python=3.11
conda activate mesa-envAfter confirming that Python and pip have been added to the environment variables, or directly using Method 2, install the project dependencies in the activated Conda environment:
- Switch the working directory to the cloned repository directory in the terminal:
cd /path/to/Group8- Run the following command to install dependencies:
pip install -r requirements.txt- If using Conda, make sure the
mesa-envenvironment is activated (runconda activate mesa-envin the terminal).
Finally, you can run the demo in the project:
- In the repository directory, run the following command to start the demo:
-
demo_01 with datacollect chart
This example demonstrates the statistical table for data collection:
solara run ./src/demo_01.py-
After running, the browser will automatically open
http://localhost:8765. -
demo_02 Agents in different colors and shapes
This example demonstrates defining shapes and colors for different agents:
solara run ./src/demo_02.pyNow, you should see the demo running in the browser. Please refer to the implementation in the demo and the official Mesa documentation and example models, and follow the two simulation design deployment guides in doc/DEL3 to participate in the improvement of the Xiaohongshu model!
Mesa Official Getting Started Documentation
Team Organization Forked Mesa Framework Repository
Team Organization Forked Advanced Example Model Repository
Mesa supports running and debugging models in real-time in Jupyter Notebook, allowing you to modify parameters, observe changes, and visualize results instantly. Here are the steps:
Example Jupyter Notebook in the Group Forked Example Model Repository
If not installed, run the following command in the terminal (ensure in the Conda environment):
# Install with Conda (recommended)
conda install jupyterlab
# Or install with pip
pip install jupyterlab- Enter the project directory (cloned repository path) in the terminal:
cd /path/to/Group8 - Start Jupyter Lab:
jupyter lab
- The browser will automatically open
http://localhost:8888, click thePython 3icon to create a new Notebook.
- Enter the following code in a new Notebook cell (example of a simple agent model):
from mesa import Model, Agent
from mesa.time import RandomActivation
from mesa.space import MultiGrid
from mesa.visualization.ModularVisualization import ModularServer
# Define Agent Class
class SimpleAgent(Agent):
def __init__(self, unique_id, model):
super().__init__(unique_id, model)
def step(self):
print(f"Agent {self.unique_id} is taking action")
# Define Model Class
class SimpleModel(Model):
def __init__(self, N=5, width=10, height=10):
self.num_agents = N
self.grid = MultiGrid(width, height, True)
self.schedule = RandomActivation(self)
# Create Agents
for i in range(self.num_agents):
a = SimpleAgent(i, self)
self.schedule.add(a)
x = self.random.randrange(self.grid.width)
y = self.random.randrange(self.grid.height)
self.grid.place_agent(a, (x, y))
def step(self):
self.schedule.step()
# Launch Mesa Visualization Server (render directly in Notebook)
server = ModularServer(SimpleModel, [], "Simple Model", {"N":5, "width":10, "height":10})
server.port = 8521 # Specify port
server.launch()- Press
Shift + Enterto run the cell. -
A new tab will automatically open in the browser displaying the model visualization interface (if no popup, manually visit
http://localhost:8521).
- Adjust model initialization parameters in the code (e.g.,
N=10to increase the number of agents). -
Re-run the cell (click the cell and press
Shift + Enter). - Refresh the browser page to see the updated model.
Add CanvasGrid visualization to the model (example code):
from mesa.visualization.modules import CanvasGrid
def agent_portrayal(agent):
return {"Shape": "circle", "Filled": "true", "r": 0.5, "Color": "red"}
grid = CanvasGrid(agent_portrayal, 10, 10, 500, 500)
server = ModularServer(SimpleModel, [grid], "Simple Model", {"N":5, "width":10, "height":10})- Enter the following code in a new Notebook cell:
from mesa.examples import boid_flockers
from mesa.visualization.ModularVisualization import ModularServer
server = ModularServer(boid_flockers.BoidFlockers, [], "Boids Model", {"population": 10})
server.port = 8522 # Avoid port conflict
server.launch()from mesa.examples import epstein_civil_violence
from mesa.visualization.ModularVisualization import ModularServer
server = ModularServer(epstein_civil_violence.CivilViolenceModel, [], "Epstein Model")
server.port = 8523
server.launch()-
Notebook Kernel Not Found:
- Ensure Jupyter Lab is started in the Conda environment: activate the environment and run
conda install ipykernel.
- Ensure Jupyter Lab is started in the Conda environment: activate the environment and run
-
Missing Dependencies:
- Install missing packages directly in the Notebook cell:
!pip install package-name.
- Install missing packages directly in the Notebook cell: