forked from ZJouba/virtual_creatures
-
Notifications
You must be signed in to change notification settings - Fork 0
/
montepython_worm.py
128 lines (106 loc) · 2.75 KB
/
montepython_worm.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from CreatureTools_n import B_Creature
import numpy as np
import multiprocessing as mp
import os
import pandas as pd
from datetime import datetime
import sys
import time
import pickle
from itertools import repeat
import tqdm
def genGen(params):
choices = [
'F',
'+',
'-',
'X',
]
proba1 = np.random.uniform(0, 1)
proba2 = 1 - proba1
rule1 = ''.join([np.random.choice(choices)
for _ in range(5)])
rule2 = ''.join([np.random.choice(choices)
for _ in range(5)])
params['rules'] = {
'X': {
'options': [
rule1,
rule2,
],
'probabilities': [proba1, proba2]
}
}
params['angle'] = np.random.randint(0, 90) # random
c = B_Creature(params)
a = (
c.l_string,
c.coords,
c.area,
c.bounds,
c.F,
c.perF,
c.perP,
c.perM,
c.maxF,
c.maxP,
c.maxM,
c.avgF,
c.avgP,
c.avgM,
c.angle,
c.rules,
)
return list(a)
def progress(count, total, status=''):
bar_len = 60
filled_len = int(round(bar_len * count / float(total)))
percents = round(100.0 * count / float(total), 1)
bar = '=' * filled_len + '-' * (bar_len - filled_len)
sys.stdout.write('[%s] %s%s ...%s\r' % (bar, percents, '%', status))
sys.stdout.flush()
if __name__ == "__main__":
iter = 100000
population = []
population = [[
'L-string',
'Coordinates',
'Area',
'Bounding Coordinates',
'No. of F',
'% of F',
'% of +',
'% of -',
'Longest F sequence',
'Longest + sequence',
'Longest - sequence',
'Average chars between Fs',
'Average chars between +s',
'Average chars between -s',
'Angle',
'Rules',
]]
params = {
'num_char': 5,
'variables': 'X',
'constants': 'F+-',
'axiom': 'FX',
'point': np.array([0, 0]),
'vector': np.array([0, 1]),
'length': 1.0,
}
with mp.Pool() as pool:
np.random.seed()
# results = list(pool.imap(genGen, repeat(params, iter)))
results = list(
tqdm.tqdm(pool.imap(genGen, repeat(params, iter)), total=iter))
population = population + results
pool.join()
sys.stdout.write('Done! Writing to CSV')
sys.stdout.flush()
population = pd.DataFrame(population[1:], columns=population[0])
curr_dir = os.path.dirname(__file__)
now = datetime.utcnow().strftime('%b %d, %Y @ %H.%M')
file_name = os.path.join(
curr_dir, 'CSVs/monte_carlo ' + now + '.p')
pickle.dump(population, open(file_name, 'wb'))