forked from martinventer/virtual_creatures
-
Notifications
You must be signed in to change notification settings - Fork 1
/
montepython_branching_2.0.py
112 lines (80 loc) · 2.8 KB
/
montepython_branching_2.0.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
from CreatureTools_n import Creature
from Tools.Classes import Environment
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
from matplotlib.collections import PatchCollection
import matplotlib.pyplot as plt
from descartes import PolygonPatch
def genGen(params, listed=False):
proba1 = np.random.uniform(0, 1)
proba2 = 1 - proba1
rule1 = ''.join([np.random.choice(params.get('choices'))
for _ in range(params.get('rule_length'))])
rule2 = ''.join([np.random.choice(params.get('choices'))
for _ in range(params.get('rule_length'))])
params['rules'] = {'X': {1: rule1, 2: rule2}}
params['angle'] = np.random.randint(0, 90) # random
c = Creature(params)
try:
c = Creature(params)
except:
pass
if listed:
return list(c.__dict__.keys())
else:
return list(c.__dict__.values())
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__":
params = {
'iterations': 100000,
'recurs': 5,
'variables': 'X',
'constants': 'F+-[]_',
'axiom': 'FX',
'length': 1.0,
'rule_length': 5,
'fitness_metric': 'Area',
'shape': 'square', # 'circle' 'square' 'rainbow' 'triangle' 'patches'
'richness': 'common', # 'scarce' 'common' 'abundant'
'scale': 'small', # 'small' 'medium' 'large'
}
params['choices'] = list(params.get(
'variables') + params.get('constants'))
# fig, ax = plt.subplots()
# env = Environment(params)
# params['env'] = env
# p = PolygonPatch(env.patches[0])
# ax.add_patch(p)
# plt.show()
init_creature = genGen(params, listed=True)
population = [init_creature]
# for _ in range(5):
# genGen(params)
with mp.Pool(mp.cpu_count()-2) as pool:
np.random.seed()
results = list(
tqdm.tqdm(pool.imap(genGen, repeat(params, params.get('iterations'))), total=params.get('iterations')))
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/branch_monte_carlo ' + now + '.p')
pickle.dump(population, open(file_name, 'wb'))