pygmo Documentation

repository·master·Indexed 19 days ago

https://github.com/esa/pygmo2

A scientific Python library for massively parallel optimization built upon the pagmo C++ library. It provides a unified interface for optimization algorithms and problems, supporting constrained, unconstrained, single-objective, and multi-objective problems. Key features include the archipelago class for parallel management, batch fitness evaluators (bfe) for simultaneous population evaluation, and a wide range of evolutionary, swarm intelligence, and local search algorithms.

Tokens
31.1K
Snippets
93
Records
144
Agent score
67%

What's inside pygmo

  1. Overview of pygmo for parallel optimization

    master

    pygmo is a scientific Python library designed for massively parallel optimization. It provides a unified interface to various optimization algorithms and problems, facilitating easy deployment in parallel environments.

    Key features include:

    • Algorithm Diversity: Combines bio-inspired and evolutionary algorithms with state-of-the-art methods like Simplex, SQP, and interior point methods.
    • Algorithmic Cooperation: Supports building 'super-algorithms' using the asynchronous, generalized island model to exploit cooperation between different algorithms.
    • Problem Versatility: Capable of solving constrained, unconstrained, single-objective, multi-objective, continuous, integer, stochastic, and deterministic optimization problems.
    • Research-Oriented: Designed for comparing novel algorithms against established state-of-the-art implementations.

    Note: pygmo is built upon the pagmo C++ library.

  2. Overview of pygmo

    master
    pygmo is a scientific Python library designed for massively parallel optimization. It provides a unified interface for both optimization algorithms and optimization problems, facilitating easy deployment in massively parallel environments.
  3. Understand pygmo's optimization capabilities

    master

    pygmo is a comprehensive optimization library that supports a wide variety of problem types and parallelization strategies:

    Supported Problem Types

    • Continuous and Integer programming
    • Single and Multi-objective optimization
    • Constrained and Unconstrained problems
    • Stochastic problems
    • With or without derivatives

    Parallelization Models

    • Coarse-grained parallelization: Uses the generalised island model, where multiple optimization instances run in parallel (potentially on different machines) and exchange information to improve time-to-solution.
    • Fine-grained parallelization: Supports batch fitness evaluation for selected algorithms, allowing single optimizations to be sped up via multithreading, GPUs, SIMD vectorization, or high-performance clusters.

    Utilities and Testing

    • Includes a library of ready-to-use problems (e.g., Rosenbrock, Rastrigin, Lennard-Jones) for testing.
    • Provides utilities for hypervolume computation, non-dominated sorting, and plotting.
  4. Algorithms exposed from C++

    master
    pygmo provides a wide range of high-performance optimization algorithms exposed from its C++ core. These include evolutionary algorithms, swarm intelligence, multi-objective optimizers, and local search methods.
  5. What is a pygmo.island and how does it work?

    master

    The pygmo.island class is the fundamental unit of parallelization in pagmo. An island is a computational unit that can be offloaded to a separate thread, process, or remote machine.

    By using an island, you can call island.evolve() to run an optimization task asynchronously, allowing your main Python script to continue executing other tasks in the meantime.

    An island's execution model is determined by its UDI (User Defined Island). The UDI defines how the evolution is offloaded (e.g., to a thread, a process, or a remote cluster).

    Key relationships:

    • An island evolves a population using a UDA (User Defined Algorithm).
    • A problem computes fitness using a UDP (User Defined Problem).
    • A collection of island objects forms an archipelago.

    Note: While you can manually manage multiple islands, it is generally recommended to use a pygmo.archipelago for parallelization tasks instead of manual island scripting.

    import pygmo as pg
    # Example of creating a thread-based island
    isl = pg.island(algo = pg.de(10), prob = pg.ackley(5), size=20, udi=pg.thread_island())
  6. What is the decorator meta-problem and when to use it

    master

    In PyGMO, a meta-problem is a User-Defined Problem (UDP) that takes another UDP as input to modify its behavior. The decorator_problem is a meta-problem that allows you to non-intrusively modify and customize any method in the public API of a UDP on-the-fly.

    When to use it:

    • To quickly, temporarily, and non-intrusively alter UDP behavior (e.g., for logging, timing, or debugging).
    • To wrap existing problems (including C++ problems) without modifying their source code.

    When NOT to use it:

    • Do not use it as a replacement for specialized meta-problems. For example, if you need to transform a multi-objective problem into a single-objective one, use pygmo.decompose instead, as it is optimized for that specific task.
    • For permanent changes, standard Python patterns like subclassing or monkey patching may be more appropriate.
  7. What is a pygmo.population and how to initialize it

    master

    A pygmo.population acts as a container for candidate solutions (individuals) for a specific pygmo.problem. Each individual consists of a decision vector (chromosome), a fitness vector, and a unique ID for tracking.

    When you initialize a population with a size greater than zero, pygmo automatically generates random candidate solutions within the problem's box bounds and performs fitness evaluations for them.

    To initialize:

    • An empty population: pg.population(prob)
    • A population with a specific size and random seed: pg.population(prob, size=N, seed=S)
    import pygmo as pg
    
    # Define a problem
    prob = pg.problem(pg.rosenbrock(dim=4))
    
    # Create an empty population
    pop1 = pg.population(prob)
    
    # Create a population with 5 individuals and a specific seed
    pop2 = pg.population(prob, size=5, seed=723782378)
    
    print(len(pop1)) # Output: 0
    print(len(pop2)) # Output: 5
  8. Configure MOEA/D weight generation and decomposition methods

    master

    When instantiating pygmo.moead, you can customize how weights are distributed and how the problem is decomposed using keyword arguments:

    Weight Generation (weight_generation)

    • grid: Provides a uniform distribution of weights but limits the possible population sizes based on the number of objectives.
    • low discrepancy: Ensures a low discrepancy spread over the objective space and allows for any number of weights (useful for flexible population sizes).

    Decomposition Method (decomposition)

    • tchebycheff: The default decomposition method.
    • boundary intersection: Can result in a better spread of the final population over the Pareto front when applicable.

    Example of inspecting current algorithm settings:

    print(algo)
  9. How to construct a `pygmo.hypervolume` object

    master

    The pygmo.hypervolume class is used to compute the hypervolume indicator (Lebesgue Measure or S-Metric) and hypervolume contributions for multi-objective optimization. You can construct it in two ways:

    1. From a pygmo.population: This uses the fitness values of the individuals in the population. Note that if the population's fitness values change, you must reconstruct the hypervolume object, as it copies the point set upon construction.
    2. From explicit coordinates: You can pass a NumPy array or a list of lists representing the coordinates of the point set. This is useful for analyzing specific geometries independently of a problem's objective function.

    Requirements/Assumptions:

    • Minimization: The implementation assumes minimization in every dimension. The reference point must be numerically larger than or equal to the points in each objective, and strictly larger in at least one objective.
    • Dimensionality: The input data and the reference point must have at least 2 dimensions.
    import pygmo as pg
    from numpy import array
    
    # Method 1: From a population
    udp = pg.problem(pg.dtlz(prob_id=2, dim=10, fdim=3))
    pop = pg.population(udp, 50)
    hv = pg.hypervolume(pop)
    
    # Method 2: From explicit coordinates (NumPy array or list)
    hv = pg.hypervolume(array([[1,0],[0.5,0.5],[0,1]]))
    hv_from_list = pg.hypervolume([[1,0],[0.5,0.5],[0,1]])
  10. Use C++-exposed benchmark problems in pygmo

    master

    pygmo provides a wide range of standard benchmark problems implemented in C++ for high performance. These include CEC competition problems, classic continuous optimization functions, and multi-objective test suites. Common available problem classes include:

    • CEC Benchmarks: pygmo.cec2014, pygmo.cec2013, pygmo.cec2009, pygmo.cec2006.
    • Classic Continuous Functions: pygmo.rosenbrock, pygmo.rastrigin, pygmo.schwefel, pygmo.ackley, pygmo.griewank.
    • Multi-objective Benchmarks: pygmo.zdt, pygmo.dtlz, pygmo.wfg.
    • Other Specialized Problems: pygmo.lennard_jones, pygmo.golomb_ruler, pygmo.inventory, pygmo.hock_schittkowski_71, pygmo.luksan_vlcek1.
  11. Use replacement policies in pygmo

    master

    In pygmo, replacement policies determine how new individuals are selected to replace existing ones in a population during evolutionary algorithms. These policies are exposed from the underlying C++ implementation to ensure high performance.

    One available policy is pygmo.fair_replace, which implements a fairness-based replacement strategy.

  12. Use C++-exposed batch evaluators

    master

    For higher performance, pygmo exposes several batch evaluators implemented in C++. These are typically used to parallelize the evaluation of a population of individuals.

    • pygmo.default_bfe: The standard batch evaluator.
    • pygmo.thread_bfe: A batch evaluator that uses multi-threading to evaluate individuals in parallel.
    • pygmo.member_bfe: A batch evaluator that evaluates members of a population.