Esempio n. 1
0
    def test_should_enter_given_coordinates(self):
        # given
        swarm = Swarm(omega=0.5, inertion=0.5, local=0.1)
        swarm._lower_constraints = array([0] * 3)
        swarm._upper_constraints = array([1000] * 3)

        initial_parameters = [[100, 200, 300], [50, 100, 150]]
        parameters_after_iteration = [[100, 200, 300], [25, 50, 75]]

        particles = [
            Particle(initial_parameters[0]),
            Particle(initial_parameters[1])
        ]

        # when

        swarm.optimize(particle_swarm=particles, best=particles[0])

        # then
        self.assertTrue(
            (particles[0].parameters_vector == parameters_after_iteration[0]
             ).all())
        self.assertTrue(
            (particles[1].parameters_vector == parameters_after_iteration[1]
             ).all())
Esempio n. 2
0
    def __init__(self,
                 position,
                 velocity,
                 canvas_width,
                 canvas_height,
                 color=None,
                 mass=None,
                 elasticity=1.0,
                 radius=None,
                 friction=0.0):
        if not color:
            color = Particle.random_color()

        Particle.__init__(self,
                          position,
                          color,
                          canvas_width,
                          canvas_height,
                          radius=radius)
        self.canvas_center = np.array(
            [self.canvas_width / 2.0 - 0.5, self.canvas_height / 2.0 - 0.5],
            dtype=np.float)

        self.velocity = np.array(velocity, dtype=np.float)
        self.friction = friction

        if not mass:
            mass = self.radius * self.radius * np.pi
        self.mass = mass

        self.elasticity = elasticity
Esempio n. 3
0
    def test_should_move_particle_to_upper_boudaries(self):
        # given
        initial = array([1, 2.0, 3.0])
        step = array([11.2, 13, 7])
        final = array([0] * len(step))

        upper_constraints = array([9, 9, 9])
        lower_constraints = array([1, 1, 1])

        # when
        particle = Particle(initial);
        particle.move(step, lower_constraints, upper_constraints);
                   
        # then        
        
        self.assertEqual(list(particle.parameters_vector), [9, 9, 9],"Vector was moved by inpropropriate value")
Esempio n. 4
0
    def test_should_update_values(self):
        # given
        img = cv.imread("..\\PSOTests\\TestImages\\threshold_test.jpg", 0)
        lower_threshold = 130
        upper_threhshold = 255
        thresholded = cv.threshold(img, lower_threshold, upper_threhshold,
                                   cv.THRESH_BINARY)
        thresholded = thresholded[1]  # strange value on index #1
        fitness_fun = BinaryImagesDiceIndex(thresholded)

        # when
        particles = [
            Particle([120 + (15 * x), 150 + (x * 15)]) for x in range(5)
        ]

        threaders = [
            Threader.SegmentationThreader(Threshold(img), p) for p in particles
        ]

        for t in threaders:
            t.start()

        for t in threaders:
            t.join()

        for t in threaders:
            t._particle.fitness = fitness_fun.get_result(t.segmentation_result)
        # then

        particle_changed = [p.fitness != 0 for p in particles]

        self.assertTrue(all(particle_changed))
Esempio n. 5
0
    def test_should_move_particle_to_upper_from_lower(self):
        # given
        initial = array([1, 2.0, 3.0])
        step1 = array([-2.3, -5.5, -4.5])
        step2 = array([15, 11.2, 9])
        final = array([0] * len(step1))

        upper_constraints = array([9, 9, 9])
        lower_constraints = array([-1, 0, 1])

        # when
        particle = Particle(initial);
        particle.move(step1, lower_constraints, upper_constraints);
        particle.move(step2, lower_constraints, upper_constraints);
        # then

        self.assertEqual(list(particle.parameters_vector), [9, 9, 9],"Vector was moved by inpropropriate value")
Esempio n. 6
0
    def test_should_have_given_speed(self):
        # given
        swarm = Swarm(omega=0.5, inertion=0.5, local=0.1)
        swarm._lower_constraints = array([0] * 3)
        swarm._upper_constraints = array([1000] * 3)
        initial_parameters = [[100, 200, 300], [50, 100, 150]]
        speed_after_iteration = [-25, -50, -75]

        particles = [
            Particle(initial_parameters[0]),
            Particle(initial_parameters[1])
        ]

        # when

        swarm.optimize(particle_swarm=particles, best=particles[0])

        # then
        self.assertEqual(list(particles[1]._speed), speed_after_iteration)
Esempio n. 7
0
    def test_should_move_particle(self):
        # given
        initial = array([1, 2.0, 3.0])
        step = array([0.2, 1, 0.4])
        final = array([0] * len(step))

        upper_constraints = array([9, 9, 9])
        lower_constraints = array([1, 1, 1])

        # when
        particle = Particle(initial);
        particle.move(step, lower_constraints, upper_constraints);

        final = initial + step      

        # then
        
        f = final;
        p = particle.parameters_vector
        self.assertEqual(list(f), list(p),"Vector was moved by inpropropriate value")
Esempio n. 8
0
    def build(self):
        pso = PSO()

        self.swarm._lower_constraints = array(self.lower_constraints)
        self.swarm._upper_constraints = array(self.upper_constraints)

        pso._swarm = self.swarm
        pso._minimal_change = self.minimal_change
        pso._segmentation_function = self.segmentation_function
        pso._fitness_function = self.fitness_function
        pso._no_change_iteration_constraint = self.no_change_iteration_constraint
        pso._max_iteration_number = self.max_iteration_number
        pso._min_iteration_number = self.min_iteration_number

        factory = ParticleFactory(self.lower_constraints,
                                  self.upper_constraints)

        #for _ in range(particles_count):
        #    random_vector_within_constraints = factory.create_parameters_vector()
        #    random_particle = Particle(random_vector_within_constraints)

        #    pso.particle_swarm.append(random_particle)

        particle_swarm = [
            Particle(factory.create_parameters_vector())
            for x in range(self.particles_count)
        ]

        particles_count = len(particle_swarm)

        if (self.constraint_callback != []):
            new_particle_swarm = self.constraint_callback(particle_swarm)
        else:
            new_particle_swarm = particle_swarm

        pso.particle_swarm = new_particle_swarm
        sys.stdout.write("\n")
        return pso