-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
67b361b
commit 5ccd16a
Showing
15 changed files
with
224 additions
and
196 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
from .dist_open_es import DistributedOpenES | ||
from .open_es import OpenES | ||
|
||
|
||
DistributedStrategies = {"DistributedOpenES": DistributedOpenES} | ||
DistributedStrategies = {"OpenES": OpenES} | ||
|
||
|
||
__all__ = ["DistributedOpenES", "DistributedStrategies"] | ||
__all__ = ["OpenES", "DistributedStrategies"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
from typing import Tuple, Optional, Union | ||
import jax | ||
import jax.numpy as jnp | ||
import chex | ||
from flax import struct | ||
from ..strategy import Strategy | ||
from ..utils import get_best_fitness_member | ||
|
||
|
||
@struct.dataclass | ||
class EvoState: | ||
mean: chex.Array | ||
sigma: chex.Array | ||
best_member: chex.Array | ||
best_fitness: float = jnp.finfo(jnp.float32).max | ||
gen_counter: int = 0 | ||
|
||
|
||
@struct.dataclass | ||
class EvoParams: | ||
sigma_init: float = 1.0 | ||
init_min: float = 0.0 | ||
init_max: float = 0.0 | ||
clip_min: float = -jnp.finfo(jnp.float32).max | ||
clip_max: float = jnp.finfo(jnp.float32).max | ||
|
||
|
||
class HillClimber(Strategy): | ||
def __init__( | ||
self, | ||
popsize: int, | ||
num_dims: Optional[int] = None, | ||
pholder_params: Optional[Union[chex.ArrayTree, chex.Array]] = None, | ||
mean_decay: float = 0.0, | ||
n_devices: Optional[int] = None, | ||
**fitness_kwargs: Union[bool, int, float] | ||
): | ||
"""Simple Gaussian Hill Climbing""" | ||
super().__init__( | ||
popsize, num_dims, pholder_params, mean_decay, n_devices, **fitness_kwargs | ||
) | ||
self.strategy_name = "HillClimber" | ||
|
||
@property | ||
def params_strategy(self) -> EvoParams: | ||
"""Return default parameters of evolution strategy.""" | ||
return EvoParams() | ||
|
||
def initialize_strategy(self, rng: chex.PRNGKey, params: EvoParams) -> EvoState: | ||
"""`initialize` the evolution strategy.""" | ||
initialization = jax.random.uniform( | ||
rng, | ||
(self.num_dims,), | ||
minval=params.init_min, | ||
maxval=params.init_max, | ||
) | ||
state = EvoState( | ||
mean=initialization, | ||
sigma=params.sigma_init * jnp.ones((self.num_dims,)), | ||
best_member=initialization, | ||
) | ||
return state | ||
|
||
def ask_strategy( | ||
self, rng: chex.PRNGKey, state: EvoState, params: EvoParams | ||
) -> Tuple[chex.Array, EvoState]: | ||
"""`ask` for new proposed candidates to evaluate next.""" | ||
# Sampling of N(0, 1) noise | ||
z = jax.random.normal( | ||
rng, | ||
(self.popsize, self.num_dims), | ||
) | ||
x = state.best_member + state.sigma.reshape(1, self.num_dims) * z | ||
return x, state | ||
|
||
def tell_strategy( | ||
self, | ||
x: chex.Array, | ||
fitness: chex.Array, | ||
state: EvoState, | ||
params: EvoParams, | ||
) -> EvoState: | ||
"""`tell` update to ES state.""" | ||
best_member, best_fitness = get_best_fitness_member(x, fitness, state, False) | ||
return state.replace(mean=best_member) |
Oops, something went wrong.