How to use the nevergrad.common.testing function in nevergrad

To help you get started, we’ve selected a few nevergrad examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github facebookresearch / nevergrad / nevergrad / functions / mlda / test_problems.py View on Github external
def test_clustering() -> None:
    data = np.random.normal(size=(20, 2))
    with patch("nevergrad.functions.mlda.datasets.get_data") as data_getter:
        data_getter.return_value = data
        func = problems.Clustering.from_mlda(name="Ruspini", num_clusters=5, rescale=True)
        np.testing.assert_equal(func.dimension, 10)
    func([k for k in range(10)])
    testing.printed_assert_equal(func.descriptors,
                                 {"instrumentation": "A(5,2)", "function_class": "Clustering", "dimension": 10,
                                  "name": "Ruspini", "num_clusters": 5, "rescale": True})
github facebookresearch / nevergrad / nevergrad / functions / mlda / test_datasets.py View on Github external
@testing.parametrized(
    german_towns=("German towns", """#    Hey, this is a comment
#
320.9  13024  346.5
320.9  13024  346.5
""", [[320.9, 13024, 346.5], [320.9, 13024, 346.5]]),
    ruspini=("Ruspini", """     5    74
11    59""", [[5, 74], [11, 59]])
)
def test_get_data(name: str, text: str, expected: List[List[float]]) -> None:
    with tempfile.TemporaryDirectory() as tmp:
        # create an example file
        filepath = Path(tmp) / "example.txt"
        with filepath.open("w") as f:
            f.write(text)
        # get the output
        with patch("nevergrad.functions.mlda.datasets.get_dataset_filepath") as path_getter:
github facebookresearch / nevergrad / nevergrad / benchmark / test_experiments.py View on Github external
@testing.parametrized(**{name: (name, maker) for name, maker in experiments.registry.items()})
def test_experiments_registry(name: str, maker: Callable[[], Iterator[experiments.Experiment]]) -> None:
    with patch("shutil.which", return_value="here"):  # do not check for missing packages
        with datasets.mocked_data():  # mock mlda data that should be downloaded
            check_maker(maker)  # this is to extract the function for reuse if other external packages need it
        if name not in {"realworld_oneshot", "mlda", "mldaas", "realworld", "powersystems", "powersystemsbig",
                        "fastgames", "bigfastgames"}:
            check_seedable(maker)  # this is a basic test on first elements, do not fully rely on it
github facebookresearch / nevergrad / nevergrad / functions / test_functionlib.py View on Github external
@testing.parametrized(
    sphere=({"name": "sphere", "block_dimension": 3, "useless_variables": 6, "num_blocks": 2}, 9.630),
    cigar=({"name": "cigar", "block_dimension": 3, "useless_variables": 6, "num_blocks": 2}, 3527289.665),
    cigar_rot=({"rotation": True, "name": "cigar", "block_dimension": 3, "useless_variables": 6, "num_blocks": 2}, 5239413.576),
    no_transform=({"name": "leadingones5", "block_dimension": 50, "useless_variables": 10}, 9.0),
    hashed=({"name": "sphere", "block_dimension": 3, "useless_variables": 6, "num_blocks": 2, "hashing": True}, 12.44),
    noisy_sphere=({"name": "sphere", "block_dimension": 3, "useless_variables": 6, "num_blocks": 2, "noise_level": .2}, 9.576),
    noisy_very_sphere=({"name": "sphere", "block_dimension": 3, "useless_variables": 6,
                        "num_blocks": 2, "noise_dissymmetry": True, "noise_level": .2}, 7.615),
)
def test_testcase_function_value(config: Dict[str, Any], expected: float) -> None:
    # make sure no change is made to the computation
    func = functionlib.ArtificialFunction(**config)
    np.random.seed(2)
    x = np.random.normal(0, 1, func.dimension)
    x *= -1 if config.get("noise_dissymmetry", False) else 1  # change sign to activate noise dissymetry
    if config.get("hashing", False):
github facebookresearch / nevergrad / nevergrad / instrumentation / test_transforms.py View on Github external
@testing.parametrized(
    tanh=(transforms.TanhBound, [1., 100.]),
    arctan=(transforms.ArctanBound, [0.9968, 99.65]),
    clipping=(transforms.Clipping, [1, 90]),
)
def test_multibounds(transform_cls: Type[transforms.BoundTransform], expected: List[float]) -> None:
    transform = transform_cls([0, 0], [1, 100])
    output = transform.forward(np.array([100, 90]))
    np.testing.assert_almost_equal(output, expected, decimal=2)
    # shapes
    with pytest.raises(ValueError):
        transform.forward(np.array([-3, 5, 4]))
    with pytest.raises(ValueError):
        transform.backward(np.array([-3, 5, 4]))
    # bound error
    with pytest.raises(ValueError):
        transform_cls([0, 0], [0, 100])
github facebookresearch / nevergrad / nevergrad / instrumentation / test_instantiate.py View on Github external
def test_folder_instantiator(self, clean_copy: bool) -> None:
        path = Path(__file__).parent / "examples"
        ifolder = instantiate.FolderInstantiator(path, clean_copy=clean_copy)
        testing.printed_assert_equal(ifolder.placeholders, _EXPECTED)
        np.testing.assert_equal(len(ifolder.file_functions), 1)
        with ifolder.instantiate(value1=12, value2=110., string="") as tmp:
            with (tmp / "script.py").open("r") as f:
                lines = f.readlines()
        np.testing.assert_equal(lines[10], "value2 = 110.0\n")
github facebookresearch / nevergrad / nevergrad / benchmark / test_xpbase.py View on Github external
def test_run_artificial_function() -> None:
    func = ArtificialFunction(name="sphere", block_dimension=2)
    xp = xpbase.Experiment(func, optimizer="OnePlusOne", budget=24, num_workers=2, batch_mode=True, seed=12)
    summary = xp.run()
    assert summary["elapsed_time"] < .5  # should be much faster
    np.testing.assert_almost_equal(summary["loss"], 0.00078544)  # makes sure seeding works!
    testing.assert_set_equal(summary.keys(), DESCRIPTION_KEYS)
    np.testing.assert_equal(summary["elapsed_budget"], 24)
    np.testing.assert_equal(summary["pseudotime"], 12)  # defaults to 1 unit per eval ( /2 because 2 workers)
github facebookresearch / nevergrad / nevergrad / optimization / test_base.py View on Github external
def test_batch_and_steady_optimization(num_workers: int, batch_mode: bool, expected: List[Tuple[str, float]]) -> None:
    # tests the suggestion (s) and update (u) patterns
    # the w3_steady is unexpected. It is designed to be efficient with a non-sequential executor, but because
    # of it, it is acting like batch mode when sequential...
    optim = LoggingOptimizer(num_workers=num_workers)
    func = CounterFunction()
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        optim.minimize(func, verbosity=2, batch_mode=batch_mode)
    testing.printed_assert_equal(optim.logs, expected)