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 climate change in Nevergrad/PCSE #1481

Open
wants to merge 144 commits into
base: pcse
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
144 commits
Select commit Hold shift + click to select a range
2fd6847
pcse
teytaud Jan 20, 2022
0ea7b9a
pcse
teytaud Jan 20, 2022
a53c0e7
fix
teytaud Jan 20, 2022
dc88a7e
pcse
teytaud Jan 20, 2022
47a63ed
fix
teytaud Jan 20, 2022
8dbd1ae
nlopt
teytaud Jan 20, 2022
1fbf9c6
nlopt
teytaud Jan 20, 2022
14b86fd
nlopt
teytaud Jan 20, 2022
681d381
fix
teytaud Jan 20, 2022
6a86208
fix
teytaud Jan 20, 2022
2e96c9f
fix
teytaud Jan 20, 2022
af8c9da
po
teytaud Jan 20, 2022
f00278e
po
teytaud Jan 20, 2022
00b5cfb
po
teytaud Jan 20, 2022
4ad0ba9
po
teytaud Jan 20, 2022
a7c7501
to
teytaud Jan 20, 2022
dc33bbf
fix
teytaud Jan 20, 2022
3506024
fix
teytaud Jan 20, 2022
51ecfaf
Update __init__.py
teytaud Jan 21, 2022
707ee80
Update experiments.py
teytaud Jan 21, 2022
dcac1a0
Update nevergrad/optimization/optimizerlib.py
teytaud Feb 16, 2022
b809b5b
Update test_optimizerlib.py
teytaud Feb 16, 2022
d213892
Update test_pcse.py
teytaud Feb 16, 2022
2b19b40
Update pcse.py
teytaud Feb 16, 2022
9bc4cfd
Update experiments.py
teytaud Feb 16, 2022
5569d69
Pcse2: irrigation challenge (#1341)
teytaud Mar 2, 2022
fc25af5
Update bench.txt
teytaud Mar 2, 2022
2a429b0
Update irrigation.py
teytaud Mar 2, 2022
7ad6abf
fix
teytaud Mar 2, 2022
8e98076
Update irrigation.py
teytaud Mar 7, 2022
8173a66
pcse_license
teytaud Mar 7, 2022
d08d67e
fix_details
teytaud Mar 7, 2022
795e757
fix
teytaud Mar 7, 2022
df05c65
dependencies
teytaud Mar 7, 2022
a465ede
dependencies
teytaud Mar 7, 2022
17d989d
dependencies
teytaud Mar 7, 2022
a49175e
dependencies
teytaud Mar 7, 2022
96638e7
dependencies
teytaud Mar 7, 2022
bfbf80d
dependencies
teytaud Mar 7, 2022
ce584b3
pcse
teytaud Mar 7, 2022
d7e5b8f
Update irrigation.py
teytaud Mar 8, 2022
e6cab5b
add_folders
teytaud Mar 8, 2022
9ab938b
Update irrigation.py
teytaud Mar 8, 2022
1d04138
black
teytaud Mar 10, 2022
af74874
fix
teytaud Mar 10, 2022
5b85c61
fix
teytaud Mar 10, 2022
d02b6f1
fix
teytaud Mar 10, 2022
08ff7c5
fix
teytaud Mar 10, 2022
7d12481
Update irrigation.py
teytaud Mar 10, 2022
d77375c
Update pcse.py
teytaud Mar 10, 2022
85b6b65
pcse
teytaud Mar 11, 2022
588db62
fix
teytaud Apr 2, 2022
091f5c0
fix
teytaud Apr 2, 2022
a417eeb
fix
teytaud Apr 2, 2022
fd56e88
fix
teytaud Apr 3, 2022
42dc439
fix
teytaud Apr 3, 2022
2051ccd
fix
teytaud Jun 18, 2022
91daaf2
pcse
teytaud Jan 20, 2022
8ce1114
ZZfix
teytaud Jan 20, 2022
bd0e147
nlopt
teytaud Jan 20, 2022
81b3369
ZZnlopt
teytaud Jan 20, 2022
efdcdcb
ZZPcse2: irrigation challenge (#1341)
teytaud Mar 2, 2022
4c1224e
ZZUpdate bench.txt
teytaud Mar 2, 2022
dcabfa1
pcse_license
teytaud Mar 7, 2022
1e40e47
fix
teytaud Mar 7, 2022
96e0475
fix
teytaud Aug 8, 2022
f95eaa4
pcse
teytaud Aug 8, 2022
e4aeb35
benin
teytaud Aug 9, 2022
f1ec8dc
fix
teytaud Aug 11, 2022
bba5795
Optimizing simultaneously variety, irrigation and crop (#1467)
teytaud Dec 6, 2022
3f9d3fa
fix
teytaud Dec 6, 2022
2e53add
fixngoptrw
teytaud Dec 9, 2022
2cfe119
ClimateChange
teytaud Dec 12, 2022
2906c05
Merge branch 'pcse' into cc
teytaud Dec 12, 2022
9ac9d16
merge_and_small_fix
teytaud Dec 12, 2022
09e4865
merge
teytaud Dec 12, 2022
c934de2
pcse
teytaud Jan 20, 2022
1257db5
pcse
teytaud Jan 20, 2022
a02c5ea
fix
teytaud Jan 20, 2022
b069972
pcse
teytaud Jan 20, 2022
ceb2069
fix
teytaud Jan 20, 2022
831768d
nlopt
teytaud Jan 20, 2022
e291e11
nlopt
teytaud Jan 20, 2022
e1bde6c
nlopt
teytaud Jan 20, 2022
1e0cd57
fix
teytaud Jan 20, 2022
c4c5ad3
fix
teytaud Jan 20, 2022
3ee1fd5
po
teytaud Jan 20, 2022
aa38bee
to
teytaud Jan 20, 2022
16f0c57
fix
teytaud Jan 20, 2022
e467b65
Update experiments.py
teytaud Jan 21, 2022
b0ca2da
Update nevergrad/optimization/optimizerlib.py
teytaud Feb 16, 2022
3fe8314
Update test_optimizerlib.py
teytaud Feb 16, 2022
c56c516
Update pcse.py
teytaud Feb 16, 2022
9b7b51c
Update experiments.py
teytaud Feb 16, 2022
fd6b068
Pcse2: irrigation challenge (#1341)
teytaud Mar 2, 2022
895bcd4
fix
teytaud Mar 2, 2022
180f3bd
pcse_license
teytaud Mar 7, 2022
6cb100c
fix_details
teytaud Mar 7, 2022
15f06c8
fix
teytaud Mar 7, 2022
594b0bc
dependencies
teytaud Mar 7, 2022
b177f9c
dependencies
teytaud Mar 7, 2022
77e7a0d
dependencies
teytaud Mar 7, 2022
35cff8b
dependencies
teytaud Mar 7, 2022
46882de
dependencies
teytaud Mar 7, 2022
9b9f476
pcse
teytaud Mar 7, 2022
ce07748
Update pcse.py
teytaud Mar 10, 2022
ab160a7
pcse
teytaud Mar 11, 2022
0e79f18
fix
teytaud Apr 2, 2022
bf25494
fix
teytaud Apr 2, 2022
8a11ad1
fix
teytaud Apr 2, 2022
35c880e
fix
teytaud Apr 3, 2022
347d296
fix
teytaud Apr 3, 2022
fbb42a4
fix
teytaud Jun 18, 2022
33fd981
Adding tabu lists (#1456)
teytaud Jun 21, 2022
82d3bd9
Simulated Annealing (#1455)
teytaud Jun 23, 2022
502ebc0
pcse
teytaud Jan 20, 2022
a849dd2
ZZfix
teytaud Jan 20, 2022
efd381a
nlopt
teytaud Jan 20, 2022
6c0e0ee
ZZnlopt
teytaud Jan 20, 2022
d2b6273
ZZPcse2: irrigation challenge (#1341)
teytaud Mar 2, 2022
4f141a1
ZZUpdate bench.txt
teytaud Mar 2, 2022
3c03ef1
fix
teytaud Aug 8, 2022
ab92ace
pcse
teytaud Aug 8, 2022
889b7ec
benin
teytaud Aug 9, 2022
2888a43
fix
teytaud Aug 11, 2022
77c188c
Optimizing simultaneously variety, irrigation and crop (#1467)
teytaud Dec 6, 2022
bad5825
merge_and_small_fix
teytaud Dec 12, 2022
bf9873b
merge
teytaud Dec 12, 2022
557de3a
fix_december
teytaud Dec 12, 2022
e3f6891
Ccmerge (#1483)
teytaud Dec 12, 2022
926f054
fix
teytaud Dec 12, 2022
69c6d12
mergefck
teytaud Dec 12, 2022
cb48391
fix
teytaud Dec 12, 2022
29aeb46
Merge branch 'pcse' into cc
teytaud Dec 12, 2022
cf28304
fix_we_have
teytaud Dec 12, 2022
5734d8b
no_print
teytaud Dec 12, 2022
cadf3be
remove_useless_comment
teytaud Dec 12, 2022
97b4183
black
teytaud Dec 12, 2022
84e83a4
PcseInsteadOfCropSimulator
teytaud Dec 12, 2022
af49856
fix
teytaud Dec 12, 2022
cbac294
fix_namecollision
teytaud Dec 12, 2022
61467b9
fix
teytaud Dec 12, 2022
4bd5a0d
Merge branch 'pcse' into cc
teytaud Dec 12, 2022
8203e18
fix
teytaud Dec 13, 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
3 changes: 2 additions & 1 deletion mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
[mypy-scipy.*,requests,pandas,compiler_gym,compiler_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.*,pymoo,pymoo.*,pybullet,pybullet_envs,pybulletgym,pyvirtualdisplay,nlopt,aquacrop.*]
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,nlopt,pcse.*,yaml.*,aquacrop.*,nevergrad.functions.gym.tuple_gym_env,nevergrad.functions.gym.multigym]
[mypy-nevergrad.functions.rl.*,torchvision,torchvision.*,nevergrad.functions.games.*,nevergrad.functions.multiobjective.pyhv,nevergrad.optimization.test_doc,pymoo,pymoo.*,pybullet,pybullet_envs,pybulletgym,pyvirtualdisplay,nlopt,pcse.*,yaml.*,aquacrop.*,nevergrad.functions.gym.multigym.nevergrad.functions.gym.tuple_gym_env,pymoo,pymoo.*,pybullet,pybullet_envs,pybulletgym,pyvirtualdisplay,pcse.*,yaml.*,aquacrop.*,nlopt]
#[mypy-nevergrad.functions.rl.*,torchvision,torchvision.*,nevergrad.functions.games.*,nevergrad.functions.multiobjective.pyhv,nevergrad.optimization.test_doc,,nevergrad.functions.gym.tuple_gym_env,nevergrad.functions.gym.multigym]
#[mypy-nevergrad.functions.rl.*,torchvision,torchvision.*,nevergrad.functions.games.*,nevergrad.functions.multiobjective.pyhv,nevergrad.optimization.test_doc,nevergrad.functions.gym.multigym,nevergrad.functions.gym.tuple_gym_env]
#[mypy-nevergrad.functions.rl.*,torchvision,torchvision.*,nevergrad.functions.games.*,nevergrad.functions.multiobjective.pyhv,nevergrad.optimization.test_doc,nevergrad.functions.gym.multigym,nevergrad.functions.gym.tuple_gym_env,pcse.*]
#[mypy-nevergrad.functions.rl.*,torchvision,torchvision.*,nevergrad.functions.games.*,nevergrad.functions.multiobjective.pyhv,nevergrad.optimization.test_doc,nevergrad.functions.gym.multigym,nevergrad.functions.gym.tuple_gym_env,pcse.*,yaml.*]
Expand Down
37 changes: 35 additions & 2 deletions nevergrad/benchmark/experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -1554,6 +1554,18 @@ def kenya_new_many_crop_and_variety_irrigation(seed: tp.Optional[int] = None) ->
return irrigation(seed, kenya=True, variety_choice=True, multi_crop=True, year_min=2016, year_max=2021)


@registry.register
def kenya_future_many_crop_and_variety_irrigation(seed: tp.Optional[int] = None) -> tp.Iterator[Experiment]:
return irrigation(seed, kenya=True, variety_choice=True, multi_crop=True, year_min=2026, year_max=2031)


@registry.register
def kenya_farfuture_many_crop_and_variety_irrigation(
seed: tp.Optional[int] = None,
) -> tp.Iterator[Experiment]:
return irrigation(seed, kenya=True, variety_choice=True, multi_crop=True, year_min=2036, year_max=2041)


@registry.register
def kenya_old_many_crop_and_variety_irrigation(seed: tp.Optional[int] = None) -> tp.Iterator[Experiment]:
return irrigation(seed, kenya=True, variety_choice=True, multi_crop=True, year_min=1996, year_max=2001)
Expand Down Expand Up @@ -1591,7 +1603,7 @@ def crop_simulator(seed: tp.Optional[int] = None) -> tp.Iterator[Experiment]:
Low dimensional problem, only 2 vars. This is optimization for model identification: we want
to find parameters so that the simulation matches observations.
"""
funcs = [CropSimulator()]
funcs = [Pcse()]
seedg = create_seed_generator(seed)
optims = ["DE", "PSO", "CMA", "NGOpt"]
for budget in [25, 50, 100, 200]:
Expand All @@ -1604,14 +1616,35 @@ def crop_simulator(seed: tp.Optional[int] = None) -> tp.Iterator[Experiment]:
yield xp


# @registry.register
# def irrigation(seed: tp.Optional[int] = None, benin: bool = False, variety_choice: bool = False) -> tp.Iterator[Experiment]:
# """Irrigation simulator. Maximize leaf area index,
# so that you get a lot of primary production.
# Sequential or 30 workers."""
## funcs = [Irrigation(i, benin=benin, variety_choice=variety_choice) for i in range(23)]
# seedg = create_seed_generator(seed)
# optims = get_optimizers("basics", seed=next(seedg))
# optims = ["DiagonalCMA", "CMA", "DE", "PSO", "TwoPointsDE", "DiscreteLenglerOnePlusOne"]
# optims += ["NGOptRW", "NGTuned"]
# for budget in [250]: #, 50, 100, 200]:
# for num_workers in [1]: #, 30, 60]:
# if num_workers < budget:
# for algo in optims:
# for fu in funcs:
# xp = Experiment(fu, algo, budget, num_workers=num_workers, seed=next(seedg))
# skip_ci(reason="Too slow")
# if not xp.is_incoherent:
# 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 = [CropSimulator()]
funcs = [Pcse()]
seedg = create_seed_generator(seed)
optims = get_optimizers("basics", seed=next(seedg))
for budget in [25, 50, 100, 200]:
Expand Down
87 changes: 86 additions & 1 deletion nevergrad/functions/irrigation/irrigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
"""


import copy
from datetime import date
from pathlib import Path
import urllib.request
import numpy as np
Expand Down Expand Up @@ -260,13 +262,96 @@ def __init__(
latitude=self.location.latitude, longitude=self.location.longitude
)
WPD[self.address] = self.weatherdataprovider
self.wdp_extend(self.weatherdataprovider)
self.set_data(symmetry, k, rice)
v = [self.meta_total_yield(np.random.rand(self.this_dimension)) for _ in range(5)]
if min(v) != max(v):
break
self.variant_choice[symmetry] = k
print(f"we work on {self.cropname} with variety {self.cropvariety} in {self.address}.")

def wdp_extend(self, wdp):

my_keys = copy.deepcopy(list(wdp.store.keys()))

def we_have(y, m, d):
try:
date(y, m, d)
except ValueError:
return False
return (date(y, m, d), 0) in my_keys

def get_val(y, m, d, slot):
my_vals = [
getattr(wdp.store[(date(y_, m, d), 0)], slot)
for y_ in range(y - 4, y + 5)
if we_have(y_, m, d)
]
assert len(my_vals) >= 3, f"big issue with data {y} {m} {d}!"
return sum(my_vals) / len(my_vals)

def extrapolate(wdp, y, m, od):
d = od
if m == 2 and d == 29:
d = 28
if we_have(y, m, d):
return
first_y = None
last_y = None
container = None
for y_ in range(y - 40, y + 1):
if not we_have(y_, m, d):
continue
if we_have(y_, m, d):
last_y = y_
if first_y is None:
first_y = y_
container = copy.deepcopy(wdp.store[(date(last_y, m, d), 0)])
assert container is not None, "No data found even in 40 years !"
for slot in container.__slots__:
if slot not in [
"IRRAD",
"TMIN",
"TMAX",
"VAP",
"RAIN",
"E0",
"ES0",
"ET0",
"WIND",
"SNOWDEPTH",
"TEMP",
"TMINRA",
]:
continue
try:
value = get_val(last_y, m, d, slot)
except AttributeError:
continue
delta = (get_val(last_y, m, d, slot) - get_val(first_y, m, d, slot)) / (last_y - first_y)
value += delta * (y - last_y)
vmin, vmax = container.ranges[slot]
if value < vmin:
value = vmin
if value > vmax:
value = vmax
setattr(container, slot, value)
wdp.store[(date(y, m, od), 0)] = container

# Extension for the future
for y in range(2020, 2050):
for m in range(1, 13):
for d in range(1, 32):
try:
date(y, m, d)
except ValueError:
continue
extrapolate(self.weatherdataprovider, y, m, d)
ok = [k for k in my_keys if k[0].year == y and k[0].month == m and k[0].day == d]
assert len(ok) < 2
assert len(ok) >= (1 if we_have(y, m, d) else 0)


def set_data(self, symmetry: int, k: int, rice: bool):
crop_types = [crop for crop, variety in self.cropd.get_crops_varieties().items()]
crop_types = [c for c in crop_types if "obacco" not in c]
Expand Down Expand Up @@ -324,7 +409,7 @@ def total_yield(self, x: np.ndarray, year: int = 2006):
]
c = self.total_irrigation
if len(x) == 10:
c = 0
c = 0.0
a0 = c * x[4] / (x[4] + x[5] + x[6] + x[7])
a1 = c * x[5] / (x[4] + x[5] + x[6] + x[7])
a2 = c * x[6] / (x[4] + x[5] + x[6] + x[7])
Expand Down
2 changes: 1 addition & 1 deletion nevergrad/functions/pcse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

# Based on https://github.com/ajwdewit/pcse_notebooks
# MIT License.
from .pcse import CropSimulator as CropSimulator
from .pcse import Pcse as Pcse
1 change: 1 addition & 0 deletions nevergrad/functions/pcse/pcse.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ def __call__(self, par_values, grad=None):
# print("Objective function value with default parameters (%s): %s" % (defaults, error))
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")
Expand Down
2 changes: 1 addition & 1 deletion nevergrad/functions/pcse/test_pcse.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


def test_pcse() -> None:
func = pcse.CropSimulator()
func = pcse.Pcse()
x = 0 * np.random.rand(func.dimension)
value = func(x)
value2 = func(x)
Expand Down
6 changes: 4 additions & 2 deletions nevergrad/optimization/test_optimizerlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,9 +737,11 @@ def test_ngopt_selection(
# assert choice == "MetaTuneRecentering"
assert "MetaTuneRecentering" in optim_string
if num_workers > 1:
# assert choice not in ["SQP", "Cobyla"]
assert "SQP" not in optim_string and "Cobyla" not in optim_string
assert opt._info()["sub-optim"] in optim_string
if "CMA" not in choice:
assert choice == opt._info()["sub-optim"]
else:
assert choice in opt._info()["sub-optim"]


def test_bo_ordering() -> None:
Expand Down
2 changes: 1 addition & 1 deletion requirements/bench.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,4 @@ box2d-py>=2.3.5
glfw
mujoco
pcse>=5.5.0
nlopt
nlopt