pymoo Documentation

repository·main·Indexed 25 days ago

https://github.com/anyoptimization/pymoo

A Python framework for Multi-Objective Optimization. The documentation includes guides on using various unconstrained and constrained optimization algorithms, as well as advanced hyperparameter optimization using HyperparameterProblem, SingleObjectiveSingleRun, and MultiRun for performance assessment.

Tokens
94.7K
Snippets
247
Records
386
Agent score
83%

What's inside pymoo

  1. Core components of the PyMoo optimization model

    main

    PyMoo's optimization framework is built around several core classes that define the behavior of evolutionary algorithms. When building or extending algorithms, you will interact with these primary components:

    • Algorithm: The main class representing the optimization process.
    • Sampling: Defines how the initial population is generated.
    • Selection: Defines how individuals are chosen from a population for reproduction.
    • Mutation: Defines how individuals are modified to maintain diversity.
    • Crossover: Defines how offspring are created from parents.
    • Survival: Defines how individuals are selected to survive to the next generation.
    • Termination: Defines the criteria for stopping the optimization process.
    • Indicator: Defines the quality metrics (e.g., hypervolume, Pareto front distance) used to evaluate populations.
    • Population: A collection of individuals.
    • Individual: A single candidate solution containing variables and objective values.
    • Result: The object containing the output of the optimization process.
  2. Choose between functional and object-oriented interfaces

    main

    PyMoo provides two distinct ways to run optimization algorithms:

    1. Functional Interface (minimize): Best for standard optimization tasks. It allows you to run an optimization in just a few lines of code by passing a problem and an algorithm to the minimize function.
    2. Object-Oriented Interface: Best for advanced users who need to customize or alter the internal behavior of an existing algorithm. This interface provides more granular control over the algorithm's components.
  3. Available Visualization Classes in pymoo

    main

    pymoo provides several visualization classes for different types of data, ranging from 2D/3D scatter plots to high-dimensional representations like Parallel Coordinate Plots and Radviz. You can use these classes directly or via factory functions.

    Supported visualization types include:

    • Scatter Plots (2D/3D/ND): Scatter class
    • Parallel Coordinate Plots (PCP): ParallelCoordinatePlot class
    • Heatmap: Heatmap class
    • Petal Diagram: Petal class
    • Radar: Radar class
    • Radviz: Radviz class
    • Star Coordinates: StarCoordinate class
    • Video: Video class
  4. Parallelization strategies in pymoo

    main

    Parallelization in pymoo is primarily used to speed up the evaluation of solutions in population-based algorithms. You can choose from several strategies depending on your hardware and implementation requirements:

    • Vectorized Operations: Uses NumPy matrix operations for efficient parallel computation.
    • Starmap Interface: Uses Python's multiprocessing.starmap to handle threads and processes.
    • Joblib: Leverages the joblib library's flexible backend system for advanced parallelization.
    • GPU Acceleration: Utilizes CUDA and PyTorch for high-performance computing.
    • Custom Parallelization: Allows you to implement your own specific parallelization strategy.
  5. Available Genetic Algorithm Operators in pymoo

    main
    pymoo provides various operators to customize genetic algorithms. These operators are categorized into Sampling, Selection, Mutation, and Crossover. Many operators can be accessed via 'convenience' strings (e.g., "real_lhs" or "tournament") which simplify their instantiation in algorithm configurations.
  6. Explore pymoo core topics

    main

    The pymoo framework is organized into several key functional areas. You can explore detailed documentation for each of the following:

    • Interface: Overview of the most important parameters of the framework interface.
    • Problems: Guidance on implementing custom problems and using built-in test problems.
    • Algorithms: Information on available optimization algorithms and how to use them.
    • Operators: Overview of evolutionary operators.
    • Customization: How to design custom evolutionary operators for specific optimization problems.
    • Visualization: Techniques for visualizing optimization results or individual solutions.
    • Multi-Criteria Decision Making (MCDM): Methods for selecting a single solution from a solution set.
    • FAQ: Frequently asked questions.
  7. License information for pymoo

    main

    The pymoo project is licensed under the Apache License, Version 2.0.

    Key terms include:

    • Grant of Copyright License: Contributors grant a perpetual, worldwide, non-exclusive, royalty-free license to reproduce, prepare derivative works, and distribute the work.
    • Grant of Patent License: Contributors grant a perpetual, worldwide, non-exclusive, royalty-free patent license for their contributions.
    • Redistribution: You may redistribute the work or derivative works provided you include a copy of the license, provide prominent notices for modified files, and retain all original copyright, patent, and trademark notices.
    • Disclaimer of Warranty: The work is provided on an "AS IS" basis, without warranties of any kind.
    • Limitation of Liability: Contributors are not liable for any damages arising from the use or inability to use the work.
  8. Define and use optimization problems in pymoo

    main

    The pymoo library provides two primary ways to work with optimization problems:

    1. Custom Problem Definition: An intuitive way to define your own optimization problems.
    2. Benchmark Problems: A collection of pre-implemented single-, multi-, and many-objective optimization problems used for testing and benchmarking algorithms.

    To get started, you can explore the Definition guide for creating custom problems or the Test Problems guide to see the available built-in benchmarks.

  9. Getting Started with pymoo

    main
    The pymoo getting started guide provides a structured walkthrough of multi-objective optimization using practical examples. It is designed for users with a basic understanding of optimization, Python, and NumPy. The guide is organized into several parts covering basics, constrained bi-objective optimization, solution set finding, multi-criteria decision making, and convergence analysis.
  10. Use Automatic Differentiation for problem gradients

    main

    If your problem is implemented using autograd, you can use the AutomaticDifferentiation wrapper to obtain gradients automatically. Wrap your Problem instance with AutomaticDifferentiation(MyProblem()). To retrieve the gradients during evaluation, pass "dF" (for objective gradients) or "dG" (for constraint gradients) to the return_values_of parameter of the evaluate method.

    import numpy as np
    import pymoo.gradient.toolbox as anp
    from pymoo.core.problem import Problem
    from pymoo.gradient.automatic import AutomaticDifferentiation
    
    class MyProblem(Problem):
        def __init__(self):
            super().__init__(n_var=10, n_obj=1, xl=-5, xu=5)
    
        def _evaluate(self, x, out, *args, **kwargs):
            out["F"] = anp.sum(anp.power(x, 2), axis=1)
    
    # Wrap the problem for automatic differentiation
    problem = AutomaticDifferentiation(MyProblem())
    
    # Evaluate and retrieve both function values (F) and gradients (dF)
    X = np.array([np.arange(10)]).astype(float)
    F, dF = problem.evaluate(X, return_values_of=["F", "dF"])
    
    # dF shape is (n_rows, n_objective, n_vars)
    print(dF.shape)
  11. Explore pymoo optimization algorithms

    main

    pymoo provides a wide variety of unconstrained and constrained optimization algorithms, including single-objective, multi-objective, and many-objective optimization.

    To use these algorithms effectively, you should explore the following core concepts:

    • Initialization: Learn how to initialize an algorithm to be run on a specific problem.
    • Usage Patterns: Understand the different ways to run algorithms, such as functional, next (iterative), and ask-and-tell interfaces, which provide varying levels of control during the optimization process.
    • Algorithm Selection: Browse the available list of algorithms categorized by their optimization type (Single-Objective Optimization (SOO) vs. Multi-Objective Optimization (MOO)).
  12. Define custom evolutionary operators for subset selection

    main

    For problems with strict constraints (like selecting a fixed number of items), it is more efficient to bake feasibility directly into the evolutionary operators rather than relying on penalty functions.

    • Sampling: Inherit from Sampling and implement _do to return initial solutions that already satisfy the constraints (e.g., by randomly picking exactly $N$ indices).
    • Crossover: Inherit from Crossover and implement _do. For subset selection, a custom crossover can combine parents and then randomly select/remove elements to maintain the required subset size.
    • Mutation: Inherit from Mutation and implement _do. A valid mutation for subset selection might involve swapping a selected element with an unselected one to preserve the subset size.
    from pymoo.core.sampling import Sampling
    from pymoo.core.crossover import Crossover
    from pymoo.core.mutation import Mutation
    
    class MySampling(Sampling):
        def _do(self, problem, n_samples, **kwargs):
            # Implementation to return valid initial samples
            pass
    
    class BinaryCrossover(Crossover):
        def __init__(self):
            super().__init__(2, 1)
    
        def _do(self, problem, X, **kwargs):
            # Implementation to return valid offspring
            pass
    
    class MyMutation(Mutation):
        def _do(self, problem, X, **kwargs):
            # Implementation to return valid mutated individuals
            pass