import pickle import warnings from pathlib import Path from numbers import Real from collections import deque import numpy as np import nevergrad.common.typing as tp from nevergrad.parametrization import parameter as p from nevergrad.common import tools as ngtools from nevergrad.common import errors as errors from nevergrad.common.decorators import Registry from . import utils from . import multiobjective as mobj OptCls = tp.Union["ConfiguredOptimizer", tp.Type["Optimizer"]] registry: Registry[OptCls] = Registry() _OptimCallBack = tp.Union[tp.Callable[["Optimizer", "p.Parameter", float], None], tp.Callable[["Optimizer"], None]] X = tp.TypeVar("X", bound="Optimizer") Y = tp.TypeVar("Y") IntOrParameter = tp.Union[int, p.Parameter] _PruningCallable = tp.Callable[[utils.Archive[utils.MultiValue]], utils.Archive[utils.MultiValue]] def _loss(param: p.Parameter) -> float: """Returns the loss if available, or inf otherwise. Used to simplify handling of losses """ return param.loss if param.loss is not None else float("inf")
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. """Samplers in [0,1]^d. """ import numpy as np from numpy.random import RandomState import nevergrad.common.typing as tp from nevergrad.common.decorators import Registry samplers: Registry[tp.Type['Sampler']] = Registry() def _get_first_primes(num: int) -> np.ndarray: """Computes the first num primes """ # upper bound for the value of the n_th prime_number (n >= 6) # https://en.wikipedia.org/wiki/Prime_number_theorem # Possible optimization, only odd numbers in is_prime array? if num < 6: return np.array([2, 3, 5, 7, 11][:num], dtype=int) is_prime = np.ones(int(1 + num * (np.log(np.log(num)) + np.log(num))), dtype=bool) is_prime[[0, 1]] = 0 for index in range(1 + int(np.sqrt(len(is_prime)))): if is_prime[index]: is_prime[index + index::index] = False primes = np.where(is_prime)[0] if len(primes) < num:
import pickle import warnings from pathlib import Path from numbers import Real from collections import deque import numpy as np import nevergrad.common.typing as tp from nevergrad.parametrization import parameter as p from nevergrad.common import tools as ngtools from nevergrad.common.decorators import Registry from . import utils from . import multiobjective as mobj registry: Registry[tp.Union["ConfiguredOptimizer", tp.Type["Optimizer"]]] = Registry() _OptimCallBack = tp.Union[tp.Callable[["Optimizer", "p.Parameter", float], None], tp.Callable[["Optimizer"], None]] X = tp.TypeVar("X", bound="Optimizer") Y = tp.TypeVar("Y") IntOrParameter = tp.Union[int, p.Parameter] _PruningCallable = tp.Callable[[utils.Archive[utils.MultiValue]], utils.Archive[utils.MultiValue]] def load(cls: tp.Type[X], filepath: tp.PathLike) -> X: """Loads a pickle file and checks that it contains an optimizer. The optimizer class is not always fully reliable though (e.g.: optimizer families) so the user is responsible for it. """ filepath = Path(filepath) with filepath.open("rb") as f: opt = pickle.load(f) assert isinstance(opt, cls), f"You should only load {cls} with this method (found {type(opt)})"
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. """Groups of optimizers for use in benchmarks """ import typing as tp import numpy as np import nevergrad as ng from nevergrad.common.decorators import Registry from nevergrad.optimization import base as obase Optim = tp.Union[obase.ConfiguredOptimizer, str] registry: Registry[tp.Callable[[], tp.Iterable[Optim]]] = Registry() def get_optimizers(*names: str, seed: tp.Optional[int] = None) -> tp.List[Optim]: """Returns an deterministically ordered list of optimizers, belonging to the provided groups. If a seed is provided, it is used to shuffle the list. Parameters ---------- *names: str names of the groups to use. See nevergrad/benchmarks/optimizer_groups.txt (generated/updated when running pytest) for the list of groups and what they contain seed: optional int a seed to shuffle the list of optimizers """ optims: tp.List[Optim] = []
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. from math import exp, sqrt, tanh import numpy as np import nevergrad.common.typing as tp from nevergrad.common.decorators import Registry registry: Registry[tp.Callable[[np.ndarray], float]] = Registry() class DiscreteFunction: def __init__(self, name: str, arity: int = 2) -> None: """Returns a classical discrete function for test, in the domain {0,1,...,arity-1}^d. The name can be onemax, leadingones, or jump. onemax(x) is the most classical case of discrete functions, adapted to minimization. It is originally designed for lists of bits. It just counts the number of 1, and returns len(x) - number of ones. However, the present function perturbates the location of the optimum, so that tests can not be easily biased by a wrong initialization. So the optimum, instead of being located at (1,1,...,1), is located at (0,1,2,...,arity-1,0,1,2,...). leadingones is the second most classical discrete function, adapted for minimization. Before perturbation of the location of the optimum as above, it returns len(x) - number of initial 1. I.e. leadingones([0 1 1 1]) = 4, leadingones([1 1 1 1]) = 0,
import pickle import warnings from pathlib import Path from numbers import Real from collections import deque import numpy as np import nevergrad.common.typing as tp from nevergrad.parametrization import parameter as p from nevergrad.common import tools as ngtools from nevergrad.common.decorators import Registry from . import utils from .multiobjective import HypervolumePareto registry: Registry[tp.Union["ConfiguredOptimizer", tp.Type["Optimizer"]]] = Registry() _OptimCallBack = tp.Union[tp.Callable[["Optimizer", "p.Parameter", float], None], tp.Callable[["Optimizer"], None]] X = tp.TypeVar("X", bound="Optimizer") Y = tp.TypeVar("Y") IntOrParameter = tp.Union[int, p.Parameter] _PruningCallable = tp.Callable[[utils.Archive[utils.MultiValue]], utils.Archive[utils.MultiValue]] def load(cls: tp.Type[X], filepath: tp.PathLike) -> X: """Loads a pickle file and checks that it contains an optimizer. The optimizer class is not always fully reliable though (e.g.: optimizer families) so the user is responsible for it. """ filepath = Path(filepath) with filepath.open("rb") as f:
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. import os import typing as tp import torch import numpy as np import lpips import cv2 from nevergrad.functions.base import UnsupportedExperiment as UnsupportedExperiment from nevergrad.common.decorators import Registry registry: Registry[tp.Any] = Registry() MODELS: tp.Dict[str, tp.Any] = {} class ImageLoss: REQUIRES_REFERENCE = True def __init__(self, reference: tp.Optional[np.ndarray] = None) -> None: if reference is not None: self.reference = reference assert len(self.reference.shape) == 3, self.reference.shape assert self.reference.min() >= 0.0 assert self.reference.max( ) <= 256.0, f"Image max = {self.reference.max()}" assert self.reference.max(
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. """Samplers in [0,1]^d. """ import numpy as np from numpy.random import RandomState import nevergrad.common.typing as tp from nevergrad.common.decorators import Registry samplers: Registry[tp.Type["Sampler"]] = Registry() def _get_first_primes(num: int) -> np.ndarray: """Computes the first num primes""" # upper bound for the value of the n_th prime_number (n >= 6) # https://en.wikipedia.org/wiki/Prime_number_theorem # Possible optimization, only odd numbers in is_prime array? if num < 6: return np.array([2, 3, 5, 7, 11][:num], dtype=int) is_prime = np.ones(int(1 + num * (np.log(np.log(num)) + np.log(num))), dtype=bool) is_prime[[0, 1]] = 0 for index in range(1 + int(np.sqrt(len(is_prime)))): if is_prime[index]: is_prime[index + index::index] = False primes = np.where(is_prime)[0] if len(primes) < num: raise RuntimeError(