@File : evolutionary.py @Time : 2026/03/25 12:20:42 @Author : Alejandro Marrero (amarrerd@ull.edu.es) @Version : 1.0 @Contact : amarrerd@ull.edu.es @License : (C)Copyright 2026, Alejandro Marrero @Desc : None

ES

Bases: BaseGenerator

Quality-Diversity instance generator based on an Evolutionary Strategy (CMA-ES).

Unlike :class:Evolutionary, which evolves Instance genotypes directly with genetic operators, ES evolves a lower-dimensional latent representation (of size generator_dimension) using CMA-ES (Covariance Matrix Adaptation Evolution Strategy, via the cma package). Each sampled latent vector is decoded into one or more instance descriptors/genotypes through descriptor_pipe (which, for this generator, must operate with "instance" as its key), evaluated against the solver portfolio, and scored for diversity against one or more archives.

ES supports maintaining several archives simultaneously: the first archive in archives is always updated, while any additional archives are updated only with the feasible subset of individuals (when keep_only_feasible is True) or with all valid individuals otherwise. CMA-ES is told the negated fitness of each sampled point (since CMA-ES minimises by convention), with infeasible/out-of-bounds points penalised with negative infinity fitness so they are effectively never favoured.

Source code in digneapy/generators/evolutionary.py
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
class ES(BaseGenerator):  # pragma: no cover
    """Quality-Diversity instance generator based on an Evolutionary Strategy (CMA-ES).

    Unlike :class:`Evolutionary`, which evolves ``Instance`` genotypes
    directly with genetic operators, ``ES`` evolves a lower-dimensional
    latent representation (of size ``generator_dimension``) using CMA-ES
    (Covariance Matrix Adaptation Evolution Strategy, via the ``cma``
    package). Each sampled latent vector is decoded into one or more
    instance descriptors/genotypes through ``descriptor_pipe`` (which, for
    this generator, must operate with ``"instance"`` as its key), evaluated
    against the solver portfolio, and scored for diversity against one or
    more archives.

    ``ES`` supports maintaining several archives simultaneously: the first
    archive in ``archives`` is always updated, while any additional archives
    are updated only with the feasible subset of individuals (when
    ``keep_only_feasible`` is True) or with all valid individuals otherwise.
    CMA-ES is told the negated fitness of each sampled point (since CMA-ES
    minimises by convention), with infeasible/out-of-bounds points penalised
    with negative infinity fitness so they are effectively never favoured.
    """

    def __init__(
        self,
        ask_fn: AskFn,
        dimension: int,
        domain: Domain,
        portfolio: Sequence[Solver],
        lambda_: np.uint32 | int,
        archives: Sequence[Archive],
        keep_only_feasible: bool = True,
        sigma: float = 0.5,
        performance_function: PerformanceFn = maximise_perf_gap_easy,
        generations: np.uint32 | int = 1_000,
        repetitions: np.uint16 | int = 1,
        descriptor_pipe: DescriptorPipeline = DescriptorPipeline("instance"),
        seed: Optional[int | np.random.SeedSequence] = None,
        phi: float | np.float64 = 0.85,
        workers: int = 1,
    ):
        """Creates a Evolutionary Instance Generator based on Novelty Search
        The generator uses a set of solvers to evaluate the instances and
        a novelty search algorithm to guide the evolution of the instances.

        Args:
            domain (Domain): Domain for which the instances are generated for.
            portfolio (Sequence[Solver]): Sequence item of callable objects that can evaluate a instance.
            pop_size (int, optional): Number of instances in the population to evolve. Defaults to 100.
            archives (Sequence[Archive]): Containers to store diverse instances.
            performance_function (PerformanceFn, optional): Performance function to calculate the performance score. Defaults to max_gap_target.
            generations (int, optional): Number of generations to perform. Defaults to 1000.
            repetitions (int, optional): Number times a solver in the portfolio must be run over the same instance. Defaults to 1.
            describe_by (DESCRIPTORS, optional): _Descriptor used to calculate the diversity. The options available are defined in the dictionary digneapy.DESCRIPTORS. Defaults to "features".
            seed (int, optional): Seed for the RNG protocol. Defaults to 42.
            phi (float, optional): Phi balance value for the weighted fitness function. Defaults to 0.85.

        Raises:
            ValueError: If ``descriptor_pipe`` has no configured transformers.
            ValueError: If ``descriptor_pipe`` is not configured with
                ``"instance"`` as its key (the only mode currently supported
                by ``ES``).
            ValueError: If ``workers`` is negative.
            TypeError: If any element of ``archives`` is not an instance of
                ``Archive``.
            KeyError: Raises error if the descriptor strategy is not available in the DESCRIPTORS dictionary
        """
        if len(descriptor_pipe._transformers) == 0:
            raise ValueError(
                "ES is expected to be use with a DescriptorPipeline that includes at least one transformer object."
            )
        if descriptor_pipe._key != "instance":
            raise ValueError(
                "ES is expected to be use with a DescriptorPipeline that uses instance as key. Performance or Features not allowed yet!"
            )

        super().__init__(
            domain=domain,
            portfolio=portfolio,
            pop_size=lambda_,
            performance_function=performance_function,
            descriptor_pipe=descriptor_pipe,
            generations=generations,
            repetitions=repetitions,
        )
        if any(param < 0 for param in (workers, dimension)):
            raise ValueError(
                f"dimension ({dimension}) and workers ({workers}) cannot be negative"
            )
        if any(not isinstance(archive, Archive) for archive in archives):
            raise TypeError("archives must be a subclass of Archive")

        self._ask_fn = ask_fn
        self._generator_dimension = dimension
        self._archives = archives
        self._sigma = sigma
        self._phi = float(phi)
        self._workers = workers
        self.seed = (
            seed
            if isinstance(seed, np.random.SeedSequence)
            else np.random.SeedSequence(seed)
        )

        self._rng = np.random.default_rng(self.seed)
        self._keep_feasible = keep_only_feasible

    def _compute_fitness(
        self, performance_biases: np.ndarray, diversity_scores: np.ndarray
    ) -> np.ndarray:
        """Calculates the fitness of each instance in the population

        Fitness is a fixed 50/50 convex combination of performance bias and
        diversity score: ``fitness = 0.5 * performance_bias + 0.5 * diversity_score``.
        Unlike :meth:`Evolutionary._compute_fitness`, the balance here is not
        configurable via a ``phi`` attribute and is hard-coded to an equal
        weighting.

        Args:
            performance_biases (np.ndarray): Performance biases or scores of each instance
            novelty_scores (np.ndarray): Novelty scores of each instance

        Returns:
            fitness of each instance (np.ndarray)
        """
        phi_r = 1.0 - self._phi
        fitness = np.zeros(len(performance_biases))
        fitness = (performance_biases * self._phi) + (diversity_scores * phi_r)
        return fitness

    def _update_archive(
        self,
        archive: Archive,
        individuals: np.ndarray,
        descriptors: np.ndarray,
        portfolio_scores: np.ndarray,
        diversity: np.ndarray,
        bias_score: np.ndarray,
        fitness: np.ndarray,
    ):
        """Build ``Instance`` objects from raw attributes and insert them into an archive.

        This helper centralises the pattern of packaging the per-individual
        attributes produced during a generation step (genotype, descriptor,
        fitness, solver scores, diversity, and performance bias) into
        ``Instance`` objects via ``build_instances_from_attributes``, and then
        extending the given ``archive`` with those instances.

        Args:
            archive (Archive): The archive to update.
            individuals (np.ndarray): Genotypes of the individuals to add.
            descriptors (np.ndarray): Descriptors associated with each
                individual, used both to build the instances and as the
                archive's novelty-comparison key.
            portfolio_scores (np.ndarray): Per-solver scores obtained when
                evaluating each individual.
            diversity (np.ndarray): Novelty/diversity score of each
                individual with respect to ``archive``.
            bias_score (np.ndarray): Performance bias of each individual.
            fitness (np.ndarray): Combined fitness of each individual.

        Returns:
            np.ndarray: The ``Instance`` objects built from the supplied
                attributes (the same objects inserted into ``archive``).
        """

        instances = build_instances_from_attributes(
            genotypes=individuals,
            descriptors=descriptors,
            fitness=fitness,
            portfolio_scores=portfolio_scores,
            diversity_scores=diversity,
            bias_score=bias_score,
        )
        archive.extend(
            instances=instances,
            descriptors=individuals,
            novelty_scores=diversity,
            objectives=bias_score,
        )
        return instances

    def __call__(
        self, verbose: bool = False
    ) -> Tuple[GenerationResult, Sequence[Archive]]:
        """Run the CMA-ES-driven generation process and return the results.

        The algorithm proceeds as follows:
        1. A random initial mean vector ``_x0`` of dimension
           ``self._generator_dimension`` is sampled, and a
           ``cma.CMAEvolutionStrategy`` is initialised with it, the configured
           ``sigma`` (step size), and a population size equal to
           ``self._pop_size`` (i.e. ``lambda_``).
        2. The domain's variable bounds are extracted once as ``mn``/``mx``.
        3. For each generation (up to ``self._generations``):
            - CMA-ES proposes a batch of latent vectors via ``strategy.ask()``;
              these are treated as raw descriptors.
            - The descriptor pipeline decodes the descriptors into concrete
              instance genotypes via ``self._descriptor_pipe``.
            - Genotypes outside the domain's bounds are filtered out via
              ``valid_mask``, since decoders (e.g. for the Knapsack domain)
              may produce out-of-range values.
            - The valid genotypes are evaluated against the solver portfolio
              to obtain performance biases and per-solver scores.
            - Diversity scores are computed against ``self._archives[0]``; if
              that archive does not support novelty scoring
              (``NotImplementedError``), diversity defaults to zero.
            - Fitness is computed via :meth:`_compute_fitness` and the
              primary archive is updated via :meth:`_update_archive`.
            - If more than one archive is configured, the remaining archives
              are updated with either the feasible subset (performance bias
              > 0, when ``self._keep_feasible`` is True) or all valid
              individuals, each scored for diversity against its own archive
              before insertion.
            - Progress is recorded in ``self._logbook``.
            - CMA-ES is informed of the fitness of every sampled point
              (including invalid ones, which receive ``-inf`` fitness so they
              are never preferred) via ``strategy.tell``, using the negated
              fitness since CMA-ES minimises.
        4. After all generations, the resulting instances are taken from
           ``self._archives[0]`` if only one archive was configured, or from
           ``self._archives[1]`` otherwise, and packaged into a
           :class:`GenerationResult`.

        Args:
            verbose (bool, optional): Whether to print/log progress during the
                run (forwarded to ``self._logbook.update``). Defaults to False.

        Returns:
            Tuple[GenerationResult, Sequence[Archive]]: A two-element tuple
                containing the packaged generation result (solver names,
                selected instances, and history) and the full sequence of
                archives maintained throughout the run.
        """
        import cma

        _x0 = self._rng.uniform(
            size=(self._generator_dimension),
        )
        strategy = cma.CMAEvolutionStrategy(
            _x0,
            sigma0=self._sigma,
            inopts={
                "popsize": self._pop_size,
            },
        )
        _cma_asker = CMAAsker(
            strategy=strategy,
            descriptor_pipeline=self._descriptor_pipe,
            domain=self._domain,
            ask_fn=self._ask_fn,
        )

        _current_generation = 0
        mn, mx = np.asarray(self._domain.bounds).T
        while _current_generation < self._generations:
            # With this new strategy pattern we have genotypes and descriptors at the same time
            genotypes, descriptors = _cma_asker.ask()

            # Here descriptors have shape (lambda_, generator_dimension)
            # descriptors = np.asarray(strategy.ask())
            # genotypes = self._descriptor_pipe(descriptors, domain=self._domain)
            # Some genotypes may be outside of the bounds of the domain
            # For instance, KPDecoder could generate negative Qs and items
            valid_mask = ((genotypes >= mn) & (genotypes <= mx)).all(axis=1)
            valid_genotypes = genotypes[valid_mask]
            valid_descriptors = descriptors[valid_mask]
            perf_biases, portfolio_scores = self._evaluate_population(valid_genotypes)
            try:
                diversity_scores = self._archives[0](descriptors=valid_descriptors)
            except NotImplementedError:
                diversity_scores = np.zeros(shape=(len(valid_descriptors), 1))

            fitness = self._compute_fitness(perf_biases, diversity_scores)
            instances = self._update_archive(
                self._archives[0],
                individuals=valid_descriptors,  # Valid genotypes extracted from the transformed
                descriptors=valid_descriptors,
                portfolio_scores=portfolio_scores,
                diversity=diversity_scores,
                bias_score=perf_biases,
                fitness=fitness,
            )
            if len(self._archives) > 1:
                _valid_indices = (
                    np.where(perf_biases > 0)[0]
                    if self._keep_feasible
                    else np.arange(len(valid_descriptors))
                )
                if len(_valid_indices) > 1:
                    # If we have more than one archive
                    # we only store the feasible instances
                    # in those result sets.
                    feasible_descriptors = descriptors[_valid_indices]
                    feasible_performances = perf_biases[_valid_indices]
                    feasible_scores = portfolio_scores[_valid_indices]
                    feasible_fitness = fitness[_valid_indices]
                    for archive in self._archives[1:]:
                        try:
                            feasible_div_scores = archive(
                                descriptors=descriptors[_valid_indices]
                            )
                        except NotImplementedError:
                            feasible_div_scores = np.zeros(
                                shape=(len(descriptors[_valid_indices]), 1)
                            )
                        _ = self._update_archive(
                            archive,
                            individuals=feasible_descriptors,
                            descriptors=feasible_descriptors,
                            portfolio_scores=feasible_scores,
                            diversity=feasible_div_scores,
                            bias_score=feasible_performances,
                            fitness=feasible_fitness,
                        )

            self._logbook.update(
                generation=_current_generation, instances=instances, feedback=verbose
            )
            # Tell the descriptors and their corresponding fitness
            full_fitness = np.full(len(instances), -np.inf)  # Invalid ones get -INF
            full_fitness[valid_mask] = fitness
            # CMA-ES minimises, so -INF becomes large unfeasible individuals
            strategy.tell(descriptors, -full_fitness)
            _current_generation += 1

        _instances = (
            self._archives[0] if len(self._archives) == 1 else self._archives[1]
        )
        return (
            GenerationResult(
                solvers=tuple(extract_solvers_name(self._portfolio)),
                instances=_instances,
                history=self._logbook,
            ),
            self._archives,
        )

