I'm using Solara to visualize a MESA simulation but I always get this message:
ValueError: Missing required model parameter: initial_fire_positions
Can someone help me figure out why it is happening? Here's the code:
import mesa
from mesa.visualization import SolaraViz, make_space_component, make_plot_component
from fire_spread import WildfireModel, FireCell
def agent_portrayal(agent):
portrayal = {"w": 1, "h": 1, "shape": "rect"}
if agent.state == "burning":
portrayal["color"] = "tab:red"
elif agent.state == "burnt":
portrayal["color"] = "black"
else: # unburnt
if agent.terrain_type == "forest":
portrayal["color"] = "forestgreen"
elif agent.terrain_type == "grassland":
portrayal["color"] = "yellowgreen"
elif agent.terrain_type == "urban":
portrayal["color"] = "lightgrey"
else:
portrayal["color"] = "green"
return portrayal
model_params = {
"grid width": {
"type": "SliderInt",
"value": 20,
"label": "Grid width:",
"min": 10,
"max": 50,
"step": 1,
},
"grid height": {
"type": "SliderInt",
"value": 20,
"label": "Grid height:",
"min": 10,
"max": 50,
"step": 1,
},
"wind_direction": {
"type": "Select",
"value": (0, 1),
"label": "Wind direction:",
"choices": [
("North", (0, 1)),
("East", (1, 0)),
("South", (0, -1)),
("West", (-1, 0)),
],
},
"initial_fire_positions": {
"type": "Select",
"value": [(10, 10)],
"label": "Initial Fire Position(s):",
"choices": [
[(10, 10)],
[(5, 10)],
[(10, 5)],
[(5, 5)],
],
},
"width": 20,
"height": 20,
}
SpaceGraph = make_space_component(agent_portrayal)
BurningPlot = make_plot_component("BurningCells")
model_instance = WildfireModel(width=20, height=20, initial_fire_positions=[(10, 10)], wind_direction=(0, 1))
page = SolaraViz(
model_instance,
components=[SpaceGraph, BurningPlot],
model_params=model_params,
name="Wildfire Spread Model",
)
page
here's the WildFireModel since it might help:
this part is the WildFireModel code, it is mostly just a model of dynamic wildfire spread through a cellular automaton, incorporating environmental variables like wind and terrain. might help in understanind what when wrong.
from mesa import Agent, Model
from mesa.space import MultiGrid
from mesa.datacollection import DataCollector
import random
class FireCell(Agent):
def __init__(self, model, fuel, moisture, terrain_type):
super().__init__(model)
self.fuel = fuel
self.moisture = moisture
self.terrain_type = terrain_type
self.state = "unburnt" # States: "unburnt", "burning", "burnt"
def step(self):
if self.state == "burning":
# Consume fuel; when fuel runs out, mark cell as burnt.
self.fuel -= 1
if self.fuel <= 0:
self.state = "burnt"
else:
# Attempt to ignite neighboring cells.
neighbors = self.model.grid.get_neighbors(self.pos, moore=True, include_center=False)
for neighbor in neighbors:
if neighbor.state == "unburnt" and self.should_ignite(neighbor):
neighbor.state = "burning"
def should_ignite(self, neighbor):
"""
Calculate the ignition probability based on a base chance and adjustments
for wind, terrain, and moisture.
"""
base_prob = 0.3
# Compute direction vector to neighbor.
dx = neighbor.pos[0] - self.pos[0]
dy = neighbor.pos[1] - self.pos[1]
wind_dx, wind_dy = self.model.wind_direction
# Increase probability if neighbor is in the wind direction.
if (dx, dy) == (wind_dx, wind_dy):
base_prob += 0.2
# Adjust probability based on terrain.
terrain_factor = {"forest": 0.2, "grassland": 0.1, "urban": -0.1}
base_prob += terrain_factor.get(neighbor.terrain_type, 0)
# Adjust for moisture (more moisture reduces chance).
base_prob *= (1 - neighbor.moisture)
# Clamp probability between 0 and 1.
base_prob = max(0.0, min(1.0, base_prob))
return self.random.random() < base_prob
class WildfireModel(Model):
def __init__(self, width, height, initial_fire_positions, wind_direction=(1, 0)):
super().__init__()
self.grid = MultiGrid(width, height, torus=False)
self.wind_direction = wind_direction
self.running = True # For BatchRunner or visualization.
# Create a FireCell agent for each grid cell using nested loops.
for x in range(width):
for y in range(height):
fuel = random.randint(3, 10) # Fuel between 3 and 10 units.
moisture = random.random() # Moisture between 0 and 1.
terrain_type = random.choice(["forest", "grassland", "urban"])
cell = FireCell(model=self, fuel=fuel, moisture=moisture, terrain_type=terrain_type)
self.grid.place_agent(cell, (x, y))
# Ignite the initial fire positions.
for pos in initial_fire_positions:
contents = self.grid.get_cell_list_contents(pos)
if contents:
contents[0].state = "burning"
# DataCollector to track burning cells over time.
self.datacollector = DataCollector(
model_reporters={"BurningCells": self.count_burning_cells}
)
def step(self):
# Use the AgentSet scheduler functionality.
self.agents.shuffle_do("step")
self.datacollector.collect(self)
def count_burning_cells(self):
return sum(1 for agent in self.agents if isinstance(agent, FireCell) and agent.state == "burning")
@property
def space(self):
"""Expose grid as the space for visualization components."""
return self.grid
# Example usage:
if __name__ == "__main__":
# Create a 20x20 grid with a fire starting at the center.
width, height = 50, 50
initial_fire = [(width // 2, height // 2)]
model = WildfireModel(width, height, initial_fire_positions=initial_fire, wind_direction=(0, 1))
# Run the model for 10 steps.
for i in range(100):
model.step()
print()
burning = model.count_burning_cells()
print(f"Step {i+1}: Burning cells = {burning}")
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744243348a4564809.html
评论列表(0条)