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

Fix: issue 198 return metrics after initialisation of repertoires #203

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion examples/aurora.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@
"\n",
"# init step of the aurora algorithm\n",
"key, subkey = jax.random.split(key)\n",
"repertoire, emitter_state, aurora_extra_info = aurora.init(\n",
"repertoire, emitter_state, init_metrics, aurora_extra_info = aurora.init(\n",
" init_variables,\n",
" aurora_extra_info,\n",
" jnp.asarray(l_value_init),\n",
Expand Down
2 changes: 1 addition & 1 deletion examples/cmame.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@
"outputs": [],
"source": [
"key, subkey = jax.random.split(key)\n",
"repertoire, emitter_state = map_elites.init(initial_population, centroids, subkey)"
"repertoire, emitter_state, init_metrics = map_elites.init(initial_population, centroids, subkey)"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion examples/cmamega.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@
"outputs": [],
"source": [
"key, subkey = jax.random.split(key)\n",
"repertoire, emitter_state = map_elites.init(initial_population, centroids, subkey)"
"repertoire, emitter_state, init_metrics = map_elites.init(initial_population, centroids, subkey)"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion examples/dcrlme.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@
"\n",
"# compute initial repertoire\n",
"key, subkey = jax.random.split(key)\n",
"repertoire, emitter_state = map_elites.init(\n",
"repertoire, emitter_state, init_metrics = map_elites.init(\n",
" init_params, centroids, subkey\n",
")"
]
Expand Down
2 changes: 1 addition & 1 deletion examples/distributed_mapelites.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@
")\n",
"\n",
"# get initial elements\n",
"repertoire, emitter_state = map_elites.get_distributed_init_fn(\n",
"repertoire, emitter_state, init_metrics = map_elites.get_distributed_init_fn(\n",
" centroids=centroids,\n",
" devices=devices,\n",
")(genotypes=init_variables, key=keys)"
Expand Down
4 changes: 2 additions & 2 deletions examples/jumanji_snake.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@
" )\n",
"\n",
" key, subkey = jax.random.split(key)\n",
" repertoire, emitter_state = algo_instance.init(\n",
" repertoire, emitter_state, init_metrics = algo_instance.init(\n",
" init_variables, population_size, subkey\n",
" )\n",
"\n",
Expand All @@ -402,7 +402,7 @@
"\n",
" # Compute initial repertoire and emitter state\n",
" key, subkey = jax.random.split(key)\n",
" repertoire, emitter_state = algo_instance.init(init_variables, centroids, subkey)"
" repertoire, emitter_state, init_metrics = algo_instance.init(init_variables, centroids, subkey)"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion examples/mapelites.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@
"\n",
"# Compute initial repertoire and emitter state\n",
"key, subkey = jax.random.split(key)\n",
"repertoire, emitter_state = map_elites.init(init_variables, centroids, subkey)"
"repertoire, emitter_state, init_metrics = map_elites.init(init_variables, centroids, subkey)"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion examples/me_sac_pbt.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@
")(training_states)\n",
"\n",
"# initialize map-elites\n",
"repertoire, emitter_state = map_elites.get_distributed_init_fn(\n",
"repertoire, emitter_state, init_metrics = map_elites.get_distributed_init_fn(\n",
" devices=devices, centroids=centroids\n",
")(genotypes=training_states, key=keys)"
]
Expand Down
2 changes: 1 addition & 1 deletion examples/me_td3_pbt.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@
")(training_states)\n",
"\n",
"# initialize map-elites\n",
"repertoire, emitter_state = map_elites.get_distributed_init_fn(\n",
"repertoire, emitter_state, init_metrics = map_elites.get_distributed_init_fn(\n",
" devices=devices, centroids=centroids\n",
")(genotypes=training_states, key=keys)"
]
Expand Down
2 changes: 1 addition & 1 deletion examples/mees.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@
"\n",
"# compute initial repertoire\n",
"key, subkey = jax.random.split(key)\n",
"repertoire, emitter_state = map_elites.init(\n",
"repertoire, emitter_state, init_metrics = map_elites.init(\n",
" init_variables, centroids, subkey\n",
")"
]
Expand Down
2 changes: 1 addition & 1 deletion examples/mels.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@
"\n",
"# Compute initial repertoire and emitter state\n",
"key, subkey = jax.random.split(key)\n",
"repertoire, emitter_state = mels.init(init_variables, centroids, subkey)"
"repertoire, emitter_state, init_metrics = mels.init(init_variables, centroids, subkey)"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion examples/mome.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@
"outputs": [],
"source": [
"key, subkey = jax.random.split(key)\n",
"repertoire, emitter_state = mome.init(\n",
"repertoire, emitter_state, init_metrics = mome.init(\n",
" genotypes,\n",
" centroids,\n",
" pareto_front_max_length,\n",
Expand Down
4 changes: 2 additions & 2 deletions examples/nsga2_spea2.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@
"\n",
"# init nsga2\n",
"key, subkey = jax.random.split(key)\n",
"repertoire, emitter_state = nsga2.init(\n",
"repertoire, emitter_state, init_metrics = nsga2.init(\n",
" genotypes,\n",
" population_size,\n",
" subkey\n",
Expand Down Expand Up @@ -296,7 +296,7 @@
"\n",
"# init spea2\n",
"key, subkey = jax.random.split(key)\n",
"repertoire, emitter_state = spea2.init(\n",
"repertoire, emitter_state, init_metrics = spea2.init(\n",
" genotypes,\n",
" population_size,\n",
" num_neighbours,\n",
Expand Down
2 changes: 1 addition & 1 deletion examples/omgmega.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@
"outputs": [],
"source": [
"key, subkey = jax.random.split(key)\n",
"repertoire, emitter_state = map_elites.init(initial_population, centroids, subkey)"
"repertoire, emitter_state, init_metrics = map_elites.init(initial_population, centroids, subkey)"
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion examples/pga_aurora.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -467,7 +467,7 @@
"\n",
"# init step of the aurora algorithm\n",
"key, subkey = jax.random.split(key)\n",
"repertoire, emitter_state, aurora_extra_info = aurora.init(\n",
"repertoire, emitter_state, init_metrics, aurora_extra_info = aurora.init(\n",
" init_variables,\n",
" aurora_extra_info,\n",
" jnp.asarray(l_value_init),\n",
Expand Down
2 changes: 1 addition & 1 deletion examples/pgame.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@
"\n",
"# compute initial repertoire\n",
"key, subkey = jax.random.split(key)\n",
"repertoire, emitter_state = map_elites.init(\n",
"repertoire, emitter_state, init_metrics = map_elites.init(\n",
" init_variables, centroids, subkey\n",
")"
]
Expand Down
2 changes: 1 addition & 1 deletion examples/qdpg.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@
"\n",
"# compute initial repertoire\n",
"key, subkey = jax.random.split(key)\n",
"repertoire, emitter_state = map_elites.init(\n",
"repertoire, emitter_state, init_metrics = map_elites.init(\n",
" init_variables, centroids, subkey\n",
")"
]
Expand Down
7 changes: 5 additions & 2 deletions qdax/baselines/genetic_algorithm.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def __init__(
@partial(jax.jit, static_argnames=("self", "population_size"))
def init(
self, genotypes: Genotype, population_size: int, key: RNGKey
) -> Tuple[GARepertoire, Optional[EmitterState]]:
) -> Tuple[GARepertoire, Optional[EmitterState], Metrics]:
"""Initialize a GARepertoire with an initial population of genotypes.

Args:
Expand Down Expand Up @@ -73,7 +73,10 @@ def init(
extra_scores=extra_scores,
)

return repertoire, emitter_state
# calculate the initial metrics
metrics = self._metrics_function(repertoire)

return repertoire, emitter_state, metrics

@partial(jax.jit, static_argnames=("self",))
def update(
Expand Down
9 changes: 6 additions & 3 deletions qdax/baselines/nsga2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from qdax.baselines.genetic_algorithm import GeneticAlgorithm
from qdax.core.containers.nsga2_repertoire import NSGA2Repertoire
from qdax.core.emitters.emitter import EmitterState
from qdax.custom_types import Genotype, RNGKey
from qdax.custom_types import Genotype, Metrics, RNGKey


class NSGA2(GeneticAlgorithm):
Expand All @@ -29,7 +29,7 @@ class NSGA2(GeneticAlgorithm):
@partial(jax.jit, static_argnames=("self", "population_size"))
def init(
self, genotypes: Genotype, population_size: int, key: RNGKey
) -> Tuple[NSGA2Repertoire, Optional[EmitterState]]:
) -> Tuple[NSGA2Repertoire, Optional[EmitterState], Metrics]:

# score initial genotypes
key, subkey = jax.random.split(key)
Expand Down Expand Up @@ -62,4 +62,7 @@ def init(
extra_scores=extra_scores,
)

return repertoire, emitter_state
# calculate the initial metrics
metrics = self._metrics_function(repertoire)

return repertoire, emitter_state, metrics
9 changes: 6 additions & 3 deletions qdax/baselines/spea2.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from qdax.baselines.genetic_algorithm import GeneticAlgorithm
from qdax.core.containers.spea2_repertoire import SPEA2Repertoire
from qdax.core.emitters.emitter import EmitterState
from qdax.custom_types import Genotype, RNGKey
from qdax.custom_types import Genotype, Metrics, RNGKey


class SPEA2(GeneticAlgorithm):
Expand All @@ -37,7 +37,7 @@ def init(
population_size: int,
num_neighbours: int,
key: RNGKey,
) -> Tuple[SPEA2Repertoire, Optional[EmitterState]]:
) -> Tuple[SPEA2Repertoire, Optional[EmitterState], Metrics]:

# score initial genotypes
key, subkey = jax.random.split(key)
Expand Down Expand Up @@ -71,4 +71,7 @@ def init(
extra_scores=extra_scores,
)

return repertoire, emitter_state
# calculate the initial metrics
metrics = self._metrics_function(repertoire)

return repertoire, emitter_state, metrics
9 changes: 7 additions & 2 deletions qdax/core/aurora.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ def init(
l_value: jnp.ndarray,
max_size: int,
key: RNGKey,
) -> Tuple[UnstructuredRepertoire, Optional[EmitterState], AuroraExtraInfo]:
) -> Tuple[
UnstructuredRepertoire, Optional[EmitterState], Metrics, AuroraExtraInfo
]:
"""Initialize an unstructured repertoire with an initial population of
genotypes. Also performs the first training of the AURORA encoder.

