-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathexample_symreg.py
More file actions
113 lines (89 loc) · 4.02 KB
/
example_symreg.py
File metadata and controls
113 lines (89 loc) · 4.02 KB
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
from tensorgp.engine import *
# Fitness function to calculate RMSE from target (Pagie Polynomial)
def calc_fit(**kwargs):
# read parameters
population = kwargs.get('population')
generation = kwargs.get('generation')
tensors = kwargs.get('tensors')
f_path = kwargs.get('f_path')
_stf = kwargs.get('stf')
target = kwargs.get('target')
fn = f_path + "gen_" + str(generation).zfill(5)
fitness = []
times = []
best_ind = 0
# set objective function according to min/max
fit = 0
condition = lambda: (fit < max_fit) # minimizing
max_fit = float('inf')
for i in range(len(tensors)):
start_ind = time.time()
fit = tensor_rmse(tensors[i], target).numpy()
if condition():
max_fit = fit
best_ind = i
times.append((time.time() - start_ind) * 1000.0)
fitness.append(fit)
population[i]['fitness'] = fit
#if generation == gens:
# save_image(tensors[best_ind], best_ind, fn, 2)
return population, best_ind
# Different types of function sets
extended_fset = {'max', 'min', 'abs', 'add', 'and', 'or', 'mult', 'sub', 'xor', 'neg', 'cos', 'sin', 'tan', 'sqrt', 'div', 'exp', 'log', 'warp'}
simple_set = {'add', 'sub', 'mult', 'div', 'sin', 'tan', 'cos'}
normal_set = {'add', 'mult', 'sub', 'div', 'cos', 'sin', 'tan', 'abs', 'sign', 'pow'}
if __name__ == "__main__":
# GP params
#dev = 'gpu' # pytorch is 'cpu' and 'cuda'
dev = '/gpu:0' # device to run, write '/cpu' to tun on cpu
gens = 10 # 50
pop_size = 50 # 50
tour_size = 3
mut_rate = 0.1
cross_rate = 0.9
max_tree_dep = 12
max_init_depth = 10
min_init_depth = 5
elite_size = 1 # 0 to turn off
runs = 1 # Number of average runs
# problems
pagie = "add(div(scalar(1.0), add(scalar(1.0), div(scalar(1.0), mult(mult(x, x), mult(x, x))))), div(scalar(1.0), add(scalar(1.0), div(scalar(1.0), mult(mult(y, y), mult(y, y))))))"
keijzer11 = "add(mult(x, y), sin(mult(sub(x, scalar(1.0), sub(y, scalar(1.0)))))"
korns3 = "add(scalar(-5.41), mult(scalar(4.9), div(sub(v, add(x, div(y, w))), mult(scalar(3.0, w)))))"
problems = [pagie] # Add to run more problems
# Domains dimensions
test_cases = [[1024, 1024]]
for p in problems:
for res in test_cases:
for r in range(runs):
#seeds = random.randint(0, 0x7fffffff)
seeds = 39485793482 # reproducibility
# create engine
engine = Engine(fitness_func=calc_fit,
population_size=pop_size,
tournament_size=tour_size,
mutation_rate=mut_rate,
crossover_rate=cross_rate,
max_tree_depth=max_tree_dep,
target_dims=res,
target=pagie,
elitism=elite_size,
method='ramped half-and-half',
max_init_depth=max_init_depth,
exp_prefix="experience_name",
objective='minimizing',
device=dev,
stop_criteria='generation',
stop_value=gens,
effective_dims=2,
do_final_transform=True,
domain=[-5, 5],
codomain=[-5, 5],
operators=normal_set,
seed=seeds,
save_to_file=10,
save_graphics=False,
show_graphics=False,
read_init_pop_from_file=None)
# run evolutionary process
engine.run()