Пример #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
Пример #2
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
Пример #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
Пример #4
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
Пример #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
Пример #6
0
    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
Пример #7
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
Пример #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
Пример #9
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()
Пример #10
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