Skip to content

Commit

Permalink
Merge pull request #158 from emitra17/156-reflect
Browse files Browse the repository at this point in the history
Particle swarm improvements
  • Loading branch information
emitra17 authored Jul 16, 2018
2 parents c938649 + c34b928 commit 69577fd
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 9 deletions.
Binary file modified Documentation.pdf
Binary file not shown.
4 changes: 2 additions & 2 deletions docs/config_keys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -539,9 +539,9 @@ The following options are only available with ``fit_type = de``, and serve to ma
* ``social = 1.7``

**particle_weight**
Inertia weight of particle. A value less than 1 can be thought of as friction that contiuously decelerates the particle.
Inertia weight of particle. A value less than 1 can be thought of as friction that contniuously decelerates the particle.

Default: 1
Default: 0.7

Example:
* ``particle_weight = 0.9``
Expand Down
13 changes: 9 additions & 4 deletions pybnf/algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -922,8 +922,10 @@ def start_run(self):
for i in range(len(new_params_list)):
p = new_params_list[i]
p.name = 'iter0p%i' % i
# Todo: Smart way to initialize velocity?
new_velocity = {v.name: np.random.uniform(-1, 1) for v in self.variables}

# As suggested by Engelbrecht 2012, set all initial velocities to 0
new_velocity = dict({v.name: 0. for v in self.variables})

self.swarm.append([p, new_velocity])
self.pset_map[p] = len(self.swarm)-1 # Index of the newly added PSet.

Expand Down Expand Up @@ -987,10 +989,13 @@ def got_result(self, res):
# If so, update based on reflection protocol and set velocity to 0
new_vars = []
for v in self.swarm[p][0]:
new_val = v.value + self.swarm[p][1][v.name]
new_vars.append(v.add(self.swarm[p][1][v.name]))
if v.log_space:
new_val = 10.**(np.log10(v.value) + self.swarm[p][1][v.name])
else:
new_val = v.value + self.swarm[p][1][v.name]
if new_val < v.lower_bound or v.upper_bound < new_val:
self.swarm[p][1][v.name] = 0.0
new_vars.append(v.add(self.swarm[p][1][v.name]))

new_pset = PSet(new_vars)
self.swarm[p][0] = new_pset
Expand Down
2 changes: 1 addition & 1 deletion pybnf/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def default_config():
'mutation_rate': 0.5, 'mutation_factor': 0.5, 'islands': 1, 'migrate_every': 20, 'num_to_migrate': 3,
'stop_tolerance': 0.002, 'de_strategy': 'rand1',

'particle_weight': 1.0, 'adaptive_n_max': 30, 'adaptive_n_stop': np.inf, 'adaptive_abs_tol': 0.0,
'particle_weight': 0.7, 'adaptive_n_max': 30, 'adaptive_n_stop': np.inf, 'adaptive_abs_tol': 0.0,
'adaptive_rel_tol': 0.0, 'cognitive': 1.5, 'social': 1.5, 'v_stop': 0.,

'local_min_limit': 5,
Expand Down
7 changes: 5 additions & 2 deletions pybnf/pset.py
Original file line number Diff line number Diff line change
Expand Up @@ -974,8 +974,11 @@ def _reflect(self, new):

while True:
if num_reflections >= 1000:
logger.error("Error in parameter reflection. Too many reflections: Init = %s, add = %s, parameter = %s" % (cur, add, self.name))
raise PybnfError("Too many reflections for parameter %s. Current value = %s, adding value %s" % (self.name, cur, add))
logger.error("Error in parameter reflection. Too many reflections: Init = %s, add = %s, parameter ="
" %s. Set parameter to a random value" % (cur, add, self.name))
# Return a random value. Not ideal, but better than crashing the whole run.
rand = np.random.uniform(lb, ub)
return 10.**rand if self.log_space else rand

num_reflections += 1
if cur + add > ub:
Expand Down

0 comments on commit 69577fd

Please sign in to comment.