Expand Down Expand Up @@ -174,7 +176,10 @@ def init(
repertoire, aurora_extra_info.model_params, iteration=0, key=key
)

return repertoire, emitter_state, updated_aurora_extra_info
# calculate the initial metrics
metrics = self._metrics_function(repertoire)

return repertoire, emitter_state, metrics, updated_aurora_extra_info

@partial(jax.jit, static_argnames=("self",))
def update(
Expand Down
6 changes: 4 additions & 2 deletions qdax/core/distributed_map_elites.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def init(
genotypes: Genotype,
centroids: Centroid,
key: RNGKey,
) -> Tuple[MapElitesRepertoire, Optional[EmitterState]]:
) -> Tuple[MapElitesRepertoire, Optional[EmitterState], Metrics]:
"""
Initialize a Map-Elites repertoire with an initial population of genotypes.
Requires the definition of centroids that can be computed with any method
Expand Down Expand Up @@ -80,8 +80,10 @@ def init(
descriptors=descriptors,
extra_scores=extra_scores,
)
# calculate the initial metrics
metrics = self._metrics_function(repertoire)

return repertoire, emitter_state
return repertoire, emitter_state, metrics

@partial(jax.jit, static_argnames=("self",))
def update(
Expand Down
7 changes: 5 additions & 2 deletions qdax/core/map_elites.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def init(
genotypes: Genotype,
centroids: Centroid,
key: RNGKey,
) -> Tuple[MapElitesRepertoire, Optional[EmitterState]]:
) -> Tuple[MapElitesRepertoire, Optional[EmitterState], Metrics]:
"""
Initialize a Map-Elites repertoire with an initial population of genotypes.
Requires the definition of centroids that can be computed with any method
Expand Down Expand Up @@ -96,7 +96,10 @@ def init(
extra_scores=extra_scores,
)

