File size: 2,946 Bytes
870b85a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Pick a few screened plans that cover the kinds of flats the 24-plan library never had, and write one single-plan library for each, so the
real chain (build-shell, prepare, execute) can be run on exactly that plan.  Plans are drawn with a fixed random seed inside each kind.
  pick_trials.py --library screened.json --out-dir trials [--seed 20261110]"""
import argparse
import json
import random
from pathlib import Path

KINDS = {
    'two_bedrooms_balcony': lambda p, n: n['bedrooms'] == 2 and n['balcony'] and not p['rplan_conversion']['contact_repairs'] and not n['hallway'],
    'two_bedrooms_no_balcony': lambda p, n: n['bedrooms'] == 2 and not n['balcony'] and not n['hallway'],
    'three_bedrooms': lambda p, n: n['bedrooms'] == 3 and n['balcony'] and not n['hallway'],
    'four_bedrooms': lambda p, n: n['bedrooms'] >= 4 and not n['hallway'],
    'study_as_bedroom': lambda p, n: 'study_used_as_bedroom' in p['rplan_conversion']['notes'],
    'contact_repaired': lambda p, n: bool(p['rplan_conversion']['contact_repairs']) and n['bedrooms'] == 2 and not n['hallway'],
    'hallway_two_bedrooms': lambda p, n: n['hallway'] and n['bedrooms'] == 2,
    'hallway_three_bedrooms': lambda p, n: n['hallway'] and n['bedrooms'] == 3,
    'indirect_access': lambda p, n: p['rplan_conversion'].get('access') == 'indirect' and not n['hallway'],
    'balcony_dropped': lambda p, n: 'balcony_dropped_not_reachable' in p['rplan_conversion']['notes'],
    'wall_moved': lambda p, n: bool(p['rplan_screening']['wall_moves']),
}


def traits(plan):
    ids = {r['room_id'] for r in plan['rooms']}
    return dict(bedrooms=sum(1 for r in plan['rooms'] if 'bedroom' in r['room_type']), balcony='balcony' in ids,
                hallway=any(i.startswith('hallway') for i in ids))


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument('--library', required=True, type=Path)
    ap.add_argument('--out-dir', required=True, type=Path)
    ap.add_argument('--seed', type=int, default=20261110)
    args = ap.parse_args()
    library = json.loads(args.library.read_text())
    rng = random.Random(args.seed)
    args.out_dir.mkdir(parents=True, exist_ok=True)
    chosen = {}
    for kind, test in KINDS.items():
        matching = [p for p in library['plans'] if test(p, traits(p))]
        if not matching:
            print('%-26s none' % kind)
            continue
        plan = rng.choice(matching)
        chosen[kind] = plan['plan_id']
        one = {k: v for k, v in library.items() if k not in ('plans', 'plan_count', 'rejected_by_reason')}
        one.update(plan_count=1, plans=[plan])
        (args.out_dir / (kind + '.json')).write_text(json.dumps(one, ensure_ascii=False))
        print('%-26s %4d plans, picked %s (%d rooms)' % (kind, len(matching), plan['plan_id'], len(plan['rooms'])))
    (args.out_dir / 'chosen.json').write_text(json.dumps(chosen, indent=2))


if __name__ == '__main__':
    main()