Skip to content

Commit

Permalink
Adds Unittest for nsgaii
Browse files Browse the repository at this point in the history
  • Loading branch information
thouska committed Sep 3, 2020
1 parent 6e7f1fe commit 0964c36
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
2 changes: 1 addition & 1 deletion spotpy/algorithms/padds.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spotpy/examples/spot_setup_rosenbrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 23 additions & 7 deletions tests/test_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,24 @@
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


#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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 0964c36

Please sign in to comment.