| 1234567891011121314151617181920212223242526272829303132333435 |
- import os
- os.environ["PYPDEVS_MINIMAL"] = "1"
- from pypdevs.minimal import Simulator
- from Schelling import Schelling
- def get_happiness_ratio(space_model):
- happy_count = 0
- total_agents = len(space_model.state.grid)
- for position, (aid, group) in space_model.state.grid.items():
- if space_model.isHappy(position, group):
- happy_count += 1
- return happy_count / total_agents
- def verify_schelling_pypdevs():
- target_steps = [1, 2, 5, 10, 20]
- print("PyPDEVS Schelling Happiness Ratio:")
- for target in target_steps:
- model = Schelling(dim=40, num_agents=1000, radius=1, happy_threshold=3, seed=42)
- sim = Simulator(model)
- sim.setTerminationTime(target + 1e-3)
- sim.simulate()
- ratio = get_happiness_ratio(model.space)
- print(f"After {target} steps: {round(ratio, 4)}")
- if __name__ == '__main__':
- verify_schelling_pypdevs()
|