__call__(verbose=False)

Run the CMA-ES-driven generation process and return the results.

The algorithm proceeds as follows: 1. A random initial mean vector _x0 of dimension self._generator_dimension is sampled, and a cma.CMAEvolutionStrategy is initialised with it, the configured sigma (step size), and a population size equal to self._pop_size (i.e. lambda_). 2. The domain's variable bounds are extracted once as mn/mx. 3. For each generation (up to self._generations): - CMA-ES proposes a batch of latent vectors via strategy.ask(); these are treated as raw descriptors. - The descriptor pipeline decodes the descriptors into concrete instance genotypes via self._descriptor_pipe. - Genotypes outside the domain's bounds are filtered out via valid_mask, since decoders (e.g. for the Knapsack domain) may produce out-of-range values. - The valid genotypes are evaluated against the solver portfolio to obtain performance biases and per-solver scores. - Diversity scores are computed against self._archives[0]; if that archive does not support novelty scoring (NotImplementedError), diversity defaults to zero. - Fitness is computed via :meth:_compute_fitness and the primary archive is updated via :meth:_update_archive. - If more than one archive is configured, the remaining archives are updated with either the feasible subset (performance bias > 0, when self._keep_feasible is True) or all valid individuals, each scored for diversity against its own archive before insertion. - Progress is recorded in self._logbook. - CMA-ES is informed of the fitness of every sampled point (including invalid ones, which receive -inf fitness so they are never preferred) via strategy.tell, using the negated fitness since CMA-ES minimises. 4. After all generations, the resulting instances are taken from self._archives[0] if only one archive was configured, or from self._archives[1] otherwise, and packaged into a :class:GenerationResult.

