def _get_search_space()

in nevergrad/optimization/externalbo.py [0:0]


def _get_search_space(param_name, param):
    if isinstance(param, p.Instrumentation):
        space = {}
        space["args"] = {
            str(idx_param): _get_search_space(str(idx_param), param[0][idx_param])  # type: ignore
            for idx_param in range(len(param[0].value))
        }
        space["kwargs"] = {
            param_name: _get_search_space(param_name, param[1][param_name])  # type: ignore
            for param_name in param[1].value.keys()
        }
        return space

    elif isinstance(param, (p.Log, p.Scalar)):
        if (param.bounds[0][0] is None) or (param.bounds[1][0] is None):
            if isinstance(param, p.Scalar) and not param.integer:
                return hp.lognormal(label=param_name, mu=0, sigma=1)
            raise ValueError(f"Scalar {param_name} not bounded.")
        elif isinstance(param, p.Log):
            return hp.loguniform(
                label=param_name, low=np.log(param.bounds[0][0]), high=np.log(param.bounds[1][0])
            )
        elif isinstance(param, p.Scalar):
            if param.integer:
                return hp.randint(label=param_name, low=int(param.bounds[0][0]), high=int(param.bounds[1][0]))
            else:
                return hp.uniform(label=param_name, low=param.bounds[0][0], high=param.bounds[1][0])

    elif isinstance(param, p.Choice):
        list_types = [
            type(param.choices[i])
            for i in range(len(param.choices))
            if not isinstance(param.choices[i], (p.Instrumentation, p.Constant))
        ]

        if len(list_types) != len(set(list_types)):
            raise NotImplementedError
        return hp.choice(
            param_name,
            [
                _get_search_space(param_name + "__" + str(i), param.choices[i])
                for i in range(len(param.choices))
            ],
        )

    elif isinstance(param, p.Constant):
        return param.value

    # Hyperopt do not support array
    raise NotImplementedError