Source code for qdpy.containers.populations

#    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 Fixed-capacity populations with FIFO eviction.

"""TODO"""

__all__ = ["Population"]

########### IMPORTS ########### {{{1
import numpy as np
from typing import Optional, Tuple, List, Iterable, Iterator, Any, TypeVar, Generic, Union, Sequence, MutableSet, MutableSequence, Type, Callable, Generator, MutableMapping, Mapping, overload

from qdpy.utils import *
from qdpy.phenotype import *
from qdpy.base import *
from qdpy.metrics import novelty, novelty_local_competition, novelty_nn
from .base import *


[docs] @registry.register class Population(Container): """Keep a fixed-size population in insertion order. A new genome at capacity evicts the oldest local entry after required parent admission succeeds. Equal genomes keep their existing entry. A parent-only offer leaves the population intact. """ def __init__(self, iterable: Optional[Iterable] = None, capacity: Optional[float] = None, **kwargs: Any) -> None: assert(capacity is not None and not np.isinf(capacity)) super().__init__(iterable, capacity=capacity, **kwargs) ## @brief Reserve FIFO eviction for a new local member at capacity. # @param individual Candidate offered to the population. # @param raise_if_not_added_to_parents Whether parent errors must propagate. # @param only_to_parents Offer the candidate to parent archives only. # @return A local storage index, or None for a parent-only offer. def _add_internal(self, individual: IndividualLike, raise_if_not_added_to_parents: bool, only_to_parents: bool) -> Optional[int]: replace_index = None if not only_to_parents and self.free < 1 and self.size and self._index_of(individual) is None: replace_index = 0 return super()._add_internal(individual, raise_if_not_added_to_parents, only_to_parents, replace_index)
## MODELINE "{{{1 ## vim:expandtab:softtabstop=4:shiftwidth=4:fileencoding=utf-8 ## vim:foldmethod=marker