# This file is part of qdpy.
#
# qdpy is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
#
# qdpy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with qdpy. If not, see <http://www.gnu.org/licenses/>.
## @file
# @brief CMA-ME emitters and UCB1-based emitter selection.
"""This file contains the CMA-ME emitters, as defined in paper "Covariance Matrix Adaptation for the Rapid Illumination of Behavior Space"(https://arxiv.org/abs/1912.02400). Additional algorithms are also included, notably the ME-MAP-Elites algorithm (Multi-Emitter MAP-Elites), as presented in the paper "Multi-Emitter MAP-Elites: Improving quality, diversity and convergence speed with heterogeneous sets of emitters". All emitters are implemented as subclasses of the CMA-ES class, so the library "cma" needs to be installed to use them. """
__all__ = ["CMAMEOptimizingEmitter", "CMAMERandomDirectionEmitter", "CMAMEImprovementEmitter", "MEMAPElitesUCB1"]
from typing import Optional, Tuple, List, Iterable, Iterator, Any, TypeVar, Generic, Union, Sequence, MutableSet, MutableSequence, Type, Callable, Generator, Mapping, MutableMapping, overload
import numpy as np
import random
from .base import *
from .evolution import *
from qdpy.utils import *
from qdpy.phenotype import *
from qdpy.base import *
from qdpy.containers import *
from qdpy import tools
try:
import cma
except ImportError:
pass
[docs]
@registry.register
class CMAMEOptimizingEmitter(CMAES):
"""Run single-objective CMA-ES and restart from an archive entry.
Use the ``cma`` extra. When the backend reaches a stopping
condition, select a centre uniformly from the retained genomes.
An empty archive uses the zero genome. The objective and loss
conventions are the same as those of CMAES.
"""
# def __init__(self, container: Container, budget: int,
# dimension: int, **kwargs):
# super().__init__(container, budget, dimension=dimension, **kwargs)
def reinit(self) -> None:
super().reinit()
self.restart()
def restart(self):
"""Replace the pycma strategy and clear its buffered feedback."""
if len(self.container):
init_ind = random.choice(self.container)
else:
init_ind = [0.] * self.dimension
self.es = cma.CMAEvolutionStrategy(init_ind, self.sigma0, self._opts)
self._pop_inds.clear()
self._pop_fitness_vals.clear()
def _stop(self) -> bool:
term_cond = self.es.stop(check=False)
#if len(term_cond) > 0:
# print(f"RESTART!!!: {term_cond}")
return len(term_cond) > 0
def _internal_ask(self, base_ind: IndividualLike) -> IndividualLike:
if self._stop():
self.restart()
return super()._internal_ask(base_ind)
[docs]
@registry.register
class CMAMERandomDirectionEmitter(CMAMEOptimizingEmitter):
"""Use a projection of behaviour descriptors as scalar CMA feedback.
A restart samples a direction using the descriptor domain. The
feedback population is formed from locally admitted candidates and
passed to pycma after projection. This emitter uses descriptor
values on the individuals and supports a multiobjective archive.
"""
_requires_single_objective = False
def restart(self):
super().restart()
features_domain = np.array(self.container.features_domain)
self._direction = np.random.normal(0., 1., features_domain.shape[0])
self._direction = features_domain[:,0] + self._direction * (features_domain[:,1] - features_domain[:,0])
self._features_mean = np.zeros(features_domain.shape[0])
def _internal_tell(self, individual: IndividualLike, added_to_container: bool, xattr: Mapping[str, Any] = {}) -> None:
if self.ignore_if_not_added_to_container and not added_to_container:
return
self._features_mean += np.array(individual.features)
if added_to_container:
self._pop_inds += [individual]
if len(self._pop_inds) >= self.es.popsize:
self._features_mean /= self.es.popsize
_pop_delta = []
for ind in self._pop_inds:
dv = ind.features - self._features_mean
_pop_delta.append(self._direction.dot(dv))
try:
self.es.tell(self._pop_inds, _pop_delta)
except RuntimeError:
pass
else:
self._pop_inds.clear()
self._pop_fitness_vals.clear()
[docs]
@registry.register
class CMAMEImprovementEmitter(CMAMEOptimizingEmitter):
"""Adapt search using cell coverage and signed fitness gains.
Read the previous cell representative before insertion. A candidate
that fills an empty cell receives priority. For a filled cell, use
the difference between candidate and previous weighted objectives.
Sum weighted objectives into scalar feedback for pycma.
"""
_requires_single_objective = False
_novel_pop_inds: MutableSequence[IndividualLike]
_novel_pop_fitness_vals: MutableSequence[Any]
def restart(self):
super().restart()
self._novel_pop_inds = []
self._novel_pop_fitness_vals = []
## @brief Measure novelty and quality gain against the still-current cell.
# @param individual Evaluated candidate proposed for admission.
# @return Feedback metadata containing novelty and signed gain.
# @details A filled cell contributes its previous weighted fitness
# before the archive can replace it. The fitness weights control
# the direction of improvement.
def _tell_before_container_update(self, individual: IndividualLike) -> Tuple[MutableMapping[str, Any], bool]:
novel = False
previous_fitness = None
if hasattr(self.container, "index_grid") and hasattr(self.container, "quality"):
try:
index = self.container.index_grid(individual)
previous_fitness = self.container.quality[index]
except (IndexError, KeyError):
pass
else:
novel = previous_fitness is None
delta = sum(self._weighted_fitness(self.container.get_ind_fitness(individual)))
if previous_fitness is not None:
delta -= sum(self._weighted_fitness(previous_fitness))
return {"novel": novel, "delta": delta}, True
def _internal_tell(self, individual: IndividualLike, added_to_container: bool, xattr: Mapping[str, Any] = {}) -> None:
if self.ignore_if_not_added_to_container and not added_to_container:
return
novel = xattr.get("novel", False)
delta = xattr.get("delta", 0.)
if added_to_container:
if novel:
self._novel_pop_inds += [individual]
self._novel_pop_fitness_vals += [-1. * delta]
else:
self._pop_inds += [individual]
self._pop_fitness_vals += [-1. * delta]
if len(self._pop_inds) + len(self._novel_pop_inds) >= self.es.popsize:
tot_pop = []
tot_pop_fitness_vals = []
# Sort individuals
sorted_pop_inds = [self._pop_inds[k] for k in argsort(self._pop_fitness_vals)]
#sorted_pop_fitness_vals = sorted(self._pop_fitness_vals)
sorted_novel_pop_inds = [self._novel_pop_inds[k] for k in argsort(self._novel_pop_fitness_vals)]
#sorted_novel_pop_fitness_vals = sorted(self._novel_pop_fitness_vals)
for i, ind in enumerate(sorted_novel_pop_inds):
tot_pop.append(ind)
tot_pop_fitness_vals.append(i)
for i, ind in enumerate(sorted_pop_inds):
tot_pop.append(ind)
tot_pop_fitness_vals.append(len(sorted_novel_pop_inds) + i)
try:
self.es.tell(tot_pop, tot_pop_fitness_vals)
except RuntimeError:
pass
else:
self._pop_inds.clear()
self._pop_fitness_vals.clear()
self._novel_pop_inds.clear()
self._novel_pop_fitness_vals.clear()
[docs]
@registry.register
class MEMAPElitesUCB1(AlgWrapper):
"""Choose active child emitters with an upper confidence bound.
The reward estimate combines each emitter's fraction of evaluations
that updated the archive with an exploration term based on evaluation
counts. Untried emitters receive ``initial_expected_rwds``.
:param algorithms: Available child emitters.
:param zeta: Coefficient of the UCB1 exploration term.
:param nb_active_emitters: Number of emitters retained per selection.
:param initial_expected_rwds: Reward estimate for a child with no evaluations.
:param shuffle_emitters: Shuffle the candidate order before ranking.
"""
zeta: float
nb_active_emitters: int
_active_emitters_idx: Sequence[int]
current_active_idx: int
initial_expected_rwds: float
shuffle_emitters: bool
def __init__(self, algorithms: Any, zeta: float = 0.0005, nb_active_emitters: int = 1, initial_expected_rwds: float = 1.,
shuffle_emitters: bool = True, **kwargs):
self.zeta = zeta
self.nb_active_emitters = nb_active_emitters
self.initial_expected_rwds = initial_expected_rwds
self.shuffle_emitters = shuffle_emitters
super().__init__(algorithms, **kwargs)
def reinit(self) -> None:
super().reinit()
self._up_active_emitters()
self.current_active_idx = 0
## @brief Rank emitters using their accumulated archive update rates.
# @details Sum the children's evaluation counts for the logarithm.
# Use a floor of one so warm-started emitters can be ranked before
# this wrapper has recorded its first evaluation.
def _up_active_emitters(self) -> None:
"""Update the list of active emitters."""
#expected_rwds = [
# self.initial_expected_rwds if alg.nb_evaluations == 0 else
# float(alg.nb_updated) / float(alg.nb_evaluations) + self.zeta * math.sqrt(math.log(self.nb_evaluations) / float(alg.nb_evaluations))
# for alg in self.algorithms]
#self._active_emitters_idx = argsort(expected_rwds, reverse=True)[:self.nb_active_emitters]
algos_idx = list(range(len(self.algorithms)))
if self.shuffle_emitters:
# Shuffle algorithm list
random.shuffle(algos_idx)
total_evals = max(1, sum(algo.nb_evaluations for algo in self.algorithms))
# Compute expected rewards
expected_rwds = [
self.initial_expected_rwds if self.algorithms[a].nb_evaluations == 0 else
float(self.algorithms[a].nb_updated) / float(self.algorithms[a].nb_evaluations) +
self.zeta * math.sqrt(math.log(total_evals) / self.algorithms[a].nb_evaluations)
for a in algos_idx]
# Update active emitters indexes list
self._active_emitters_idx = [algos_idx[a] for a in argsort(expected_rwds, reverse=True)][:self.nb_active_emitters]
#print(f"DEBUG _up_active_emitters: {argsort(expected_rwds, reverse=True)}")
#print(f"DEBUG _up_active_emitters: {expected_rwds}")
#print(f"DEBUG _up_active_emitters: {self._active_emitters_idx}")
def next(self, verbose: bool = False) -> None:
"""Switch to the next algorithm in Sequence `self.algorithms`, if there is one."""
if self.current_active_idx < len(self._active_emitters_idx) - 1:
self.current_active_idx += 1
else:
self._up_active_emitters()
self.current_active_idx = 0
next_idx = self._active_emitters_idx[self.current_active_idx]
self.switch_to(next_idx, verbose=verbose)
#print(f"NEXT: {next_idx}")
# MODELINE "{{{1
# vim:expandtab:softtabstop=4:shiftwidth=4:fileencoding=utf-8
# vim:foldmethod=marker