From 0964c36028366995330cffbe60bfdfa90bf8a262 Mon Sep 17 00:00:00 2001 From: thouska Date: Thu, 3 Sep 2020 12:30:01 +0200 Subject: [PATCH] Adds Unittest for nsgaii --- spotpy/algorithms/padds.py | 2 +- spotpy/examples/spot_setup_rosenbrock.py | 2 +- tests/test_algorithms.py | 30 ++++++++++++++++++------ 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/spotpy/algorithms/padds.py b/spotpy/algorithms/padds.py index 90105f33..7e9e8670 100644 --- a/spotpy/algorithms/padds.py +++ b/spotpy/algorithms/padds.py @@ -89,7 +89,7 @@ def __init__(self, *args, **kwargs): self.r = kwargs.pop("r") except KeyError: self.r = 0.2 # default value - + self._return_all_likes=True #alloes multi-objective calibration super(padds, self).__init__(*args, **kwargs) self.np_random = np.random diff --git a/spotpy/examples/spot_setup_rosenbrock.py b/spotpy/examples/spot_setup_rosenbrock.py index 67f45c6c..99632b05 100644 --- a/spotpy/examples/spot_setup_rosenbrock.py +++ b/spotpy/examples/spot_setup_rosenbrock.py @@ -36,7 +36,7 @@ def evaluation(self): observations = [0] return observations - def objectivefunction(self, simulation, evaluation): + def objectivefunction(self, simulation, evaluation, params=None): #SPOTPY expects to get one or multiple values back, #that define the performence of the model run diff --git a/tests/test_algorithms.py b/tests/test_algorithms.py index f6bfce4b..e8a809d9 100644 --- a/tests/test_algorithms.py +++ b/tests/test_algorithms.py @@ -12,10 +12,9 @@ import sys sys.path.append(".") import spotpy -import numpy as np +#from spotpy.examples.tutorial_padds import padds_spot_setup from spotpy.examples.spot_setup_rosenbrock import spot_setup -from spotpy.examples.tutorial_padds import padds_spot_setup -from spotpy.describe import describe +#from spotpy.describe import describe import os from spotpy.likelihoods import gaussianLikelihoodMeasErrorOut as GausianLike @@ -23,6 +22,14 @@ #https://docs.python.org/3/library/unittest.html class TestAlgorithms(unittest.TestCase): + + def multi_obj_func(self, evaluation, simulation, params=None): + #used to overwrite objective function in hymod example + like1 = abs(spotpy.objectivefunctions.bias(evaluation, simulation)) + like2 = spotpy.objectivefunctions.rmse(evaluation, simulation) + like3 = spotpy.objectivefunctions.rsquared(evaluation, simulation)*-1 + return [like2, like3] + def setUp(self): # How many digits to match in case of floating point answers self.tolerance = 7 @@ -115,11 +122,20 @@ def test_fast(self): results = sampler.getdata() self.assertEqual(len(results), self.rep) #Si values should be returned - def test_padds(self): - sampler=spotpy.algorithms.padds(padds_spot_setup(),parallel=self.parallel, dbname='RosenPADDS', dbformat=self.dbformat, sim_timeout=self.timeout) - sampler.sample(self.rep) +# def test_padds(self): +# sampler=spotpy.algorithms.padds(padds_spot_setup(),parallel=self.parallel, dbname='Rosen', dbformat=self.dbformat, sim_timeout=self.timeout) +# sampler.sample(self.rep) +# results = sampler.getdata() +# self.assertEqual(len(results)+5, self.rep) #Si values should be returned + + def test_nsgaii(self): + generations=40 + n_pop = 20 + skip_duplicates = True + sampler=spotpy.algorithms.NSGAII(spot_setup(self.multi_obj_func),parallel=self.parallel, dbname='Rosen', dbformat=self.dbformat, sim_timeout=self.timeout) + sampler.sample(generations, n_obj= 3, n_pop = n_pop, skip_duplicates = skip_duplicates) results = sampler.getdata() - self.assertEqual(len(results)+5, self.rep) #Si values should be returned + self.assertLessEqual(len(results), generations*n_pop) @classmethod def tearDownClass(cls):