Skip to content

Commit

Permalink
Update README
Browse files Browse the repository at this point in the history
  • Loading branch information
hrntsm committed Oct 18, 2024
1 parent a42f29b commit 3f5883f
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 231 deletions.
90 changes: 43 additions & 47 deletions package/samplers/nsgaii_with_initial_trials/README.md
Original file line number Diff line number Diff line change
@@ -1,81 +1,77 @@
---
author: Hiroaki Natsume
title: MOEA/D sampler
description: Sampler using MOEA/D algorithm. MOEA/D stands for "Multi-Objective Evolutionary Algorithm based on Decomposition.
tags: [Sampler, Multi-Objective Optimization, Evolutionary Algorithms]
title: NSGAII sampler with Initial Trials
description: Sampler using NSGAII algorithm with initial trials.
tags: [Sampler, Multi-Objective, Genetic Algorithm]
optuna_versions: [4.0.0]
license: MIT License
---

## Abstract

Sampler using MOEA/D algorithm. MOEA/D stands for "Multi-Objective Evolutionary Algorithm based on Decomposition.
If Optuna's built-in NSGAII has a study obtained from another sampler, but continues with that study, it cannot be used as the first generation, and optimization starts from zero.
This means that even if you already know good individuals, you cannot use it in the GA.
In this implementation, the already sampled results are included in the initial individuals of the GA to perform the optimization.

This sampler is specialized for multiobjective optimization. The objective function is internally decomposed into multiple single-objective subproblems to perform optimization.

It may not work well with multi-threading. Check results carefully.
Note, however, that this has the effect that the implementation does not necessarily support multi-threading in the generation of the initial generation.
After the initial generation, the implementation is similar to the built-in NSGAII.

## Class or Function Names

- MOEADSampler

## Installation

```
pip install scipy
```

or

```
pip install -r https://hub.optuna.org/samplers/moead/requirements.txt
```
- NSGAIIwITSampler

## Example

```python
import optuna
import optunahub


def objective(trial: optuna.Trial) -> tuple[float, float]:
x = trial.suggest_float("x", 0, 5)
y = trial.suggest_float("y", 0, 3)

v0 = 4 * x**2 + 4 * y**2
v1 = (x - 5) ** 2 + (y - 5) ** 2
return v0, v1


population_size = 100
n_trials = 1000
storage = optuna.storages.InMemoryStorage()

mod = optunahub.load_module("samplers/moead")
sampler = mod.MOEADSampler(
population_size=population_size,
scalar_aggregation_func="tchebycheff",
n_neighbors=population_size // 10,
# Sampling 0 generation using enqueueing & qmc sampler
study = optuna.create_study(
directions=["minimize", "minimize"],
sampler=optuna.samplers.QMCSampler(seed=42),
study_name="test",
storage=storage,
)
study.enqueue_trial(
{
"x": 0,
"y": 0,
}
)
study.optimize(objective, n_trials=128)

# Using sampling results as the initial generation
sampler = optunahub.load_module(
"samplers/nsgaii_with_initial_trials",
).NSGAIIwITSampler(population_size=25, seed=42)

study = optuna.create_study(
directions=["minimize", "minimize"],
sampler=sampler,
study_name="test",
storage=storage,
load_if_exists=True,
)
study = optuna.create_study(sampler=sampler)
study.optimize(objective, n_trials=n_trials)
study.optimize(objective, n_trials=100)

optuna.visualization.plot_pareto_front(study).show()
```

## Others

Comparison between Random, NSGAII and MOEA/D with ZDT1 as the objective function.
See `compare_2objective.py` in moead directory for details.

### Pareto Front Plot

| MOEA/D | NSGAII | Random |
| --------------------------- | ---------------------------- | ---------------------------- |
| ![MOEA/D](images/moead.png) | ![NSGAII](images/nsgaii.png) | ![Random](images/random.png) |

### Compare

![Compare](images/compare_pareto_front.png)

### Reference
The implementation is similar to Optuna's NSGAII except for the handling of initial generations. The license and documentation are below.