Parameters:
  • verbose (bool, default: False ) –

    Whether to print/log progress during the run (forwarded to self._logbook.update). Defaults to False.

Returns:
  • Tuple[GenerationResult, Sequence[Archive]]

    Tuple[GenerationResult, Sequence[Archive]]: A two-element tuple containing the packaged generation result (solver names, selected instances, and history) and the full sequence of archives maintained throughout the run.

Source code in digneapy/generators/evolutionary.py
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
def __call__(
    self, verbose: bool = False
) -> Tuple[GenerationResult, Sequence[Archive]]:
    """Run the CMA-ES-driven generation process and return the results.

    The algorithm proceeds as follows:
    1. A random initial mean vector ``_x0`` of dimension
       ``self._generator_dimension`` is sampled, and a
       ``cma.CMAEvolutionStrategy`` is initialised with it, the configured
       ``sigma`` (step size), and a population size equal to
       ``self._pop_size`` (i.e. ``lambda_``).
    2. The domain's variable bounds are extracted once as ``mn``/``mx``.
    3. For each generation (up to ``self._generations``):
        - CMA-ES proposes a batch of latent vectors via ``strategy.ask()``;
          these are treated as raw descriptors.
        - The descriptor pipeline decodes the descriptors into concrete
          instance genotypes via ``self._descriptor_pipe``.
        - Genotypes outside the domain's bounds are filtered out via
          ``valid_mask``, since decoders (e.g. for the Knapsack domain)
          may produce out-of-range values.
        - The valid genotypes are evaluated against the solver portfolio
          to obtain performance biases and per-solver scores.
        - Diversity scores are computed against ``self._archives[0]``; if
          that archive does not support novelty scoring
          (``NotImplementedError``), diversity defaults to zero.
        - Fitness is computed via :meth:`_compute_fitness` and the
          primary archive is updated via :meth:`_update_archive`.
        - If more than one archive is configured, the remaining archives
          are updated with either the feasible subset (performance bias
          > 0, when ``self._keep_feasible`` is True) or all valid
          individuals, each scored for diversity against its own archive
          before insertion.
        - Progress is recorded in ``self._logbook``.
        - CMA-ES is informed of the fitness of every sampled point
          (including invalid ones, which receive ``-inf`` fitness so they
          are never preferred) via ``strategy.tell``, using the negated
          fitness since CMA-ES minimises.
    4. After all generations, the resulting instances are taken from
       ``self._archives[0]`` if only one archive was configured, or from
       ``self._archives[1]`` otherwise, and packaged into a
       :class:`GenerationResult`.

    Args:
        verbose (bool, optional): Whether to print/log progress during the
            run (forwarded to ``self._logbook.update``). Defaults to False.

    Returns:
        Tuple[GenerationResult, Sequence[Archive]]: A two-element tuple
            containing the packaged generation result (solver names,
            selected instances, and history) and the full sequence of
            archives maintained throughout the run.
    """
    import cma

    _x0 = self._rng.uniform(
        size=(self._generator_dimension),
    )
    strategy = cma.CMAEvolutionStrategy(
        _x0,
        sigma0=self._sigma,
        inopts={
            "popsize": self._pop_size,
        },
    )
    _cma_asker = CMAAsker(
        strategy=strategy,
        descriptor_pipeline=self._descriptor_pipe,
        domain=self._domain,
        ask_fn=self._ask_fn,
    )

    _current_generation = 0
    mn, mx = np.asarray(self._domain.bounds).T
    while _current_generation < self._generations:
        # With this new strategy pattern we have genotypes and descriptors at the same time
        genotypes, descriptors = _cma_asker.ask()

        # Here descriptors have shape (lambda_, generator_dimension)
        # descriptors = np.asarray(strategy.ask())
        # genotypes = self._descriptor_pipe(descriptors, domain=self._domain)
        # Some genotypes may be outside of the bounds of the domain
        # For instance, KPDecoder could generate negative Qs and items
        valid_mask = ((genotypes >= mn) & (genotypes <= mx)).all(axis=1)
        valid_genotypes = genotypes[valid_mask]
        valid_descriptors = descriptors[valid_mask]
        perf_biases, portfolio_scores = self._evaluate_population(valid_genotypes)
        try:
            diversity_scores = self._archives[0](descriptors=valid_descriptors)
        except NotImplementedError:
            diversity_scores = np.zeros(shape=(len(valid_descriptors), 1))

        fitness = self._compute_fitness(perf_biases, diversity_scores)
        instances = self._update_archive(
            self._archives[0],
            individuals=valid_descriptors,  # Valid genotypes extracted from the transformed
            descriptors=valid_descriptors,
            portfolio_scores=portfolio_scores,
            diversity=diversity_scores,
            bias_score=perf_biases,
            fitness=fitness,
        )
        if len(self._archives) > 1:
            _valid_indices = (
                np.where(perf_biases > 0)[0]
                if self._keep_feasible
                else np.arange(len(valid_descriptors))
            )
            if len(_valid_indices) > 1:
                # If we have more than one archive
                # we only store the feasible instances
                # in those result sets.
                feasible_descriptors = descriptors[_valid_indices]
                feasible_performances = perf_biases[_valid_indices]
                feasible_scores = portfolio_scores[_valid_indices]
                feasible_fitness = fitness[_valid_indices]
                for archive in self._archives[1:]:
                    try:
                        feasible_div_scores = archive(
                            descriptors=descriptors[_valid_indices]
                        )
                    except NotImplementedError:
                        feasible_div_scores = np.zeros(
                            shape=(len(descriptors[_valid_indices]), 1)
                        )
                    _ = self._update_archive(
                        archive,
                        individuals=feasible_descriptors,
                        descriptors=feasible_descriptors,
                        portfolio_scores=feasible_scores,
                        diversity=feasible_div_scores,
                        bias_score=feasible_performances,
                        fitness=feasible_fitness,
                    )

        self._logbook.update(
            generation=_current_generation, instances=instances, feedback=verbose
        )
        # Tell the descriptors and their corresponding fitness
        full_fitness = np.full(len(instances), -np.inf)  # Invalid ones get -INF
        full_fitness[valid_mask] = fitness
        # CMA-ES minimises, so -INF becomes large unfeasible individuals
        strategy.tell(descriptors, -full_fitness)
        _current_generation += 1

    _instances = (
        self._archives[0] if len(self._archives) == 1 else self._archives[1]
    )
    return (
        GenerationResult(
            solvers=tuple(extract_solvers_name(self._portfolio)),
            instances=_instances,
            history=self._logbook,
        ),
        self._archives,
    )

