-
Notifications
You must be signed in to change notification settings - Fork 1
/
mvsr.py
213 lines (173 loc) · 6.98 KB
/
mvsr.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
import numpy as np
from pyoperon.sklearn import SymbolicRegressor
from sklearn.metrics import r2_score, mean_squared_error
import pyoperon as Operon
import random, time, sys, os, json
from scipy import stats
import sympy as sp
import warnings
warnings.filterwarnings("ignore")
def MultiViewSR(
path,
minL=1,
maxL=20,
maxD=10,
generations=1000,
OperationSet=None,
seed=44,
verbose=True,
use_single_view=None,
explicit_params=True,
):
all_examples = [x for x in os.listdir(path) if "csv" in x]
all_examples = sorted(all_examples)
if use_single_view is not None:
if use_single_view >= len(all_examples):
raise ValueError("Example asked do not exist")
n_sets = len(all_examples)
problems = []
selectors = []
smallest_data = np.array(
[
len(np.loadtxt(f"{path}/{i}", delimiter=",", skiprows=1))
for i in all_examples
]
).min()
for i in all_examples:
Z = np.loadtxt(f"{path}/{i}", delimiter=",", skiprows=1)
# np.random.shuffle(Z)
ds = Operon.Dataset(Z)
training_range = Operon.Range(0, smallest_data)
test_range = Operon.Range(0, smallest_data)
# define the regression target
target = ds.Variables[-1] # take the last column in the dataset as the target
# take all other variables as inputs
inputs = [h for h in ds.VariableHashes if h != target.Hash]
# initialize a problem object which encapsulates the data, input, target and training/test ranges
problem = Operon.Problem(ds, training_range, test_range)
problem.Target = target
problem.InputHashes = inputs
# use tournament selection with a group size of 5
# we are doing single-objective optimization so the objective index is 0
selector = Operon.TournamentSelector(objective_index=0)
selector.TournamentSize = 10
selectors.append(selector)
# initialize the primitive set (add, sub, mul, div, exp, log, sin, cos), constants and variables are implicitly added
if OperationSet != None:
OperationSet = Operon.PrimitiveSet.Arithmetic | OperationSet
else:
OperationSet = Operon.PrimitiveSet.Arithmetic
problem.ConfigurePrimitiveSet(OperationSet)
pset = problem.PrimitiveSet
problems.append(problem)
# define a tree creator (responsible for producing trees of given lengths)
btc = Operon.BalancedTreeCreator(pset, problems[0].InputHashes, bias=0.0)
tree_initializer = Operon.UniformLengthTreeInitializer(btc)
tree_initializer.ParameterizeDistribution(minL, maxL)
tree_initializer.MaxDepth = maxD
# define a coefficient initializer (this will initialize the coefficients in the tree)
coeff_initializer = Operon.NormalCoefficientInitializer()
coeff_initializer.ParameterizeDistribution(0, 1)
# define several kinds of mutation
mut_onepoint = Operon.NormalOnePointMutation()
mut_changeVar = Operon.ChangeVariableMutation(inputs)
mut_changeFunc = Operon.ChangeFunctionMutation(pset)
mut_replace = Operon.ReplaceSubtreeMutation(btc, coeff_initializer, maxD, maxL)
# use a multi-mutation operator to apply them at random
mutation = Operon.MultiMutation()
mutation.Add(mut_onepoint, 1)
mutation.Add(mut_changeVar, 1)
mutation.Add(mut_changeFunc, 1)
mutation.Add(mut_replace, 1)
# define crossover
crossover_internal_probability = (
0.9 # probability to pick an internal node as a cut point
)
crossover = Operon.SubtreeCrossover(crossover_internal_probability, maxD, maxL)
# define fitness evaluation
evaluator = Operon.MultiEvaluator(problems[0])
evaluators = []
interpreters = []
metrics = []
optimizers = []
for i in range(n_sets):
interpreter = Operon.DispatchTable() # tree interpreter
interpreters.append(interpreter)
error_metric = Operon.MSE() # use the coefficient of determination as fitness
metrics.append(error_metric)
evaluator_i = Operon.Evaluator(problems[i], interpreter, metrics[-1], True)
optimizers.append(
Operon.Optimizer(
interpreter,
problems[i],
optimizer="lbfgs",
likelihood="gaussian",
iterations=100,
batchsize=smallest_data,
)
)
evaluator_i.Optimizer = optimizers[-1]
# evaluator_i.LocalOptimizationIterations = 100
evaluators.append(evaluator_i)
if use_single_view is None:
evaluator.Add(evaluators[-1])
else:
if i == use_single_view:
evaluator.Add(evaluators[-1])
aggregateEvaluator = Operon.AggregateEvaluator(evaluator)
# aggregateEvaluator.AggregateType = Operon.AggregateType.HarmonicMean
aggregateEvaluator.AggregateType = Operon.AggregateType.Max
evaluator.Budget = 1000 * 1000 # computational budget
# define how new offspring are created
generator = Operon.BasicOffspringGenerator(
aggregateEvaluator, crossover, mutation, selector, selector
)
# initialize an algorithm configuration
config = Operon.GeneticAlgorithmConfig(
generations=generations,
max_evaluations=100000000,
local_iterations=0,
population_size=1000,
pool_size=5,
p_crossover=1.0,
p_mutation=0.25,
epsilon=1e-10,
seed=seed,
time_limit=86400,
)
# define how the offspring are merged back into the population - here we replace the worst parents with the best offspring
reinserter = Operon.ReplaceWorstReinserter(objective_index=0)
gp = Operon.GeneticProgrammingAlgorithm(
problems[0], config, tree_initializer, coeff_initializer, generator, reinserter
)
# report some progress
gen = 0
max_ticks = 50
interval = (
1
if config.Generations < max_ticks
else int(np.round(config.Generations / max_ticks, 0))
)
t0 = time.time()
# initialize a rng
rng = Operon.RomuTrio(seed)
# run the algorithm
gp.Run(rng, threads=1)
best = gp.BestModel
agg_model_string = Operon.InfixFormatter.Format(best.Genotype, ds, 15)
if explicit_params:
# Internally operon uses A*f(x)+B and fits A and B. We make sure that A and B appears explicity in the solutions
# to later be replaces by parameters. Not applying can biais the analysis in particular for simple cases
agg_model_string = "1.00001*(" + agg_model_string + ")+0.00001"
scores = []
if verbose:
print("Agg. estimator: ", aggregateEvaluator(rng, best))
print(f"{sp.sympify(agg_model_string)}\n")
minimized = []
for i, e in enumerate(evaluators):
scores.append(e(rng, best))
if verbose:
print(e.CallCount, e.ResidualEvaluations, e.JacobianEvaluations)
print(f"Eval. {i}: ", e(rng, best))
print(f"{sp.N(agg_model_string, 4)}\n")
return agg_model_string, scores