Skip to content

Commit

Permalink
Fix for #226
Browse files Browse the repository at this point in the history
  • Loading branch information
thouska committed Sep 2, 2019
1 parent 6097c10 commit e29e403
Showing 1 changed file with 11 additions and 20 deletions.
31 changes: 11 additions & 20 deletions spotpy/algorithms/sceua.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def simulate(self, id_params_tuple):

else: # complex-evolution
igs, x, xf, cx, cf, sce_vars = id_params_tuple
self.npg, self.nopt, self.ngs, self.nspl, self.nps, self.bl, self.bu, self.status, self.stochastic_parameters = sce_vars
self.npg, self.nopt, self.ngs, self.nspl, self.nps, self.bl, self.bu, self.stochastic_parameters, discarded_runs = sce_vars
# Partition the population into complexes (sub-populations);
k1 = np.arange(self.npg, dtype=int)
k2 = k1 * self.ngs + igs
Expand Down Expand Up @@ -107,7 +107,7 @@ def simulate(self, id_params_tuple):
s = cx[lcs, :]
sf = cf[lcs]

snew, fnew, simulation = self._cceua(s, sf)
snew, fnew, simulation, discarded_runs = self._cceua(s, sf, discarded_runs)
likes.append(fnew)
pars.append(snew)
sims.append(simulation)
Expand All @@ -125,11 +125,8 @@ def simulate(self, id_params_tuple):
cf = np.sort(cf)
cx = cx[idx, :]

if self.status.stop:
print('Stopping simulation mode')
return igs, likes, pars, sims, cx, cf, k1, k2
# Replace the complex back into the population;
return igs, likes, pars, sims, cx, cf, k1, k2
return igs, likes, pars, sims, cx, cf, k1, k2, discarded_runs

def sample(self, repetitions, ngs=20, kstop=100, pcento=0.0000001, peps=0.0000001):
"""
Expand Down Expand Up @@ -246,12 +243,13 @@ def sample(self, repetitions, ngs=20, kstop=100, pcento=0.0000001, peps=0.000000
proceed = False

sce_vars = [self.npg, self.nopt, self.ngs, self.nspl,
self.nps, self.bl, self.bu, self.status, self.stochastic_parameters]
self.nps, self.bl, self.bu, self.stochastic_parameters, self.discarded_runs]
param_generator = ((rep, x, xf, cx, cf, sce_vars)
for rep in range(int(self.ngs)))
for igs, likes, pars, sims, cx, cf, k1, k2 in self.repeat(param_generator):
for igs, likes, pars, sims, cx, cf, k1, k2, discarded_runs in self.repeat(param_generator):
x[k2, :] = cx[k1, :]
xf[k2] = cf[k1]
self.discard_runs = discarded_runs
for i in range(len(likes)):
if not self.status.stop:
like = self.postprocessing(i, pars[i], sims[i], chains=i+1)
Expand Down Expand Up @@ -331,7 +329,7 @@ def sample(self, repetitions, ngs=20, kstop=100, pcento=0.0000001, peps=0.000000
self.final_call()


def _cceua(self, s, sf):
def _cceua(self, s, sf, discarded_runs):
# This is the subroutine for generating a new point in a simplex
#
# s(.,.) = the sorted simplex in order of increasing function values
Expand Down Expand Up @@ -380,12 +378,9 @@ def _cceua(self, s, sf):
## fnew = functn(self.nopt,snew);
_, _, simulations = _algorithm.simulate(self, (1, snew))
like = self.postprocessing(1, snew, simulations, save_run=False, block_print=True)
self.discarded_runs+=1
discarded_runs+=1

fnew = like
if self.status.stop:
print('Stopping complex evolution')
return snew, fnew, simulations

# Reflection failed; now attempt a contraction point:
if fnew > fw:
Expand All @@ -394,22 +389,18 @@ def _cceua(self, s, sf):

_, _, simulations = _algorithm.simulate(self, (2, snew))
like = self.postprocessing(2, snew, simulations, save_run=False, block_print=True)
self.discarded_runs+=1
discarded_runs+=1
fnew = like
if self.status.stop:
print('Stopping complex evolution')
return snew, fnew, simulations


# Both reflection and contraction have failed, attempt a random point;
if fnew > fw:
snew = self._sampleinputmatrix(1, self.nopt)[0]
_, _, simulations = _algorithm.simulate(self, (3, snew))
like = self.postprocessing(3, snew, simulations, save_run=False, block_print=True)
self.discarded_runs+=1
discarded_runs+=1
fnew = like
# END OF CCE
return snew, fnew, simulations
return snew, fnew, simulations, discarded_runs

def _sampleinputmatrix(self, nrows, npars):
'''
Expand Down

0 comments on commit e29e403

Please sign in to comment.