Beispiel #1
0
    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_equal(self):
        """Test information equal operator."""
        position1 = matrix([4, 5, 6])
        position2 = matrix([4, 5, 6])
        fitness1 = 4
        fitness2 = 4
        info1 = Information(position=position1, fitness=fitness1)
        info2 = Information(position=position2, fitness=fitness2)

        assert info1 == info2
Beispiel #3
0
    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 test_internal(self):
        """Test data inside information object."""
        position = matrix([4, 5, 6])
        fitness = 4
        info = Information(position=position, fitness=fitness)

        assert (position == info.position).all()
        assert fitness == info.fitness
Beispiel #5
0
    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
Beispiel #6
0
    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_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()
Beispiel #8
0
    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
Beispiel #9
0
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
        ))
Beispiel #10
0
    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()
Beispiel #11
0
    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()
Beispiel #12
0
    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
Beispiel #13
0
 def __init__(self, id):
     self.id = id
     self.current = Information()
     self.velocity = 0
     self.best = self.current
Beispiel #14
0
        [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)

logging.basicConfig(filename='logs/find_point.log', level=logging.DEBUG)

engine = ParticleEngine(config={'max_iterations': 100}, environment=env)
engine.start()
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
    )

# particles inside env 2
for i in range(int(sys.argv[2])):
    FollowBestParticle(
        environment=env2,
        id="P{}env2".format(i),
        current=Information(position=next(position_generator)),
        velocity=random_generator.random(),
        vmax=5
    )

# Load logging configuration
                                                beacon_index=1,
                                                num_of_points=total)

position_generator_3 = on_beacon_sphere_surface(environment=env,
                                                beacon_index=2,
                                                num_of_points=total)

position_generator_4 = on_beacon_sphere_surface(environment=env,
                                                beacon_index=3,
                                                num_of_points=total)

for i in range(total):
    # start around beacon 1
    PSOParticle(environment=env,
                id="P{}env1".format(i),
                current=Information(position=next(position_generator_1)),
                velocity=random_generator.random(),
                vmax=1)
    # start around beacon 2
    PSOParticle(environment=env,
                id="P{}env1".format(i + total),
                current=Information(position=next(position_generator_2)),
                velocity=random_generator.random(),
                vmax=1)
    # start around beacon 3
    PSOParticle(environment=env,
                id="P{}env1".format(i + total * 2),
                current=Information(position=next(position_generator_3)),
                velocity=random_generator.random(),
                vmax=1)
    # start around beacon 4