return repertoire, emitter_state
# calculate the initial metrics
metrics = self._metrics_function(repertoire)

return repertoire, emitter_state, metrics

@partial(jax.jit, static_argnames=("self",))
def update(
Expand Down
8 changes: 6 additions & 2 deletions qdax/core/mels.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def init(
genotypes: Genotype,
centroids: Centroid,
key: RNGKey,
) -> Tuple[MELSRepertoire, Optional[EmitterState]]:
) -> Tuple[MELSRepertoire, Optional[EmitterState], Metrics]:
"""Initialize a MAP-Elites Low-Spread repertoire with an initial
population of genotypes. Requires the definition of centroids that can
be computed with any method such as CVT or Euclidean mapping.
Expand Down Expand Up @@ -107,4 +107,8 @@ def init(
descriptors=descriptors,
extra_scores=extra_scores,
)
return repertoire, emitter_state

# calculate the initial metrics
metrics = self._metrics_function(repertoire)

return repertoire, emitter_state, metrics
9 changes: 6 additions & 3 deletions qdax/core/mome.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from qdax.core.containers.mome_repertoire import MOMERepertoire
from qdax.core.emitters.emitter import EmitterState
from qdax.core.map_elites import MAPElites
from qdax.custom_types import Centroid, RNGKey
from qdax.custom_types import Centroid, Metrics, RNGKey


class MOME(MAPElites):
Expand All @@ -27,7 +27,7 @@ def init(
centroids: Centroid,
pareto_front_max_length: int,
key: RNGKey,
) -> Tuple[MOMERepertoire, Optional[EmitterState]]:
) -> Tuple[MOMERepertoire, Optional[EmitterState], Metrics]:
"""Initialize a MOME grid with an initial population of genotypes. Requires
the definition of centroids that can be computed with any method such as
CVT or Euclidean mapping.
Expand Down Expand Up @@ -68,4 +68,7 @@ def init(
extra_scores=extra_scores,
)

