Skip to content

Commit

Permalink
Merge pull request #241 from bees4ever/fix-(p)dds-parameter-bounds
Browse files Browse the repository at this point in the history
fix parameter running out of bounds causes by wrong index
  • Loading branch information
thouska authored Sep 8, 2020
2 parents 309e8d9 + 4391506 commit bd4d57d
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 8 deletions.
8 changes: 4 additions & 4 deletions spotpy/algorithms/dds.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,9 @@ def calculate_next_s_test(self, previous_x_curr, rep, rep_limit, r):
new_x_curr[j] = new_value # change relevant dec var value in x_curr

if dvn_count == 0: # no DVs selected at random, so select ONE
dec_var = np.int(np.ceil(amount_params * self.np_random.rand()))
new_value = self.dds_generator.neigh_value_mixed(previous_x_curr, r, dec_var - 1, self.min_bound[j],self.max_bound[j])

new_x_curr[dec_var - 1] = new_value # change relevant decision variable value in s_test
dec_var = np.int(np.ceil(amount_params * self.np_random.rand())) - 1
new_value = self.dds_generator.neigh_value_mixed(previous_x_curr, r, dec_var, self.min_bound[dec_var],
self.max_bound[dec_var])
new_x_curr[dec_var] = new_value # change relevant decision variable value in s_test

return new_x_curr
2 changes: 1 addition & 1 deletion spotpy/algorithms/padds.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,11 @@ def calculate_next_s_test(self, previous_x_curr, rep, rep_limit, r):
new_x_curr[j] = new_value # change relevant dec var value in x_curr

if dvn_count == 0: # no DVs selected at random, so select ONE

dec_var = np.int(np.ceil(amount_params * self.np_random.rand()))
new_value = self.dds_generator.neigh_value_mixed(previous_x_curr, r, dec_var - 1, self.min_bound[dec_var - 1],self.max_bound[dec_var - 1])

new_x_curr[dec_var - 1] = new_value # change relevant decision variable value in s_test

return new_x_curr


Expand Down
15 changes: 15 additions & 0 deletions spotpy/examples/spot_setup_dds.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ def _objfunc_switcher(self, name):
for j in range(2)] + [Uniform('c' + str(j), -500, 700, 1.5, 3.0, -500, 700,
doc=str(j) + 'continuous parameter within a boundary')
for j in range(8)]
elif name == "cmf_style":
self.objfunc = ackley10
self.params = [Uniform(.5, 5., optguess=1.5, doc='saturated depth at beginning'),
Uniform(.001, .8, optguess=.1, doc='porosity of matrix [m3 Pores / m3 Soil]'),
Uniform(1., 240., optguess=10.,
doc='ssaturated conductivity of macropores [m/day]'),
Uniform(.0001, .5, optguess=.05, doc='macropore fraction [m3/m3]'),
Uniform(.005, 1., optguess=.05,
doc='mean distance between the macropores [m]'),
Uniform(0., 1., optguess=0.,
doc='water content when matric potential pointing towards -infinity'),
Uniform(.5, 1., optguess=.99,
doc='wetness above which the parabolic extrapolation is used instead of VGM'),
Uniform(0., 50, optguess=.1,
doc='exchange rate [1/day] for macropore-matrix-exchange')]

def parameters(self):
if self.params is None:
Expand Down
24 changes: 21 additions & 3 deletions spotpy/examples/tutorial_padds.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
import numpy as np
import sys
from spotpy.parameter import Uniform
try:
import spotpy
except ModuleNotFoundError:
Expand Down Expand Up @@ -48,10 +49,27 @@ def ZDT1(x):


class padds_spot_setup(object):
def __init__(self):
def __init__(self, default=True):
self.params = []
for i in range(30):
self.params.append(spotpy.parameter.Uniform(str(i+1), 0, 1, 0, 0, 0, 1,doc="param no " + str(i+1)))
if default:
for i in range(30):
self.params.append(spotpy.parameter.Uniform(str(i+1), 0, 1, 0, 0, 0, 1,doc="param no " + str(i+1)))
else:
self.params = [Uniform(.5, 5., optguess=1.5, doc='saturated depth at beginning'),
Uniform(.001, .8, optguess=.1, doc='porosity of matrix [m3 Pores / m3 Soil]'),
Uniform(1., 240., optguess=10.,
doc='ssaturated conductivity of macropores [m/day]'),
Uniform(.0001, .5, optguess=.05, doc='macropore fraction [m3/m3]'),
Uniform(.005, 1., optguess=.05,
doc='mean distance between the macropores [m]'),
Uniform(0., 1., optguess=0.,
doc='water content when matric potential pointing towards -infinity'),
Uniform(.5, 1., optguess=.99,
doc='wetness above which the parabolic extrapolation is used instead of VGM'),
Uniform(0., 50, optguess=.1,
doc='exchange rate [1/day] for macropore-matrix-exchange')]
for i in range(8,30):
self.params.append(Uniform(str(i+1), 0, 1, 0, 0, 0, 1,doc="param no " + str(i+1)))

def parameters(self):
return spotpy.parameter.generate(self.params)
Expand Down
18 changes: 18 additions & 0 deletions tests/test_dds.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,24 @@ def test_run_own_initial_1(self):
def test_run_own_initial_2(self):
self.run_a_dds("own_input_2")

def outside_bound(self, x_curr, min_bound, max_bound):
out_left = min_bound > x_curr # [x<x_curr self.min_bound, self.max_bound
out_right = max_bound < x_curr

self.assertNotIn(True, out_right)
self.assertNotIn(True, out_left)

def test_bounds(self):
self.spot_setup._objfunc_switcher("cmf_style")
sampler = spotpy.algorithms.dds(self.spot_setup, parallel="seq", dbname='test_DDS', dbformat="csv",
sim_timeout=self.timeout)
sampler._set_np_random(self.f_random)

results = sampler.sample(1000,1)
print("results",results[0]['sbest'])
print(sampler.min_bound, sampler.max_bound)
self.outside_bound(results[0]['sbest'], sampler.min_bound, sampler.max_bound)

def run_a_dds(self, run):
original_result = self.json_helper(run)

Expand Down
16 changes: 16 additions & 0 deletions tests/test_padds.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,22 @@ def test_run_8(self):
def test_run_9(self):
self.run_a_dds(9)

def outside_bound(self, x_curr, min_bound, max_bound):
out_left = min_bound > x_curr # [x<x_curr self.min_bound, self.max_bound
out_right = max_bound < x_curr

self.assertNotIn(True, out_right)
self.assertNotIn(True, out_left)

def test_bounds(self):
self.spot_setup = padds_spot_setup(False)
sampler = spotpy.algorithms.padds(self.spot_setup, parallel="seq", dbname='test_DDS', dbformat="csv",
sim_timeout=self.timeout)
sampler._set_np_random(self.f_random)
results = sampler.sample(100, 1)

self.outside_bound(results[0]['sbest'], sampler.min_bound, sampler.max_bound)

def test_chc(self):
self.assertArrayEqual([0.01851852, 0. , 0.01851852, 0.01851852, 0.,0.01851852],
chc([[1,10], [2,9.8],[3,5] ,[4, 4], [8,2], [10,1]]))
Expand Down

0 comments on commit bd4d57d

Please sign in to comment.