Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add catcma #91

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions package/samplers/catcma/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Masahiro Nomura

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.
88 changes: 88 additions & 0 deletions package/samplers/catcma/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---
author: "Masahiro Nomura"
title: "CatCMA Sampler"
description: "Categorical and Continuous Optimization with CMA-ES."
tags: ["sampler", "Mixed-variable optimization", "Evolutionary algorithms", "CMA-ES"]
optuna_versions: ["3.6.1"]
license: "MIT License"
---

## Class or Function Names
- CatCMASampler

## Installation
```bash
pip install -r requirements.txt
```

## Example
```python
import numpy as np
import optuna
from optuna.distributions import CategoricalDistribution
from optuna.distributions import FloatDistribution
import optunahub


def objective(trial: optuna.Trial) -> float:
x1 = trial.suggest_float("x1", -1, 1)
x2 = trial.suggest_float("x2", -1, 1)
x3 = trial.suggest_float("x3", -1, 1)
X = np.array([x1, x2, x3])

c1 = trial.suggest_categorical("c1", [0, 1, 2])
c2 = trial.suggest_categorical("c2", [0, 1, 2])
c3 = trial.suggest_categorical("c3", [0, 1, 2])
C = np.array([c1, c2, c3])

return sum(X**2) + len(C) - sum(C == 0)


if __name__ == "__main__":
mod = optunahub.load_module(
package="samplers/catcma",
)
CatCmaSampler = mod.CatCmaSampler

study = optuna.create_study(
sampler=CatCmaSampler(
search_space={
"x1": FloatDistribution(-1, 1),
"x2": FloatDistribution(-1, 1),
"x3": FloatDistribution(-1, 1),
"c1": CategoricalDistribution([0, 1, 2]),
"c2": CategoricalDistribution([0, 1, 2]),
"c3": CategoricalDistribution([0, 1, 2]),
}
)
)
study.optimize(objective, n_trials=20)
print(study.best_params)

# You can omit the search space definition before optimization.
# Then, the search space will be estimated during the first trial.
# In this case, independent_sampler (default: RandomSampler) will be used instead of the CatCma algorithm for the first trial.
study = optuna.create_study(sampler=CatCmaSampler())
study.optimize(objective, n_trials=20)
print(study.best_params)
```

## Others

### Reference

Ryoki Hamano, Shota Saito, Masahiro Nomura, Kento Uchida, Shinichi Shirakawa , CatCMA : Stochastic Optimization for Mixed-Category Problems, GECCO'24

See the [paper](https://arxiv.org/abs/2405.09962) for more details.


### BibTeX
```bibtex
@article{hamano2024catcma,
title={CatCMA: Stochastic Optimization for Mixed-Category Problems},
author={Hamano, Ryoki and Saito, Shota and Nomura, Masahiro and Uchida, Kento and Shirakawa, Shinichi},
journal={arXiv preprint arXiv:2405.09962},
year={2024}
}
```

4 changes: 4 additions & 0 deletions package/samplers/catcma/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .catcma import CatCmaSampler


__all__ = ["CatCmaSampler"]
Loading