__init__(ask_fn, dimension, domain, portfolio, lambda_, archives, keep_only_feasible=True, sigma=0.5, performance_function=maximise_perf_gap_easy, generations=1000, repetitions=1, descriptor_pipe=DescriptorPipeline('instance'), seed=None, phi=0.85, workers=1)

Creates a Evolutionary Instance Generator based on Novelty Search The generator uses a set of solvers to evaluate the instances and a novelty search algorithm to guide the evolution of the instances.

Parameters:
  • domain (Domain) –

    Domain for which the instances are generated for.

  • portfolio (Sequence[Solver]) –

    Sequence item of callable objects that can evaluate a instance.

  • pop_size (int) –

    Number of instances in the population to evolve. Defaults to 100.

  • archives (Sequence[Archive]) –

    Containers to store diverse instances.

  • performance_function (PerformanceFn, default: maximise_perf_gap_easy ) –

    Performance function to calculate the performance score. Defaults to max_gap_target.

  • generations (int, default: 1000 ) –

    Number of generations to perform. Defaults to 1000.

  • repetitions (int, default: 1 ) –

    Number times a solver in the portfolio must be run over the same instance. Defaults to 1.

  • describe_by (DESCRIPTORS) –

    _Descriptor used to calculate the diversity. The options available are defined in the dictionary digneapy.DESCRIPTORS. Defaults to "features".

  • seed (int, default: None ) –

    Seed for the RNG protocol. Defaults to 42.

  • phi (float, default: 0.85 ) –

    Phi balance value for the weighted fitness function. Defaults to 0.85.

Raises:
  • ValueError

    If descriptor_pipe has no configured transformers.

  • ValueError

    If descriptor_pipe is not configured with "instance" as its key (the only mode currently supported by ES).

  • ValueError

    If workers is negative.

  • TypeError

    If any element of archives is not an instance of Archive.

  • KeyError

    Raises error if the descriptor strategy is not available in the DESCRIPTORS dictionary

Source code in digneapy/generators/evolutionary.py
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
def __init__(
    self,
    ask_fn: AskFn,
    dimension: int,
    domain: Domain,
    portfolio: Sequence[Solver],
    lambda_: np.uint32 | int,
    archives: Sequence[Archive],
    keep_only_feasible: bool = True,
    sigma: float = 0.5,
    performance_function: PerformanceFn = maximise_perf_gap_easy,
    generations: np.uint32 | int = 1_000,
    repetitions: np.uint16 | int = 1,
    descriptor_pipe: DescriptorPipeline = DescriptorPipeline("instance"),
    seed: Optional[int | np.random.SeedSequence] = None,
    phi: float | np.float64 = 0.85,
    workers: int = 1,
):
    """Creates a Evolutionary Instance Generator based on Novelty Search
    The generator uses a set of solvers to evaluate the instances and
    a novelty search algorithm to guide the evolution of the instances.

    Args:
        domain (Domain): Domain for which the instances are generated for.
        portfolio (Sequence[Solver]): Sequence item of callable objects that can evaluate a instance.
        pop_size (int, optional): Number of instances in the population to evolve. Defaults to 100.
        archives (Sequence[Archive]): Containers to store diverse instances.
        performance_function (PerformanceFn, optional): Performance function to calculate the performance score. Defaults to max_gap_target.
        generations (int, optional): Number of generations to perform. Defaults to 1000.
        repetitions (int, optional): Number times a solver in the portfolio must be run over the same instance. Defaults to 1.
        describe_by (DESCRIPTORS, optional): _Descriptor used to calculate the diversity. The options available are defined in the dictionary digneapy.DESCRIPTORS. Defaults to "features".
        seed (int, optional): Seed for the RNG protocol. Defaults to 42.
        phi (float, optional): Phi balance value for the weighted fitness function. Defaults to 0.85.

    Raises:
        ValueError: If ``descriptor_pipe`` has no configured transformers.
        ValueError: If ``descriptor_pipe`` is not configured with
            ``"instance"`` as its key (the only mode currently supported
            by ``ES``).
        ValueError: If ``workers`` is negative.
        TypeError: If any element of ``archives`` is not an instance of
            ``Archive``.
        KeyError: Raises error if the descriptor strategy is not available in the DESCRIPTORS dictionary
    """
    if len(descriptor_pipe._transformers) == 0:
        raise ValueError(
            "ES is expected to be use with a DescriptorPipeline that includes at least one transformer object."
        )
    if descriptor_pipe._key != "instance":
        raise ValueError(
            "ES is expected to be use with a DescriptorPipeline that uses instance as key. Performance or Features not allowed yet!"
        )

    super().__init__(
        domain=domain,
        portfolio=portfolio,
        pop_size=lambda_,
        performance_function=performance_function,
        descriptor_pipe=descriptor_pipe,
        generations=generations,
        repetitions=repetitions,
    )
    if any(param < 0 for param in (workers, dimension)):
        raise ValueError(
            f"dimension ({dimension}) and workers ({workers}) cannot be negative"
        )
    if any(not isinstance(archive, Archive) for archive in archives):
        raise TypeError("archives must be a subclass of Archive")

    self._ask_fn = ask_fn
    self._generator_dimension = dimension
    self._archives = archives
    self._sigma = sigma
    self._phi = float(phi)
    self._workers = workers
    self.seed = (
        seed
        if isinstance(seed, np.random.SeedSequence)
        else np.random.SeedSequence(seed)
    )

    self._rng = np.random.default_rng(self.seed)
    self._keep_feasible = keep_only_feasible

Evolutionary

Bases: BaseGenerator

Quality-Diversity instance generator based on a Genetic Algorithm.

This generator evolves a population of Instance genotypes using standard genetic operators (selection, crossover, mutation, replacement) combined with a Novelty Search mechanism: each candidate instance is scored both on solver performance (via the portfolio) and on how different it is from previously seen instances (via an Archive of descriptors). The two scores are blended into a single fitness value using the phi balance parameter, which drives the evolutionary process towards instances that are simultaneously challenging for the solvers and diverse with respect to the archive.

Optionally, a second Archive (solution_set) can be maintained to keep a curated set of the best/most representative instances separately from the exploratory archive used to drive novelty. This solution_set only stores feasible instances, those for whom the performance of the target solver is better than the rest of the portfolio.

Source code in digneapy/generators/evolutionary.py
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
class Evolutionary(BaseGenerator):
    """Quality-Diversity instance generator based on a Genetic Algorithm.

    This generator evolves a population of ``Instance`` genotypes using
    standard genetic operators (selection, crossover, mutation, replacement)
    combined with a Novelty Search mechanism: each candidate instance is
    scored both on solver performance (via the portfolio) and on how
    different it is from previously seen instances (via an ``Archive`` of
    descriptors).
    The two scores are blended into a single fitness value using the ``phi``
    balance parameter, which drives the evolutionary
    process towards instances that are simultaneously challenging for the
    solvers and diverse with respect to the archive.

    Optionally, a second ``Archive`` (``solution_set``) can be maintained to
    keep a curated set of the best/most representative instances separately
    from the exploratory archive used to drive novelty. This ``solution_set``
    only stores feasible instances, those for whom the performance of the
    target solver is better than the rest of the portfolio.
    """

    def __init__(
        self,
        domain: Domain,
        portfolio: Sequence[Solver],
        pop_size: np.uint32 | int,
        archive: Archive,
        solution_set: Optional[Archive] = None,
        performance_function: PerformanceFn = maximise_perf_gap_easy,
        generations: np.uint32 | int = np.uint32(1000),
        repetitions: np.uint16 | int = np.uint16(1),
        descriptor_pipe: DescriptorPipeline = DescriptorPipeline("features"),
        cxrate: float | np.float64 = 0.5,
        mutrate: float | np.float64 = 0.8,
        crossover: CrossoverLike = UCX(),
        mutation: MutationLike = UMut(),
        selection: SelectionLike = BinarySelection(),
        replacement: ReplacementLike = Generational(),
        phi: float | np.float64 = 0.85,
        seed: Optional[int | np.random.SeedSequence] = None,
    ):
        """Creates a Evolutionary Instance Generator based on Novelty Search
        The generator uses a set of solvers to evaluate the instances and
        a novelty search algorithm to guide the evolution of the instances.

        Args:
            domain (Domain): Domain for which the instances are generated for.
            portfolio (Sequence[Solver]): Sequence item of callable objects that can evaluate a instance.
            pop_size (int, optional): Number of instances in the population to evolve. Defaults to 100.
            archive (Archive): Container to store diverse instances.
            solution_set (Optional[Archive], optional): Solution set to store the instances. Defaults to None.
            performance_function (PerformanceFn, optional): Performance function to calculate the performance score. Defaults to max_gap_target.
            generations (int, optional): Number of generations to perform. Defaults to 1000.
            repetitions (int, optional): Number times a solver in the portfolio must be run over the same instance. Defaults to 1.
            describe_by (DESCRIPTORS, optional): _Descriptor used to calculate the diversity. The options available are defined in the dictionary digneapy.DESCRIPTORS. Defaults to "features".
            cxrate (float, optional): Crossover rate. Defaults to 0.5.
            mutrate (float, optional): Mutation rate. Defaults to 0.8.
            crossover (Crossover, optional): Crossover operator. Defaults to uniform_crossover.
            mutation (Mutation, optional): Mutation operator. Defaults to uniform_one_mutation.
            selection (Selection, optional): Selection operator. Defaults to binary_tournament_selection.
            replacement (Replacement, optional): Replacement operator. Defaults to generational_replacement.
            performance_function (PerformanceFn, optional): Performance function to calculate the performance score. Defaults to max_gap_target.
            phi (float, optional): Phi balance value for the weighted fitness function. Defaults to 0.85.
            seed (int, optional): Seed for the RNG protocol. Defaults to 42.

        Raises:
            ValueError: Raises error if phi is not a floating point value or it is not in the range [0.0-1.0]
            KeyError: Raises error if the descriptor strategy is not available in the DESCRIPTORS dictionary
            TypeError: If ``archive`` is not an instance of ``Archive``.
            TypeError: If ``solution_set`` is provided and is not an instance of ``Archive``.
        """
        super().__init__(
            domain,
            portfolio,
            pop_size,
            performance_function,
            descriptor_pipe,
            generations=generations,
            repetitions=repetitions,
        )

        try:
            cxrate = float(cxrate)
            mutrate = float(mutrate)
            phi = float(phi)
            if any(p <= 0.0 or p > 1.0 for p in (cxrate, mutrate, phi)):
                raise ValueError()

        except (TypeError, ValueError) as exc:
            raise ValueError(
                f"cxrate ({cxrate}), mutrate ({mutrate}) "
                f"and phi ({phi}) must be floating point values in the "
                f"range (0.0, 1.0]. {exc}"
            ) from exc

        if not isinstance(archive, Archive):
            raise TypeError(f"archive ({archive}) must be a subclass of Archive")

        if solution_set is not None and not isinstance(solution_set, Archive):
            raise TypeError(
                f"solution_set ({solution_set}) must be a subclass of Archive"
            )

        self.phi = phi
        self._archive = archive
        self._solution_set = solution_set
        self.offspring_size = self._pop_size
        self.cxrate = cxrate
        self.mutrate = mutrate
        self.crossover = crossover
        self.mutation = mutation
        self.selection = selection
        self.replacement = replacement
        self.seed = (
            seed
            if isinstance(seed, np.random.SeedSequence)
            else np.random.SeedSequence(seed)
        )

        self._rng = np.random.default_rng(self.seed)

    def __call__(self, verbose: bool = False) -> GenerationResult:
        """Run the full evolutionary process and return the generated instances.

        The algorithm proceeds as follows:
        1. An initial population of ``pop_size`` instances is sampled from
           ``self._domain`` and evaluated against the solver portfolio to
           obtain performance biases and per-solver scores.
        2. Descriptors are computed for the initial population via
           ``self._descriptor_pipe`` (used later for novelty comparisons).
        3. For each of the ``self._generations`` generations:
            - An offspring population of size ``pop_size`` is produced via
              :meth:`generate` (selection + crossover + mutation).
            - The offspring is evaluated against the portfolio and its
              descriptors are computed.
            - Novelty scores are obtained by querying ``self._archive`` with
              the offspring descriptors.
            - A combined fitness is computed via :meth:`_compute_fitness`,
              blending performance bias and novelty according to ``self.phi``.
            - The offspring genotypes are wrapped into ``Instance`` objects
              carrying their fitness, descriptors, and scores.
            - The offspring (all of it, not just feasible individuals — see
              the commented-out filtering logic) is added to ``self._archive``,
              and optionally to ``self._solution_set`` if one was provided.
            - The current population is replaced using ``self.replacement``.
            - Progress for the generation is recorded in ``self._logbook``.
        4. After all generations, the resulting instances (from
           ``self._solution_set`` if present, otherwise from ``self._archive``)
           are packaged into a :class:`GenerationResult` together with the
           solver names and the evolutionary history.

        Args:
            verbose (bool, optional): Whether to print/log progress during the
                run (forwarded to ``self._logbook.update``). Defaults to False.

        Returns:
            GenerationResult: The final set of generated instances together
                with solver names and the recorded evolutionary history.
        """

        self._population = self._domain.generate_instances(n=self._pop_size)
        _, portfolio_scores = self._evaluate_population(self._population)
        _ = self._descriptor_pipe(
            population=self._population,
            scores=portfolio_scores,
            domain=self._domain,
        )
        for generation in range(self._generations):
            offs_genotypes = self.generate(self._pop_size)
            offs_perf_bias, offs_portfolio_scores = self._evaluate_population(
                offs_genotypes
            )
            offs_descriptors = self._descriptor_pipe(
                population=offs_genotypes,
                scores=offs_portfolio_scores,
                domain=self._domain,
            )
            offs_novelty_scores = self._archive(descriptors=offs_descriptors)
            offs_fitness = self._compute_fitness(offs_perf_bias, offs_novelty_scores)

            # Update to include this
            # 1. Novelty Scores --> novelty_scores
            # 2. Performance bias --> perf_biases
            # 3. Fitness --> oiffspring_fitness
            # 4. Descriptor --> descriptors
            offspring = build_instances_from_attributes(
                genotypes=offs_genotypes,
                descriptors=offs_descriptors,
                fitness=offs_fitness,
                portfolio_scores=offs_portfolio_scores,
                diversity_scores=offs_novelty_scores,
                bias_score=offs_perf_bias,
            )

            self._archive.extend(
                instances=offspring,
                descriptors=offs_descriptors,
                novelty_scores=offs_novelty_scores,
                objectives=offs_perf_bias,
            )

            if self._solution_set is not None:
                # Only the feasible instances are considered to be included
                # in the archive and the solution set.
                feasible_indices = np.where(offs_perf_bias > 0)[0]
                offs_feasible_perf_bias = offs_perf_bias[feasible_indices]
                offs_feasible_descriptors = offs_descriptors[feasible_indices]
                offs_feasible_novelty_scores = self._solution_set(
                    descriptors=offs_feasible_descriptors
                )
                self._solution_set.extend(
                    instances=[offspring[i] for i in feasible_indices],
                    descriptors=offs_feasible_descriptors,
                    novelty_scores=offs_feasible_novelty_scores,
                    objectives=offs_feasible_perf_bias,
                )

            # However the whole offspring population is used in the replacement operator
            self._population = self.replacement(self._population, offspring)

            # Record the stats and update the performed gens
            self._logbook.update(
                generation=generation, instances=self._population, feedback=verbose
            )

        if verbose:  # pragma: no cover
            # Clear the terminal
            blank = " " * 80
            print(f"\r{blank}\r", end="")

        _instances = (
            self._solution_set if self._solution_set is not None else self._archive
        )
        return GenerationResult(
            solvers=tuple(extract_solvers_name(self._portfolio)),
            instances=_instances,
            history=self._logbook,
        )

    def generate(self, pop_size: np.uint32 | int) -> np.ndarray:
        """Generates a offspring population of size |offspring_size| from the current population

        For each offspring to produce, two parents are drawn from the current
        population using ``self.selection`` and recombined/mutated via the
        private :meth:`__reproduce` helper.

        Args:
            offspring_size (int): offspring size. Defaults to pop_size.

        Returns:
            Sequence[Instance]  Returns a sequence with the instances definitions, the offspring population.
        """
        offspring = [None] * pop_size  # np.empty(offspring_size, dtype=Instance)
        for i in range(pop_size):
            p_1 = self.selection(self._population)
            p_2 = self.selection(self._population)
            child = self._reproduce(p_1, p_2)
            offspring[i] = child

        return np.asarray(offspring)

    def _reproduce(self, parent_1: Instance, parent_2: Instance) -> Instance:
        """Generates a new offspring instance from two parent instances

        ``parent_1`` is cloned to form the base of the offspring. With
        probability ``self.cxrate`` the clone undergoes crossover with
        ``parent_2`` via ``self.crossover``; in either case, the (possibly
        recombined) offspring is then mutated via ``self.mutation`` using the
        domain's lower/upper bounds to keep values within range.

        Args:
            parent_1 (Instance): First Parent
            parent_1 (Instance): Second Parent

        Returns:
            Instance: New offspring
        """
        offspring = parent_1.clone()

        if self._rng.random() < self.cxrate:
            offspring = self.crossover(offspring, parent_2)
            return self.mutation(offspring, self._domain.lbs, self._domain.ubs)
        else:
            return self.mutation(offspring, self._domain.lbs, self._domain.ubs)

    def _compute_fitness(
        self, performance_biases: np.ndarray, novelty_scores: np.ndarray
    ) -> np.ndarray:
        """Calculates the fitness of each instance in the population

        Fitness is a convex combination of performance bias and novelty:
        ``fitness = phi * performance_bias + (1 - phi) * novelty_score``.
        A higher ``self.phi`` favours instances that are harder/more
        discriminating for the solver portfolio, while a lower ``self.phi``
        favours instances that are more novel with respect to the archive.

        Args:
            performance_biases (np.ndarray): Performance biases or scores of each instance
            novelty_scores (np.ndarray): Novelty scores of each instance

        Returns:
            fitness of each instance (np.ndarray)
        """
        phi_r = 1.0 - self.phi
        fitness = np.zeros(len(performance_biases))
        fitness = (performance_biases * self.phi) + (novelty_scores * phi_r)
        return fitness

__call__(verbose=False)

Run the full evolutionary process and return the generated instances.

The algorithm proceeds as follows: 1. An initial population of pop_size instances is sampled from self._domain and evaluated against the solver portfolio to obtain performance biases and per-solver scores. 2. Descriptors are computed for the initial population via self._descriptor_pipe (used later for novelty comparisons). 3. For each of the self._generations generations: - An offspring population of size pop_size is produced via :meth:generate (selection + crossover + mutation). - The offspring is evaluated against the portfolio and its descriptors are computed. - Novelty scores are obtained by querying self._archive with the offspring descriptors. - A combined fitness is computed via :meth:_compute_fitness, blending performance bias and novelty according to self.phi. - The offspring genotypes are wrapped into Instance objects carrying their fitness, descriptors, and scores. - The offspring (all of it, not just feasible individuals — see the commented-out filtering logic) is added to self._archive, and optionally to self._solution_set if one was provided. - The current population is replaced using self.replacement. - Progress for the generation is recorded in self._logbook. 4. After all generations, the resulting instances (from self._solution_set if present, otherwise from self._archive) are packaged into a :class:GenerationResult together with the solver names and the evolutionary history.

Parameters:
  • verbose (bool, default: False ) –

    Whether to print/log progress during the run (forwarded to self._logbook.update). Defaults to False.

