Skip to content

Commit 38762af

Browse files
committed
move constraints into Problem class
1 parent a58037a commit 38762af

File tree

6 files changed

+24
-31
lines changed

6 files changed

+24
-31
lines changed

src/Drivers/hiopbbpy/BODriverEX.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,10 @@ def con_jac_ineq(x):
5757
for prob_type in prob_type_l:
5858
print()
5959
if prob_type == "LpNorm":
60-
problem = LpNormProblem(nx, xlimits)
60+
problem = LpNormProblem(nx, xlimits, constraints=None)
6161
else:
62-
problem = BraninProblem()
62+
problem = BraninProblem(constraints=None)
63+
problem.set_constraints(user_constraint)
6364

6465
for acq_type in acq_type_l:
6566
print("Problem name: ", problem.name)
@@ -84,8 +85,8 @@ def con_jac_ineq(x):
8485
}
8586

8687
# Instantiate and run Bayesian Optimization
87-
bo = BOAlgorithm(gp_model, x_train, y_train, options = options, user_constraints = user_constraint) #EI or LCB
88-
bo.optimize(problem)
88+
bo = BOAlgorithm(problem, gp_model, x_train, y_train, options = options) #EI or LCB
89+
bo.optimize()
8990

9091
sys.exit(retval)
9192

src/Drivers/hiopbbpy/LpNormProblem.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
from hiopbbpy.problems.problem import Problem
99

1010
class LpNormProblem(Problem):
11-
def __init__(self, ndim, xlimits, p=2.0):
11+
def __init__(self, ndim, xlimits, p=2.0, constraints=None):
1212
name = "LpNormProblem"
13-
super().__init__(ndim, xlimits, name=name)
13+
super().__init__(ndim, xlimits, name=name, constraints=constraints)
1414
self.p = p
1515

1616
def _evaluate(self, x):

src/hiopbbpy/opt/boalgorithm.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,19 +64,18 @@ def getOptimalObjective(self):
6464

