forked from projectmesa/mesa
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cache grid pos #4
Comments
Main editCaching methods class TestModel(mesa.Model):
"""A model with some number of agents."""
def __init__(self, N, width, height):
super().__init__()
# self.num_agents = N
# self.grid = mesa.space.MultiGrid(1,1, True)
# self.schedule = mesa.time.RandomActivation(self)
# Create agents
# for i in range(self.num_agents):
# a = MoneyAgent(i, self)
# self.schedule.add(a)
# Add the agent to a random grid cell
# x = self.random.randrange(self.grid.width)
# y = self.random.randrange(self.grid.height)
# self.grid.place_agent(a, (x, y))
# self.datacollector = mesa.DataCollector()
def step(self):
# self.datacollector.collect(self)
self._steps += 1 The TestModel acts as a dummy model to be fed into SolaraViz. This allows the visualizer to step while the data displayed is taken from the cached data. Limitations:
|
Main editCaching methods @solara.component
def PlotMatplotlib(model, measure, dependencies: list[any] | None = None):
fig = Figure()
ax = fig.subplots()
# TODO: Check
model_files = glob.glob(f"output_dir/model_data_*.parquet")
model_dfs = []
for model_file in model_files:
table = pq.read_table(model_file)
df = table.to_pandas()
model_dfs.append(df)
df = pd.concat(model_dfs, ignore_index=True)[:model._steps+1]
if isinstance(measure, str):
ax.plot(df.loc[:, measure])
ax.set_ylabel(measure)
elif isinstance(measure, dict):
for m, color in measure.items():
ax.plot(df.loc[:, m], label=m, color=color)
fig.legend()
elif isinstance(measure, list | tuple):
for m in measure:
ax.plot(df.loc[:, m], label=m)
fig.legend()
# Set integer x axis
ax.xaxis.set_major_locator(MaxNLocator(integer=True))
solara.FigureMatplotlib(fig, dependencies=dependencies) The PlotMatplotlib will now plot the matplotlib graph from cached data. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What's the problem this feature will solve?
There should be a method to cache the positions of agents at each step of the simulation so that we can display the agents on the grid visualization.
Describe the solution you'd like
The text was updated successfully, but these errors were encountered: