Beispiel #1
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
Beispiel #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
Beispiel #3
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
Beispiel #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