1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
use geo::{Area, BooleanOps, Contains};
use rand::Rng;
use rand_xorshift::XorShiftRng;
use abstutil::prettyprint_usize;
use map_model::{BuildingID, Map};
use crate::{CensusArea, CensusPerson, Config};
pub fn assign_people_to_houses(
areas: Vec<CensusArea>,
map: &Map,
rng: &mut XorShiftRng,
_config: &Config,
) -> Vec<CensusPerson> {
let mut people = Vec::new();
for area in areas {
for (home, n) in distribute_population_to_homes(area.polygon, area.population, map, rng) {
for _ in 0..n {
people.push(CensusPerson {
home,
age: rng.gen_range(5..95),
employed: rng.gen_bool(0.7),
owns_car: rng.gen_bool(0.5),
});
}
}
}
people
}
pub fn distribute_population_to_homes(
polygon: geo::Polygon,
population: usize,
map: &Map,
rng: &mut XorShiftRng,
) -> Vec<(BuildingID, usize)> {
let map_boundary = geo::Polygon::from(map.get_boundary_polygon().clone());
let bldgs: Vec<map_model::BuildingID> = map
.all_buildings()
.iter()
.filter(|b| {
polygon.contains(&geo::Point::from(b.label_center)) && b.bldg_type.has_residents()
})
.map(|b| b.id)
.collect();
let pct_overlap = polygon.intersection(&map_boundary).unsigned_area() / polygon.unsigned_area();
let num_residents = (pct_overlap * (population as f64)) as usize;
debug!(
"Distributing {} residents to {} buildings. {}% of this area overlapped with the map, \
scaled residents accordingly.",
prettyprint_usize(num_residents),
prettyprint_usize(bldgs.len()),
(pct_overlap * 100.0) as usize
);
let mut count_per_home = Vec::new();
let mut rand_nums: Vec<f64> = (0..bldgs.len()).map(|_| rng.gen_range(0.0..1.0)).collect();
let sum: f64 = rand_nums.iter().sum();
for b in bldgs {
let n = (rand_nums.pop().unwrap() / sum * (num_residents as f64)) as usize;
count_per_home.push((b, n));
}
count_per_home
}