facebookresearch / nevergrad

A Python toolbox for performing gradient-free optimization
https://facebookresearch.github.io/nevergrad/
MIT License
3.96k stars 354 forks source link

add modular-CMA in Nevergrad #1021

Open teytaud opened 3 years ago

teytaud commented 3 years ago

Modular CMA is packaged and should be easy to integrate in Nevergrad. Example of usage of "modcma" below:

import random import numpy as np from modcma import AskTellCMAES

DIMENSION = 10 POPULATION_SIZE = 20 STEP_SIZE = 1.0 TARGET = 79.34

def objective_function(x): """'Simple sphere function""" return np.linalg.norm(x) + TARGET

def example1(): """Example of running ask and tell consecutively.""" cma = AskTellCMAES( DIMENSION, lambda_=POPULATION_SIZE, init_sigma=STEP_SIZE, )

for generation in range(100):
    for _ in range(POPULATION_SIZE):
        xi = cma.ask()
        cma.tell(xi, objective_function(xi))

print(cma.parameters.fopt, cma.parameters.xopt)

def example2(): """Example of running ask first and telling in random order.""" cma = AskTellCMAES(DIMENSION, lambda_=POPULATION_SIZE, init_sigma=STEP_SIZE)

for generation in range(100):
    X = []
    for _ in range(POPULATION_SIZE):
        X.append(cma.ask())
    random.shuffle(X)

    np.random.shuffle(X)
    for xi in X:
        cma.tell(xi, objective_function(xi))

print(cma.parameters.fopt, cma.parameters.xopt)

def example3(): """Example of running ask-tell until convergence.""" cma = AskTellCMAES( DIMENSION, target=TARGET + 1e-8, lambda_=POPULATION_SIZE, init_sigma=STEP_SIZE ) while not any(cma.break_conditions): xi = cma.ask() cma.tell(xi, objective_function(xi))

print(cma.parameters.fopt, cma.parameters.xopt)

if name == "main": np.random.seed(42) example1() example2() example3()

CarolaDoerr commented 3 years ago

@Dvermetten and @jacobdenobel should be able to either do this or to help with any questions

c-bata commented 3 years ago

Hi :) I'm just wondering, can I instead add CMA-ES support in Nevergrad by using cmaes library (repo: https://github.com/CyberAgent/cmaes)?

cmaes library provides ask-and-tell API so that it is easy to integrate with Nevergrad. The implementation is reliable as adopted by Optuna framework and continuously benchmarked on GitHub Actions. https://medium.com/optuna/introduction-to-cma-es-sampler-ee68194c8f88

Furthermore, one of the most important characteristics of this library is Warm Starting CMA-ES support, one of the state-of-the-art methods on transfer hyperparameter optimization. Warm Starting CMA-ES utilizes the result on a source task and enables CMA-ES to be initialized on the promising region. Consequently, It significantly reduces the required number of evaluations.

I'm glad if you will have an interest in cmaes library. Thank you.

teytaud commented 3 years ago

in progress, #1133