# 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 warnings import numpy as np import nevergrad.common.typing as tp from . import discretization from . import utils from . import core from .container import Tuple from .data import Array # weird pylint issue on "Descriptors" # pylint: disable=no-value-for-parameter C = tp.TypeVar("C", bound="Choice") T = tp.TypeVar("T", bound="TransitionChoice") class BaseChoice(core.Dict): class ChoiceTag(tp.NamedTuple): cls: tp.Type[core.Parameter] arity: int @classmethod def as_tag(cls, param: core.Parameter) -> "BaseChoice.ChoiceTag": # arrays inherit tags to identify them as bound to a choice if cls in param.heritage: # type: ignore output = param.heritage[cls] # type: ignore assert isinstance(output, cls) return output
# # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. import inspect from pathlib import Path import numbers import numpy as np import nevergrad.common.typing as tp from nevergrad.common import errors from nevergrad.common.errors import ( # pylint: disable=unused-import UnsupportedExperiment as UnsupportedExperiment, ) from nevergrad.parametrization import parameter as p from nevergrad.optimization import multiobjective as mobj EF = tp.TypeVar("EF", bound="ExperimentFunction") ME = tp.TypeVar("ME", bound="MultiExperiment") def _reset_copy(obj: p.Parameter) -> p.Parameter: """Copy a parameter and resets its random state to obtain variability""" out = obj.copy() out._set_random_state(None) # propagates None to sub-parameters return out # pylint: disable=too-many-instance-attributes class ExperimentFunction: """Combines a function and its parametrization for running experiments (see benchmark subpackage) Parameters
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") def load(cls: tp.Type[X], filepath: tp.PathLike) -> X: """Loads a pickle file and checks that it contains an optimizer.
# This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. import uuid import warnings import numpy as np import nevergrad.common.typing as tp from nevergrad.common import errors from . import utils from ._layering import ValueProperty as ValueProperty from ._layering import Layered as Layered from ._layering import Level as Level # pylint: disable=no-value-for-parameter,pointless-statement,import-outside-toplevel P = tp.TypeVar("P", bound="Parameter") # pylint: disable=too-many-public-methods class Parameter(Layered): """Class providing the core functionality of a parameter, aka value, internal/model parameters, mutation, recombination and additional features such as shared random state, constraint check, hashes, generation and naming. The value field should sent to the function to optimize. Example ------- >>> ng.p.Array(shape=(2,)).value array([0., 0.]) """
def _tobytes(x: tp.ArrayLike) -> bytes: x = np.array(x, copy=False) # for compatibility assert x.ndim == 1, f"Input shape: {x.shape}" assert x.dtype == np.float, f"Incorrect type {x.dtype} is not float" return x.tobytes() _ERROR_STR = ( "Generating numpy arrays from the bytes keys is inefficient, " "work on archive.bytesdict.<keys,items>() directly and convert with " "np.frombuffer if you can. You can also use archive.<keys,items>_as_arrays() " "but it is less efficient.") Y = tp.TypeVar("Y") class Archive(tp.Generic[Y]): """A dict-like object with numpy arrays as keys. The underlying `bytesdict` dict stores the arrays as bytes since arrays are not hashable. Keys can be converted back with np.frombuffer(key) """ def __init__(self) -> None: self.bytesdict: tp.Dict[bytes, Y] = {} def __setitem__(self, x: tp.ArrayLike, value: Y) -> None: self.bytesdict[_tobytes(x)] = value def __getitem__(self, x: tp.ArrayLike) -> Y: return self.bytesdict[_tobytes(x)]
# # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. import functools import warnings import numpy as np import nevergrad.common.typing as tp from . import core from . import utils from . import transforms as trans # pylint: disable=no-value-for-parameter BoundValue = tp.Optional[tp.Union[float, int, np.int, np.float, np.ndarray]] A = tp.TypeVar("A", bound="Array") P = tp.TypeVar("P", bound=core.Parameter) class BoundChecker: """Simple object for checking whether an array lies between provided bounds. Parameter --------- lower: float or None minimum value upper: float or None maximum value Note
# 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 operator import functools from collections import OrderedDict import nevergrad.common.typing as tp import numpy as np from . import utils from . import core D = tp.TypeVar("D", bound="Container") class Container(core.Parameter): """Parameter which can hold other parameters. This abstract implementation is based on a dictionary. Parameters ---------- **parameters: Any the objects or Parameter which will provide values for the dict Note ---- This is the base structure for all container Parameters, and it is used to hold the internal/model parameters for all Parameter classes. """ def __init__(self, **parameters: tp.Any) -> None:
# This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. # import warnings import numpy as np import nevergrad.common.typing as tp from nevergrad.common import errors from . import _layering from . import core from .container import Dict from . import utils # pylint: disable=no-value-for-parameter,import-outside-toplevel D = tp.TypeVar("D", bound="Data") P = tp.TypeVar("P", bound=core.Parameter) def _param_string(parameters: Dict) -> str: """Hacky helper for nice-visualizatioon""" substr = f"[{parameters._get_parameters_str()}]" if substr == "[]": substr = "" return substr class Mutation(core.Parameter): """Custom mutation or recombination This is an experimental API
# 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 uuid import warnings import operator import functools from collections import OrderedDict import numpy as np import nevergrad.common.typing as tp from . import utils # pylint: disable=no-value-for-parameter P = tp.TypeVar("P", bound="Parameter") D = tp.TypeVar("D", bound="Dict") # pylint: disable=too-many-instance-attributes,too-many-public-methods class Parameter: """Abstract class providing the core functionality of a parameter, aka value, internal/model parameters, mutation, recombination and additional features such as shared random state, constraint check, hashes, generation and naming. """ def __init__(self, **parameters: tp.Any) -> None: # Main features self.uid = uuid.uuid4().hex self.parents_uids: tp.List[str] = [] self.heritage: tp.Dict[str, tp.Any] = {
# LICENSE file in the root directory of this source tree. import warnings import functools import numpy as np import nevergrad.common.typing as tp from nevergrad.common import errors from . import _layering from ._layering import Int as Int from .data import Data from .core import Parameter from . import discretization from . import transforms as trans from . import utils D = tp.TypeVar("D", bound=Data) Op = tp.TypeVar("Op", bound="Operation") BL = tp.TypeVar("BL", bound="BoundLayer") class Operation(_layering.Layered, _layering.Filterable): _LAYER_LEVEL = _layering.Level.OPERATION _LEGACY = False def __init__(self, *args: tp.Any, **kwargs: tp.Any) -> None: super().__init__() if any( isinstance(x, Parameter) for x in args + tuple(kwargs.values())): raise errors.NevergradTypeError(
# 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 uuid import copy import bisect from enum import Enum import numpy as np from nevergrad.common import errors import nevergrad.common.typing as tp L = tp.TypeVar("L", bound="Layered") F = tp.TypeVar("F", bound="Filterable") In = tp.TypeVar("In") Out = tp.TypeVar("Out") class Level(Enum): """Lower level is deeper in the structure""" ROOT = 0 OPERATION = 10 # final INTEGER_CASTING = 800 ARRAY_CASTING = 900 SCALAR_CASTING = 950 CONSTRAINT = 1000 # must be the last layer
raise FailedJobError("Job got killed for an unknown reason.") stderr = process.communicate()[1] # we already got stdout stdout = "\n".join(outlines) retcode = process.poll() if stderr and (retcode or self.verbose): print(stderr.decode(), file=sys.stderr) if retcode: subprocess_error = subprocess.CalledProcessError(retcode, process.args, output=stdout, stderr=stderr) raise FailedJobError(stderr.decode()) from subprocess_error return stdout X = tp.TypeVar("X") class Subobjects(tp.Generic[X]): """Identifies suboject of a class and applies functions recursively on them. Parameters ---------- object: Any an object containing other (sub)objects base: Type the base class of the subobjects (to filter out other items) attribute: str the attribute containing the subobjects
# 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 pathlib import Path import numbers import numpy as np import nevergrad.common.typing as tp from nevergrad.parametrization import parameter as p EF = tp.TypeVar("EF", bound="ExperimentFunction") class ExperimentFunctionCopyError(NotImplementedError): """Raised when the experiment function fails to copy itself (for benchmarks) """ class ExperimentFunction: """Combines a function and its parametrization for running experiments (see benchmark subpackage) Parameters ---------- function: callable the callable to convert parametrization: Parameter the parametrization of the function Notes ----- - you can redefine custom "evaluation_function" and "compute_pseudotime" for custom behaviors in experiments
# 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 copy import bisect from enum import Enum import numpy as np from nevergrad.common import errors import nevergrad.common.typing as tp L = tp.TypeVar("L", bound="Layered") F = tp.TypeVar("F", bound="Filterable") X = tp.TypeVar("X") class Level(Enum): """Lower level is deeper in the structure""" ROOT = 0 OPERATION = 10 # final INTEGER_CASTING = 800 ARRAY_CASTING = 900 SCALAR_CASTING = 950 CONSTRAINT = 1000 # must be the last layer class Layered:
# 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 copy import bisect from enum import Enum import numpy as np from nevergrad.common import errors import nevergrad.common.typing as tp L = tp.TypeVar("L", bound="Layered") X = tp.TypeVar("X") class Level(Enum): """Lower level is deeper in the structure""" ROOT = 0 OPERATION = 10 # final ARRAY_CASTING = 800 INTEGER_CASTING = 900 CONSTRAINT = 1000 # must be the last layer class Layered: """Hidden API for overriding/modifying the behavior of a Parameter,