Skip to content

Commit 9270292

Browse files
authored
Adds himmelblau as a toy black box (#303)
1 parent 31eac5b commit 9270292

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

src/poli/objective_repository/toy_continuous_problem/definitions.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,33 @@ def levy(x: np.ndarray):
430430
return -(term1 + term2 + term3)
431431

432432

433+
def himmelblau(x: np.ndarray):
434+
"""
435+
Compute the Himmelblau function.
436+
437+
Parameters
438+
----------
439+
x (np.ndarray): A 2D numpy array of shape [b, d] where 'b' is the batch size and 'd' is the dimensionality.
440+
441+
Returns
442+
-------
443+
np.ndarray: The value of the Himmelblau function at each point in 'x'. Shape is [b,].
444+
445+
References
446+
----------
447+
[1] Surjanovic, S. and Bingham, D. Virtual Library of Simulation Experiments:
448+
Test Functions and Datasets. [https://www.sfu.ca/~ssurjano/optimization.html]
449+
"""
450+
assert len(x.shape) == 2, "Input x must be a 2D array."
451+
d = x.shape[1]
452+
assert d == 2, "Dimensionality must be 2."
453+
454+
x1 = x[:, 0]
455+
x2 = x[:, 1]
456+
457+
return -((x1**2 + x2 - 11) ** 2 + (x1 + x2**2 - 7) ** 2)
458+
459+
433460
if __name__ == "__main__":
434461
b = branin_2d
435462
maximal_b = b(np.array([[-np.pi, 12.275], [np.pi, 2.275], [9.42478, 2.475]]))

src/poli/objective_repository/toy_continuous_problem/toy_continuous_problem.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
easom,
2727
egg_holder,
2828
hartmann_6d,
29+
himmelblau,
2930
levy,
3031
rosenbrock,
3132
shifted_sphere,
@@ -55,13 +56,15 @@
5556
"branin_2d",
5657
"rosenbrock",
5758
"levy",
59+
"himmelblau",
5860
]
5961
TWO_DIMENSIONAL_PROBLEMS = [
6062
"easom",
6163
"cross_in_tray",
6264
"egg_holder",
6365
"camelback_2d",
6466
"branin_2d",
67+
"himmelblau",
6568
]
6669
SIX_DIMENSIONAL_PROBLEMS = ["hartmann_6d"]
6770

@@ -248,6 +251,12 @@ def __init__(
248251
self.optima_location = np.array([1.0] * n_dims)
249252
self.solution_length = n_dims
250253
self.x0 = np.array([[0.0] * self.solution_length])
254+
elif name == "himmelblau":
255+
self.function = himmelblau
256+
self.limits = [-5.0, 5.0]
257+
self.optima_location = np.array([3.0, 2.0])
258+
self.solution_length = n_dims
259+
self.x0 = np.array([[0.0] * self.solution_length])
251260
else:
252261
raise ValueError(f"Expected {name} to be one of {POSSIBLE_FUNCTIONS}")
253262

0 commit comments

Comments
 (0)