Skip to content

Commit

Permalink
Fixed thouska#175 and thouska#166 with extensive check to preserve th…
Browse files Browse the repository at this point in the history
…e constants

in sceua and developed a number of tests to ensure the constants stays
constant in all samplers.
  • Loading branch information
philippkraft committed Sep 5, 2018
1 parent 931dec6 commit 9382dbf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
16 changes: 9 additions & 7 deletions spotpy/algorithms/sceua.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def simulate(self, id_params_tuple):
lcs.sort()

# Construct the simplex:
s = np.zeros((self.nps, self.nopt))

s = cx[lcs, :]
sf = cf[lcs]

Expand Down Expand Up @@ -188,7 +188,7 @@ def sample(self, repetitions, ngs=20, kstop=100, pcento=0.0000001, peps=0.000000
self.bl, self.bu = self.parameter()['minbound'], self.parameter()[
'maxbound']
bound = self.bu - self.bl # np.array
stochastic_parameters = bound != 0
self.stochastic_parameters = bound != 0
if self.breakpoint == 'read' or self.breakpoint == 'readandwrite':
data_frombreak = self.read_breakdata(self.dbname)
icall = data_frombreak[0]
Expand Down Expand Up @@ -250,7 +250,7 @@ def sample(self, repetitions, ngs=20, kstop=100, pcento=0.0000001, peps=0.000000

# Computes the normalized geometric range of the parameters
gnrng = np.exp(
np.mean(np.log((np.max(x, axis=0) - np.min(x, axis=0)) / bound)))
np.mean(np.log((np.max(x[:, self.stochastic_parameters], axis=0) - np.min(x[:, self.stochastic_parameters], axis=0)) / bound[self.stochastic_parameters])))

# Check for convergency;
if icall >= repetitions:
Expand Down Expand Up @@ -294,7 +294,7 @@ def sample(self, repetitions, ngs=20, kstop=100, pcento=0.0000001, peps=0.000000
for rep in range(int(self.ngs)))
for igs, likes, pars, sims, cx, cf, k1, k2 in self.repeat(param_generator):
icall += len(likes)
x[k2, stochastic_parameters] = cx[k1, stochastic_parameters]
x[k2, :] = cx[k1, :]
xf[k2] = cf[k1]
#print(len(likes))
for i in range(len(likes)):
Expand Down Expand Up @@ -336,7 +336,7 @@ def sample(self, repetitions, ngs=20, kstop=100, pcento=0.0000001, peps=0.000000

# Computes the normalized geometric range of the parameters
gnrng = np.exp(
np.mean(np.log((np.max(x, axis=0) - np.min(x, axis=0)) / bound)))
np.mean(np.log((np.max(x[:, self.stochastic_parameters], axis=0) - np.min(x[:, self.stochastic_parameters], axis=0)) / bound[self.stochastic_parameters])))

# Check for convergency;
if icall >= repetitions:
Expand Down Expand Up @@ -397,7 +397,7 @@ def _cceua(self, s, sf, icall):
# iviol = flag indicating if constraints are violated
# = 1 , yes
# = 0 , no

constant_parameters = np.invert(self.stochastic_parameters)
self.nps, self.nopt = s.shape
alpha = 1.0
beta = 0.5
Expand All @@ -411,7 +411,7 @@ def _cceua(self, s, sf, icall):

# Attempt a reflection point
snew = ce + alpha * (ce - sw)

snew[constant_parameters] = sw[constant_parameters]
# Check if is outside the bounds:
ibound = 0
s1 = snew - self.bl
Expand Down Expand Up @@ -440,6 +440,8 @@ def _cceua(self, s, sf, icall):
# Reflection failed; now attempt a contraction point:
if fnew > fw:
snew = sw + beta * (ce - sw)
snew[constant_parameters] = sw[constant_parameters]

simulations = self.model(snew)
like = self.postprocessing(icall, snew, simulations, save=False)
# like = self.objectivefunction(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class RosenbrockWithList(Rosenbrock):
x = parameter.Uniform(-10, 10, 1.5, 3.0, -10, 10, doc='x value of Rosenbrock function')
y = parameter.Uniform(-10, 10, 1.5, 3.0, -10, 10, doc='y value of Rosenbrock function')
z = parameter.Uniform(-10, 10, 1.5, 3.0, -10, 10, doc='z value of Rosenbrock function')
l = parameter.List(range(10), repeat=True)
l = parameter.List(np.arange(0, 10), repeat=True, doc='list parameter for testing')


def get_all_samplers():
Expand All @@ -80,8 +80,7 @@ def sampler_with_constant(self, sampler_class):
sampler_name = sampler_class.__name__
print(sampler_name)
sampler = sampler_class(self.setup, dbformat='ram', save_sim=False)
sampler.sample(100)
print(sampler.datawriter.ram[-1])
sampler.sample(1000)
print('-' * 50)
self.assertTrue(all(line[-2] == 0 for line in sampler.datawriter.ram),
msg='Parameter c == 0 not true in all lines')
Expand Down Expand Up @@ -126,7 +125,6 @@ def setUp(self):
self.setup = RosenbrockWithList()
print(spotpy.describe.setup(self.setup))


def test_list_parameter(self):

for sampler_name, sampler_class in get_all_samplers():
Expand Down

0 comments on commit 9382dbf

Please sign in to comment.