-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtreesize_h.py
executable file
·255 lines (230 loc) · 8.91 KB
/
treesize_h.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
import numpy as np
import random
import funcEval
from tree_subt import add_subt_cf
from minpack_conf import curve_fit_2
from tree2func import tree2f
from eval_str import eval_
from g_address import get_address
from speciation import ind_specie, specie_gpo
def eval_prob(population):
n_nodes = []
for ind in population:
n_nodes.append(len(ind))
nn_nodes = np.asarray(n_nodes)
av_size = np.mean(nn_nodes)
c = 1.5
for ind in population:
ind.LS_probability(0.0)
ratio=(len(ind)/av_size)
y = c-ratio
if len(ind) < (0.5 * av_size):
ind.LS_probability(1.)
elif (len(ind) >= (0.5 * av_size)) and (len(ind) <= (1.5 * av_size)):
ind.LS_probability(y)
def best_pop(population):
orderbyfit=list()
orderbyfit = sorted(population, key=lambda ind:ind.fitness.values)
return orderbyfit[0]
def best_set_pop(population):
orderbyfit=list()
orderbyfit = sorted(population, key=lambda ind:ind.fitness.values)
selected=len(population)*.10
p_sel=int(np.round(selected,0))
return orderbyfit[:p_sel]
def bestrand_set_pop(population):
orderbyfit=list()
orderbyfit = sorted(population, key=lambda ind:ind.fitness.values)
selected=len(population)*.10
p_sel=int(np.round(selected,0))
ind_sel=orderbyfit[:p_sel]
ind_list=list()
for ind in ind_sel:
coin=random.randint(0,1)
if coin:
ind_list.append(ind)
return ind_list
def random_set_pop(population):
orderbyfit=list()
for ind in population:
coin=random.randint(0,1)
if coin:
orderbyfit.append(ind)
return orderbyfit
def ls_bestset(population, p, n, pset, direccion, problem, benchmark_flag): # size heuristic
best_set=best_set_pop(population)
for ind in best_set:
strg = ind.__str__()
args = []
if len(pset.arguments) > 1:
for arg in pset.arguments:
args.append(arg)
l_strg = add_subt_cf(strg, args)
c = tree2f()
cd = c.convert(l_strg)
xdata, ydata = get_address(p, n, problem, direccion, benchmark_flag)
beta_opt, beta_cov, success, nfev = curve_fit_2(eval_,cd , xdata, ydata, p0=ind.get_params(), method='trf', max_nfev=40)
if not success:
ind.LS_applied_set(1)
ind.LS_story_set(1)
else:
ind.LS_applied_set(1)
ind.LS_story_set(1)
funcEval.cont_evalp += nfev
ind.params_set(beta_opt)
def ls_random(population, p, n, pset, direccion, problem, benchmark_flag): # size heuristic
random_set=random_set_pop(population)
for ind in random_set:
strg = ind.__str__()
args = []
if len(pset.arguments) >= 1:
for arg in pset.arguments:
args.append(arg)
l_strg = add_subt_cf(strg, args)
c = tree2f()
cd = c.convert(l_strg)
xdata, ydata = get_address(p, n, problem, direccion, benchmark_flag)
beta_opt, beta_cov, success, nfev = curve_fit_2(eval_,cd , xdata, ydata, p0=ind.get_params(), method='trf', max_nfev=40)
if not success:
ind.LS_applied_set(1)
ind.LS_story_set(1)
else:
ind.LS_applied_set(1)
ind.LS_story_set(1)
funcEval.cont_evalp += nfev
ind.params_set(beta_opt)
def ls_randbestset(population, p, n, pset, direccion, problem, benchmark_flag): # size heuristic
best_set=bestrand_set_pop(population)
for ind in best_set:
strg = ind.__str__()
args = []
if len(pset.arguments) >= 1:
for arg in pset.arguments:
args.append(arg)
l_strg = add_subt_cf(strg, args)
c = tree2f()
cd = c.convert(l_strg)
xdata, ydata = get_address(p, n,problem,direccion, benchmark_flag)
beta_opt, beta_cov, success, nfev = curve_fit_2(eval_,cd , xdata, ydata, p0=ind.get_params(), method='trf', max_nfev=40)
if not success:
ind.LS_applied_set(1)
ind.LS_story_set(1)
else:
ind.LS_applied_set(1)
ind.LS_story_set(1)
funcEval.cont_evalp += nfev
ind.params_set(beta_opt)
def trees_h(population, p, n, pset, direccion, problem, benchmark_flag): # size heuristic
eval_prob(population)
for ind in population:
if random.random() <= ind.get_LS_prob():
strg = ind.__str__()
args = []
if len(pset.arguments) > 1:
for arg in pset.arguments:
args.append(arg)
l_strg = add_subt_cf(strg, args)
c = tree2f()
cd = c.convert(l_strg)
xdata, ydata = get_address(p, n, problem, direccion, benchmark_flag)
beta_opt, beta_cov, success, nfev = curve_fit_2(eval_,cd , xdata, ydata, p0=ind.get_params(), method='trf', max_nfev=40)
if not success:
ind.LS_applied_set(1)
ind.LS_story_set(1)
else:
ind.LS_applied_set(1)
ind.LS_story_set(1)
funcEval.cont_evalp += nfev
ind.params_set(beta_opt)
def trees_h_wo(population, p, n, pset, direccion): # size heuristic
eval_prob(population)
for ind in population:
if random.random() <= ind.get_LS_prob():
ind.LS_applied_set(1)
ind.LS_story_set(1)
def all_pop(population, p, n, pset, direccion, problem, benchmark_flag): # size heuristic
for ind in population:
strg = ind.__str__()
args = []
if len(pset.arguments) > 1:
for arg in pset.arguments:
args.append(arg)
l_strg = add_subt_cf(strg, args)
c = tree2f()
cd = c.convert(l_strg)
xdata, ydata = get_address(p, n, problem, direccion, benchmark_flag)
beta_opt, beta_cov, success, nfev = curve_fit_2(eval_, cd, xdata, ydata, p0=ind.get_params(), method='trf', max_nfev=40)
if not success:
ind.LS_applied_set(1)
ind.LS_story_set(1)
else:
ind.LS_applied_set(1)
ind.LS_story_set(1)
ind.params_set(beta_opt)
funcEval.cont_evalp+=nfev
def best_pop_ls(population, p, n, pset, direccion, problem, benchmark_flag): # best of the pop
ind = best_pop(population)
strg = ind.__str__()
args = []
if len(pset.arguments) > 1:
for arg in pset.arguments:
args.append(arg)
l_strg = add_subt_cf(strg, args)
c = tree2f()
cd = c.convert(l_strg)
xdata,ydata = get_address(p, n, problem, direccion, benchmark_flag)
beta_opt, beta_cov, success, nfev = curve_fit_2(eval_, cd, xdata, ydata, p0=ind.get_params(), method='trf', max_nfev=40)
if not success:
ind.LS_applied_set(1)
ind.LS_story_set(1)
else:
ind.LS_applied_set(1)
ind.LS_story_set(1)
ind.params_set(beta_opt)
funcEval.cont_evalp += nfev
def best_specie(population, p, n, pset, direccion, problem, benchmark_flag): # best of each specie
for ind in population:
if ind.bestspecie_get()==1:
strg=ind.__str__()
args=[]
if len(pset.arguments) > 0:
for arg in pset.arguments:
args.append(arg)
l_strg=add_subt_cf(strg, args)
c = tree2f()
cd=c.convert(l_strg)
xdata,ydata=get_address(p, n, problem, direccion, benchmark_flag)
beta_opt, beta_cov, success, nfev = curve_fit_2(eval_,cd , xdata, ydata, p0=ind.get_params(), method='trf', max_nfev=40)
if not success:
ind.LS_applied_set(1)
ind.LS_story_set(1)
else:
ind.LS_applied_set(1)
ind.LS_story_set(1)
ind.params_set(beta_opt)
funcEval.cont_evalp+=nfev
def specie_h(population, p, n, pset, direccion, problem, benchmark_flag): # heuristic applied by specie
gpo_specie=specie_gpo(population)
for specie in gpo_specie:
eval_prob(specie[0])
for ind in population:
if ind.bestspecie_get()==1:
if random.random()<=ind.get_LS_prob():
strg=ind.__str__()
args=[]
if len(pset.arguments) > 0:
for arg in pset.arguments:
args.append(arg)
l_strg=add_subt_cf(strg, args)
c = tree2f()
cd=c.convert(l_strg)
xdata,ydata=get_address(p, n, problem, direccion, benchmark_flag)
beta_opt, beta_cov, success, nfev = curve_fit_2(eval_,cd , xdata, ydata, p0=ind.get_params(), method='trf', max_nfev=40)
if not success:
ind.LS_applied_set(1)
ind.LS_story_set(1)
else:
ind.LS_applied_set(1)
ind.LS_story_set(1)
ind.params_set(beta_opt)
funcEval.cont_evalp += nfev