|
|
@@ -13,6 +13,40 @@ function run_model(rng, numagents, griddims, min_to_be_happy, radius)
|
|
|
step!(model, 20)
|
|
|
end
|
|
|
|
|
|
+function get_happiness_ratio(model)
|
|
|
+ happy_count = 0
|
|
|
+ for agent in allagents(model)
|
|
|
+ count_similar = 0
|
|
|
+ for neighbor in nearby_agents(agent, model, model.radius)
|
|
|
+ if agent.group == neighbor.group
|
|
|
+ count_similar += 1
|
|
|
+ end
|
|
|
+ end
|
|
|
+ if count_similar >= model.min_to_be_happy
|
|
|
+ happy_count += 1
|
|
|
+ end
|
|
|
+ end
|
|
|
+ return happy_count / length(allagents(model))
|
|
|
+end
|
|
|
+
|
|
|
+function verify_schelling()
|
|
|
+ rng = rng_model()
|
|
|
+ model = schelling_model(rng, 1000, (40, 40), 3, 1)
|
|
|
+
|
|
|
+ target_steps = [1, 2, 5, 10, 20]
|
|
|
+ current_step = 0
|
|
|
+
|
|
|
+ println("Agents.jl Schelling Happiness Ratio:")
|
|
|
+ for target in target_steps
|
|
|
+ step!(model, target - current_step)
|
|
|
+ current_step = target
|
|
|
+
|
|
|
+ ratio = get_happiness_ratio(model)
|
|
|
+ println("After $target steps: ", round(ratio, sigdigits=4))
|
|
|
+ end
|
|
|
+end
|
|
|
+# verify_schelling()
|
|
|
+
|
|
|
n_run = 100
|
|
|
|
|
|
a = @benchmark run_model(rng, 1000, (40, 40), 3, 1) setup=(rng = rng_model()) evals=1 samples=n_run seconds=1e6
|