return repertoire, emitter_state
# calculate the initial metrics
metrics = self._metrics_function(repertoire)

return repertoire, emitter_state, metrics
4 changes: 3 additions & 1 deletion tests/baselines_test/cmame_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,9 @@ def metrics_fn(repertoire: MapElitesRepertoire) -> Dict[str, jnp.ndarray]:
)

key, subkey = jax.random.split(key)
repertoire, emitter_state = map_elites.init(initial_population, centroids, subkey)
repertoire, emitter_state, init_metrics = map_elites.init(
initial_population, centroids, subkey
)

(
repertoire,
Expand Down
4 changes: 3 additions & 1 deletion tests/baselines_test/cmamega_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ def metrics_fn(repertoire: MapElitesRepertoire) -> Dict[str, jnp.ndarray]:
)

key, subkey = jax.random.split(key)
repertoire, emitter_state = map_elites.init(initial_population, centroids, subkey)
repertoire, emitter_state, init_metrics = map_elites.init(
initial_population, centroids, subkey
)

(
repertoire,
Expand Down
4 changes: 3 additions & 1 deletion tests/baselines_test/dcrlme_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,9 @@ def play_step_fn(

# compute initial repertoire
key, subkey = jax.random.split(key)
repertoire, emitter_state = map_elites.init(init_params, centroids, subkey)
repertoire, emitter_state, init_metrics = map_elites.init(
init_params, centroids, subkey
)

@jax.jit
def update_scan_fn(carry: Any, key: RNGKey) -> Any:
Expand Down
Loading
Loading