forked from CustomComputingGroup/MLO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
surrogatemodel.py
68 lines (49 loc) · 2.17 KB
/
surrogatemodel.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
import logging
from classifiers import Classifier, SupportVectorMachineClassifier
from regressors import Regressor, GaussianProcessRegressor
class SurrogateModel(object):
def __init__(self, fitness, configuration, controller):
self.fitness = fitness
self.configuration = configuration
self.classifier = Classifier()
self.regressor = Regressor(controller)
def train(self, pop):
raise NotImplementedError('SurrogateModel is an abstract class, this '
'should not be called.')
def model_particles(self, particles):
MU, S2 = self.regressor.predict(particles)
return self.classifier.predict(particles), MU, S2
def add_training_instance(self, part, code, fitness):
pass
def __getstate__(self):
# Don't pickle fitness and configuration
d = dict(self.__dict__)
del d['fitness']
del d['configuration']
return d
class DummySurrogateModel(SurrogateModel):
def train(self, pop):
return True
def model_particle(self, particle):
return 0, 0, 0
class ProperSurrogateModel(SurrogateModel):
def __init__(self, fitness, configuration, controller):
super(ProperSurrogateModel, self).__init__(fitness, configuration,
controller)
if configuration.classifier == 'SupportVectorMachine':
self.classifier = SupportVectorMachineClassifier()
else:
logging.error('Classifier type {} not found'.format(
configuration.classifier))
if configuration.regressor == 'GaussianProcess':
self.regressor = GaussianProcessRegressor(controller)
else:
logging.error('Regressor type {} not found'.format(
configuration.regressor))
def train(self, pop):
dimensions = self.fitness.dimensions
return self.classifier.train(pop) and self.regressor.train(
pop, self.configuration, dimensions)
def add_training_instance(self, part, code, fitness):
self.classifier.add_training_instance(part, code)
self.regressor.add_training_instance(part, fitness)