python - How to use nevergrad to find the minimum of a cost function over the field of integers?
问题描述
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 the optimal, but really it's close :
optimal(1, 23,5)
0.13013924406458133
optimal(1, 24,5)
0.13007783167425113
- How can we make nevergrad more robust?
- Which optimizer should we use?
- Is there a way to run nevergrad multiple times with different "initial conditions" and take the optimal results among all multiple tries?
解决方案
推荐阅读
- python - 使用python进行语料库分析
- r - 如何在 linux 中签入在 Windows 中开发的 R 包
- r - 根据条件 R 删除行
- python - 当网络的输出类型为列表时,如何在pytorch中生成网络的架构图?
- c++ - std::filesystem:file_size 给我一个不连贯的值
- angular - Angular Observable 等待方法在返回前执行
- amazon-s3 - AWS Airflow S3 Key Sensor 未触发
- c# - 打开一个包含现有 cookie 的网站 (C# Selenium)
- r - cur_group_id 按大小而不是字母顺序
- apache-kafka - 经纪人不可用