Skip to content

Commit

Permalink
Merge pull request #79 from mist714/add-woa
Browse files Browse the repository at this point in the history
Add Whale Optimization Algorithm to samplers
  • Loading branch information
HideakiImamura authored Jun 21, 2024
2 parents 48f524c + 767336d commit 006a6bb
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 0 deletions.
21 changes: 21 additions & 0 deletions package/samplers/whale_optimization/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 @mist714

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
49 changes: 49 additions & 0 deletions package/samplers/whale_optimization/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
---
author: "mist714"
title: "Sampler using Whale Optimization Algorithm"
description: "Swarm Algorithm Inspired by Pod of Whale"
tags: ["sampler"]
optuna_versions: ["3.6.1"]
license: "MIT License"
---

## Class or Function Names
- WhaleOptimizationSampler

## Example
```python
from __future__ import annotations

import matplotlib.pyplot as plt
import optuna
import optunahub

from package.samplers.whale_optimization.whale_optimization import WhaleOptimizationSampler


WhaleOptimizationSampler = optunahub.load_module(
"samplers/whale_optimization"
).WhaleOptimizationSampler

if __name__ == "__main__":

def objective(trial: optuna.trial.Trial) -> float:
x = trial.suggest_float("x", -10, 10)
y = trial.suggest_float("y", -10, 10)
return x**2 + y**2

sampler = WhaleOptimizationSampler(
{
"x": optuna.distributions.FloatDistribution(-10, 10),
"y": optuna.distributions.FloatDistribution(-10, 10),
}
)
study = optuna.create_study(sampler=sampler)
study.optimize(objective, n_trials=100)
optuna.visualization.matplotlib.plot_optimization_history(study)
plt.show()
```

## Others
### Reference
Mirjalili, Seyedali & Lewis, Andrew. (2016). The Whale Optimization Algorithm. Advances in Engineering Software. 95. 51-67. 10.1016/j.advengsoft.2016.01.008.
4 changes: 4 additions & 0 deletions package/samplers/whale_optimization/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .whale_optimization import WhaleOptimizationSampler


__all__ = ["WhaleOptimizationSampler"]
29 changes: 29 additions & 0 deletions package/samplers/whale_optimization/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from __future__ import annotations

import matplotlib.pyplot as plt
import optuna
import optunahub


WhaleOptimizationSampler = optunahub.load_module( # type: ignore
"samplers/whale_optimization"
).WhaleOptimizationSampler


if __name__ == "__main__":

def objective(trial: optuna.trial.Trial) -> float:
x = trial.suggest_float("x", -10, 10)
y = trial.suggest_float("y", -10, 10)
return x**2 + y**2

sampler = WhaleOptimizationSampler(
{
"x": optuna.distributions.FloatDistribution(-10, 10),
"y": optuna.distributions.FloatDistribution(-10, 10),
}
)
study = optuna.create_study(sampler=sampler)
study.optimize(objective, n_trials=100)
optuna.visualization.matplotlib.plot_optimization_history(study)
plt.show()
107 changes: 107 additions & 0 deletions package/samplers/whale_optimization/whale_optimization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
from __future__ import annotations

from typing import Any

import numpy as np
import optuna
import optunahub


SimpleSampler = optunahub.load_module("samplers/simple").SimpleSampler


class WhaleOptimizationSampler(SimpleSampler): # type: ignore
def __init__(
self,
search_space: dict[str, optuna.distributions.BaseDistribution],
population_size: int = 10,
max_iter: int = 40,
) -> None:
super().__init__(search_space)
self._rng = np.random.RandomState()
self.population_size = population_size
self.max_iter = max_iter
assert all(
isinstance(dist, optuna.distributions.FloatDistribution)
for dist in search_space.values()
)
self.lower_bound = np.asarray([dist.low for dist in search_space.values()])
self.upper_bound = np.asarray([dist.high for dist in search_space.values()])
self.dim = len(search_space)
self.leader_pos = (
np.random.rand(self.dim) * (self.upper_bound - self.lower_bound) + self.lower_bound
)
self.leader_score = np.inf
self.positions = (
np.random.rand(self.population_size, self.dim) * (self.upper_bound - self.lower_bound)
+ self.lower_bound
)
self.queue: list[dict[str, Any]] = []

def sample_relative(
self,
study: optuna.study.Study,
trial: optuna.trial.FrozenTrial,
search_space: dict[str, optuna.distributions.BaseDistribution],
) -> dict[str, Any]:
if len(search_space) == 0:
return {}
if len(self.queue) != 0:
return self.queue.pop(0)
last_trials = study.get_trials(states=(optuna.trial.TrialState.COMPLETE,))[
-self.population_size :
]
current_iter = len(study.get_trials(states=(optuna.trial.TrialState.COMPLETE,)))
new_positions = np.asarray([list(e.params.values()) for e in last_trials])
fitnesses = np.asarray([e.value for e in last_trials])
if current_iter > self.population_size:
self.tell(new_positions, fitnesses)
a = 2 - current_iter * (2 / self.max_iter)
a2 = -1 + current_iter * (-1 / self.max_iter)
new_positions = np.zeros_like(self.positions)

for i in range(self.positions.shape[0]):
r1, r2 = np.random.rand(), np.random.rand()
A = 2 * a * r1 - a
C = 2 * r2
b, L = 1, ((a2 - 1) * np.random.rand() + 1)
p = np.random.rand()

if p < 0.5:
if np.abs(A) >= 1:
rand_leader_index = np.random.randint(self.population_size)
X_rand = self.positions[rand_leader_index, :]
D_X_rand = np.abs(C * X_rand - self.positions[i, :])
new_positions[i, :] = X_rand - A * D_X_rand
else:
D_Leader = np.abs(C * self.leader_pos - self.positions[i, :])
new_positions[i, :] = self.leader_pos - A * D_Leader
else:
distance2Leader = np.abs(self.leader_pos - self.positions[i, :])
new_positions[i, :] = (
distance2Leader * np.exp(b * L) * np.cos(L * 2 * np.pi) + self.leader_pos
)

param_list = [
{k: v for k, v in zip(search_space.keys(), new_pos)} for new_pos in new_positions
]
self.queue.extend(param_list)
return self.queue.pop(0)

def tell(self, new_positions: np.ndarray, fitnesses: np.ndarray) -> None:
self.positions = np.clip(new_positions, self.lower_bound, self.upper_bound)
min_index = np.argmin(fitnesses)
min_fitness = fitnesses[min_index]
if min_fitness < self.leader_score:
self.leader_score = min_fitness
self.leader_pos = self.positions[min_index].copy()

def sample_independent(
self,
study: "optuna.Study",
trial: "optuna.trial.FrozenTrial",
param_name: str,
param_distribution: optuna.distributions.BaseDistribution,
) -> Any:
independent_sampler = optuna.samplers.RandomSampler(seed=777)
return independent_sampler.sample_independent(study, trial, param_name, param_distribution)

0 comments on commit 006a6bb

Please sign in to comment.