### Install Nevergrad with benchmark and all extras Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/getting_started.md Installs Nevergrad with additional dependencies for benchmarking and testing tools. The `[benchmark]` flag installs requirements for the benchmarking subpackage, while `[all]` installs everything. ```bash pip install nevergrad[benchmark] ``` ```bash pip install --use-deprecated=legacy-resolver -e .[all] ``` -------------------------------- ### R: Nevergrad Optimization Setup and Execution Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/r.md This R code snippet demonstrates how to set up and run Nevergrad for optimization. It includes installing Nevergrad via reticulate, selecting an optimizer, defining parameter instrumentation, and executing an optimization loop. The code also shows how to integrate parallel processing using doParallel for evaluating multiple hyperparameters simultaneously. Ensure 'pip=TRUE' is used during installation for correct setup. ```R # Library and environment for Reticulate/Nevergrad. library("reticulate") conda_create("r-reticulate") conda_install("r-reticulate", "nevergrad", pip=TRUE) use_condaenv("r-reticulate") # Only if you use parallelism. library(doParallel) # Choose your optimization method below. optimizer_name <- "NgIohTuned" # optimizer_name <- "DoubleFastGADiscreteOnePlusOne" # optimizer_name <- "OnePlusOne" # optimizer_name <- "DE" # optimizer_name <- "RandomSearch" # optimizer_name <- "TwoPointsDE" # optimizer_name <- "Powell" # optimizer_name <- "MetaModel" CRASH !!!! # optimizer_name <- "SQP" # optimizer_name <- "Cobyla" # optimizer_name <- "NaiveTBPSA" # optimizer_name <- "DiscreteOnePlusOne" # optimizer_name <- "cGA" # optimizer_name <- "ScrHammersleySearch" # Now we can play with Nevergrad as usual. # We assume here that we have 17 continuous hyperparameters with values in [0, 1]. # We can do other instrumentations, as discussed below. my_tuple <- tuple(17) instrumentation <- ng$p$Array(shape=my_tuple) instrumentation$set_bounds(0., 1.) num_workers <- 3 # We want to be able to evaluate 3 hyperparametrizations simultaneously. num_iterations <- 100 * num_workers # Let us say we have a budget of 100xnum_workers hyperparameters to evaluate. # Let us create a Nevergrad optimization method. optimizer <- ng$optimizers$registry[optimizer_name](instrumentation, budget=num_iterations, num_workers=num_workers) # Dummy initializations. nevergrad_hp <- 0 nevergrad_hp_val <- 0 score <- 0 for (i in 1:num_iterations) { for (j in 1:num_workers) { nevergrad_hp[j] <- optimizer$ask() nevergrad_hp_val[j] <- nevergrad_hp[j]$value } # Sequential version. # for (j in 1:num_workers) { # In a perfect world this would be parallel. # score[j] <- norm(nevergrad_hp_val[j]) # } # Parallel version. # Actually this could be asynchronous, Nevergrad is ok for that, you do not have to # do the tell's in the same order as the ask's. registerDoParallel(cores=num_workers) getDoParWorkers() foreach(i=1:num_workers) %dopar% score[j] <- norm(nevergrad_hp_val[j]) for (j in 1:num_workers) { optimizer$tell(nevergrad_hp[j], score[j]) } } print(optimizer$recommend()$value) ``` -------------------------------- ### Initialize Nevergrad Optimizers Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/optimizers_ref.md Examples showing how to instantiate common Nevergrad optimization classes. These classes typically require a parametrization object and budget constraints. ```python import nevergrad as ng # Example: Initializing SPSA optimizer optimizer = ng.optimizers.SPSA(parametrization=10, budget=100) # Example: Initializing a Rescaled optimizer rescaled_opt = ng.optimizers.Rescaled(base_optimizer=ng.optimizers.MetaCMA, scale=0.001) ``` -------------------------------- ### Ask and Tell Interface - Sequential Optimization Example Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/optimization.md Demonstrates the basic usage of the ask and tell interface for sequential optimization, where candidates are asked, evaluated, and then told to the optimizer. ```APIDOC ## POST /optimizer/ask ### Description Suggests a candidate point for function evaluation. ### Method POST ### Endpoint /optimizer/ask ### Parameters #### Query Parameters - **optimizer_id** (string) - Required - The ID of the optimizer instance. ### Response #### Success Response (200) - **candidate** (object) - The suggested candidate point for evaluation. ### Response Example ```json { "candidate": { "args": [0.1, 0.2], "kwargs": {"y": 1.5} } } ``` ## POST /optimizer/tell ### Description Updates the optimizer with the function value for a previously asked candidate. ### Method POST ### Endpoint /optimizer/tell ### Parameters #### Query Parameters - **optimizer_id** (string) - Required - The ID of the optimizer instance. - **candidate** (object) - Required - The candidate point that was evaluated. - **loss** (float) - Required - The computed loss for the candidate. ### Response #### Success Response (200) - **status** (string) - "OK" indicating the update was successful. ### Response Example ```json { "status": "OK" } ``` ## POST /optimizer/provide_recommendation ### Description Returns the best candidate found by the optimizer so far. ### Method POST ### Endpoint /optimizer/provide_recommendation ### Parameters #### Query Parameters - **optimizer_id** (string) - Required - The ID of the optimizer instance. ### Response #### Success Response (200) - **recommendation** (object) - The best candidate recommended by the optimizer. ### Response Example ```json { "recommendation": { "args": [0.15, 0.25], "kwargs": {"y": 1.4} } } ``` ``` -------------------------------- ### Initialize Nevergrad Evolution Strategy Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/optimizers_ref.md Demonstrates the setup of the experimental EvolutionStrategy class, which supports configurable recombination ratios and population management strategies. ```python from nevergrad.families import EvolutionStrategy # Initialize with specific offspring and ranker settings optimizer = EvolutionStrategy( recombination_ratio=0.1, popsize=40, offsprings=20, only_offsprings=True, ranker="nsga2" ) ``` -------------------------------- ### Basic Optimization Example in Python Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/getting_started.md Demonstrates how to perform a basic optimization using Nevergrad. It defines a simple quadratic function and uses the NGOpt optimizer to find its minimum. The recommendation object holds the best found value. ```python import nevergrad as ng def square(x): return sum((x - 0.5) ** 2) # optimization on x as an array of shape (2,) optimizer = ng.optimizers.NGOpt(parametrization=2, budget=100) recommendation = optimizer.minimize(square) # best value print(recommendation.value) # >>> [0.49999998 0.50000004] ``` -------------------------------- ### Constraint Handling Source: https://context7.com/facebookresearch/nevergrad/llms.txt Illustrates how to register and utilize cheap constraints, both boolean and float-valued, to guide the optimization process towards feasible solutions. ```APIDOC ## Constraint Handling Nevergrad supports cheap constraints through the `register_cheap_constraint` method. Constraints can be boolean (satisfied/not) or float-valued (>= 0 means satisfied) for smoother optimization. ### Method ```python import nevergrad as ng import numpy as np def objective_with_constraints(x): """Minimize sum of squares.""" return np.sum(x ** 2) # Create parametrization param = ng.p.Array(shape=(5,))) # Add constraints: sum of x must be >= 2, and x[0] must be >= 0.5 param.register_cheap_constraint(lambda x: np.sum(x) - 2) # Float-valued: >= 0 is OK param.register_cheap_constraint(lambda x: x[0] - 0.5) # x[0] >= 0.5 optimizer = ng.optimizers.NGOpt(parametrization=param, budget=200) recommendation = optimizer.minimize(objective_with_constraints, verbosity=0) solution = recommendation.value print(f"Solution: {solution}") print(f"Sum constraint satisfied: {np.sum(solution) >= 2}") print(f"x[0] constraint satisfied: {solution[0] >= 0.5}") ``` ``` -------------------------------- ### Suggesting a Point for Optimization with Array Parametrization in Python Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/optimization.md Demonstrates using the `suggest` method with an optimizer parametrized by an `ng.p.Array`. This allows you to provide a specific array as the initial suggestion, which will be returned by the next `ask` call. The example also shows the equivalent `spawn_child` method. ```python import nevergrad as ng optim = ng.optimizers.NGOpt(parametrization=2, budget=100) optim.suggest([12, 12]) candidate = optim.ask() # equivalent to: candidate = optim.parametrization.spawn_child(new_value=[12, 12]) # you can then use to tell the loss optim.tell(candidate, 2.0) ``` -------------------------------- ### Suggesting Initial Points for Optimization Source: https://context7.com/facebookresearch/nevergrad/llms.txt Seeds an optimizer with known good solutions using the `suggest` method or by manually creating and `tell`ing candidates. This leverages prior knowledge to guide the optimization process more effectively. ```python import nevergrad as ng def objective(x, y): return (x - 3) ** 2 + (y - 7) ** 2 # Create parametrization param = ng.p.Instrumentation( x=ng.p.Scalar(lower=0, upper=10), y=ng.p.Scalar(lower=0, upper=10) ) optimizer = ng.optimizers.NGOpt(parametrization=param, budget=50) # Suggest a starting point (will be used in next ask) optimizer.suggest(x=2.5, y=6.5) # Alternative: Create and tell a candidate directly candidate = optimizer.parametrization.spawn_child(new_value=((tuple(), {"x": 3.1, "y": 7.2}))) optimizer.tell(candidate, objective(**candidate.kwargs)) # Run optimization recommendation = optimizer.minimize(objective) print(f"Result: x={recommendation.kwargs['x']:.2f}, y={recommendation.kwargs['y']:.2f}") ``` -------------------------------- ### Register Cheap Constraints for Optimization Source: https://context7.com/facebookresearch/nevergrad/llms.txt Shows how to enforce constraints on parameters using the register_cheap_constraint method. It supports both boolean and float-valued constraints to guide the optimization process. ```python import nevergrad as ng import numpy as np def objective_with_constraints(x): return np.sum(x ** 2) param = ng.p.Array(shape=(5,)) param.register_cheap_constraint(lambda x: np.sum(x) - 2) param.register_cheap_constraint(lambda x: x[0] - 0.5) optimizer = ng.optimizers.NGOpt(parametrization=param, budget=200) recommendation = optimizer.minimize(objective_with_constraints, verbosity=0) solution = recommendation.value print(f"Solution: {solution}") print(f"Sum constraint satisfied: {np.sum(solution) >= 2}") print(f"x[0] constraint satisfied: {solution[0] >= 0.5}") ``` -------------------------------- ### Handling Constraints in Nevergrad Optimization Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/optimization.md Illustrates how to incorporate constraints into the optimization process using Nevergrad. This can be done by providing constraint violation information directly to the 'tell' method or by defining constraints within the 'minimize' method. The example also shows how to register 'cheap' constraints, which are repeatedly evaluated until satisfied, using a lambda function. ```python import nevergrad as ng def square(x): return sum((x - 0.5) ** 2) optimizer = ng.optimizers.NGOpt(parametrization=2, budget=100) # define a constraint on first variable of x: x[0] >= 1 optimizer.parametrization.register_cheap_constraint(lambda x: x[0] >= 1) recommendation = optimizer.minimize(square, verbosity=2) print(recommendation.value) ``` ```python import nevergrad as ng def square(x): return sum((x - 0.5) ** 2) optimizer = ng.optimizers.NGOpt(parametrization=2, budget=100) # define a constraint on first variable of x: x[0] - 1 (>= 0 if ok) optimizer.parametrization.register_cheap_constraint(lambda x: x[0] - 1) recommendation = optimizer.minimize(square, verbosity=2) print(recommendation.value) ``` -------------------------------- ### Resume Optimization from Checkpoint Source: https://context7.com/facebookresearch/nevergrad/llms.txt Demonstrates how to continue an optimization process using a loaded optimizer instance. It iterates through a budget, asks for candidates, and cleans up the checkpoint file upon completion. ```python for _ in range(50): candidate = loaded_optimizer.ask() loaded_optimizer.tell(candidate, objective(candidate.value)) recommendation = loaded_optimizer.provide_recommendation() print(f"Final result: {recommendation.value}") checkpoint_path.unlink() ``` -------------------------------- ### Build Documentation Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/contributing.md Command to generate project documentation locally from the docs directory. ```bash make html ``` -------------------------------- ### Install Nevergrad using conda Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/getting_started.md Installs Nevergrad from the conda-forge channel. This is an alternative installation method for users who prefer using the Anaconda distribution and its package manager. ```bash conda install -c conda-forge nevergrad ``` -------------------------------- ### Initialize and Use Nevergrad Optimizers Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/optimizers_ref.md Demonstrates how to instantiate a Nevergrad optimizer, such as the BayesOptim class, and retrieve the recommended candidate after the optimization process. This process involves defining the parametrization and budget before calling the recommend method. ```python import nevergrad as ng from nevergrad.optimization import optimizerlib # Initialize the optimizer optimizer = optimizerlib.BayesOptim(parametrization=10, budget=100) # Enable pickling if necessary optimizer.enable_pickling() # Retrieve the best candidate after optimization recommendation = optimizer.recommend() print(recommendation.args, recommendation.kwargs) ``` -------------------------------- ### Install Nevergrad using pip Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/getting_started.md Installs the Nevergrad library using pip. This is the standard method for installing Python packages. It ensures you have the latest stable release. ```bash pip install nevergrad ``` -------------------------------- ### Install Nevergrad from main branch using pip Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/getting_started.md Installs Nevergrad directly from its main branch on GitHub. This is useful for testing the latest development version. It requires git to be installed. ```bash pip install git+https://github.com/facebookresearch/nevergrad@main#egg=nevergrad ``` -------------------------------- ### Install Nevergrad in Development Mode Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/contributing.md Commands to install the package in editable mode with all dependencies. Use the zsh-specific syntax if required by your shell. ```bash pip install -e .[all] ``` ```bash pip install -e '.[all]' ``` -------------------------------- ### Initialize Scipy-based Optimizer Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/optimizers_ref.md Demonstrates how to instantiate a NonObjectOptimizer which wraps Scipy optimization methods like Nelder-Mead or SQP. ```python from nevergrad.families import NonObjectOptimizer # Initialize a Nelder-Mead optimizer optimizer = NonObjectOptimizer(method='Nelder-Mead', random_restart=True) ``` -------------------------------- ### GET /nevergrad/p/Parameter/standardized_data Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/parametrization_ref.md Retrieves the standardized data representation of a parameter in the optimization space. ```APIDOC ## GET /nevergrad/p/Parameter/standardized_data ### Description Returns the standardized data representing the value of the instance as an array in the optimization space. ### Method GET ### Endpoint /nevergrad/p/Parameter/standardized_data ### Parameters #### Query Parameters - **reference** (Parameter) - Required - The reference instance for representation in the standardized data space. ### Response #### Success Response (200) - **data** (np.ndarray) - The representation of the value in the optimization space. ### Response Example { "data": [0.1, -0.5, 0.2] } ``` -------------------------------- ### Suggesting a Point for Optimization with Instrumentation in Python Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/optimization.md Illustrates how to use the `suggest` method to pre-seed an optimizer with a specific candidate point when using `ng.p.Instrumentation`. The subsequent `ask` call will return this suggested point, allowing for controlled initialization or providing prior knowledge. ```python import nevergrad as ng param = ng.p.Instrumentation(ng.p.Choice(["a", "b", "c"]), lr=ng.p.Log(lower=0.001, upper=1.0)) optim = ng.optimizers.NGOpt(parametrization=param, budget=100) optim.suggest("c", lr=0.02) candidate = optim.ask() # equivalent to: candidate = optim.parametrization.spawn_child(new_value=(("c",), {"lr": 0.02})) # you can then use to tell the loss optim.tell(candidate, 2.0) ``` -------------------------------- ### nevergrad.optimizers.base.ConfiguredOptimizer Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/optimizers_ref.md Creates optimizer-like instances with configuration, allowing for flexible optimizer setup. ```APIDOC ## nevergrad.optimizers.base.ConfiguredOptimizer ### Description Creates optimizer-like instances with configuration. ### Parameters #### Path Parameters - **OptimizerClass** (type) - Required - class of the optimizer to configure, or another ConfiguredOptimizer (config will then be ignored except for the optimizer name/representation) - **config** (dict) - Required - dictionnary of all the configurations - **as_config** (bool) - Optional - whether to provide all config as kwargs to the optimizer instantiation (default, see ConfiguredCMA for an example), or through a config kwarg referencing self. (if True, see EvolutionStrategy for an example) #### NOTE This provides a default repr which can be bypassed through set_name ``` -------------------------------- ### Optimizer Core Methods: ask, tell, provide_recommendation Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/optimizers_ref.md The core functionality of Nevergrad optimizers revolves around three main methods: `ask()` to get a candidate point, `tell(candidate, loss)` to report the evaluation result, and `provide_recommendation()` to get the best found solution. This forms the basis of the optimization loop. ```python def ask() -> Parameter: """Provides a point to explore.""" pass def tell(candidate, loss): """Lets you provide the loss associated to points.""" pass def provide_recommendation() -> Parameter: """Provides the best final candidate.""" pass ``` -------------------------------- ### Configure ConfSplitOptimizer for variable partitioning Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/optimizers_ref.md Demonstrates how to initialize the ConfSplitOptimizer to distribute optimization tasks across multiple variables. It shows different ways to define the number of optimizers and variables per optimizer. ```python from nevergrad.families import ConfSplitOptimizer # Using explicit variable counts per optimizer opt1 = ConfSplitOptimizer(num_vars=[2, 2, 2, 2, 2])(parametrization=10, num_workers=3) # Using number of optimizers opt2 = ConfSplitOptimizer(num_workers=3, num_optims=5)(parametrization=10) ``` -------------------------------- ### GET /optimizers Source: https://github.com/facebookresearch/nevergrad/blob/main/nevergrad/benchmark/optimizer_groups.txt Retrieves the list of available optimizers categorized by their specific use cases within the Nevergrad library. ```APIDOC ## GET /optimizers ### Description Returns a structured list of available optimization algorithms categorized by their performance characteristics and intended use cases. ### Method GET ### Endpoint /optimizers ### Parameters None ### Request Example GET /optimizers ### Response #### Success Response (200) - **large** (array) - List of large-scale optimization algorithms. - **multimodal** (array) - List of algorithms suitable for multimodal landscapes. - **noisy** (array) - List of algorithms designed for noisy objective functions. - **oneshot** (array) - List of one-shot optimization strategies. - **scipy** (array) - List of algorithms wrapping SciPy methods. #### Response Example { "large": ["CMA", "PSO", "DE"], "multimodal": ["NaiveTBPSA", "MultiCMA"], "noisy": ["SPSA", "TBPSA"] } ``` -------------------------------- ### Perform Basic Optimization with NGOpt Source: https://context7.com/facebookresearch/nevergrad/llms.txt Demonstrates the use of the NGOpt meta-optimizer to minimize a simple quadratic objective function. It shows how to define a parameter space and retrieve the optimal solution. ```python import nevergrad as ng def objective_function(x): """Simple quadratic function to minimize.""" return sum((x - 0.5) ** 2) # Create optimizer with 2-dimensional continuous parameter space optimizer = ng.optimizers.NGOpt(parametrization=2, budget=100) # Run minimization recommendation = optimizer.minimize(objective_function) # Access the optimal solution print(recommendation.value) print(recommendation.loss) ``` -------------------------------- ### Define Placeholders in External Code Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/parametrization.md Example of using NG_ARG placeholders in C code to enable optimization of external scripts. ```cpp int step_size = 0.1 // @nevergrad@ step_size = NG_ARG{step|any comment} ``` -------------------------------- ### Code Formatting and Linting Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/contributing.md Commands to install pre-commit hooks for automated style enforcement and to manually format code using black. ```bash pre-commit install ``` ```bash black nevergrad ``` -------------------------------- ### Basic Optimization with NGOpt Source: https://context7.com/facebookresearch/nevergrad/llms.txt Demonstrates how to use the NGOpt optimizer for basic minimization of a continuous objective function with a specified budget. ```APIDOC ## Basic Optimization with NGOpt ### Description This example shows how to perform basic optimization using Nevergrad's `NGOpt` optimizer. It defines a simple quadratic objective function and minimizes it over a 2-dimensional continuous parameter space with a budget of 100 evaluations. ### Method `optimizer.minimize(objective_function)` ### Endpoint N/A (This is a Python library usage example) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python import nevergrad as ng def objective_function(x): """Simple quadratic function to minimize.""" return sum((x - 0.5) ** 2) # Create optimizer with 2-dimensional continuous parameter space optimizer = ng.optimizers.NGOpt(parametrization=2, budget=100) # Run minimization recommendation = optimizer.minimize(objective_function) # Access the optimal solution print(recommendation.value) # Output: array([0.49971112, 0.5002944]) print(recommendation.loss) # Output: ~1.2e-7 ``` ### Response #### Success Response (200) N/A (This is a Python library usage example) #### Response Example ``` [0.49971112 0.5002944] 1.23456e-07 ``` ``` -------------------------------- ### Get Parameter Value Hash Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/parametrization_ref.md Retrieves a hashable object representing the current value of the parameter instance. This is useful for tracking parameter states. ```python param.get_value_hash() ``` -------------------------------- ### Configure Chaining Optimizer Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/optimizers_ref.md Shows how to set up the Chaining optimizer to run a sequence of different optimization algorithms, each with a specified budget. This allows for complex optimization strategies where the state is passed between consecutive algorithms. ```python from nevergrad.optimization import optimizerlib # Define a sequence of optimizers and their respective budgets chain = optimizerlib.Chaining( optimizers=[optimizerlib.CMA, optimizerlib.SQP], budgets=[50, 50] ) ``` -------------------------------- ### Initialize MetaModel Optimizer Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/optimizers_ref.md Wraps an existing optimizer with a metamodel to improve performance based on frequency ratios. ```python from nevergrad.families import ParametrizedMetaModel # Add a metamodel to an optimizer meta_optimizer = ParametrizedMetaModel(frequency_ratio=0.9, degree=2) ``` -------------------------------- ### Parallel Basic Optimizers in Nevergrad Source: https://github.com/facebookresearch/nevergrad/blob/main/nevergrad/benchmark/optimizer_groups.txt These are fundamental optimizers that can be utilized in parallel settings. They provide a good starting point for parallel optimization strategies. ```python parallel_basics = ['NGOpt10', 'CMandAS2', 'CMA', 'DE', 'MetaModel'] ``` -------------------------------- ### Optimization Loop with Ask/Tell Interface Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/machinelearning.md Demonstrates the manual ask/tell loop for optimization, allowing for fine-grained control and asynchronous execution patterns. ```APIDOC ## POST /optimize/ask-tell ### Description Performs a manual optimization loop where the user requests parameters via 'ask' and reports results via 'tell'. ### Method POST ### Parameters #### Request Body - **optimizer_name** (string) - Required - Name of the optimizer to use. - **budget** (int) - Required - Total number of iterations. ### Request Example { "optimizer_name": "CMA", "budget": 1200 } ### Response #### Success Response (200) - **recommendation** (object) - The final optimized parameters and their associated function value. ``` -------------------------------- ### Sequential Optimization with Ask and Tell Interface in Python Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/optimization.md Demonstrates a sequential optimization loop using Nevergrad's ask and tell interface. It initializes an instrumentation and an optimizer, then iteratively asks for a candidate, evaluates a function, and tells the result back to the optimizer. Finally, it retrieves the best recommendation. ```python import nevergrad as ng def square(x, y=12): return sum((x - 0.5) ** 2) + abs(y) instrum = ng.p.Instrumentation(ng.p.Array(shape=(2,)), y=ng.p.Scalar()) optimizer = ng.optimizers.NGOpt(parametrization=instrum, budget=100, num_workers=1) for _ in range(optimizer.budget): x = optimizer.ask() loss = square(*x.args, **x.kwargs) optimizer.tell(x, loss) recommendation = optimizer.provide_recommendation() print(recommendation.value) ``` -------------------------------- ### Advanced Parametrization with Instrumentation Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/optimization.md This example demonstrates advanced function optimization using `ng.p.Instrumentation`. It defines a parametrization that optimizes both a continuous array `x` (bounded) and a continuous scalar `y`. The `NGOpt` optimizer is used with this complex parametrization. ```python import nevergrad as ng def square(x, y=12): return sum((x - 0.5) ** 2) + abs(y) instrum = ng.p.Instrumentation( ng.p.Array(shape=(2,)).set_bounds(lower=-12, upper=12), y=ng.p.Scalar() ) optimizer = ng.optimizers.NGOpt(parametrization=instrum, budget=100) recommendation = optimizer.minimize(square) print(recommendation.value) ``` -------------------------------- ### Parallel Optimization with Instrumentation Source: https://context7.com/facebookresearch/nevergrad/llms.txt Demonstrates how to define a parameter space using Instrumentation and run an optimization task in parallel using a ProcessPoolExecutor. This approach is ideal for computationally expensive objective functions. ```python import nevergrad as ng from concurrent import futures param = ng.p.Instrumentation( x=ng.p.Scalar(lower=-10, upper=10), y=ng.p.Scalar(lower=-10, upper=10), z=ng.p.Scalar(lower=-10, upper=10) ) optimizer = ng.optimizers.NGOpt(parametrization=param, budget=50, num_workers=4) with futures.ProcessPoolExecutor(max_workers=optimizer.num_workers) as executor: recommendation = optimizer.minimize(slow_objective, executor=executor, batch_mode=False) print(f"Optimal: x={recommendation.kwargs['x']:.2f}, y={recommendation.kwargs['y']:.2f}, z={recommendation.kwargs['z']:.2f}") ``` -------------------------------- ### Get Recommended Candidate (Python) Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/optimizers_ref.md Returns the best candidate parameter that minimizes the loss, based on the optimization budget used. Similar to `provide_recommendation`, the `p.Parameter` object contains `args` and `kwargs` for direct use with the objective function. ```python recommend() -> Parameter ``` -------------------------------- ### Optimization with Constraints Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/optimization.md Explains how to apply constraints during optimization using the `tell` method or the `minimize` function, and introduces the concept of 'cheap' constraints. ```APIDOC ## Optimization with Constraints When optimizing with constraints, you can pass constraint violations directly to the `tell` method or the `minimize` function. ### Using `tell` with constraints: ```python optimizer.tell(candidate, value, [constraint_violation1, constraint_violation2, constraint_violation3]) ``` ### Using `minimize` with constraints: ```python optimizer.minimize(loss_function, constraint_violations) ``` where `constraint_violations` maps a candidate to a vector of constraint violations. ### Cheap Constraints 'Cheap' constraints do not aim to reduce call counts and repeat mutations until a satisfiable point is reached. #### Example with boolean constraint: ```python import nevergrad as ng def square(x): return sum((x - 0.5) ** 2) optimizer = ng.optimizers.NGOpt(parametrization=2, budget=100) # define a constraint on the first variable of x: x[0] >= 1 optimizer.parametrization.register_cheap_constraint(lambda x: x[0] >= 1) recommendation = optimizer.minimize(square, verbosity=2) print(recommendation.value) # >>> [1.00037625, 0.50683314] ``` #### Example with float-valued constraint (>= 0 if ok): ```python import nevergrad as ng def square(x): return sum((x - 0.5) ** 2) optimizer = ng.optimizers.NGOpt(parametrization=2, budget=100) # define a constraint on the first variable of x: x[0] - 1 optimizer.parametrization.register_cheap_constraint(lambda x: x[0] - 1) recommendation = optimizer.minimize(square, verbosity=2) print(recommendation.value) # >>> [1.00037625, 0.50683314] ``` **Warning:** Float-valued constraints are generally preferred over boolean ones. ``` -------------------------------- ### Suggesting Points for Inoculation Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/optimization.md Explains how to inoculate the optimizer with prior knowledge by suggesting specific points. ```APIDOC ## POST /optimizer/suggest ### Description Suggests a specific point to the optimizer, influencing the next `ask` call. This is useful for providing prior knowledge or starting points. ### Method POST ### Endpoint /optimizer/suggest ### Parameters #### Query Parameters - **optimizer_id** (string) - Required - The ID of the optimizer instance. - **args** (array) - Optional - Positional arguments for the suggested point. - **kwargs** (object) - Optional - Keyword arguments for the suggested point. ### Request Example ```json { "optimizer_id": "my_optimizer_123", "args": ["c"], "kwargs": {"lr": 0.02} } ``` ### Response #### Success Response (200) - **status** (string) - "OK" indicating the suggestion was registered. ### Response Example ```json { "status": "OK" } ``` ## POST /optimizer/parametrization/spawn_child ### Description Creates a child candidate from the optimizer's parametrization with a specified new value. This candidate can then be used with `tell`. ### Method POST ### Endpoint /optimizer/parametrization/spawn_child ### Parameters #### Query Parameters - **optimizer_id** (string) - Required - The ID of the optimizer instance. - **new_value** (any) - Required - The new value to set for the spawned child candidate. ### Request Example ```json { "optimizer_id": "my_optimizer_456", "new_value": [12, 12] } ``` ### Response #### Success Response (200) - **candidate** (object) - The spawned child candidate. ### Response Example ```json { "candidate": { "args": [[12, 12]], "kwargs": {} } } ``` **Note**: Some optimizers do not support suggesting points or telling non-asked points. Attempting to do so will result in a `TellNotAskedNotSupportedError`. ``` -------------------------------- ### Initialize Nevergrad SplitOptimizer Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/optimizers_ref.md Demonstrates how to initialize the SplitOptimizer to manage multiple optimizers across different variable subsets. It shows both explicit variable assignment and automatic distribution. ```python from nevergrad.optimization.optimizerlib import ConfSplitOptimizer, SplitOptimizer # Using explicit variable counts per optimizer opt1 = ConfSplitOptimizer(num_vars=[2, 2, 2, 2, 2])(parametrization=10, num_workers=3) # Equivalent using the SplitOptimizer class directly opt2 = SplitOptimizer(parametrization=10, num_workers=3, num_vars=[2, 2, 2, 2, 2]) # Distributing across 5 optimizers automatically opt3 = SplitOptimizer(parametrization=10, num_workers=3, num_optims=5) ``` -------------------------------- ### Parallel Optimization with Multiple Workers Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/optimization.md This example demonstrates how to perform optimization in parallel using multiple workers. It configures the `NGOpt` optimizer with `num_workers=2` and utilizes `concurrent.futures.ThreadPoolExecutor` to manage parallel evaluations of the `square` function with a specified instrumentation and budget. ```python from concurrent import futures import nevergrad as ng def square(x, y=12): return sum((x - 0.5) ** 2) + abs(y) instrum = ng.p.Instrumentation( ng.p.Array(shape=(2,)).set_bounds(lower=-12, upper=12), y=ng.p.Scalar() ) optimizer = ng.optimizers.NGOpt(parametrization=instrum, budget=10, num_workers=2) # We use ThreadPoolExecutor for CircleCI but please # use the line just below, with ProcessPoolExecutor instead (unless your # code is I/O bound rather than CPU bound): with futures.ThreadPoolExecutor(max_workers=optimizer.num_workers) as executor: #with futures.ProcessPoolExecutor(max_workers=optimizer.num_workers) as executor: recommendation = optimizer.minimize(square, executor=executor, batch_mode=False) ``` -------------------------------- ### Cast to Integer with Nevergrad Int Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/parametrization_ref.md This snippet demonstrates how to use the `nevergrad.ops.Int` class to cast data to integers. It supports deterministic rounding to the nearest integer and non-deterministic sampling where integers are chosen based on proximity to the original value. The example shows its usage with an array parameter. ```python import nevergrad as ng # Deterministic mode: 0.2 is cast to 0 param_deterministic = ng.ops.Int(deterministic=True)(ng.p.Array(shape=(3,))) # Non-deterministic mode: 0.2 can be cast to 0 or 1 param_non_deterministic = ng.ops.Int(deterministic=False)(ng.p.Array(shape=(3,))) ``` -------------------------------- ### Bounded Array Optimization Source: https://context7.com/facebookresearch/nevergrad/llms.txt Demonstrates how to define and use bounded arrays for optimization parameters, ensuring that solutions stay within specified lower and upper limits. ```APIDOC ## Bounded Array Optimization Arrays can be constrained with lower and upper bounds, and mutations are automatically adapted to respect these constraints. This is essential for optimization problems with physical or practical limits on parameter values. ### Method ```python import nevergrad as ng import numpy as np def constrained_objective(x): """Objective with optimal solution at boundary.""" # Minimum at [1, 1, 1, ...] which is at the upper bound return np.sum((x - 1) ** 2) + 0.1 * np.sum(np.abs(x)) # Create bounded array parameter param = ng.p.Array(shape=(10,)).set_bounds(lower=-2, upper=1) # PSO handles bounds naturally optimizer = ng.optimizers.PSO(parametrization=param, budget=500) recommendation = optimizer.minimize(constrained_objective) print(f"Solution: {recommendation.value}") print(f"All within bounds: {np.all(recommendation.value >= -2) and np.all(recommendation.value <= 1)}") # Output: True ``` ``` -------------------------------- ### Minimize a simple function with NGOpt Source: https://github.com/facebookresearch/nevergrad/blob/main/README.md Demonstrates how to define a simple objective function and use the NGOpt optimizer to find the minimum value within a specified budget. ```python import nevergrad as ng def square(x): return sum((x - .5)**2) optimizer = ng.optimizers.NGOpt(parametrization=2, budget=100) recommendation = optimizer.minimize(square) print(recommendation.value) ``` -------------------------------- ### Get Pareto Front (Python) Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/optimizers_ref.md Retrieves the Pareto front, which represents a set of optimal solutions in a multi-objective optimization context. It can return the full front or a subset based on specified criteria like 'random', 'loss-covering', 'domain-covering', or 'hypervolume'. The losses of each parameter can be accessed via `parameter.losses`. ```python pareto_front(size: int | None = None, subset: str = 'random', subset_tentatives: int = 12) -> List[Parameter] ``` -------------------------------- ### Bayesian Optimization for Expensive Black-Box Functions Source: https://context7.com/facebookresearch/nevergrad/llms.txt Implements Bayesian Optimization (BO), suitable for low-dimensional, expensive functions. It uses a Gaussian Process surrogate model and an acquisition function (like UCB) to efficiently guide the search. Note that BO in Nevergrad typically does not support parallelization. ```python import nevergrad as ng def expensive_blackbox(x, y, z): """Simulated expensive evaluation.""" return (x - 1) ** 2 + (y + 0.5) ** 2 + (z - 2) ** 2 # Configure Bayesian Optimization bo_config = ng.families.ParametrizedBO( initialization="LHS", # Latin Hypercube Sampling for init init_budget=10, # Number of initial random samples middle_point=True, # Include the center point utility_kind="ucb", # Upper Confidence Bound acquisition utility_kappa=2.576 # Exploration parameter ) param = ng.p.Instrumentation( x=ng.p.Scalar(lower=-5, upper=5), y=ng.p.Scalar(lower=-5, upper=5), z=ng.p.Scalar(lower=-5, upper=5) ) # Note: BO doesn't support parallelization optimizer = bo_config(parametrization=param, budget=30, num_workers=1) recommendation = optimizer.minimize(expensive_blackbox) print(f"BO result: x={recommendation.kwargs['x']:.2f}, " f"y={recommendation.kwargs['y']:.2f}, z={recommendation.kwargs['z']:.2f}") ``` -------------------------------- ### Configure Portfolio Optimizer Source: https://context7.com/facebookresearch/nevergrad/llms.txt Shows how to combine multiple optimization algorithms into a single portfolio. This approach improves robustness by leveraging different strategies simultaneously during the optimization process. ```python import nevergrad as ng def complex_objective(x): import numpy as np return np.sum(x ** 2) + 10 * np.sum(np.sin(x) ** 2) + np.sum(np.abs(x)) portfolio_config = ng.families.ConfPortfolio( optimizers=[ ng.optimizers.CMA, ng.optimizers.TwoPointsDE, ng.optimizers.PSO, ng.optimizers.OnePlusOne ], warmup_ratio=0.3 ) optimizer = portfolio_config(parametrization=15, budget=500, num_workers=4) recommendation = optimizer.minimize(complex_objective) print(f"Portfolio result: loss = {recommendation.loss:.6f}") ``` -------------------------------- ### List and Select Optimizers Programmatically Source: https://context7.com/facebookresearch/nevergrad/llms.txt Explains how to access the Nevergrad optimizer registry to list available algorithms and implement custom logic for selecting an optimizer based on problem dimensions and budget. ```python import nevergrad as ng all_optimizers = sorted(ng.optimizers.registry.keys()) def select_optimizer(dimension, budget, is_noisy=False): if is_noisy: return ng.optimizers.TBPSA elif dimension < 10 and budget > 100: return ng.optimizers.CMA elif budget < 50: return ng.optimizers.OnePlusOne else: return ng.optimizers.NGOpt opt_class = select_optimizer(dimension=5, budget=200, is_noisy=False) optimizer = opt_class(parametrization=5, budget=200) ``` -------------------------------- ### Chaining Optimization Algorithms in Nevergrad Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/optimization.md Shows how to chain multiple Nevergrad optimizers together to form a composite optimization strategy. This allows information to be passed from one optimizer to the next, distributing the budget across them. Examples include chaining with different budget allocation strategies based on the number of workers or the dimension of the problem. ```python # Running LHSSearch with budget num_workers and then DE: DEwithLHS = Chaining([LHSSearch, DE], ["num_workers"]) # Runninng LHSSearch with budget the dimension and then DE: DEwithLHSdim = Chaining([LHSSearch, DE], ["dimension"]) # Runnning LHSSearch with budget 30 and then DE: DEwithLHS30 = Chaining([LHSSearch, DE], [30]) ``` -------------------------------- ### Saving and Loading Optimizer Checkpoints Source: https://context7.com/facebookresearch/nevergrad/llms.txt Demonstrates how to save (pickle) and load optimizer states using the `dump` and `load` methods. This is crucial for checkpointing long-running optimization jobs, allowing them to be resumed after interruptions. ```python import nevergrad as ng from pathlib import Path def objective(x): return sum((x - 0.42) ** 2) # Create and run partial optimization optimizer = ng.optimizers.NGOpt(parametrization=5, budget=100) optimizer.enable_pickling() # Required for some optimizers # Run partial optimization for _ in range(50): candidate = optimizer.ask() optimizer.tell(candidate, objective(candidate.value)) # Save checkpoint checkpoint_path = Path("optimizer_checkpoint.pkl") optimizer.dump(checkpoint_path) print(f"Saved at iteration {optimizer.num_tell}") # Later: Load and continue loaded_optimizer = ng.optimizers.NGOpt.load(checkpoint_path) print(f"Resumed at iteration {loaded_optimizer.num_tell}") ``` -------------------------------- ### Perform Manual Ask-and-Tell Optimization Loop Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/machinelearning.md Illustrates the manual ask-and-tell optimization pattern. This approach provides fine-grained control over the optimization process, allowing for batch processing or custom parallelization strategies. ```python import nevergrad as ng budget = 1200 for name in ["RandomSearch", "ScrHammersleySearch", "TwoPointsDE", "PortfolioDiscreteOnePlusOne", "CMA", "PSO"]: optim = ng.optimizers.registry[name](parametrization=instru, budget=budget) for u in range(budget // 3): x1 = optim.ask() x2 = optim.ask() x3 = optim.ask() y1 = myfunction(*x1.args, **x1.kwargs) y2 = myfunction(*x2.args, **x2.kwargs) y3 = myfunction(*x3.args, **x3.kwargs) optim.tell(x1, y1) optim.tell(x2, y2) optim.tell(x3, y3) recommendation = optim.recommend() print("* ", name, " provides a vector of parameters with test error ", myfunction(*recommendation.args, **recommendation.kwargs)) ``` -------------------------------- ### Image Generation with Diverse Latent Variables using Nevergrad Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/examples/diversity/Diversity in image generation with Nevergrad.md This example demonstrates integrating Nevergrad's quasi-randomization into an image generation pipeline. It iterates through a set of quasi-randomized latent vectors to generate images, aiming for greater diversity compared to using standard random latent vectors. This is particularly relevant for latent diffusion models. ```python import numpy as np from nevergrad.common import quasi_randomize # Assume my_latent_image_generator is a function that creates an image from a latent vector def my_latent_image_generator(latent_vector): # Placeholder for actual image generation logic print(f"Generating image for latent vector of shape: {latent_vector.shape}") return np.zeros(latent_vector.shape) # Dummy image # x is a batch of 50 random latent vectors, with size 256x256 and 3 channels x = np.random.randn(50, 256, 256, 3) derand_x = quasi_randomize(x) Images = [] for i in range(50): Images.append(my_latent_image_generator(derand_x[i])) # The generated 'Images' list should exhibit more diversity than if x[i] was used. ``` -------------------------------- ### Registering Callbacks for Ask and Tell Methods in Nevergrad Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/optimization.md Demonstrates how to register custom callback functions for the 'ask' and 'tell' methods of a Nevergrad optimizer. Callbacks can be used to monitor or log optimization progress. The 'ask' callbacks receive the optimizer instance, while 'tell' callbacks receive the optimizer, candidate, and its value. This example shows a 'tell' callback that prints the candidate and its value. ```python import nevergrad as ng def my_function(x): return abs(sum(x - 1)) def print_candidate_and_value(optimizer, candidate, value): print(candidate, value) optimizer = ng.optimizers.NGOpt(parametrization=2, budget=4) optimizer.register_callback("tell", print_candidate_and_value) optimizer.minimize(my_function) ``` -------------------------------- ### Perform Multiobjective Minimization Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/optimization.md Shows how to define a multiobjective function, set a reference point, and extract the Pareto-front using Nevergrad optimizers. ```python import nevergrad as ng import numpy as np def multiobjective(x): return [np.sum(x**2), np.sum((x - 1) ** 2)] optimizer = ng.optimizers.CMA(parametrization=3, budget=100) optimizer.tell(ng.p.MultiobjectiveReference(), [5, 5]) optimizer.minimize(multiobjective, verbosity=2) for param in sorted(optimizer.pareto_front(), key=lambda p: p.losses[0]): print(f"{param} with losses {param.losses}") ``` -------------------------------- ### Initialize Nevergrad Differential Evolution Optimizer Source: https://github.com/facebookresearch/nevergrad/blob/main/docs/optimizers_ref.md Demonstrates how to instantiate the DifferentialEvolution optimizer class. This class is designed for continuous optimization and supports various crossover strategies and population sizing. ```python from nevergrad.families import DifferentialEvolution # Initialize with custom crossover and population size optimizer = DifferentialEvolution( crossover="twopoints", popsize="standard", F1=0.8, F2=0.8 ) ```