src/estimagic/optimization/get_algorithm.py
def _adjust_options_to_algorithm(
    algo_options, lower_bounds, upper_bounds, algorithm, algo_name
):
    """Reduce the algo_options and check if bounds are compatible with algorithm."""

    # convert algo option keys to valid Python arguments
    algo_options = {key.replace(".", "_"): val for key, val in algo_options.items()}

    valid = set(inspect.signature(algorithm).parameters)

    if isinstance(algorithm, partial):
        partialed_in = set(algorithm.args).union(set(algorithm.keywords))
        valid = valid.difference(partialed_in)

    reduced = {key: val for key, val in algo_options.items() if key in valid}

    ignored = {key: val for key, val in algo_options.items() if key not in valid}

    if ignored:
        warnings.warn(
            "The following algo_options were ignored because they are not compatible "
            f"with {algo_name}:\n\n {ignored}"
        )

    if "lower_bounds" not in valid and not (lower_bounds == -np.inf).all():
        raise ValueError(
            f"{algo_name} does not support lower bounds but your optimization "
            "problem has lower bounds (either because you specified them explicitly "
            "or because they were implied by other constraints)."
        )

    if "upper_bounds" not in valid and not (upper_bounds == np.inf).all():
        raise ValueError(
            f"{algo_name} does not support upper bounds but your optimization "
            "problem has upper bounds (either because you specified them explicitly "
            "or because they were implied by other constraints)."
        )

    if "lower_bounds" in valid:
        reduced["lower_bounds"] = lower_bounds

    if "upper_bounds" in valid:
        reduced["upper_bounds"] = upper_bounds

    return reduced
Reduce the algo_options and check if bounds are compatible with algorithm.
Reduce the algo_options and check if bounds are compatible with algorithm.
Reduce the algo_options and check if bounds are compatible with algorithm .
def _adjust_options_to_algorithm(
    algo_options, lower_bounds, upper_bounds, algorithm, algo_name
):
    algo_options = {key.replace(".", "_"): val for key, val in algo_options.items()}
    valid = set(inspect.signature(algorithm).parameters)
    if isinstance(algorithm, partial):
        partialed_in = set(algorithm.args).union(set(algorithm.keywords))
        valid = valid.difference(partialed_in)
    reduced = {key: val for key, val in algo_options.items() if key in valid}
    ignored = {key: val for key, val in algo_options.items() if key not in valid}
    if ignored:
        warnings.warn(
            "The following algo_options were ignored because they are not compatible "
            f"with {algo_name}:\n\n {ignored}"
        )
    if "lower_bounds" not in valid and not (lower_bounds == -np.inf).all():
        raise ValueError(
            f"{algo_name} does not support lower bounds but your optimization "
            "problem has lower bounds (either because you specified them explicitly "
            "or because they were implied by other constraints)."
        )
    if "upper_bounds" not in valid and not (upper_bounds == np.inf).all():
        raise ValueError(
            f"{algo_name} does not support upper bounds but your optimization "
            "problem has upper bounds (either because you specified them explicitly "
            "or because they were implied by other constraints)."
        )
    if "lower_bounds" in valid:
        reduced["lower_bounds"] = lower_bounds
    if "upper_bounds" in valid:
        reduced["upper_bounds"] = upper_bounds
    return reduced
def _adjust_options_to_algorithm ( algo_options , lower_bounds , upper_bounds , algorithm , algo_name ) : algo_options = { key . replace ( "." , "_" ) : val for key , val in algo_options . items ( ) } valid = set ( inspect . signature ( algorithm ) . parameters ) if isinstance ( algorithm , partial ) : partialed_in = set ( algorithm . args ) . union ( set ( algorithm . keywords ) ) valid = valid . difference ( partialed_in ) reduced = { key : val for key , val in algo_options . items ( ) if key in valid } ignored = { key : val for key , val in algo_options . items ( ) if key not in valid } if ignored : warnings . warn ( "The following algo_options were ignored because they are not compatible " f"with {algo_name}:\n\n {ignored}" ) if "lower_bounds" not in valid and not ( lower_bounds == - np . inf ) . all ( ) : raise ValueError ( f"{algo_name} does not support lower bounds but your optimization " "problem has lower bounds (either because you specified them explicitly " "or because they were implied by other constraints)." ) if "upper_bounds" not in valid and not ( upper_bounds == np . inf ) . all ( ) : raise ValueError ( f"{algo_name} does not support upper bounds but your optimization " "problem has upper bounds (either because you specified them explicitly " "or because they were implied by other constraints)." ) if "lower_bounds" in valid : reduced [ "lower_bounds" ] = lower_bounds if "upper_bounds" in valid : reduced [ "upper_bounds" ] = upper_bounds return reduced
Reduce the algo_options and check if bounds are compatible with algorithm.
Reduce the algo_options and check if bounds are compatible with algorithm .
"""Reduce the algo_options and check if bounds are compatible with algorithm.""" # convert algo option keys to valid Python arguments
algo_options
lower_bounds
upper_bounds
algorithm
algo_name