Skip to content

Commit

Permalink
Merge pull request #571 from htm-community/swarming-fix2
Browse files Browse the repository at this point in the history
Fixed particle swarming optimization
  • Loading branch information
breznak authored Jul 17, 2019
2 parents 13446b0 + 9c48edd commit 8e11cdb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
6 changes: 3 additions & 3 deletions py/htm/examples/mnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def load_images(file_name):
default_parameters = {
'potentialRadius': 7,
'boostStrength': 7.0,
'columnDimensions': (28*28*8, 1),
'columnDimensions': (79, 79),
'dutyCyclePeriod': 1402,
'localAreaDensity': 0.1,
'minPctOverlapDutyCycle': 0.2,
Expand Down Expand Up @@ -149,8 +149,8 @@ def main(parameters=default_parameters, argv=None, verbose=True):
score = score / len(test_data)

print('Score:', 100 * score, '%')
return score < 0.95
return score


if __name__ == '__main__':
sys.exit( main() )
sys.exit( main() < 0.95 )
18 changes: 14 additions & 4 deletions py/htm/optimization/swarming.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ class ParticleData:
p.best - ParameterSet
p.score - float
p.age - Number of times this particle has been evaluated/updated.
p.lock - Is this particle currently being evaluated?
"""
def __init__(self, initial_parameters, swarm=None):
self.parameters = ParameterSet( initial_parameters )
self.best = None
self.best_score = None
self.age = 0
self.initialize_velocities(swarm)
self.lock = False

def initialize_velocities(self, swarm=None):
# Make a new parameter structure for the velocity data.
Expand Down Expand Up @@ -116,7 +118,6 @@ class ParticleSwarmOptimization(BaseOptimizer):
Attributes:
pso.lab - Laboratory
pso.particles - Number of particles to use.
pso.next_particle - Index into pso.swarm
pso.swarm_path - Data File for this particle swarm.
pso.swarm - List of ParticleData
pso.best - ParameterSet
Expand All @@ -142,7 +143,6 @@ def __init__(self, lab, args):
self.lab = lab
self.swarm = []
self.particles = args.swarming
self.next_particle = random.randrange( self.particles )
self.best = None
self.best_score = None
assert( self.particles >= args.processes )
Expand All @@ -164,11 +164,20 @@ def __init__(self, lab, args):
if( len(self.swarm) >= 3 ):
new_particle.update_position()
self.swarm.append( new_particle )
# Clear all of the mutex locks before starting.
for particle in self.swarm:
particle.lock = False

def suggest_parameters(self):
particle_data = self.swarm[self.next_particle]
self.next_particle = (self.next_particle + 1) % self.particles
unlocked_particles = [p for p in self.swarm if not p.lock]
if not unlocked_particles:
print("Thread blocked waiting for particle to evaluate.")
time.sleep( 60 )
return self.suggest_parameters()

particle_data = random.choice( unlocked_particles )
particle_data.parameters.typecast( self.lab.structure )
particle_data.lock = True
return particle_data.parameters

def collect_results(self, parameters, score):
Expand Down Expand Up @@ -198,6 +207,7 @@ def collect_results(self, parameters, score):
self.best_score = score
print("New global best score %g."%score)
particle.update( score, self.best )
particle.lock = False
self.save()

def save(self):
Expand Down

0 comments on commit 8e11cdb

Please sign in to comment.