forked from bdsul/grape
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_parity.py
270 lines (212 loc) · 10.1 KB
/
example_parity.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 27 15:21:08 2021
@author: allan
"""
import grape
import algorithms
from functions import not_, and_, or_, nand_, nor_
from os import path
import pandas as pd
import numpy as np
from deap import creator, base, tools
import random
problem = 'parity4'
if problem == 'parity3':
X_train = np.zeros([3,8], dtype=bool)
Y_train = np.zeros([8,], dtype=bool)
data = pd.read_table(r"datasets/parity3.csv")
for i in range(3):
for j in range(8):
X_train[i,j] = data['d'+ str(i)].iloc[j]
for i in range(8):
Y_train[i] = data['output'].iloc[i]
GRAMMAR_FILE = 'parity3.bnf'
elif problem == 'parity4':
X_train = np.zeros([4,16], dtype=bool)
Y_train = np.zeros([16,], dtype=bool)
data = pd.read_table(r"datasets/parity4.csv")
for i in range(4):
for j in range(16):
X_train[i,j] = data['d'+ str(i)].iloc[j]
for i in range(16):
Y_train[i] = data['output'].iloc[i]
GRAMMAR_FILE = 'parity4.bnf'
elif problem == 'parity5':
X_train = np.zeros([5,32], dtype=bool)
Y_train = np.zeros([32,], dtype=bool)
data = pd.read_table(r"datasets/parity5.csv")
for i in range(5):
for j in range(32):
X_train[i,j] = data['d'+ str(i)].iloc[j]
for i in range(32):
Y_train[i] = data['output'].iloc[i]
GRAMMAR_FILE = 'parity5.bnf'
BNF_GRAMMAR = grape.Grammar(r"grammars/" + GRAMMAR_FILE)
def mae(y, yhat):
"""
Calculate mean absolute error between inputs.
:param y: The expected input (i.e. from dataset).
:param yhat: The given input (i.e. from phenotype).
:return: The mean absolute error.
"""
compare = np.equal(y,yhat)
return 1 - np.mean(compare)
def fitness_eval(individual, points, penalty_divider=None, penalise_greater_than=None):
x = points[0]
Y = points[1]
if individual.invalid == True:
return np.NaN,
# Evaluate the expression
try:
pred = eval(individual.phenotype)
except (FloatingPointError, ZeroDivisionError, OverflowError,
MemoryError):
# FP err can happen through eg overflow (lots of pow/exp calls)
# ZeroDiv can happen when using unprotected operators
return np.NaN,
assert np.isrealobj(pred)
fitness = mae(Y, pred)
individual.fitness_each_sample = np.equal(Y, pred)
if penalise_greater_than and penalty_divider:
if len(individual.genome) > penalise_greater_than:
fitness += len(individual.genome) / penalty_divider
return fitness,
POPULATION_SIZE = 1000
MAX_GENERATIONS = 50
P_CROSSOVER = 0.8
P_MUTATION = 0.01
ELITE_SIZE = 1#round(0.01*POPULATION_SIZE) #it should be smaller or equal to HALLOFFAME_SIZE
HALLOFFAME_SIZE = 1#round(0.01*POPULATION_SIZE) #it should be at least 1
RANDOM_SEED = 42 #Pay attention that the seed is set up inside the loop of runs, so you are going to have similar runs
MIN_INIT_GENOME_LENGTH = 30 #used only for random initialisation
MAX_INIT_GENOME_LENGTH = 50
random_initilisation = False #put True if you use random initialisation
MAX_INIT_TREE_DEPTH = 8 #equivalent to 6 in GP with this grammar
MIN_INIT_TREE_DEPTH = 3
MAX_TREE_DEPTH = 35 #equivalent to 17 in GP with this grammar
MAX_WRAPS = 0
CODON_SIZE = 255
CODON_CONSUMPTION = 'lazy'
GENOME_REPRESENTATION = 'list'
MAX_GENOME_LENGTH = None
#Set the next two parameters with integer values, if you want to use the penalty approach
PENALTY_DIVIDER = None
PENALISE_GREATER_THAN = None
TOURNAMENT_SIZE = 7
toolbox = base.Toolbox()
# define a single objective, minimising fitness strategy:
creator.create("FitnessMin", base.Fitness, weights=(-1.0,))
creator.create('Individual', grape.Individual, fitness=creator.FitnessMin)
toolbox.register("populationCreator", grape.sensible_initialisation, creator.Individual)
#toolbox.register("populationCreator", grape.random_initialisation, creator.Individual)
#toolbox.register("populationCreator", grape.PI_Grow, creator.Individual)
toolbox.register("evaluate", fitness_eval, penalty_divider=PENALTY_DIVIDER, penalise_greater_than=PENALISE_GREATER_THAN)
#toolbox.register("evaluate", fitness_eval)
# Tournament selection:
toolbox.register("select", tools.selTournament, tournsize=TOURNAMENT_SIZE)
# Single-point crossover:
toolbox.register("mate", grape.crossover_onepoint)
# Flip-int mutation:
toolbox.register("mutate", grape.mutation_int_flip_per_codon)
REPORT_ITEMS = ['gen', 'invalid', 'avg', 'std', 'min', 'max',
'best_ind_length', 'avg_length',
'best_ind_nodes', 'avg_nodes',
'best_ind_depth', 'avg_depth',
'avg_used_codons', 'best_ind_used_codons',
'behavioural_diversity',
'structural_diversity', 'fitness_diversity',
'selection_time', 'generation_time']
N_RUNS = 3
for i in range(N_RUNS):
print()
print()
print("Run:", i+1)
print()
random.seed(RANDOM_SEED) #Comment this line or set a different RANDOM_SEED each run if you want distinct results
# create initial population (generation 0):
if random_initilisation:
population = toolbox.populationCreator(pop_size=POPULATION_SIZE,
bnf_grammar=BNF_GRAMMAR,
min_init_genome_length=MIN_INIT_GENOME_LENGTH,
max_init_genome_length=MAX_INIT_GENOME_LENGTH,
max_init_depth=MAX_TREE_DEPTH,
codon_size=CODON_SIZE,
codon_consumption=CODON_CONSUMPTION,
genome_representation=GENOME_REPRESENTATION
)
else:
population = toolbox.populationCreator(pop_size=POPULATION_SIZE,
bnf_grammar=BNF_GRAMMAR,
min_init_depth=MIN_INIT_TREE_DEPTH,
max_init_depth=MAX_INIT_TREE_DEPTH,
codon_size=CODON_SIZE,
codon_consumption=CODON_CONSUMPTION,
genome_representation=GENOME_REPRESENTATION
)
# define the hall-of-fame object:
hof = tools.HallOfFame(HALLOFFAME_SIZE)
# prepare the statistics object:
stats = tools.Statistics(key=lambda ind: ind.fitness.values)
stats.register("avg", np.nanmean)
stats.register("std", np.nanstd)
stats.register("min", np.nanmin)
stats.register("max", np.nanmax)
# perform the Grammatical Evolution flow:
population, logbook = algorithms.ge_eaSimpleWithElitism(population, toolbox, cxpb=P_CROSSOVER, mutpb=P_MUTATION,
ngen=MAX_GENERATIONS, elite_size=ELITE_SIZE,
bnf_grammar=BNF_GRAMMAR,
codon_size=CODON_SIZE,
max_tree_depth=MAX_TREE_DEPTH,
max_genome_length=MAX_GENOME_LENGTH,
points_train=[X_train, Y_train],
codon_consumption=CODON_CONSUMPTION,
report_items=REPORT_ITEMS,
genome_representation=GENOME_REPRESENTATION,
stats=stats, halloffame=hof, verbose=False)
import textwrap
best = hof.items[0].phenotype
print("Best individual: \n","\n".join(textwrap.wrap(best,80)))
print("\nTraining Fitness: ", hof.items[0].fitness.values[0])
print("Depth: ", hof.items[0].depth)
print("Length of the genome: ", len(hof.items[0].genome))
print(f'Used portion of the genome: {hof.items[0].used_codons/len(hof.items[0].genome):.2f}')
max_fitness_values, mean_fitness_values = logbook.select("max", "avg")
min_fitness_values, std_fitness_values = logbook.select("min", "std")
best_ind_length = logbook.select("best_ind_length")
avg_length = logbook.select("avg_length")
selection_time = logbook.select("selection_time")
generation_time = logbook.select("generation_time")
gen, invalid = logbook.select("gen", "invalid")
avg_used_codons = logbook.select("avg_used_codons")
best_ind_used_codons = logbook.select("best_ind_used_codons")
best_ind_nodes = logbook.select("best_ind_nodes")
avg_nodes = logbook.select("avg_nodes")
best_ind_depth = logbook.select("best_ind_depth")
avg_depth = logbook.select("avg_depth")
behavioural_diversity = logbook.select("behavioural_diversity")
structural_diversity = logbook.select("structural_diversity")
fitness_diversity = logbook.select("fitness_diversity")
import csv
r = RANDOM_SEED
header = REPORT_ITEMS
with open("results/" + str(r) + ".csv", "w", encoding='UTF8', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter='\t')
writer.writerow(header)
for value in range(len(max_fitness_values)):
writer.writerow([gen[value], invalid[value], mean_fitness_values[value],
std_fitness_values[value], min_fitness_values[value],
max_fitness_values[value],
best_ind_length[value],
avg_length[value],
best_ind_nodes[value],
avg_nodes[value],
best_ind_depth[value],
avg_depth[value],
avg_used_codons[value],
best_ind_used_codons[value],
behavioural_diversity[value],
structural_diversity[value],
fitness_diversity[value],
selection_time[value],
generation_time[value]])