def test_register_new_local_best_result(self): """Register new local best result.""" class Environment(object): def setInfo(self, info): self.info = info def updateParticleBest(self, info): assert info == self.info def register(self, particle): assert self == particle.environment env = Environment() position = matrix([1, 2, 3]) fitness = 5 info = Information(position=position, fitness=fitness) env.setInfo(Information(position=position, fitness=fitness)) particle = Particle( environment=env, best=info) position = matrix([4, 5, 6]) fitness = 4 info = Information(position=position, fitness=fitness) env.setInfo(info) particle.current = info
def test_cognition(self): """Test cognition.""" config = {} config['cognition'] = 4 env = Environment(config=config) assert env.config['cognition'] == 4 env = Environment(config={}) assert env.config['cognition'] == 1
def test_social(self): """Test social.""" config = {} config['social'] = 4 env = Environment(config=config) assert env.config['social'] == 4 env = Environment(config={}) assert env.config['social'] == 1
def test_inertial_weight(self): """Test inertial_weight.""" config = {} config['inertial_weight'] = 4 env = Environment(config=config) assert env.config['inertial_weight'] == 4 env = Environment(config={}) assert env.config['inertial_weight'] == 1
def test_env_best_result(self): """Test the computation of global best result.""" env = Environment(config={}) position = matrix([0, 0, 0]) particles = {} particles[1] = Particle( environment=env, current=Information(position=position, fitness=3)) particles[2] = Particle( environment=env, current=Information(position=position, fitness=10)) particles[3] = Particle( environment=env, current=Information(position=position, fitness=40)) particles[4] = Particle( environment=env, current=Information(position=position, fitness=25)) particles[5] = Particle( environment=env, current=Information(position=position, fitness=1)) particles[6] = Particle( environment=env, current=Information(position=position, fitness=37)) best = env.best assert particles[5] == best assert particles[5].best == best.best
def test_generate(self): """Test generation.""" class Random(object): """Randomizer.""" def random(self): """Return random [0,1) matrix 3x1.""" return matrix([1, 1, 1]) env = Environment({ 'random': Random(), 'base': matrix([ [0, 0, 0], # beacon 1 [60, -40, 0], # beacon 2 [50, 50, 0], # beacon 3 ]), }) num_beacons = len(env.config['base']) position_gen = around_beacons(environment=env) for i in range(3 * num_beacons): position = next(position_gen) new_val = env.config['base'][i % num_beacons] + Random().random() assert (new_val == position).all()
def test_randomizer(self): """Test randomizer.""" class Random(object): def random(self): return "random" config = {} config['random'] = Random() env = Environment(config=config) assert env.config['random'].random() == "random" config['random'] = random env = Environment(config=config) assert 0 <= env.config['random'].random() < 1
def test_particle_register_environment(self): """Test if particle update environment when set new best value.""" env = Environment(config={}) position = matrix([1, 2, 3]) fitness = 5 myinfo = Information(position=position, fitness=fitness) p1 = Particle(environment=env, best=myinfo) assert env.best == p1
def test_index(self): """Test particle index.""" env = Environment(config={}) particles = [] for i in range(10): particles.append(Particle(environment=env, id=i)) for i in range(len(particles)): p = env.particles[i] assert p.index == i
def test_register_new_local_best_result(self): """Register new local best result.""" class Environment(object): def setInfo(self, info): self.info = info def updateParticleBest(self, info): assert info == self.info def register(self, particle): assert self == particle.environment env = Environment() position = matrix([1, 2, 3]) fitness = 5 info = Information(position=position, fitness=fitness) env.setInfo(Information(position=position, fitness=fitness)) particle = Particle(environment=env, best=info) position = matrix([4, 5, 6]) fitness = 4 info = Information(position=position, fitness=fitness) env.setInfo(info) particle.current = info
def test_id(self): """Test if of a particle.""" position = [1, 2, 3] fitness = 5 myid = "myid" particle = Particle(environment=Environment(config={}), id=myid, best=Information(position=position, fitness=fitness)) assert myid == particle.id particle = Particle(environment=Environment(config={}), best=Information(position=position, fitness=fitness)) assert str(particle) == particle.id # test set id == 0 env = Environment(config={}) p = Particle(environment=env, id=0) assert p.id == 0 assert env.particles[0] == p
def test_best_neighbor_env(self): """Test the best particle from all environments neighbor.""" position = matrix([0, 0, 0]) env1 = Environment({}) env2 = Environment({}) env1.registerNeighbor(env2) particles = {} particles[1] = Particle( environment=env1, current=Information(position=position, fitness=3)) particles[2] = Particle( environment=env2, current=Information(position=position, fitness=10)) particles[3] = Particle( environment=env1, current=Information(position=position, fitness=40)) particles[4] = Particle( environment=env2, current=Information(position=position, fitness=25)) particles[5] = Particle( environment=env1, current=Information(position=position, fitness=1)) particles[6] = Particle( environment=env2, current=Information(position=position, fitness=37)) best = env1.neighborBest assert particles[5] == best assert particles[5].best == best.best best = env2.neighborBest assert particles[5] == best assert particles[5].best == best.best
def test_update_best(self): """Test update best result.""" env = Environment(config={}) old_best = Information(position=None, fitness=5) better_best = Information(position=None, fitness=3) worse_best = Information(position=None, fitness=7) particle = Particle(environment=env, current=old_best) assert particle.best == old_best particle.current = better_best assert particle.best == better_best particle.current = worse_best assert particle.best == better_best
def builder(base, point, params, random_generator, position_initializator): """Sistematically generate environments from parameters combination.""" index = 1 for param in parameters_matrix(params): # read parameters from the matrix error = param[0] inertial_weight = param[1] cognition = param[2] social = param[3] num_particles = param[4] velocity_max = param[5] iterations_per_particle = param[6] # point disturbed by a gaussian noise disturbed_point_gen = apply_noise_linear(point=point, error=error) # compute radius radius = matrix([distance(b, next(disturbed_point_gen)) for b in base]) # build environment configuration env_config = { 'inertial_weight': inertial_weight, 'cognition': cognition, 'social': social, 'random': random_generator, 'base': base, 'radius': radius } env = Environment(config=env_config) # particle position generator position_generator = position_initializator(env) # particles inside env for i in range(num_particles): # particles PSOParticle( environment=env, id="P{}env{}".format(i, index), current=Information(position=next(position_generator)), velocity=velocity_max * random_generator.random(), vmax=velocity_max ) # engine for env yield (param, ParticleEngine( config={ 'max_iterations': iterations_per_particle }, environment=env ))
def test_move_defined_values(self): """Test move.""" class Random(object): def random(self): return 1 config = { 'inertial_weight': 1, 'cognition': 1, 'social': 1, 'random': Random(), 'base': matrix([ [0, 0, 0], # beacon 1 [7, 5, 0], # beacon 2 [5, -4, 0], # beacon 3 ]), 'radius': matrix([ 3.024, # beacon 1 6.4, # beacon 2 6.63, # beacon 3 ]) } env = Environment(config) particle = PSOParticle(environment=env, current=Information(position=matrix([1, 1, 1]))) particle.fitness() assert 20.14 < particle.best.fitness < 20.15 particle.move() assert (particle.current.position == matrix([2, 2, 2])).all() particle.fitness() assert 10.85 < particle.best.fitness < 10.86 particle.move() assert (particle.current.position == matrix([3, 3, 3])).all() particle.fitness() assert 47.85 < particle.current.fitness < 47.86 assert 10.85 < particle.best.fitness < 10.86 particle.move() assert (particle.current.position == matrix([2, 2, 2])).all()
def test_move_one_values(self): """Test move.""" class Random(object): def random(self): return 1 info = Information(position=matrix([1, 1, 1]), fitness=5) binfo = Information(position=matrix([1, 1, 1]), fitness=5) velocity = matrix([1, 1, 1]) config = {} config['inertial_weight'] = 1 config['cognition'] = 1 config['social'] = 1 config['random'] = Random() env = Environment(config) particle = PSOParticle(environment=env, current=info, best=binfo, velocity=velocity) new_info = particle.move() assert (particle.velocity == matrix([1, 1, 1])).all() assert (new_info.position == matrix([2, 2, 2])).all()
def test_register_neighbord(self): """Test register neighboard.""" env1 = Environment({}) env2 = Environment({}) assert env1 not in env2.neighbors assert env2 not in env1.neighbors assert len(list(filter(lambda x: x == env1, env1.neighbors))) == 1 assert len(list(filter(lambda x: x == env2, env2.neighbors))) == 1 env1.registerNeighbor(env2) assert env1 in env2.neighbors assert env2 in env1.neighbors assert len(list(filter(lambda x: x == env1, env1.neighbors))) == 1 assert len(list(filter(lambda x: x == env2, env2.neighbors))) == 1 # try to register 2 times env1.registerNeighbor(env2) assert len(list(filter(lambda x: x == env2, env1.neighbors))) == 1 assert len(list(filter(lambda x: x == env1, env2.neighbors))) == 1
def test_set_high_velocity(self): """Test set high velocity.""" env = Environment(config={}) position = matrix([1, 2, 3]) fitness = 5 velocity = matrix([3, 100, 10]) info = Information(position=position, fitness=fitness) particle = Particle(current=info, environment=env, vmax=1000, velocity=velocity) assert (particle.velocity == matrix([3, 100, 10])).all() particle = Particle(current=info, environment=env, vmax=20, velocity=velocity) assert (particle.velocity == matrix([3, 20, 10])).all() particle = Particle(current=info, environment=env, vmax=4, velocity=velocity) assert (particle.velocity == matrix([3, 4, 4])).all() particle = Particle(current=info, environment=env, vmax=1, velocity=velocity) assert (particle.velocity == matrix([1, 1, 1])).all()
'base': matrix([ [0, 0, 0], # beacon 1 [60, -40, 0], # beacon 2 [50, 50, 0], # beacon 3 [25, 0, 25] # beacon 4 ]), 'radius': matrix([ 20, # beacon 1 60, # beacon 2 60, # beacon 3 30, # beacon 4 ]) } env1 = Environment(config=env_config) env2 = Environment(config=env_config) # connect environments env1.registerNeighbor(env2) position_generator = around_beacons(env1) # particles inside env 1 for i in range(int(sys.argv[1])): PSOParticle(environment=env1, id="P{}env1".format(i), current=Information(position=next(position_generator)), velocity=random_generator.random(), vmax=5) # particles inside env 2
def builder(base, point, params, random_generator, position_initializator): """Sistematically generate environments from parameters combination.""" index = 1 for param in parameters_matrix(params): # read parameters from the matrix error = param[0] inertial_weight = param[1] cognition = param[2] social = param[3] num_psoparticles = param[4] num_fbparticles = param[5] velocity_max = param[6] iterations_per_particle = param[7] # point disturbed by a gaussian noise disturbed_point_gen = apply_noise_linear(point=point, error=error) # compute radius radius = matrix([distance(b, next(disturbed_point_gen)) for b in base]) # build environments configuration env_config = { 'inertial_weight': inertial_weight, 'cognition': cognition, 'social': social, 'random': random_generator, 'base': base, 'radius': radius } # [ENV1 - PSOParticles] env1 = Environment(config=env_config) # particle position generator position_generator = position_initializator(env1) # particles inside env1 for i in range(num_psoparticles): # particles PSOParticle( environment=env1, id="P{}env1-{}".format(i, index), current=Information(position=next(position_generator)), velocity=random_generator.random(), vmax=velocity_max ) # [ENV2 - FollowBestParticles] env2 = Environment(config=env_config) # particle position generator position_generator = position_initializator(env2) # particles inside env2 for i in range(num_fbparticles): # particles FollowBestParticle( environment=env2, id="P{}env2-{}".format(i, index), current=Information(position=next(position_generator)), velocity=random_generator.random(), vmax=velocity_max ) # connect environments env1.registerNeighbor(env2) # engines for env1 and env2 yield (param, ParticleEngine( config={ 'max_iterations': iterations_per_particle }, environment=env1 ), ParticleEngine( config={ 'max_iterations': iterations_per_particle }, environment=env2 ))
'random': Random(), 'base': matrix([ [0, 0, 0], # beacon 1 [7, 5, 0], # beacon 2 [5, -4, 0], # beacon 3 ]), 'radius': matrix([ 3.024, # beacon 1 6.4, # beacon 2 6.63, # beacon 3 ]) } env = Environment(config=config) PSOParticle(environment=env, id="P1", current=Information(position=config['base'][0]), velocity=matrix([0.1, 0.1, 0.1]), vmax=3) PSOParticle(environment=env, id="P2", current=Information(position=config['base'][1]), vmax=10) PSOParticle(environment=env, id="P3", current=Information(position=config['base'][2]), vmax=10)
base = next(base_generator) point = next(point_generator) error = 10 disturbed_point_gen = apply_noise_linear(point=point, error=error) radius = matrix([distance(b, next(disturbed_point_gen)) for b in base]) env_config = { 'inertial_weight': 2, 'cognition': 2, 'social': 1, 'random': random_generator, 'base': base, 'radius': radius } env1 = Environment(config=env_config) position_generator = around_beacons(env1) vmax = 5 # particles inside env 1 for i in range(int(sys.argv[1])): PSOParticle(environment=env1, id="P{}env1".format(i), current=Information(position=next(position_generator)), velocity=vmax * random_generator.random() - matrix([vmax / 2, vmax / 2, vmax / 2]), vmax=vmax) # Load logging configuration
[60, -40, 0], # beacon 2 [50, 50, 0], # beacon 3 [25, 0, 25] # beacon 4 ]), 'radius': matrix([ 20, # beacon 1 60, # beacon 2 60, # beacon 3 30, # beacon 4 ]) } config_env2 = deepcopy(config_env1) config_env2['random'] = Random3D() env1 = Environment(config=config_env1) env2 = Environment(config=config_env2) # connect environments env1.registerNeighbor(env2) position_generator = around_beacons(env1) # particles inside env 1 for i in range(int(sys.argv[1])): GlobalBestPSOParticle( environment=env1, id="P{}env1".format(i), current=Information(position=next(position_generator)), velocity=random_generator.random(), vmax=5 )
def test_empty_environment(self): """Test empty environment.""" env = Environment(config={}) with pytest.raises(EmptyEnvironment): env.best