CAMS-Replicator-2D-Simulation/main.py

52 lines
1.3 KiB
Python
Raw Normal View History

2023-09-30 14:53:32 +00:00
from sim import Environment, CAMSReverseAndSidestepAgent, Direction
2023-10-01 10:24:44 +00:00
import datetime
2023-09-30 14:53:32 +00:00
import matplotlib.pyplot as plt
import numpy as np
2023-10-01 10:24:44 +00:00
import os
from pathlib import Path
2023-09-30 15:26:39 +00:00
from matplotlib.animation import FuncAnimation
2023-09-30 14:53:32 +00:00
2023-10-01 10:24:44 +00:00
ANIMATION_OUTPUT_DIRECTORY = "./output"
2023-09-30 14:53:32 +00:00
STEPS = 1000
np.random.seed(123456)
2023-09-30 15:26:39 +00:00
fig, ax = plt.subplots()
2023-09-30 23:45:10 +00:00
env = Environment((100, 100))
2023-09-30 15:26:39 +00:00
2023-10-01 10:24:44 +00:00
start_time = datetime.datetime.now()
start_time_timestamp = start_time.strftime("%Y-%m-%dT%H%M%S%z")
saved_animation_filename = "CAMS-replicator-simulation-{}.mp4".format(start_time_timestamp)
output_path = os.path.join(ANIMATION_OUTPUT_DIRECTORY, saved_animation_filename)
if not os.path.exists(ANIMATION_OUTPUT_DIRECTORY):
os.makedirs(ANIMATION_OUTPUT_DIRECTORY)
2023-09-30 14:53:32 +00:00
first_agent = CAMSReverseAndSidestepAgent(
environment=env,
2023-09-30 23:45:10 +00:00
position=(50,50),
initial_direction=Direction.NORTH,
required_resources=500
2023-09-30 14:53:32 +00:00
)
im = ax.imshow(env.render(), aspect="equal", origin="lower")
2023-09-30 15:26:39 +00:00
def update(frame_number):
2023-09-30 17:39:26 +00:00
print(frame_number)
2023-09-30 14:53:32 +00:00
env.step()
im.set_data(env.render())
2023-09-30 15:26:39 +00:00
return im,
ani = FuncAnimation(
fig=fig,
func=update,
frames=STEPS,
2023-10-01 10:24:44 +00:00
blit=True,
interval=1000/60,
repeat=False
)
#plt.show()
2023-09-30 15:26:39 +00:00
2023-10-01 10:24:44 +00:00
print("Saving animation, this might take some time...")
ani.save(output_path, progress_callback=(lambda i, n: print(f'Saving frame {i}/{n}')))
print("Animation saved.")