Returns:
  • GenerationResult( GenerationResult ) –

    The final set of generated instances together with solver names and the recorded evolutionary history.

Source code in digneapy/generators/evolutionary.py
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
def __call__(self, verbose: bool = False) -> GenerationResult:
    """Run the full evolutionary process and return the generated instances.

    The algorithm proceeds as follows:
    1. An initial population of ``pop_size`` instances is sampled from
       ``self._domain`` and evaluated against the solver portfolio to
       obtain performance biases and per-solver scores.
    2. Descriptors are computed for the initial population via
       ``self._descriptor_pipe`` (used later for novelty comparisons).
    3. For each of the ``self._generations`` generations:
        - An offspring population of size ``pop_size`` is produced via
          :meth:`generate` (selection + crossover + mutation).
        - The offspring is evaluated against the portfolio and its
          descriptors are computed.
        - Novelty scores are obtained by querying ``self._archive`` with
          the offspring descriptors.
        - A combined fitness is computed via :meth:`_compute_fitness`,
          blending performance bias and novelty according to ``self.phi``.
        - The offspring genotypes are wrapped into ``Instance`` objects
          carrying their fitness, descriptors, and scores.
        - The offspring (all of it, not just feasible individuals — see
          the commented-out filtering logic) is added to ``self._archive``,
          and optionally to ``self._solution_set`` if one was provided.
        - The current population is replaced using ``self.replacement``.
        - Progress for the generation is recorded in ``self._logbook``.
    4. After all generations, the resulting instances (from
       ``self._solution_set`` if present, otherwise from ``self._archive``)
       are packaged into a :class:`GenerationResult` together with the
       solver names and the evolutionary history.

    Args:
        verbose (bool, optional): Whether to print/log progress during the
            run (forwarded to ``self._logbook.update``). Defaults to False.

    Returns:
        GenerationResult: The final set of generated instances together
            with solver names and the recorded evolutionary history.
    """

    self._population = self._domain.generate_instances(n=self._pop_size)
    _, portfolio_scores = self._evaluate_population(self._population)
    _ = self._descriptor_pipe(
        population=self._population,
        scores=portfolio_scores,
        domain=self._domain,
    )
    for generation in range(self._generations):
        offs_genotypes = self.generate(self._pop_size)
        offs_perf_bias, offs_portfolio_scores = self._evaluate_population(
            offs_genotypes
        )
        offs_descriptors = self._descriptor_pipe(
            population=offs_genotypes,
            scores=offs_portfolio_scores,
            domain=self._domain,
        )
        offs_novelty_scores = self._archive(descriptors=offs_descriptors)
        offs_fitness = self._compute_fitness(offs_perf_bias, offs_novelty_scores)

        # Update to include this
        # 1. Novelty Scores --> novelty_scores
        # 2. Performance bias --> perf_biases
        # 3. Fitness --> oiffspring_fitness
        # 4. Descriptor --> descriptors
        offspring = build_instances_from_attributes(
            genotypes=offs_genotypes,
            descriptors=offs_descriptors,
            fitness=offs_fitness,
            portfolio_scores=offs_portfolio_scores,
            diversity_scores=offs_novelty_scores,
            bias_score=offs_perf_bias,
        )

        self._archive.extend(
            instances=offspring,
            descriptors=offs_descriptors,
            novelty_scores=offs_novelty_scores,
            objectives=offs_perf_bias,
        )

        if self._solution_set is not None:
            # Only the feasible instances are considered to be included
            # in the archive and the solution set.
            feasible_indices = np.where(offs_perf_bias > 0)[0]
            offs_feasible_perf_bias = offs_perf_bias[feasible_indices]
            offs_feasible_descriptors = offs_descriptors[feasible_indices]
            offs_feasible_novelty_scores = self._solution_set(
                descriptors=offs_feasible_descriptors
            )
            self._solution_set.extend(
                instances=[offspring[i] for i in feasible_indices],
                descriptors=offs_feasible_descriptors,
                novelty_scores=offs_feasible_novelty_scores,
                objectives=offs_feasible_perf_bias,
            )

        # However the whole offspring population is used in the replacement operator
        self._population = self.replacement(self._population, offspring)

        # Record the stats and update the performed gens
        self._logbook.update(
            generation=generation, instances=self._population, feedback=verbose
        )

    if verbose:  # pragma: no cover
        # Clear the terminal
        blank = " " * 80
        print(f"\r{blank}\r", end="")

    _instances = (
        self._solution_set if self._solution_set is not None else self._archive
    )
    return GenerationResult(
        solvers=tuple(extract_solvers_name(self._portfolio)),
        instances=_instances,
        history=self._logbook,
    )

__init__(domain, portfolio, pop_size, archive, solution_set=None, performance_function=maximise_perf_gap_easy, generations=np.uint32(1000), repetitions=np.uint16(1), descriptor_pipe=DescriptorPipeline('features'), cxrate=0.5, mutrate=0.8, crossover=UCX(), mutation=UMut(), selection=BinarySelection(), replacement=Generational(), phi=0.85, seed=None)

Creates a Evolutionary Instance Generator based on Novelty Search The generator uses a set of solvers to evaluate the instances and a novelty search algorithm to guide the evolution of the instances.

