-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* aquacrop * fix * fix * fix * Update ac.py * black * Update experiments.py (#1361) * fix * Update bench.txt * fix * fix * fix * tentative_pip3 * yet_another_tentative_fi * yet_another_tentative_fi * fix * fix_suffering * desperate_try * desperate_try * desperate_try * desperate_try * fix * desperate_try * desperate_try * desperate_try * desperate_try * fix * Update config.yml * fix * Update setup.py * Update main.txt * fix
- Loading branch information
Showing
5 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# 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. | ||
|
||
from .ac import NgAquacrop as NgAquacrop |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
# 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. | ||
|
||
""" | ||
Optimization of the FAO crop management model. | ||
Based on | ||
https://colab.research.google.com/github/thomasdkelly/aquacrop/blob/master/tutorials/AquaCrop_OSPy_Notebook_3.ipynb#scrollTo=YDm931IGNxCb | ||
""" | ||
|
||
from nevergrad.parametrization import parameter | ||
from ..base import ExperimentFunction | ||
from ..base import UnsupportedExperiment as UnsupportedExperiment | ||
|
||
# pylint: disable=too-many-locals,too-many-statements | ||
|
||
# Inspired by | ||
# https://colab.research.google.com/github/thomasdkelly/aquacrop/blob/master/tutorials/AquaCrop_OSPy_Notebook_3.ipynb#scrollTo=YDm931IGNxCb | ||
|
||
# In the colab it was: | ||
# from aquacrop.classes import * | ||
# from aquacrop.core import * | ||
|
||
|
||
class NgAquacrop(ExperimentFunction): | ||
def __init__(self, num_smts: int, max_irr_seasonal: float) -> None: | ||
self.num_smts = num_smts | ||
self.max_irr_seasonal = max_irr_seasonal | ||
super().__init__(self.loss, parametrization=parameter.Array(shape=(num_smts,))) | ||
|
||
def loss(self, smts): | ||
try: | ||
import aquacrop | ||
except ImportError: | ||
raise UnsupportedExperiment("Please install aquacrop==0.2 for FAO aquacrop experiments") | ||
path = aquacrop.core.get_filepath("champion_climate.txt") | ||
wdf = aquacrop.core.prepare_weather(path) | ||
|
||
def run_model(smts, max_irr_season, year1, year2): | ||
""" | ||
Function to run model and return results for given set of soil moisture targets. | ||
""" | ||
|
||
maize = aquacrop.classes.CropClass("Maize", PlantingDate="05/01") # define crop | ||
loam = aquacrop.classes.SoilClass("ClayLoam") # define soil | ||
init_wc = aquacrop.classes.InitWCClass( | ||
wc_type="Pct", value=[70] | ||
) # define initial soil water conditions | ||
|
||
irrmngt = aquacrop.classes.IrrMngtClass( | ||
IrrMethod=1, SMT=smts, MaxIrrSeason=max_irr_season | ||
) # define irrigation management | ||
|
||
# create and run model | ||
model = aquacrop.core.AquaCropModel( | ||
f"{year1}/05/01", f"{year2}/10/31", wdf, loam, maize, IrrMngt=irrmngt, InitWC=init_wc | ||
) | ||
model.initialize() | ||
model.step(till_termination=True) | ||
return model.Outputs.Final | ||
|
||
def evaluate(smts) -> float: # ,max_irr_season,test=False): | ||
""" | ||
Function to run model and calculate reward (yield) for given set of soil moisture targets | ||
""" | ||
max_irr_season = self.max_irr_seasonal | ||
assert len(smts) == self.num_smts | ||
out = run_model(smts, max_irr_season, year1=2016, year2=2018) | ||
# get yields. | ||
reward = out["Yield (tonne/ha)"].mean() | ||
return -reward | ||
|
||
return evaluate(smts) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# 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 ac | ||
|
||
|
||
def test_ac() -> None: | ||
func = ac.NgAquacrop(4, 12.0) | ||
x = 50.0 * np.random.rand(func.dimension) | ||
value = func(x) | ||
value2 = func(x) | ||
x = 50.0 * np.random.rand(func.dimension) | ||
value3 = func(x) | ||
np.testing.assert_almost_equal(value, value2) | ||
assert value != value3 |