-
Notifications
You must be signed in to change notification settings - Fork 240
/
surrogate_model.py
128 lines (106 loc) · 3.38 KB
/
surrogate_model.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
"""
Class definition for SurrogateModel, the base class for all surrogate models.
"""
from openmdao.utils.options_dictionary import OptionsDictionary
class SurrogateModel(object):
"""
Base class for surrogate models.
Parameters
----------
**kwargs : dict
Options dictionary.
Attributes
----------
options : <OptionsDictionary>
Dictionary with general pyoptsparse options.
trained : bool
True when surrogate has been trained.
"""
def __init__(self, **kwargs):
"""
Initialize all attributes.
"""
self.trained = False
self.options = OptionsDictionary(parent_name=type(self).__name__)
self._declare_options()
self.options.update(kwargs)
def train(self, x, y):
"""
Train the surrogate model with the given set of inputs and outputs.
Parameters
----------
x : array-like
Training input locations..
y : array-like
Model responses at given inputs.
"""
self.trained = True
def predict(self, x):
"""
Calculate a predicted value of the response based on the current trained model.
Parameters
----------
x : array-like
Point(s) at which the surrogate is evaluated.
"""
if not self.trained:
msg = "{0} has not been trained, so no prediction can be made."\
.format(type(self).__name__)
raise RuntimeError(msg)
def vectorized_predict(self, x):
"""
Calculate predicted values of the response based on the current trained model.
Parameters
----------
x : array-like
Vectorized point(s) at which the surrogate is evaluated.
"""
pass
def linearize(self, x):
"""
Calculate the jacobian of the interpolant at the requested point.
Parameters
----------
x : array-like
Point at which the surrogate Jacobian is evaluated.
"""
pass
def _declare_options(self):
"""
Declare options before kwargs are processed in the init method.
"""
pass
class MultiFiSurrogateModel(SurrogateModel):
"""
Base class for surrogate models using multi-fidelity training data.
Parameters
----------
**kwargs : dict
Options dictionary.
"""
def train(self, x, y):
"""
Train the surrogate model with the given set of inputs and outputs.
Parameters
----------
x : array-like
Point(s) at which the surrogate is evaluated.
y : array-like
Model responses at given inputs.
"""
super().train(x, y)
self.train_multifi([x], [y])
def train_multifi(self, x, y):
"""
Train the surrogate model, based on the given multi-fidelity training data.
Parameters
----------
x : list of double array_like elements
A list of arrays with the input at which observations were made, from highest
fidelity to lowest fidelity. Designs must be nested
with X[i] = np.vstack([..., X[i+1]).
y : list of double array_like elements
A list of arrays with the observations of the scalar output to be predicted,
from highest fidelity to lowest fidelity.
"""
pass