[Python] How to use nevergrad to find the minimum of a cost function over the field of...

Discussão em 'Python' iniciado por Stack, Abril 26, 2025.

  1. Stack

    Stack Membro Participativo

    As part of our Poolkeh paper, we thought to use nevergrad. However, sadly it doesn't always return the same result, nor the most optimal one.

    We tried DiscreteOnePlusOne as an optimizer, but it didn't find the optimal results. OnePlusOne worked ok, but didn't give the best solution and it needed some hints like this one:

    if s1 < s2*(1+r0):
    return np.Inf


    We explored the case of pooling COVID-19 tests with two steps, here is the complete code:

    #!pip install nevergrad
    import numpy as np
    def optimal(r0: float, s1:int, s2:int):
    r0 = r0/100
    if s1 < s2*(1+r0):
    return np.Inf

    p1=1-np.power(1-r0,s1)
    r1=r0/p1
    p2=1-np.power(1-r1,s2)
    return 1/s1 + p1/s2 + p1*p2

    import nevergrad as ng

    def findBestStategy(r0: float):
    '''
    r0 is in %
    '''
    parametrization = ng.p.Instrumentation(
    r0 = r0,
    s1=ng.p.Scalar(lower=1, upper=100).set_integer_casting(),
    s2=ng.p.Scalar(lower=1, upper=100).set_integer_casting(),
    )
    optimizer = ng.optimizers.OnePlusOne(parametrization=parametrization, budget=2000, num_workers=1)
    recommendation = optimizer.minimize(optimal)
    return recommendation.kwargs
    findBestStategy(1)
    {'r0': 1, 's1': 23, 's2': 5}


    This is not optimal, though really it's close:

    optimal(1, 23,5)
    0.13013924406458133
    optimal(1, 24,5)
    0.13007783167425113

    1. How can we make nevergrad more robust?
    2. Which optimizer should we use?
    3. Is there a way to run nevergrad multiple times with different "initial conditions" and take the optimal results among all multiple tries?

    Continue reading...

Compartilhe esta Página