Skip to content

Commit 3244e40

Browse files
committed
adding the child class evaluator and using it for batched objective evaluations in the BO loop
1 parent 0134038 commit 3244e40

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

src/hiopbbpy/opt/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from .boalgorithm import (BOAlgorithmBase, BOAlgorithm)
22
from .acquisition import (acquisition, LCBacquisition, EIacquisition)
33
from .optproblem import (IpoptProb)
4+
from .evaluator import Evaluator
45

56
__all__ = [
67
"BOAlgorithmBase"
@@ -9,4 +10,5 @@
910
"LCBacquisition"
1011
"EIacquisition"
1112
"IpoptProb"
13+
"Evaluator"
1214
]

src/hiopbbpy/opt/boalgorithm.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import warnings
1313
from ..surrogate_modeling.gp import GaussianProcess
1414
from .acquisition import LCBacquisition, EIacquisition
15+
from .evaluator import Evaluator
1516
from ..problems.problem import Problem
1617
from .optproblem import IpoptProb
1718

@@ -23,6 +24,7 @@ def __init__(self):
2324
self.xtrain = None # Training data
2425
self.ytrain = None # Training data
2526
self.prob = None # Problem structure
27+
self.evaluator = None # compute control objective evaluations
2628
self.bo_maxiter = 20 # Maximum number of Bayesian optimization steps
2729
self.n_start = 10 # estimating acquisition global optima by determining local optima n_start times and then determining the discrete max of that set
2830
self.batch_size = 1 # batch size
@@ -93,6 +95,9 @@ def __init__(self, prob:Problem, gpsurrogate:GaussianProcess, xtrain, ytrain,
9395
f"batched BO only supported for expected-improvement"
9496
self.setAcquisitionType(acquisition_type, batch_size)
9597

98+
self.evaluator = options.get('evaluator', Evaluator())
99+
assert isinstance(self.evaluator, Evaluator)
100+
96101
if options and 'opt_solver' in options:
97102
opt_solver = options['opt_solver']
98103
assert opt_solver in ["SLSQP", "IPOPT"], f"Invalid opt_solver: {opt_solver}"
@@ -196,7 +201,8 @@ def optimize(self):
196201
y_train_virtual = np.vstack([y_train_virtual, y_virtual])
197202

198203
# TODO: include a parallel evaluator
199-
y_new = self.prob.evaluate(np.atleast_2d(x_train[-self.batch_size:]))
204+
y_new = self.evaluator.run(self.prob.evaluate, x_train[-self.batch_size:])
205+
#y_new = self.prob.evaluate(np.atleast_2d(x_train[-self.batch_size:]))
200206

201207

202208
y_train = np.vstack([y_train, y_new])

src/hiopbbpy/opt/evaluator.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
Abstract evaluator to control how (batched) objective evaluations are performed
3+
4+
Authors: Tucker Hartland <hartland1@llnl.gov>
5+
Nai-Yuan Chiang <chiang7@llnl.gov>
6+
"""
7+
8+
9+
class Evaluator(object):
10+
"""
11+
An interface for evaluation of a function at x points (nsamples of dimension nx).
12+
User can derive this interface and override the run() method to implement custom multiprocessing.
13+
"""
14+
15+
def run(self, fun, x):
16+
"""
17+
Evaluates fun at x.
18+
19+
Parameters
20+
---------
21+
fun : function to evaluate: (nsamples, nx) -> (nsample, 1)
22+
23+
x : np.ndarray[nsamples, nx]
24+
nsamples points of nx dimensions.
25+
26+
Returns
27+
-------
28+
np.ndarray[nsample, 1]
29+
fun evaluations at the nsamples points.
30+
31+
"""
32+
return fun(x)

0 commit comments

Comments
 (0)