6565
# A subclass of BOAlgorithmBase implementing a full Bayesian Optimization workflow
6666
class BOAlgorithm(BOAlgorithmBase):
67-
def __init__(self, gpsurrogate, xtrain, ytrain,
67+
def __init__(self, prob:Problem, gpsurrogate:GaussianProcess, xtrain, ytrain,
6868
user_grad = None,
69-
user_constraints = None,
7069
options = None):
7170
super().__init__()
7271

7372
assert isinstance(gpsurrogate, GaussianProcess)
7473

7574
self.setTrainingData(xtrain, ytrain)
75+
self.prob = prob
7676
self.gpsurrogate = gpsurrogate
7777
self.bounds = self.gpsurrogate.get_bounds()
7878
self.fun_grad = None
79-
self.constraints = None
8079

8180
if options and 'bo_maxiter' in options:
8281
self.bo_maxiter = options['bo_maxiter']
@@ -101,16 +100,13 @@ def __init__(self, gpsurrogate, xtrain, ytrain,
101100
opt_solver = "SLSQP"
102101
self.set_method(opt_solver)
103102

104-
if user_constraints:
105-
self.constraints = user_constraints
106-
107103
if user_grad:
108104
self.fun_grad = user_grad
109105

110106

111107
# Method to set up a callback function to minimize the acquisition function
112108
def _setup_acqf_minimizer_callback(self):
113-
self.acqf_minimizer_callback = lambda fun, x0: minimizer(fun, x0, self.opt_solver, self.bounds, self.constraints, self.solver_options)
109+
self.acqf_minimizer_callback = lambda fun, x0: minimizer(fun, x0, self.opt_solver, self.bounds, self.prob.constraints, self.solver_options)
114110

115111
# Method to train the GP model
116112
def _train_surrogate(self, x_train, y_train):
@@ -164,8 +160,7 @@ def set_options(self, solver_options):
164160
self.solver_options = solver_options
165161

166162
# Method to perform Bayesian optimization
167-
def optimize(self, prob:Problem):
168-
self.prob = prob
163+
def optimize(self):
169164
x_train = self.xtrain
170165
y_train = self.ytrain
171166

@@ -184,7 +179,7 @@ def optimize(self, prob:Problem):
184179
x_new = self._find_best_point(x_train, y_train)
185180

186181
# Evaluate the new sample point
187-
y_new = prob.evaluate(np.atleast_2d(x_new))
182+
y_new = self.prob.evaluate(np.atleast_2d(x_new))
188183

189184
# Update training set
190185
x_train = np.vstack([x_train, x_new])
@@ -229,8 +224,8 @@ def minimizer(fun, x0, method, bounds, constraints, solver_options):
229224
ipopt_prob = IpoptProb(fun['obj'], fun['grad'], constraints, bounds, solver_options)
230225
sol, info = ipopt_prob.solve(x0)
231226

232-
status = info.get('status', -1)
233-
msg = info.get('status_msg', -1)
227+
status = info.get('status', -999)
228+
msg = info.get('status_msg', b'unknown error')
234229
if status == 0:
235230
# ipopt returns 0 as success
236231
success = True

src/hiopbbpy/opt/optproblem.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def __init__(self, objective, gradient, constraints_list, xbounds, solver_option
4646
lb=self.xl,
4747
ub=self.xu,
4848
cl=self.cl,
49-
cu=self.xu
49+
cu=self.cu
5050
)
5151

5252
def objective(self, x):
@@ -76,4 +76,4 @@ def solve(self, x0, solver_options=None):
7676
self.nlp.add_option(key, value)
7777

7878
# Solve the optimization problem
79-
return self.nlp.solve(x0)
79+
return self.nlp.solve(x0)

src/hiopbbpy/problems/BraninProblem.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919

2020
# define the Branin problem class
2121
class BraninProblem(Problem):
22-
def __init__(self):
22+
def __init__(self, constraints=None):
2323
ndim = 2
2424
xlimits = np.array([[-5.0, 10], [0.0, 15]])
2525
name = 'Branin'
26-
super().__init__(ndim, xlimits, name=name)
26+
super().__init__(ndim, xlimits, name=name, constraints=constraints)
2727

2828
def _evaluate(self, x: np.ndarray) -> np.ndarray:
2929

@@ -40,17 +40,10 @@ def _evaluate(self, x: np.ndarray) -> np.ndarray:
4040
arg1 = (x[:,1] - b * x[:,0]**2 + c * x[:,0] - r)
4141
y[:,0] = arg1**2 + s * (1 - t) * np.cos(x[:,0]) + s
4242

43-
'''
44-
# compute derivatives
45-
dy_dx0 = 2*arg1*(-2*b*x[:,0]+c) - s*(1-t)*np.sin(x[:,0])
46-
dy_dx1 = 2*arg1
47-
48-
dy_dx = np.array([dy_dx0, dy_dx1])
49-
'''
50-
5143
return y
5244

5345

5446

5547

5648

49+

src/hiopbbpy/problems/problem.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
# define the general optimization problem class
1212
class Problem:
13-
def __init__(self, ndim, xlimits, name=None):
13+
def __init__(self, ndim, xlimits, name=None, constraints=None):
1414
self.ndim = ndim
1515
self.xlimits = xlimits
1616
assert self.xlimits.shape[0] == ndim
@@ -19,6 +19,7 @@ def __init__(self, ndim, xlimits, name=None):
1919
else:
2020
self.name = name
2121
self.sampler = qmc.LatinHypercube(ndim)
22+
self.constraints = constraints
2223

2324
def _evaluate(self, x: np.ndarray) -> np.ndarray:
2425
"""
@@ -80,6 +81,9 @@ def sample(self, nsample: int) -> np.ndarray:
8081

8182
return xsample
8283

84+
def set_constraints(self, constraints: dict):
85+
self.constraints = constraints
86+
8387

8488

8589

0 commit comments

Comments
 (0)