Skip to content

Commit e2f356e

Browse files
committed
feature: Ackley function
1 parent 3ef7c75 commit e2f356e

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

examples/visualize_optimizers.py

+40
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,26 @@
9696
}
9797

9898

99+
def ackley(
100+
x: Union[Tuple[torch.Tensor, torch.Tensor], torch.Tensor], a: float = 20.0, b: float = 0.2, c: float = 2.0 * np.pi
101+
) -> torch.Tensor:
102+
"""
103+
Ackley function (non-convex, global minimum at (0, 0)).
104+
105+
Args:
106+
x: Input tensor containing 2D coordinates.
107+
a: hyperparameter.
108+
b: hyperparameter.
109+
c: hyperparameter.
110+
111+
Returns:
112+
torch.Tensor: Computed function value.
113+
"""
114+
sum_sq = -a * torch.exp(-b * torch.sqrt(0.5 * (x[0] ** 2 + x[1] ** 2)))
115+
cos = -torch.exp(0.5 * (torch.cos(c * x[0]) + torch.cos(c * x[1])))
116+
return sum_sq + cos + np.exp(1) + a
117+
118+
99119
def rosenbrock(x: Union[Tuple[torch.Tensor, torch.Tensor], torch.Tensor]) -> torch.Tensor:
100120
"""
101121
Rosenbrock function (non-convex, global minimum at (1, 1)).
@@ -441,6 +461,26 @@ def main():
441461
seed=SEARCH_SEED,
442462
)
443463

464+
print('Executing Ackley experiments...') # noqa: T201
465+
execute_experiments(
466+
[
467+
(
468+
OPTIMIZERS['ranger'],
469+
{
470+
'lr': hp.quniform('lr', 0, 1, 0.01),
471+
},
472+
)
473+
],
474+
ackley,
475+
initial_state=(-2.0, -2.0),
476+
output_dir=output_dir,
477+
experiment_name='ackley',
478+
x_range=(-5, 5),
479+
y_range=(-5, 5),
480+
minimum=(0.0, 0.0),
481+
seed=SEARCH_SEED,
482+
)
483+
444484

445485
if __name__ == '__main__':
446486
main()

0 commit comments

Comments
 (0)