From a6e921d4456acbfa3bb1000f0acabfbbdb9b6505 Mon Sep 17 00:00:00 2001 From: thouska Date: Fri, 9 Oct 2020 14:18:16 +0200 Subject: [PATCH] Fix multi objective tests --- spotpy/examples/tutorial_padds_hymod.py | 2 +- tests/test_algorithms.py | 23 ++++++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/spotpy/examples/tutorial_padds_hymod.py b/spotpy/examples/tutorial_padds_hymod.py index c239285f..0c2c53d2 100644 --- a/spotpy/examples/tutorial_padds_hymod.py +++ b/spotpy/examples/tutorial_padds_hymod.py @@ -123,7 +123,7 @@ def plot_parameter_histogram(ax, results, parameter): ax11.plot(q5,color='dimgrey',linestyle='solid') ax11.plot(q95,color='dimgrey',linestyle='solid') ax11.fill_between(np.arange(0,len(q5),1),list(q5),list(q95),facecolor='dimgrey',zorder=0, - linewidth=0,label='parameter uncertainty') + linewidth=0,label='parameter uncertainty') ax11.plot(spot_setup.evaluation(),'r.',label='data') ax11.set_ylim(-50,450) ax11.set_xlim(0,729) diff --git a/tests/test_algorithms.py b/tests/test_algorithms.py index f6bfce4b..0fbb3f56 100644 --- a/tests/test_algorithms.py +++ b/tests/test_algorithms.py @@ -14,15 +14,24 @@ import spotpy import numpy as np from spotpy.examples.spot_setup_rosenbrock import spot_setup -from spotpy.examples.tutorial_padds import padds_spot_setup +from spotpy.examples.spot_setup_hymod_python import spot_setup as spot_setup_hymod from spotpy.describe import describe import os +import numpy as np from spotpy.likelihoods import gaussianLikelihoodMeasErrorOut as GausianLike #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 np.array([like1, like2, like3]) + def setUp(self): # How many digits to match in case of floating point answers self.tolerance = 7 @@ -116,8 +125,16 @@ def test_fast(self): 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) + sampler=spotpy.algorithms.padds(spot_setup_hymod(self.multi_obj_func),parallel=self.parallel, dbname='Rosen', dbformat=self.dbformat, sim_timeout=self.timeout) + sampler.sample(int(self.rep*0.5), metric='ones') + results = sampler.getdata() + self.assertEqual(len(results)+5, int(self.rep*0.5)) + + def test_nsgaii(self): + generations=20 + n_pop = 10 + sampler=spotpy.algorithms.NSGAII(spot_setup_hymod(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) results = sampler.getdata() self.assertEqual(len(results)+5, self.rep) #Si values should be returned