Q. Zhang and H. Li,
"MOEA/D: A Multiobjective Evolutionary Algorithm Based on Decomposition," in IEEE Transactions on Evolutionary Computation, vol. 11, no. 6, pp. 712-731, Dec. 2007,
[doi: 10.1109/TEVC.2007.892759](https://doi.org/10.1109/TEVC.2007.892759).
- [Documentation](https://optuna.readthedocs.io/en/stable/reference/samplers/generated/optuna.samplers.NSGAIISampler.html)
- [License](https://github.com/optuna/optuna/blob/master/LICENSE)
210 changes: 26 additions & 184 deletions package/samplers/nsgaii_with_initial_trials/example.py
Original file line number Diff line number Diff line change
@@ -1,202 +1,44 @@
import numpy as np
import optuna
from optuna.samplers import NSGAIISampler
from optuna.samplers.nsgaii import BLXAlphaCrossover
import optuna.storages.journal
import optunahub

from nsgaii_with_initial_trials import NSGAIIwITSampler


file_path = "./journal.log"
lock_obj = optuna.storages.journal.JournalFileOpenLock(file_path)

storage = optuna.storages.JournalStorage(
optuna.storages.journal.JournalFileBackend(file_path, lock_obj=lock_obj),
)

storage = optuna.storages.InMemoryStorage()


def objective(trial: optuna.Trial) -> tuple[float, float]:
# ZDT1
n_variables = 30

x = np.array([trial.suggest_float(f"x{i}", 0, 1) for i in range(n_variables)])
g = 1 + 9 * np.sum(x[1:]) / (n_variables - 1)
f1 = x[0]
f2 = g * (1 - (f1 / g) ** 0.5)

return f1, f2


population_size = 50
n_trials = 1000
seed = 42
x = trial.suggest_float("x", 0, 5)
y = trial.suggest_float("y", 0, 3)
v0 = 4 * x**2 + 4 * y**2
v1 = (x - 5) ** 2 + (y - 5) ** 2
return v0, v1


samplers = [
NSGAIISampler(
population_size=population_size,
seed=seed,
crossover=BLXAlphaCrossover(),
),
# NSGAIIwITSampler(
# population_size=population_size,
# seed=seed,
# crossover=BLXAlphaCrossover(),
# ),
NSGAIIwITSampler(
population_size=population_size,
seed=seed,
crossover=BLXAlphaCrossover(),
),
]

studies = []
title = ["NSGAII", "NSGAIIwInitialTrials"]
for i, sampler in enumerate(samplers):
study = optuna.create_study(
sampler=sampler,
study_name=title[i],
directions=["minimize", "minimize"],
storage=storage,
)

if i == 1:
study.enqueue_trial(
{
"x0": 0,
"x1": 1,
"x2": 0,
"x3": 0,
"x4": 0,
"x5": 0,
"x6": 0,
"x7": 0,
"x8": 0,
"x9": 0,
"x10": 0,
"x11": 0,
"x12": 0,
"x13": 0,
"x14": 0,
"x15": 0,
"x16": 0,
"x17": 0,
"x18": 0,
"x19": 0,
"x20": 0,
"x21": 0,
"x22": 0,
"x23": 0,
"x24": 0,
"x25": 0,
"x26": 0,
"x27": 0,
"x28": 0,
"x29": 0,
}
)
study.enqueue_trial(
{
"x0": 0.5,
"x1": 1,
"x2": 0,
"x3": 0,
"x4": 0,
"x5": 0,
"x6": 0,
"x7": 0,
"x8": 0,
"x9": 0,
"x10": 0,
"x11": 0,
"x12": 0,
"x13": 0,
"x14": 0,
"x15": 0,
"x16": 0,
"x17": 0,
"x18": 0,
"x19": 0,
"x20": 0,
"x21": 0,
"x22": 0,
"x23": 0,
"x24": 0,
"x25": 0,
"x26": 0,
"x27": 0,
"x28": 0,
"x29": 0,
}
)
study.enqueue_trial(
{
"x0": 1,
"x1": 1,
"x2": 0,
"x3": 0,
"x4": 0,
"x5": 0,
"x6": 0,
"x7": 0,
"x8": 0,
"x9": 0,
"x10": 0,
"x11": 0,
"x12": 0,
"x13": 0,
"x14": 0,
"x15": 0,
"x16": 0,
"x17": 0,
"x18": 0,
"x19": 0,
"x20": 0,
"x21": 0,
"x22": 0,
"x23": 0,
"x24": 0,
"x25": 0,
"x26": 0,
"x27": 0,
"x28": 0,
"x29": 0,
}
)

study.optimize(objective, n_trials=n_trials)
studies.append(study)

optuna.visualization.plot_pareto_front(study).show()

sampler1 = optuna.samplers.QMCSampler(seed=seed, qmc_type="halton", scramble=True)
storage = optuna.storages.InMemoryStorage()
# Sampling 0 generation using enqueueing & qmc sampler
study = optuna.create_study(
sampler=sampler1,
study_name="Random+NSGAII",
directions=["minimize", "minimize"],
sampler=optuna.samplers.QMCSampler(seed=42),
study_name="test",
storage=storage,
)
study.optimize(objective, n_trials=2 * n_trials)
sampler2 = NSGAIIwITSampler(
population_size=population_size,
seed=seed,
crossover=BLXAlphaCrossover(),
study.enqueue_trial(
{
"x": 0,
"y": 0,
}
)
study.optimize(objective, n_trials=128)

# Using previous sampling results as the initial generation,
# sampled by NSGAII.
sampler = optunahub.load_module(
"samplers/nsgaii_with_initial_trials",
).NSGAIIwITSampler(population_size=25, seed=42)

study = optuna.create_study(
sampler=sampler2,
study_name="Random+NSGAII",
directions=["minimize", "minimize"],
sampler=sampler,
study_name="test",
storage=storage,
load_if_exists=True,
)
study.optimize(objective, n_trials=n_trials // 2)
optuna.visualization.plot_pareto_front(study).show()
studies.append(study)
study.optimize(objective, n_trials=100)


m = optunahub.load_module("visualization/plot_pareto_front_multi")
fig = m.plot_pareto_front(studies)
fig.show()
optuna.visualization.plot_pareto_front(study).show()

0 comments on commit 3f5883f

Please sign in to comment.