Skip to content

Instantly share code, notes, and snippets.

@nielsmh nielsmh/housecargogen_sim.py Secret
Last active Nov 9, 2018

Embed
What would you like to do?
OpenTTD house cargo generation algorithm simulation
from __future__ import print_function
import random
from collections import Counter
import csv
import sys
def gen_bitcount(tick, pop):
r = random.randrange(0x10000)
if (tick & 1) and pop < 128: return ""
if ((r & 1) == 0) == ((r & 512) == 0): return ""
genmax = (pop + 7) // 8
if tick & 1: genmax -= 16
genmask = 2**genmax - 1
return bin(r & genmask).count("1")
def gen_bitcount2(tick, pop):
r = random.randrange(0x10000)
if ((r & 1) == 0) == ((r & 512) == 0): return ""
roundoffset = (7, 15)[tick & 1]
genmax = (pop + roundoffset) // 16
genmask = 2**genmax - 1
return bin(r & genmask).count("1")
def gen_original(tick, pop):
r = random.randrange(0x100)
if r < pop:
return r // 8 + 1
else:
return ""
def make_flatdist(genrange):
def gen_flatdist(tick, pop):
r = random.randrange(0x100)
if r < pop:
return random.randrange(genrange) + 1
else:
return ""
gen_flatdist.__name__ = "{0}({1})".format(gen_flatdist.__name__, genrange)
return gen_flatdist
def display(ticks, alg, pop):
data = [alg(n, pop) for n in range(ticks)]
print("Generated pax: {0}".format(sum(d for d in data if d)))
data = Counter("{0:3}".format(d) for d in data)
keys = sorted(data.keys())
for k in keys:
bar = "=" * (data[k] // 8)
if data[k] % 8 >= 4: bar += "-"
if k == " ": bar = ""
print("{0:3}\t{1:6}\t{2}".format(k, data[k], bar))
def run_display(pops, algs):
for alg in algs:
print(" -----===== " + alg.__name__ + " =====-----")
for p in pops:
print("House population: {0}".format(p))
display(1000, alg, p)
print()
def run_csv(pops, algs):
f = csv.writer(sys.stdout, delimiter="\t", lineterminator="\n")
f.writerow(["alg", "housepop", "prod", "count"])
for alg in algs:
for pop in pops:
data = [alg(n, pop) for n in range(5000)]
data = Counter(str(d) for d in data)
for prod, count in data.items():
f.writerow([alg.__name__, pop, prod, count])
if __name__ == "__main__":
mode = "display"
if len(sys.argv) > 1: mode = sys.argv[1]
pops = (5, 8, 14, 16, 25, 35, 45, 65, 80, 90, 100, 120, 140, 170, 190, 210, 220, 250)
algs = (gen_original, gen_bitcount2, make_flatdist(4), make_flatdist(8), make_flatdist(16))
{
"display": run_display,
"csv": run_csv
}[mode](pops, algs)
-----===== gen_original =====-----
House population: 5
Generated pax: 21
0 979
1 21 ==-
House population: 8
Generated pax: 26
0 974
1 26 ===
House population: 14
Generated pax: 74
0 950
1 26 ===
2 24 ===
House population: 16
Generated pax: 107
0 926
1 41 =====
2 33 ====
House population: 25
Generated pax: 239
0 892
1 30 ===-
2 27 ===
3 49 ======
4 2
House population: 35
Generated pax: 469
0 827
1 36 ====-
2 44 =====-
3 39 ====-
4 42 =====
5 12 =-
House population: 45
Generated pax: 633
0 812
1 33 ====
2 38 ====-
3 27 ===
4 32 ====
5 33 ====
6 25 ===
House population: 65
Generated pax: 1131
0 745
1 37 ====-
2 34 ====
3 29 ===-
4 37 ====-
5 24 ===
6 29 ===-
7 30 ===-
8 28 ===-
9 7 -
House population: 80
Generated pax: 1767
0 673
1 30 ===-
2 31 ===-
3 31 ===-
4 41 =====
5 38 ====-
6 37 ====-
7 33 ====
8 31 ===-
9 23 ==-
10 32 ====
House population: 90
Generated pax: 2214
0 648
1 36 ====-
2 28 ===-
3 26 ===
4 29 ===-
5 30 ===-
6 38 ====-
7 19 ==
8 30 ===-
9 34 ====
10 38 ====-
11 37 ====-
12 7 -
House population: 100
Generated pax: 2599
0 617
1 35 ====
2 30 ===-
3 27 ===
4 25 ===
5 37 ====-
6 30 ===-
7 25 ===
8 34 ====
9 30 ===-
10 25 ===
11 42 =====
12 30 ===-
13 13 =-
House population: 120
Generated pax: 3570
0 544
1 36 ====-
2 31 ===-
3 35 ====
4 26 ===
5 29 ===-
6 29 ===-
7 36 ====-
8 35 ====
9 28 ===-
10 32 ====
11 20 ==-
12 22 ==-
13 35 ====
14 29 ===-
15 33 ====
House population: 140
Generated pax: 5044
0 455
1 33 ====
2 32 ====
3 28 ===-
4 34 ====
5 33 ====
6 34 ====
7 30 ===-
8 24 ===
9 31 ===-
10 32 ====
11 31 ===-
12 20 ==-
13 32 ====
14 39 ====-
15 36 ====-
16 32 ====
17 30 ===-
18 14 =-
House population: 170
Generated pax: 7394
0 344
1 35 ====
2 27 ===
3 20 ==-
4 42 =====
5 36 ====-
6 26 ===
7 24 ===
8 27 ===
9 39 ====-
10 28 ===-
11 31 ===-
12 25 ===
13 25 ===
14 23 ==-
15 41 =====
16 31 ===-
17 39 ====-
18 40 =====
19 38 ====-
20 23 ==-
21 30 ===-
22 6 -
House population: 190
Generated pax: 9154
0 244
1 35 ====
2 37 ====-
3 30 ===-
4 38 ====-
5 33 ====
6 34 ====
7 27 ===
8 32 ====
9 39 ====-
10 40 =====
11 35 ====
12 29 ===-
13 19 ==
14 26 ===
15 30 ===-
16 24 ===
17 35 ====
18 38 ====-
19 25 ===
20 24 ===
21 35 ====
22 30 ===-
23 33 ====
24 28 ===-
House population: 210
Generated pax: 10998
0 175
1 37 ====-
2 36 ====-
3 24 ===
4 25 ===
5 38 ====-
6 30 ===-
7 36 ====-
8 34 ====
9 44 =====-
10 33 ====
11 31 ===-
12 25 ===
13 31 ===-
14 27 ===
15 33 ====
16 31 ===-
17 35 ====
18 33 ====
19 23 ==-
20 23 ==-
21 33 ====
22 37 ====-
23 31 ===-
24 36 ====-
25 26 ===
26 27 ===
27 6 -
House population: 220
Generated pax: 11982
0 131
1 38 ====-
2 26 ===
3 34 ====
4 37 ====-
5 34 ====
6 29 ===-
7 36 ====-
8 33 ====
9 44 =====-
10 37 ====-
11 34 ====
12 37 ====-
13 22 ==-
14 28 ===-
15 18 ==
16 39 ====-
17 38 ====-
18 27 ===
19 27 ===
20 33 ====
21 24 ===
22 18 ==
23 32 ====
24 39 ====-
25 36 ====-
26 28 ===-
27 27 ===
28 14 =-
House population: 250
Generated pax: 16194
0 24
1 33 ====
2 27 ===
3 32 ====
4 23 ==-
5 30 ===-
6 37 ====-
7 31 ===-
8 26 ===
9 25 ===
10 36 ====-
11 31 ===-
12 27 ===
13 36 ====-
14 37 ====-
15 30 ===-
16 26 ===
17 21 ==-
18 33 ====
19 35 ====
20 27 ===
21 33 ====
22 26 ===
23 34 ====
24 37 ====-
25 30 ===-
26 25 ===
27 32 ====
28 18 ==
29 39 ====-
30 47 =====-
31 44 =====-
32 8 =
-----===== gen_bitcount2 =====-----
House population: 5
Generated pax: 130
0 870
1 130 ================
House population: 8
Generated pax: 133
0 867
1 133 ================-
House population: 14
Generated pax: 247
0 753
1 247 ==============================-
House population: 16
Generated pax: 240
0 760
1 240 ==============================
House population: 25
Generated pax: 504
0 622
1 252 ===============================-
2 126 ===============-
House population: 35
Generated pax: 679
0 571
1 217 ===========================
2 174 =====================-
3 38 ====-
House population: 45
Generated pax: 759
0 568
1 178 ======================
2 181 ======================-
3 73 =========
House population: 65
Generated pax: 1036
0 564
1 91 ===========
2 161 ====================
3 121 ===============
4 55 ======-
5 8 =
House population: 80
Generated pax: 1271
0 515
1 68 ========-
2 161 ====================
3 162 ====================
4 75 =========
5 19 ==
House population: 90
Generated pax: 1387
0 528
1 64 ========
2 122 ===============
3 131 ================
4 99 ============
5 46 =====-
6 10 =
House population: 100
Generated pax: 1663
0 477
1 46 =====-
2 123 ===============
3 152 ===================
4 121 ===============
5 59 =======
6 18 ==
7 4 -
House population: 120
Generated pax: 1771
0 528
1 32 ====
2 50 ======
3 123 ===============
4 128 ================
5 86 ==========-
6 43 =====
7 10 =
House population: 140
Generated pax: 2277
0 504
1 11 =
2 28 ===-
3 71 ========-
4 136 =================
5 115 ==============
6 77 =========-
7 49 ======
8 8 =
9 1
House population: 170
Generated pax: 2678
0 519
2 5 -
3 39 ====-
4 76 =========-
5 107 =============
6 116 ==============-
7 99 ============
8 31 ===-
9 5 -
10 3
House population: 190
Generated pax: 2850
0 521
1 1
2 3
3 22 ==-
4 62 =======-
5 104 =============
6 109 =============-
7 104 =============
8 46 =====-
9 22 ==-
10 5 -
11 1
House population: 210
Generated pax: 3283
0 516
2 3
3 8 =
4 25 ===
5 62 =======-
6 110 =============-
7 118 ==============-
8 86 ==========-
9 54 ======-
10 15 =-
11 3
House population: 220
Generated pax: 3325
0 521
2 1
3 12 =-
4 25 ===
5 61 =======-
6 94 ===========-
7 103 ============-
8 92 ===========-
9 62 =======-
10 19 ==
11 7 -
12 3
House population: 250
Generated pax: 3919
0 509
3 3
4 11 =
5 36 ====-
6 53 ======-
7 96 ============
8 98 ============
9 90 ===========
10 60 =======-
11 28 ===-
12 14 =-
13 2
-----===== gen_flatdist(4) =====-----
House population: 5
Generated pax: 55
0 980
1 2
2 6 -
3 7 -
4 5 -
House population: 8
Generated pax: 73
0 971
1 8 =
2 6 -
3 7 -
4 8 =
House population: 14
Generated pax: 133
0 947
1 13 =-
2 14 =-
3 12 =-
4 14 =-
House population: 16
Generated pax: 156
0 939
1 13 =-
2 19 ==
3 11 =
4 18 ==
House population: 25
Generated pax: 313
0 878
1 25 ===
2 36 ====-
3 28 ===-
4 33 ====
House population: 35
Generated pax: 337
0 867
1 35 ====
2 32 ====
3 26 ===
4 40 =====
House population: 45
Generated pax: 405
0 835
1 39 ====-
2 49 ======
3 40 =====
4 37 ====-
House population: 65
Generated pax: 639
0 744
1 56 =======
2 73 =========
3 71 ========-
4 56 =======
House population: 80
Generated pax: 805
0 687
1 66 ========
2 85 ==========-
3 79 =========-
4 83 ==========
House population: 90
Generated pax: 858
0 655
1 89 ===========
2 80 ==========
3 95 ===========-
4 81 ==========
House population: 100
Generated pax: 932
0 616
1 104 =============
2 101 ============-
3 90 ===========
4 89 ===========
House population: 120
Generated pax: 1137
0 553
1 112 ==============
2 98 ============
3 119 ==============-
4 118 ==============-
House population: 140
Generated pax: 1353
0 451
1 144 ==================
2 141 =================-
3 129 ================
4 135 ================-
House population: 170
Generated pax: 1564
0 353
1 174 =====================-
2 166 ====================-
3 170 =====================
4 137 =================
House population: 190
Generated pax: 1923
0 242
1 167 ====================-
2 205 =========================-
3 198 ========================-
4 188 =======================-
House population: 210
Generated pax: 2034
0 170
1 227 ============================
2 192 ========================
3 221 ===========================-
4 190 =======================-
House population: 220
Generated pax: 2110
0 144
1 213 ==========================-
2 237 =============================-
3 201 =========================
4 205 =========================-
House population: 250
Generated pax: 2421
0 24
1 256 ================================
2 236 =============================-
3 243 ==============================
4 241 ==============================
-----===== gen_flatdist(8) =====-----
House population: 5
Generated pax: 59
0 983
1 2
2 4 -
3 2
4 4 -
5 3
6 2
House population: 8
Generated pax: 146
0 964
1 6 -
2 4 -
3 7 -
4 5 -
5 4 -
6 4 -
7 1
8 5 -
House population: 14
Generated pax: 256
0 950
1 4 -
2 6 -
3 2
4 8 =
5 2
6 13 =-
7 6 -
8 9 =
House population: 16
Generated pax: 232
0 947
1 7 -
2 8 =
3 8 =
4 1
5 10 =
6 8 =
7 5 -
8 6 -
House population: 25
Generated pax: 434
0 906
1 9 =
2 9 =
3 18 ==
4 12 =-
5 10 =
6 13 =-
7 7 -
8 16 ==
House population: 35
Generated pax: 696
0 847
1 23 ==-
2 19 ==
3 18 ==
4 15 =-
5 13 =-
6 18 ==
7 28 ===-
8 19 ==
House population: 45
Generated pax: 904
0 804
1 29 ===-
2 17 ==
3 19 ==
4 29 ===-
5 28 ===-
6 21 ==-
7 22 ==-
8 31 ===-
House population: 65
Generated pax: 1195
0 740
1 29 ===-
2 35 ====
3 33 ====
4 31 ===-
5 25 ===
6 33 ====
7 42 =====
8 32 ====
House population: 80
Generated pax: 1376
0 684
1 37 ====-
2 43 =====
3 45 =====-
4 48 ======
5 42 =====
6 30 ===-
7 32 ====
8 39 ====-
House population: 90
Generated pax: 1704
0 637
1 51 ======
2 26 ===
3 52 ======-
4 39 ====-
5 43 =====
6 44 =====-
7 54 ======-
8 54 ======-
House population: 100
Generated pax: 1693
0 618
1 43 =====
2 44 =====-
3 57 =======
4 53 ======-
5 53 ======-
6 49 ======
7 44 =====-
8 39 ====-
House population: 120
Generated pax: 2114
0 530
1 48 ======
2 70 ========-
3 67 ========
4 58 =======
5 53 ======-
6 54 ======-
7 56 =======
8 64 ========
House population: 140
Generated pax: 2681
0 420
1 52 ======-
2 54 ======-
3 90 ===========
4 88 ===========
5 81 ==========
6 76 =========-
7 74 =========
8 65 ========
House population: 170
Generated pax: 3039
0 337
1 88 ===========
2 78 =========-
3 73 =========
4 73 =========
5 81 ==========
6 99 ============
7 83 ==========
8 88 ===========
House population: 190
Generated pax: 3462
0 250
1 79 =========-
2 94 ===========-
3 103 ============-
4 83 ==========
5 88 ===========
6 102 ============-
7 106 =============
8 95 ===========-
House population: 210
Generated pax: 3719
0 170
1 117 ==============-
2 102 ============-
3 103 ============-
4 100 ============-
5 93 ===========-
6 90 ===========
7 116 ==============-
8 109 =============-
House population: 220
Generated pax: 3923
0 149
1 82 ==========
2 102 ============-
3 118 ==============-
4 113 ==============
5 111 =============-
6 107 =============
7 110 =============-
8 108 =============-
House population: 250
Generated pax: 4392
0 30
1 127 ===============-
2 123 ===============
3 102 ============-
4 98 ============
5 150 ==================-
6 142 =================-
7 105 =============
8 123 ===============
-----===== gen_flatdist(16) =====-----
House population: 5
Generated pax: 126
0 978
1 4 -
2 1
3 4 -
4 3
5 2
7 1
8 1
10 2
12 3
15 1
House population: 8
Generated pax: 296
0 962
1 3
2 3
3 2
4 2
5 5 -
6 4 -
7 1
8 2
9 2
10 3
11 1
12 2
13 1
14 3
15 1
16 3
House population: 14
Generated pax: 345
0 956
1 5 -
2 3
3 2
4 5 -
5 4 -
6 2
7 3
8 1
9 3
10 1
11 2
12 2
14 3
15 6 -
16 2
House population: 16
Generated pax: 443
0 950
1 5 -
3 3
4 2
5 3
6 2
7 4 -
8 4 -
9 3
10 4 -
11 5 -
12 3
13 2
14 2
15 6 -
16 2
House population: 25
Generated pax: 780
0 892
1 6 -
2 9 =
3 10 =
4 14 =-
5 8 =
6 11 =
7 4 -
8 9 =
9 7 -
10 5 -
11 1
12 5 -
13 3
14 5 -
15 5 -
16 6 -
House population: 35
Generated pax: 1337
0 846
1 11 =
2 8 =
3 7 -
4 14 =-
5 11 =
6 7 -
7 2
8 7 -
9 11 =
10 15 =-
11 6 -
12 15 =-
13 15 =-
14 7 -
15 10 =
16 8 =
House population: 45
Generated pax: 1517
0 815
1 12 =-
2 17 ==
3 10 =
4 13 =-
5 9 =
6 8 =
7 12 =-
8 11 =
9 19 ==
10 12 =-
11 12 =-
12 9 =
13 11 =
14 11 =
15 8 =
16 11 =
House population: 65
Generated pax: 2040
0 775
1 11 =
2 14 =-
3 11 =
4 12 =-
5 8 =
6 13 =-
7 18 ==
8 16 ==
9 18 ==
10 11 =
11 9 =
12 17 ==
13 16 ==
14 18 ==
15 15 =-
16 18 ==
House population: 80
Generated pax: 2647
0 676
1 21 ==-
2 17 ==
3 30 ===-
4 24 ===
5 21 ==-
6 15 =-
7 22 ==-
8 25 ===
9 16 ==
10 21 ==-
11 23 ==-
12 18 ==
13 16 ==
14 19 ==
15 16 ==
16 20 ==-
House population: 90
Generated pax: 2893
0 669
1 27 ===
2 15 =-
3 17 ==
4 19 ==
5 15 =-
6 17 ==
7 17 ==
8 26 ===
9 27 ===
10 18 ==
11 29 ===-
12 19 ==
13 17 ==
14 27 ===
15 20 ==-
16 21 ==-
House population: 100
Generated pax: 3305
0 605
1 26 ===
2 20 ==-
3 28 ===-
4 29 ===-
5 29 ===-
6 25 ===
7 25 ===
8 27 ===
9 20 ==-
10 26 ===
11 18 ==
12 16 ==
13 31 ===-
14 29 ===-
15 22 ==-
16 24 ===
House population: 120
Generated pax: 4282
0 506
1 34 ====
2 30 ===-
3 27 ===
4 27 ===
5 26 ===
6 38 ====-
7 31 ===-
8 25 ===
9 37 ====-
10 29 ===-
11 23 ==-
12 27 ===
13 36 ====-
14 38 ====-
15 32 ====
16 34 ====
House population: 140
Generated pax: 4737
0 447
1 31 ===-
2 36 ====-
3 35 ====
4 33 ====
5 45 =====-
6 29 ===-
7 32 ====
8 29 ===-
9 36 ====-
10 34 ====
11 41 =====
12 28 ===-
13 32 ====
14 42 =====
15 33 ====
16 37 ====-
House population: 170
Generated pax: 5832
0 320
1 41 =====
2 36 ====-
3 43 =====
4 45 =====-
5 44 =====-
6 52 ======-
7 41 =====
8 34 ====
9 41 =====
10 45 =====-
11 40 =====
12 39 ====-
13 45 =====-
14 44 =====-
15 49 ======
16 41 =====
House population: 190
Generated pax: 6462
0 240
1 42 =====
2 56 =======
3 47 =====-
4 42 =====
5 52 ======-
6 41 =====
7 41 =====
8 50 ======
9 49 ======
10 54 ======-
11 52 ======-
12 56 =======
13 49 ======
14 40 =====
15 40 =====
16 49 ======
House population: 210
Generated pax: 6727
0 192
1 73 =========
2 39 ====-
3 42 =====
4 49 ======
5 65 ========
6 64 ========
7 43 =====
8 42 =====
9 53 ======-
10 36 ====-
11 50 ======
12 49 ======
13 50 ======
14 59 =======
15 47 =====-
16 47 =====-
House population: 220
Generated pax: 7346
0 142
1 43 =====
2 51 ======
3 53 ======-
4 59 =======
5 57 =======
6 48 ======
7 50 ======
8 65 ========
9 56 =======
10 60 =======-
11 49 ======
12 57 =======
13 48 ======
14 66 ========
15 48 ======
16 48 ======
House population: 250
Generated pax: 8394
0 15
1 62 =======-
2 65 ========
3 55 ======-
4 70 ========-
5 67 ========
6 67 ========
7 49 ======
8 52 ======-
9 66 ========
10 57 =======
11 62 =======-
12 53 ======-
13 62 =======-
14 62 =======-
15 71 ========-
16 65 ========
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.