Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Python Crop Simulation #1339

Open
wants to merge 204 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
204 commits
Select commit Hold shift + click to select a range
1323e4c
pcse
teytaud Jan 20, 2022
d5f5a48
pcse
teytaud Jan 20, 2022
9a69ed3
fix
teytaud Jan 20, 2022
f351125
pcse
teytaud Jan 20, 2022
d82572d
fix
teytaud Jan 20, 2022
548e26b
nlopt
teytaud Jan 20, 2022
ef1d9db
nlopt
teytaud Jan 20, 2022
6d521d6
nlopt
teytaud Jan 20, 2022
40fa906
fix
teytaud Jan 20, 2022
3c39904
fix
teytaud Jan 20, 2022
df0859e
fix
teytaud Jan 20, 2022
5ebef57
po
teytaud Jan 20, 2022
c225aba
po
teytaud Jan 20, 2022
8d64d9f
po
teytaud Jan 20, 2022
38c52f2
po
teytaud Jan 20, 2022
7212971
to
teytaud Jan 20, 2022
d7d30b6
Merge branch 'main' of github.com:facebookresearch/nevergrad into pcse
teytaud Jan 20, 2022
53c3e7c
fix
teytaud Jan 20, 2022
7d379fb
fix
teytaud Jan 20, 2022
4ba35a5
Update __init__.py
teytaud Jan 21, 2022
461c47a
Update experiments.py
teytaud Jan 21, 2022
e8b61d9
Update nevergrad/optimization/optimizerlib.py
teytaud Feb 16, 2022
1ffd9e3
Update test_optimizerlib.py
teytaud Feb 16, 2022
2485297
Update test_pcse.py
teytaud Feb 16, 2022
bb0bfdf
Update pcse.py
teytaud Feb 16, 2022
292ce5c
Update experiments.py
teytaud Feb 16, 2022
3af4cf3
Merge branch 'main' of github.com:facebookresearch/nevergrad into pcse
teytaud Mar 2, 2022
1691eaa
Pcse2: irrigation challenge (#1341)
teytaud Mar 2, 2022
c75d0e8
Update bench.txt
teytaud Mar 2, 2022
a1d172b
Update irrigation.py
teytaud Mar 2, 2022
fc2b4a4
fix
teytaud Mar 2, 2022
e07a6f9
Merge branch 'main' into pcse
teytaud Mar 7, 2022
ec48d5e
Update irrigation.py
teytaud Mar 7, 2022
87100b9
pcse_license
teytaud Mar 7, 2022
f349f85
fix_details
teytaud Mar 7, 2022
7265466
fix
teytaud Mar 7, 2022
fb56213
dependencies
teytaud Mar 7, 2022
0c326e9
dependencies
teytaud Mar 7, 2022
8bc105b
dependencies
teytaud Mar 7, 2022
5bd8f63
dependencies
teytaud Mar 7, 2022
6857caa
dependencies
teytaud Mar 7, 2022
f6a00cf
dependencies
teytaud Mar 7, 2022
31ea8ba
pcse
teytaud Mar 7, 2022
6429b5f
Update irrigation.py
teytaud Mar 8, 2022
77a3953
add_folders
teytaud Mar 8, 2022
9b470ab
Update irrigation.py
teytaud Mar 8, 2022
87b7239
black
teytaud Mar 10, 2022
b4b1a04
Merge branch 'main' into pcse
teytaud Mar 10, 2022
103868f
fix
teytaud Mar 10, 2022
2af1d82
fix
teytaud Mar 10, 2022
5d780eb
fix
teytaud Mar 10, 2022
31740f0
fix
teytaud Mar 10, 2022
6b9bc45
fix
teytaud Mar 10, 2022
2599cbd
fix
teytaud Mar 10, 2022
b54a37a
Update irrigation.py
teytaud Mar 10, 2022
696f80f
Update pcse.py
teytaud Mar 10, 2022
db55266
pcse
teytaud Mar 11, 2022
9e12218
fix
teytaud Apr 2, 2022
35ec76a
fix
teytaud Apr 2, 2022
940d129
fix
teytaud Apr 2, 2022
647c0c4
fix
teytaud Apr 3, 2022
c1b34c1
fix
teytaud Apr 3, 2022
7bfbb90
rebase
teytaud Jun 18, 2022
22234da
fix
teytaud Jun 18, 2022
86a1e5e
Register DiscreteDE (#1454)
teytaud Jun 18, 2022
1cd59e0
Adding tabu lists (#1456)
teytaud Jun 21, 2022
964aa17
Simulated Annealing (#1455)
teytaud Jun 23, 2022
2a7597b
comparisons on full (#1449)
teytaud Jun 24, 2022
fd110f3
Registering some NLOPT stuff (#1451)
teytaud Jul 4, 2022
a12524a
Removed duplicate RotatedTwoPointsDE (#1463)
Game4Move78 Jul 22, 2022
e356c46
Register DiscreteDE (#1454)
teytaud Jun 18, 2022
ac9eabf
Adding tabu lists (#1456)
teytaud Jun 21, 2022
0fbf7a1
Simulated Annealing (#1455)
teytaud Jun 23, 2022
e4ecea2
comparisons on full (#1449)
teytaud Jun 24, 2022
73af7b8
Registering some NLOPT stuff (#1451)
teytaud Jul 4, 2022
1827795
Removed duplicate RotatedTwoPointsDE (#1463)
Game4Move78 Jul 22, 2022
374661a
pcse
teytaud Jan 20, 2022
1c27e29
ZZfix
teytaud Jan 20, 2022
29ee157
nlopt
teytaud Jan 20, 2022
be5b993
ZZnlopt
teytaud Jan 20, 2022
e466891
ZZPcse2: irrigation challenge (#1341)
teytaud Mar 2, 2022
1c606b0
ZZUpdate bench.txt
teytaud Mar 2, 2022
7443e1f
pcse_license
teytaud Mar 7, 2022
930058d
fix
teytaud Mar 7, 2022
8ddf33a
fix
teytaud Aug 8, 2022
ed094cc
pcse
teytaud Aug 8, 2022
23c2689
benin
teytaud Aug 9, 2022
8403d6c
New wizard for real-world problems (#1464)
teytaud Aug 10, 2022
d38a918
fix
teytaud Aug 11, 2022
51c6f42
Nevergrad needs a big update (#1475)
teytaud Dec 2, 2022
c15feb0
Constraint penalty (#1476)
teytaud Dec 2, 2022
6a3b51a
Bug in RCWA code. (#1473)
AnMoreau Dec 2, 2022
d959f7d
benchmarks for penalized objective functions (#1477)
teytaud Dec 5, 2022
8df3d76
Update test_doc.py (#1474)
teytaud Dec 6, 2022
66f3965
Optimizing simultaneously variety, irrigation and crop (#1467)
teytaud Dec 6, 2022
da543e5
NG: fix and simplify dependencies (#1478)
teytaud Dec 7, 2022
c6e5c9c
fix (#1480)
teytaud Dec 9, 2022
8c0ff1b
pcse
teytaud Jan 20, 2022
0c58080
pcse
teytaud Jan 20, 2022
361ddf8
fix
teytaud Jan 20, 2022
0ee24c3
pcse
teytaud Jan 20, 2022
19b6c9b
fix
teytaud Jan 20, 2022
6177135
nlopt
teytaud Jan 20, 2022
187a982
nlopt
teytaud Jan 20, 2022
8e4ee6e
nlopt
teytaud Jan 20, 2022
81f565a
fix
teytaud Jan 20, 2022
d520d20
fix
teytaud Jan 20, 2022
b787a88
fix
teytaud Jan 20, 2022
9e10089
po
teytaud Jan 20, 2022
de9ac16
po
teytaud Jan 20, 2022
6a67322
po
teytaud Jan 20, 2022
0bf1a28
po
teytaud Jan 20, 2022
7c5daa6
to
teytaud Jan 20, 2022
c4b5c91
fix
teytaud Jan 20, 2022
8cf14e3
fix
teytaud Jan 20, 2022
f497b4d
Update __init__.py
teytaud Jan 21, 2022
5eb3572
Update experiments.py
teytaud Jan 21, 2022
6a6119c
Update nevergrad/optimization/optimizerlib.py
teytaud Feb 16, 2022
8abe56a
Update test_optimizerlib.py
teytaud Feb 16, 2022
da41db1
Update test_pcse.py
teytaud Feb 16, 2022
b7afb75
Update pcse.py
teytaud Feb 16, 2022
cb2baa9
Update experiments.py
teytaud Feb 16, 2022
cb62a22
Pcse2: irrigation challenge (#1341)
teytaud Mar 2, 2022
2ea70a6
Update bench.txt
teytaud Mar 2, 2022
40637cc
Update irrigation.py
teytaud Mar 2, 2022
1d72064
fix
teytaud Mar 2, 2022
4346c19
Update irrigation.py
teytaud Mar 7, 2022
1bbf0c2
pcse_license
teytaud Mar 7, 2022
d629fdf
fix_details
teytaud Mar 7, 2022
693304d
fix
teytaud Mar 7, 2022
12da43d
dependencies
teytaud Mar 7, 2022
cac772f
dependencies
teytaud Mar 7, 2022
b4ec6be
dependencies
teytaud Mar 7, 2022
2342474
dependencies
teytaud Mar 7, 2022
df4e8ee
dependencies
teytaud Mar 7, 2022
05f0b56
dependencies
teytaud Mar 7, 2022
fc009ec
pcse
teytaud Mar 7, 2022
7c24bdf
Update irrigation.py
teytaud Mar 8, 2022
69490cb
add_folders
teytaud Mar 8, 2022
80d67ba
Update irrigation.py
teytaud Mar 8, 2022
974a028
black
teytaud Mar 10, 2022
a6cbca7
fix
teytaud Mar 10, 2022
90ae9ba
fix
teytaud Mar 10, 2022
71af923
fix
teytaud Mar 10, 2022
576bc89
fix
teytaud Mar 10, 2022
e7f92e1
Update irrigation.py
teytaud Mar 10, 2022
b747fd4
Update pcse.py
teytaud Mar 10, 2022
c05f160
pcse
teytaud Mar 11, 2022
a5f3258
fix
teytaud Apr 2, 2022
76ecf15
fix
teytaud Apr 2, 2022
41be911
fix
teytaud Apr 2, 2022
672e1b6
fix
teytaud Apr 3, 2022
71a83a8
fix
teytaud Apr 3, 2022
a495087
fix
teytaud Jun 18, 2022
9bbf37d
pcse
teytaud Jan 20, 2022
ceb3f65
ZZfix
teytaud Jan 20, 2022
e31d46e
nlopt
teytaud Jan 20, 2022
a1079d0
ZZnlopt
teytaud Jan 20, 2022
c50fa85
ZZPcse2: irrigation challenge (#1341)
teytaud Mar 2, 2022
2c573be
ZZUpdate bench.txt
teytaud Mar 2, 2022
a616182
pcse_license
teytaud Mar 7, 2022
9623a2d
fix
teytaud Mar 7, 2022
7155a02
fix
teytaud Aug 8, 2022
399ab02
pcse
teytaud Aug 8, 2022
f4c4323
benin
teytaud Aug 9, 2022
17ba288
fix
teytaud Aug 11, 2022
157ca98
Optimizing simultaneously variety, irrigation and crop (#1467)
teytaud Dec 6, 2022
c048376
merge
teytaud Dec 12, 2022
7982e4c
Superpcse (#1484)
teytaud Dec 12, 2022
dace039
pcse
teytaud Jan 20, 2022
8dd1a17
pcse
teytaud Jan 20, 2022
6240352
fix
teytaud Jan 20, 2022
cda07ce
pcse
teytaud Jan 20, 2022
2cba104
fix
teytaud Jan 20, 2022
e56d4e2
nlopt
teytaud Jan 20, 2022
19cbc13
nlopt
teytaud Jan 20, 2022
abb9ae3
fix
teytaud Jan 20, 2022
d3b97ce
fix
teytaud Jan 20, 2022
9299e19
fix
teytaud Jan 20, 2022
5480d31
po
teytaud Jan 20, 2022
d849756
Update experiments.py
teytaud Jan 21, 2022
98b6c5f
Update nevergrad/optimization/optimizerlib.py
teytaud Feb 16, 2022
7b4dca7
Update test_pcse.py
teytaud Feb 16, 2022
ed9c32a
Update experiments.py
teytaud Feb 16, 2022
292fdc1
Pcse2: irrigation challenge (#1341)
teytaud Mar 2, 2022
a389d2f
fix
teytaud Mar 2, 2022
5b3627d
fix
teytaud Mar 7, 2022
c760aae
dependencies
teytaud Mar 7, 2022
1d1cb80
black
teytaud Mar 10, 2022
6389f6b
fix
teytaud Mar 10, 2022
0a4840d
fix
teytaud Mar 10, 2022
6a6b2a9
Update pcse.py
teytaud Mar 10, 2022
a1243ee
pcse
teytaud Mar 11, 2022
5e34325
fix
teytaud Jun 18, 2022
acd792f
pcse
teytaud Jan 20, 2022
e293dfc
ZZfix
teytaud Jan 20, 2022
946f5ca
nlopt
teytaud Jan 20, 2022
c2bc52d
ZZnlopt
teytaud Jan 20, 2022
cdb657b
ZZPcse2: irrigation challenge (#1341)
teytaud Mar 2, 2022
25e8681
pcse
teytaud Aug 8, 2022
e23551d
benin
teytaud Aug 9, 2022
adf3f31
Optimizing simultaneously variety, irrigation and crop (#1467)
teytaud Dec 6, 2022
d37da25
Superpcse (#1484)
teytaud Dec 12, 2022
750f908
fix
teytaud Dec 12, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
[mypy-scipy.*,requests,pandas,compiler_gym,compiler_gym.*,gym,gym.*,gym_anm,matplotlib.*,pytest,cma,bayes_opt.*,torchvision.models,torch.*,mpl_toolkits.*,fcmaes.*,tqdm,pillow,PIL,PIL.Image,sklearn.*,pyomo.*,pyproj,IOHexperimenter.*,tensorflow,koncept.models,cv2,imquality,imquality.brisque,lpips,mixsimulator.*,networkx.*,cdt.*,pymoo,pymoo.*,bayes_optim.*,olympus.*]
ignore_missing_imports = True

[mypy-nevergrad.functions.rl.agents,torchvision,torchvision.*,nevergrad.functions.games.*,nevergrad.functions.multiobjective.pyhv,nevergrad.optimization.test_doc,,pymoo,pymoo.*,pybullet,pybullet_envs,pybulletgym,pyvirtualdisplay]
[mypy-nevergrad.functions.rl.agents,torchvision,torchvision.*,nevergrad.functions.games.*,nevergrad.functions.multiobjective.pyhv,nevergrad.optimization.test_doc,,pymoo,pymoo.*,pybullet,pybullet_envs,pybulletgym,pyvirtualdisplay,pcse.*,yaml.*]
ignore_missing_imports = True
ignore_errors = True

Expand Down
21 changes: 21 additions & 0 deletions nevergrad/benchmark/experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from nevergrad.functions.powersystems import PowerSystem
from nevergrad.functions.stsp import STSP
from nevergrad.functions.rocket import Rocket
from nevergrad.functions.pcse import Pcse
from nevergrad.functions.mixsimulator import OptimizeMix
from nevergrad.functions.unitcommitment import UnitCommitmentProblem
from nevergrad.functions import control
Expand Down Expand Up @@ -1176,6 +1177,26 @@ def rocket(seed: tp.Optional[int] = None, seq: bool = False) -> tp.Iterator[Expe
yield xp


@registry.register
def pcse(seed: tp.Optional[int] = None) -> tp.Iterator[Experiment]:
"""Crop simulator.

Low dimensional problem, only 2 vars. This is optimization for model identification: we want
to find parameters so that the simulation matches observations.
"""
funcs = [Pcse()]
seedg = create_seed_generator(seed)
optims = get_optimizers("basics", seed=next(seedg))
for budget in [25, 50, 100, 200]:
for num_workers in [1, 10, 40]:
if num_workers < budget:
for algo in optims:
for fu in funcs:
xp = Experiment(fu, algo, budget, num_workers=num_workers, seed=next(seedg))
if not xp.is_incoherent:
yield xp


@registry.register
def mono_rocket(seed: tp.Optional[int] = None) -> tp.Iterator[Experiment]:
"""Sequential counterpart of the rocket problem."""
Expand Down
8 changes: 8 additions & 0 deletions nevergrad/functions/pcse/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

# Based on https://github.com/ajwdewit/pcse_notebooks
# MIT License.
from .pcse import Pcse as Pcse
143 changes: 143 additions & 0 deletions nevergrad/functions/pcse/pcse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

"""
Approximate crop Simulation
Based on
https://raw.githubusercontent.com/purdue-orbital/pcse-simulation/master/Simulation2.py
"""


import numpy as np
from ..base import ArrayExperimentFunction

# pylint: disable=too-many-locals,too-many-statements


class Pcse(ArrayExperimentFunction):
jrapin marked this conversation as resolved.
Show resolved Hide resolved
def __init__(self) -> None:
import yaml
import pandas as pd
Comment on lines +34 to +35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

standard imports should be on top, pcse can be kept inline to avoid issues

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

standard imports should be on top, pcse can be kept inline to avoid issues

Done.

from pcse.models import Wofost72_PP
from pcse.base import ParameterProvider
from pcse.db import NASAPowerWeatherDataProvider

# from pcse.fileinput import YAMLAgroManagementReader, YAMLCropDataProvider
from pcse.fileinput import YAMLCropDataProvider
from pcse.util import WOFOST72SiteDataProvider, DummySoilDataProvider

# Weather data for Netherlands
wdp = NASAPowerWeatherDataProvider(latitude=52, longitude=5)
# Standard crop parameter library
cropd = YAMLCropDataProvider()
# We don't need soil for potential production, so we use dummy values
soild = DummySoilDataProvider()
# Some site parameters
sited = WOFOST72SiteDataProvider(WAV=50, CO2=360.0)
# Package everyting into a single parameter object
params = ParameterProvider(cropdata=cropd, sitedata=sited, soildata=soild)

# Here we define the agromanagement for sugar beet
agro_yaml = """
- 2006-01-01:
CropCalendar:
crop_name: sugarbeet
variety_name: Sugarbeet_603
crop_start_date: 2006-03-31
crop_start_type: emergence
crop_end_date: 2006-10-20
crop_end_type: harvest
max_duration: 300
TimedEvents: null
StateEvents: null
"""
agro = yaml.safe_load(agro_yaml)

wofost = Wofost72_PP(params, wdp, agro)
wofost.run_till_terminate()
df = pd.DataFrame(wofost.get_output())
df.index = pd.to_datetime(df.day)
df.tail()

# get daily observations for those
ix = (df.index.dayofweek == 0) & (df.LAI.notnull())
df_pseudo_obs = df.loc[ix]

class ModelRerunner(object):
"""Reruns a given model with different values of parameters TWDI and SPAN.

Returns a pandas DataFrame with simulation results of the model with given
parameter values.
"""

parameters = ["TDWI", "SPAN"]

def __init__(self, params, wdp, agro):
self.params = params
self.wdp = wdp
self.agro = agro

def __call__(self, par_values):
# Check if correct number of parameter values were provided
if len(par_values) != len(self.parameters):
msg = "Optimizing %i parameters, but %i values were provided!" % (
len(self.parameters),
len(par_values),
)
raise RuntimeError(msg)
# Clear any existing overrides
self.params.clear_override()
# Set overrides for the new parameter values
for parname, value in zip(self.parameters, par_values):
self.params.set_override(parname, value)
# Run the model with given parameter values
wofost = Wofost72_PP(self.params, self.wdp, self.agro)
wofost.run_till_terminate()
df = pd.DataFrame(wofost.get_output())
df.index = pd.to_datetime(df.day)
return df

class ObjectiveFunctionCalculator(object):
"""Computes the objective function.

This class runs the simulation model with given parameter values and returns the objective
function as the sum of squared difference between observed and simulated LAI.
."""

def __init__(self, params, wdp, agro, observations):
self.modelrerunner = ModelRerunner(params, wdp, agro)
self.df_observations = observations
self.n_calls = 0

def __call__(self, par_values, grad=None):
"""Runs the model and computes the objective function for given par_values.

The input parameter 'grad' must be defined in the function call, but is only
required for optimization methods where analytical gradients can be computed.
"""
self.n_calls += 1
# print(".", end="")
# Run the model and collect output
df_simulations = self.modelrerunner(par_values)
# compute the differences by subtracting the DataFrames
# Note that the dataframes automatically join on the index (dates) and column names
df_differences = df_simulations - self.df_observations
# Compute the RMSE on the LAI column
obj_func = np.sqrt(np.mean(df_differences.LAI ** 2))
return obj_func

objfunc_calculator = ObjectiveFunctionCalculator(params, wdp, agro, df_pseudo_obs)
# defaults = [cropd["TDWI"], cropd["SPAN"]]
# error = objfunc_calculator(defaults)
# print("Objective function value with default parameters (%s): %s" % (defaults, error))

import nevergrad as ng
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on top

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


TDWI_range = [0.1, 0.6]
SPAN_range = [30, 40]
param = ng.p.Array(
shape=(2,), lower=(TDWI_range[0], SPAN_range[0]), upper=(TDWI_range[1], SPAN_range[1])
).set_name("2hp")
super().__init__(objfunc_calculator, parametrization=param)
17 changes: 17 additions & 0 deletions nevergrad/functions/pcse/test_pcse.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

import numpy as np
from . import pcse


def test_rocket() -> None:
jrapin marked this conversation as resolved.
Show resolved Hide resolved
func = pcse.Pcse()
x = 0 * np.random.rand(func.dimension)
value = func(x)
value2 = func(x)
assert value > -1000.0 # type: ignore
assert value < 1000.0 # type: ignore
np.testing.assert_almost_equal(value, value2)
2 changes: 1 addition & 1 deletion nevergrad/optimization/optimizerlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -2407,7 +2407,7 @@ def optim(self) -> base.Optimizer:
if self._optim is None:
self._optim = self._select_optimizer_cls()(self.parametrization, self.budget, self.num_workers)
self._optim = self._optim if not isinstance(self._optim, NGOptBase) else self._optim.optim
logger.debug("%s selected %s optimizer.", *(x.name for x in (self, self._optim)))
logger.debug("%s selected %s optimizer.", *(x.name for x in (self, self._optim)))
teytaud marked this conversation as resolved.
Show resolved Hide resolved
return self._optim

def _select_optimizer_cls(self) -> base.OptCls:
Expand Down
27 changes: 16 additions & 11 deletions nevergrad/optimization/test_optimizerlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.

import re
import sys
import time
import random
Expand Down Expand Up @@ -711,20 +710,26 @@ def test_ngopt_selection(
# pylint: disable=expression-not-assigned
opt = optlib.registry[name](param, budget=budget, num_workers=num_workers)
# pylint: disable=pointless-statement
opt.optim # type: ignore
pattern = rf".*{name} selected (?P<name>\w+?) optimizer\."
match = re.match(pattern, caplog.text.splitlines()[-1])
assert match is not None, f"Did not detect selection in logs: {caplog.text}"
choice = match.group("name")
# The pattern matching from the log does not work anymore,
# hence some modifications below. We might delete the old version.
o = str(opt.optim) # type: ignore
jrapin marked this conversation as resolved.
Show resolved Hide resolved
# import re
# pattern = rf".*{name} selected (?P<name>\w+?) optimizer\."
# match = re.match(pattern, caplog.text.splitlines()[-1])
# assert match is not None, f"Did not detect selection in logs: {caplog.text}"
# choice = match.group("name")
if expected != "#CONTINUOUS":
assert choice == expected
# assert choice == expected
assert expected in o
else:
print(f"Continuous param={param} budget={budget} workers={num_workers} --> {choice}")
# print(f"Continuous param={param} budget={budget} workers={num_workers} --> {choice}")
if num_workers >= budget > 600:
assert choice == "MetaTuneRecentering"
# assert choice == "MetaTuneRecentering"
assert "MetaTuneRecentering" in o
if num_workers > 1:
assert choice not in ["SQP", "Cobyla"]
assert choice == opt._info()["sub-optim"]
# assert choice not in ["SQP", "Cobyla"]
assert "SQP" not in o and "Cobyla" not in o
assert opt._info()["sub-optim"] in o


def test_bo_ordering() -> None:
Expand Down
1 change: 1 addition & 0 deletions requirements/bench.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,4 @@ olymp==0.0.1b0 ; sys_platform == "linux"
silence_tensorflow # for olymp
tensorflow_probability # for olymp
bayes-optim==0.2.5.5
pcse