Пример #1
0
    def __init__(self,
                 num_batches=None,
                 batch_size=1,
                 duplicate_molecules=True,
                 duplicate_batches=True,
                 key_maker=Inchi(),
                 fitness_modifier=None,
                 random_seed=None):
        """
        Initialize a :class:`Roulette` instance.

        Parameters
        ----------
        num_batches : :class:`int`, optional
            The number of batches to yield. If ``None`` then yielding
            will continue forever or until the generator is exhausted,
            whichever comes first.

        batch_size : :class:`int`, optional
            The number of molecules yielded at once.

        duplicate_molecules : :class:`bool`, optional
            If ``True`` the same molecule can be yielded in more than
            one batch.

        duplicate_batches : :class:`bool`, optional
            If ``True`` the same batch can be yielded more than once.

        key_maker : :class:`.MoleculeKeyMaker`, optional
            Used to get the keys of molecules. If two molecules have
            the same key, they are considered duplicates.

        fitness_modifier : :class:`callable`, optional
            Takes the `population` on which :meth:`.select` is called
            and returns a :class:`dict`, which maps records in the
            `population` to the fitness values the :class:`.Selector`
            should use. If ``None``, the regular fitness values of the
            records are used.

        random_seed : :class:`int`, optional
            The random seed to use.

        """

        if num_batches is None:
            num_batches = float('inf')

        if fitness_modifier is None:
            fitness_modifier = self._get_fitness_values

        self._generator = np.random.RandomState(random_seed)
        self._duplicate_molecules = duplicate_molecules
        self._duplicate_batches = duplicate_batches
        self._num_batches = num_batches
        self._batch_size = batch_size
        super().__init__(
            key_maker=key_maker,
            fitness_modifier=fitness_modifier,
        )
Пример #2
0
    def __init__(
        self,
        num_batches=None,
        batch_size=1,
        duplicate_molecules=True,
        duplicate_batches=True,
        key_maker=Inchi(),
        fitness_modifier=None,
    ):
        """
        Initialize a :class:`.Worst` instance.

        Parameters
        ----------
        num_batches : :class:`int`, optional
            The number of batches to yield. If ``None`` then yielding
            will continue forever or until the generator is exhausted,
            whichever comes first.

        batch_size : :class:`int`, optional
            The number of molecules yielded at once.

        duplicate_molecules : :class:`bool`, optional
            If ``True`` the same molecule can be yielded in more than
            one batch.

        duplicate_batches : :class:`bool`, optional
            If ``True`` the same batch can be yielded more than once.
            Duplicate batches can occur if the same molecule is found
            multiple times in a population.

        key_maker : :class:`.MoleculeKeyMaker`, optional
            Used to get the keys of molecules, which are used to
            determine if two molecules are duplicates of each
            other.

        fitness_modifier : :class:`callable`, optional
            Takes the `population` on which :meth:`.select` is called
            and returns a :class:`dict`, which maps records in the
            `population` to the fitness values the :class:`.Selector`
            should use. If ``None``, the regular fitness values of the
            records are used.

        """

        if fitness_modifier is None:
            fitness_modifier = self._get_fitness_values

        self._duplicate_molecules = duplicate_molecules
        self._duplicate_batches = duplicate_batches
        self._num_batches = num_batches
        self._batch_size = batch_size
        super().__init__(
            key_maker=key_maker,
            fitness_modifier=fitness_modifier,
        )
Пример #3
0
    def __init__(
            self,
            building_blocks,
            is_replaceable,
            key_maker=Inchi(),
            name='SimilarBuildingBlock',
            random_seed=None,
    ):
        """
        Initialize a :class:`.SimilarBuildingBlock` instance.

        Parameters
        ----------
        building_blocks : :class:`tuple` of :class:`.BuildingBlock`
            A group of molecules which are used to replace building
            blocks in molecules being mutated.

        is_replaceable : :class:`callable`
            A function which takes a :class:`.BuildingBlock` and
            returns ``True`` or ``False``. This function is applied to
            every building block in the molecule being mutated.
            Building blocks which returned ``True`` are liable for
            substitution by one of the molecules in `building_blocks`.

        key_maker : :class:`.MoleculeKeyMaker`, optional
            Molecules which return the same key, will iterate through
            the same set of similar molecules.

        name : :class:`str`, optional
            A name to help identify the mutator instance.

        random_seed : :class:`bool`, optional
            The random seed to use.

        """

        self._building_blocks = building_blocks
        self._is_replaceable = is_replaceable
        self._key_maker = key_maker
        self._name = name
        self._generator = np.random.RandomState(random_seed)
        self._similar_building_blocks = {}
Пример #4
0
    def __init__(
        self,
        initial_population,
        fitness_calculator,
        mutator,
        crosser,
        generation_selector,
        mutation_selector,
        crossover_selector,
        fitness_normalizer=NullFitnessNormalizer(),
        key_maker=Inchi(),
        num_processes=None,
    ):
        """
        Initialize a :class:`EvolutionaryAlgorithm` instance.

        Parameters
        ----------
        initial_population : :class:`tuple` of :class:`.MoleculeRecord`
            The initial population the EA should use.

        fitness_calculator : :class:`.FitnessCalculator`
            Calculates fitness values.

        mutator : :class:`.MoleculeMutator`
            Carries out mutation operations.

        crosser : :class:`.MoleculeCrosser`
            Carries out crossover operations.

        generation_selector : :class:`.Selector`
            Selects the next generation.

        mutation_selector : :class:`.Selector`
            Selects molecules for mutation.

        crossover_selector : :class:`.Selector`
            Selects molecules for crossover.

        fitness_normalizer : :class:`.FitnessNormalizer`
            Normalizes fitness values.

        key_maker : :class:`.MoleculeKeyMaker`, optional
            Used to detect duplicate molecules in the EA. If two
            molecules in a generation return the same key, one of them
            is removed.

        num_processes : :class:`int`, optional
            The number of parallel processes the EA should create.
            If ``None``, all available cores will be used.

        """

        if num_processes == 1:
            self._implementation = Serial(
                initial_population=initial_population,
                fitness_calculator=fitness_calculator,
                mutator=mutator,
                crosser=crosser,
                generation_selector=generation_selector,
                mutation_selector=mutation_selector,
                crossover_selector=crossover_selector,
                fitness_normalizer=fitness_normalizer,
                key_maker=key_maker,
                logger=logger,
            )

        else:
            self._implementation = Parallel(
                initial_population=initial_population,
                fitness_calculator=fitness_calculator,
                mutator=mutator,
                crosser=crosser,
                generation_selector=generation_selector,
                mutation_selector=mutation_selector,
                crossover_selector=crossover_selector,
                fitness_normalizer=fitness_normalizer,
                key_maker=key_maker,
                logger=logger,
                num_processes=num_processes,
            )