Example #1
0
    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
Example #2
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
    ]),
    '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
    )

# particles inside env 2
for i in range(int(sys.argv[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
               ))