Parameters:
  • domain (Domain) –

    Domain for which the instances are generated for.

  • portfolio (Sequence[Solver]) –

    Sequence item of callable objects that can evaluate a instance.

  • pop_size (int) –

    Number of instances in the population to evolve. Defaults to 100.

  • archive (Archive) –

    Container to store diverse instances.

  • solution_set (Optional[Archive], default: None ) –

    Solution set to store the instances. Defaults to None.

  • performance_function (PerformanceFn, default: maximise_perf_gap_easy ) –

    Performance function to calculate the performance score. Defaults to max_gap_target.

  • generations (int, default: uint32(1000) ) –

    Number of generations to perform. Defaults to 1000.

  • repetitions (int, default: uint16(1) ) –

    Number times a solver in the portfolio must be run over the same instance. Defaults to 1.

  • describe_by (DESCRIPTORS) –

    _Descriptor used to calculate the diversity. The options available are defined in the dictionary digneapy.DESCRIPTORS. Defaults to "features".

  • cxrate (float, default: 0.5 ) –

    Crossover rate. Defaults to 0.5.

  • mutrate (float, default: 0.8 ) –

    Mutation rate. Defaults to 0.8.

  • crossover (Crossover, default: UCX() ) –

    Crossover operator. Defaults to uniform_crossover.

  • mutation (Mutation, default: UMut() ) –

    Mutation operator. Defaults to uniform_one_mutation.

  • selection (Selection, default: BinarySelection() ) –

    Selection operator. Defaults to binary_tournament_selection.

  • replacement (Replacement, default: Generational() ) –

    Replacement operator. Defaults to generational_replacement.

  • performance_function (PerformanceFn, default: maximise_perf_gap_easy ) –

    Performance function to calculate the performance score. Defaults to max_gap_target.

  • phi (float, default: 0.85 ) –

    Phi balance value for the weighted fitness function. Defaults to 0.85.

  • seed (int, default: None ) –

    Seed for the RNG protocol. Defaults to 42.

Raises:
  • ValueError

    Raises error if phi is not a floating point value or it is not in the range [0.0-1.0]

  • KeyError

    Raises error if the descriptor strategy is not available in the DESCRIPTORS dictionary

  • TypeError

    If archive is not an instance of Archive.

  • TypeError

    If solution_set is provided and is not an instance of Archive.

Source code in digneapy/generators/evolutionary.py
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
def __init__(
    self,
    domain: Domain,
    portfolio: Sequence[Solver],
    pop_size: np.uint32 | int,
    archive: Archive,
    solution_set: Optional[Archive] = None,
    performance_function: PerformanceFn = maximise_perf_gap_easy,
    generations: np.uint32 | int = np.uint32(1000),
    repetitions: np.uint16 | int = np.uint16(1),
    descriptor_pipe: DescriptorPipeline = DescriptorPipeline("features"),
    cxrate: float | np.float64 = 0.5,
    mutrate: float | np.float64 = 0.8,
    crossover: CrossoverLike = UCX(),
    mutation: MutationLike = UMut(),
    selection: SelectionLike = BinarySelection(),
    replacement: ReplacementLike = Generational(),
    phi: float | np.float64 = 0.85,
    seed: Optional[int | np.random.SeedSequence] = None,
):
    """Creates a Evolutionary Instance Generator based on Novelty Search
    The generator uses a set of solvers to evaluate the instances and
    a novelty search algorithm to guide the evolution of the instances.

    Args:
        domain (Domain): Domain for which the instances are generated for.
        portfolio (Sequence[Solver]): Sequence item of callable objects that can evaluate a instance.
        pop_size (int, optional): Number of instances in the population to evolve. Defaults to 100.
        archive (Archive): Container to store diverse instances.
        solution_set (Optional[Archive], optional): Solution set to store the instances. Defaults to None.
        performance_function (PerformanceFn, optional): Performance function to calculate the performance score. Defaults to max_gap_target.
        generations (int, optional): Number of generations to perform. Defaults to 1000.
        repetitions (int, optional): Number times a solver in the portfolio must be run over the same instance. Defaults to 1.
        describe_by (DESCRIPTORS, optional): _Descriptor used to calculate the diversity. The options available are defined in the dictionary digneapy.DESCRIPTORS. Defaults to "features".
        cxrate (float, optional): Crossover rate. Defaults to 0.5.
        mutrate (float, optional): Mutation rate. Defaults to 0.8.
        crossover (Crossover, optional): Crossover operator. Defaults to uniform_crossover.
        mutation (Mutation, optional): Mutation operator. Defaults to uniform_one_mutation.
        selection (Selection, optional): Selection operator. Defaults to binary_tournament_selection.
        replacement (Replacement, optional): Replacement operator. Defaults to generational_replacement.
        performance_function (PerformanceFn, optional): Performance function to calculate the performance score. Defaults to max_gap_target.
        phi (float, optional): Phi balance value for the weighted fitness function. Defaults to 0.85.
        seed (int, optional): Seed for the RNG protocol. Defaults to 42.

    Raises:
        ValueError: Raises error if phi is not a floating point value or it is not in the range [0.0-1.0]
        KeyError: Raises error if the descriptor strategy is not available in the DESCRIPTORS dictionary
        TypeError: If ``archive`` is not an instance of ``Archive``.
        TypeError: If ``solution_set`` is provided and is not an instance of ``Archive``.
    """
    super().__init__(
        domain,
        portfolio,
        pop_size,
        performance_function,
        descriptor_pipe,
        generations=generations,
        repetitions=repetitions,
    )

    try:
        cxrate = float(cxrate)
        mutrate = float(mutrate)
        phi = float(phi)
        if any(p <= 0.0 or p > 1.0 for p in (cxrate, mutrate, phi)):
            raise ValueError()

    except (TypeError, ValueError) as exc:
        raise ValueError(
            f"cxrate ({cxrate}), mutrate ({mutrate}) "
            f"and phi ({phi}) must be floating point values in the "
            f"range (0.0, 1.0]. {exc}"
        ) from exc

    if not isinstance(archive, Archive):
        raise TypeError(f"archive ({archive}) must be a subclass of Archive")

    if solution_set is not None and not isinstance(solution_set, Archive):
        raise TypeError(
            f"solution_set ({solution_set}) must be a subclass of Archive"
        )

    self.phi = phi
    self._archive = archive
    self._solution_set = solution_set
    self.offspring_size = self._pop_size
    self.cxrate = cxrate
    self.mutrate = mutrate
    self.crossover = crossover
    self.mutation = mutation
    self.selection = selection
    self.replacement = replacement
    self.seed = (
        seed
        if isinstance(seed, np.random.SeedSequence)
        else np.random.SeedSequence(seed)
    )

    self._rng = np.random.default_rng(self.seed)

generate(pop_size)

Generates a offspring population of size |offspring_size| from the current population

For each offspring to produce, two parents are drawn from the current population using self.selection and recombined/mutated via the private :meth:__reproduce helper.

Parameters:
  • offspring_size (int) –

    offspring size. Defaults to pop_size.

Returns:
  • ndarray

    Sequence[Instance] Returns a sequence with the instances definitions, the offspring population.

Source code in digneapy/generators/evolutionary.py
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
def generate(self, pop_size: np.uint32 | int) -> np.ndarray:
    """Generates a offspring population of size |offspring_size| from the current population

    For each offspring to produce, two parents are drawn from the current
    population using ``self.selection`` and recombined/mutated via the
    private :meth:`__reproduce` helper.

    Args:
        offspring_size (int): offspring size. Defaults to pop_size.

    Returns:
        Sequence[Instance]  Returns a sequence with the instances definitions, the offspring population.
    """
    offspring = [None] * pop_size  # np.empty(offspring_size, dtype=Instance)
    for i in range(pop_size):
        p_1 = self.selection(self._population)
        p_2 = self.selection(self._population)
        child = self._reproduce(p_1, p_2)
        offspring[i] = child

    return np.asarray(offspring)