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

Use Parameter in OnePlusOne #599

Merged
merged 5 commits into from
Apr 2, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
variables when the `full_range_sampling` is `True` [598](https://github.com/facebookresearch/nevergrad/pull/598).
This required some ugly hacks, help is most welcome to find nices solutions.
- `full_range_sampling` is activated by default if both range are provided in `Array.set_bounds`.
- Propagate parametrization system features (generation tracking, ...) to `OnePlusOne` based algorithms [599](https://github.com/facebookresearch/nevergrad/pull/599).


## v0.4.0 (2019-03-09)
Expand Down
2 changes: 1 addition & 1 deletion nevergrad/benchmark/experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ def photonics(seed: tp.Optional[int] = None) -> tp.Iterator[Experiment]:
def bragg_structure(seed: tp.Optional[int] = None) -> tp.Iterator[Experiment]:
seedg = create_seed_generator(seed)
recombinable: tp.List[tp.Union[str, ConfiguredOptimizer]] = [
ng.families.EvolutionStrategy(recombination_ratio=0.1, popsize=40).set_name("DES"),
ng.families.EvolutionStrategy(recombination_ratio=0.1, popsize=40).set_name("Pairwise-ES"),
ng.families.DifferentialEvolution(crossover="parametrization").set_name("Param-DE")
]
algos: tp.List[tp.Union[str, ConfiguredOptimizer]] = ["TwoPointsDE", "DE", "CMA", "NaiveTBPSA", "DiagonalCMA"]
Expand Down
54 changes: 32 additions & 22 deletions nevergrad/optimization/optimizerlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,45 +69,55 @@ def __init__(
self.mutation = mutation
self.crossover = crossover

def _internal_ask(self) -> ArrayLike:
def _internal_ask_candidate(self) -> p.Parameter:
# pylint: disable=too-many-return-statements, too-many-branches
noise_handling = self.noise_handling
if not self._num_ask:
return np.zeros(self.dimension)
out = self.parametrization.spawn_child()
out._meta["sigma"] = self._sigma
return out
# for noisy version
if noise_handling is not None:
limit = (0.05 if isinstance(noise_handling, str) else noise_handling[1]) * len(self.archive) ** 3
strategy = noise_handling if isinstance(noise_handling, str) else noise_handling[0]
if self._num_ask <= limit:
if strategy in ["cubic", "random"]:
idx = self._rng.choice(len(self.archive))
return np.frombuffer(list(self.archive.bytesdict.keys())[idx]) # type: ignore
return list(self.archive.values())[idx].parameter.spawn_child() # type: ignore
elif strategy == "optimistic":
return self.current_bests["optimistic"].parameter.get_standardized_data(reference=self.parametrization)
return self.current_bests["optimistic"].parameter.spawn_child()
# crossover
mutator = mutations.Mutator(self._rng)
pessimistic = self.current_bests["pessimistic"].parameter.get_standardized_data(reference=self.parametrization)
pessimistic = self.current_bests["pessimistic"].parameter.spawn_child()
ref = self.parametrization
if self.crossover and self._num_ask % 2 == 1 and len(self.archive) > 2:
return mutator.crossover(pessimistic, mutator.get_roulette(self.archive, num=2))
data = mutator.crossover(pessimistic.get_standardized_data(reference=ref),
mutator.get_roulette(self.archive, num=2))
return pessimistic.set_standardized_data(data, reference=ref)
# mutating
mutation = self.mutation
if mutation == "gaussian": # standard case
return pessimistic + self._sigma * self._rng.normal(0, 1, self.dimension) # type: ignore
elif mutation == "cauchy":
return pessimistic + self._sigma * self._rng.standard_cauchy(self.dimension) # type: ignore
elif mutation == "crossover":
if self._num_ask % 2 == 0 or len(self.archive) < 3:
return mutator.portfolio_discrete_mutation(pessimistic)
else:
return mutator.crossover(pessimistic, mutator.get_roulette(self.archive, num=2))
if mutation in ("gaussian", "cauchy"): # standard case
step = (self._rng.normal(0, 1, self.dimension) if mutation == "gaussian" else
self._rng.standard_cauchy(self.dimension))
out = pessimistic.set_standardized_data(self._sigma * step)
out._meta["sigma"] = self._sigma
return out
else:
func: Callable[[ArrayLike], ArrayLike] = { # type: ignore
"discrete": mutator.discrete_mutation,
"fastga": mutator.doerr_discrete_mutation,
"doublefastga": mutator.doubledoerr_discrete_mutation,
"portfolio": mutator.portfolio_discrete_mutation,
}[mutation]
return func(pessimistic)
pessimistic_data = pessimistic.get_standardized_data(reference=ref)
if mutation == "crossover":
if self._num_ask % 2 == 0 or len(self.archive) < 3:
data = mutator.portfolio_discrete_mutation(pessimistic_data)
else:
data = mutator.crossover(pessimistic_data, mutator.get_roulette(self.archive, num=2))
else:
func: Callable[[ArrayLike], ArrayLike] = { # type: ignore
"discrete": mutator.discrete_mutation,
"fastga": mutator.doerr_discrete_mutation,
"doublefastga": mutator.doubledoerr_discrete_mutation,
"portfolio": mutator.portfolio_discrete_mutation,
}[mutation]
data = func(pessimistic_data)
return pessimistic.set_standardized_data(data, reference=ref)

def _internal_tell(self, x: ArrayLike, value: float) -> None:
# only used for cauchy and gaussian
Expand Down
4 changes: 2 additions & 2 deletions nevergrad/optimization/test_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def test_log_parameters(tmp_path: Path) -> None:
logs = logger.load_flattened()
assert len(logs) == 32
assert isinstance(logs[-1]["1"], float)
assert len(logs[-1]) == 34
assert len(logs[-1]) == 35
logs = logger.load_flattened(max_list_elements=2)
assert len(logs[-1]) == 26
assert len(logs[-1]) == 27
# deletion
logger = callbacks.ParametersLogger(filepath, append=False)
assert not logger.load()
Expand Down