Exemple #1
0
    def test_addition(self):
        landscape_point1 = LandscapePosition(self.objective_function, self.coordinates1)
        landscape_point2 = LandscapePosition(self.objective_function, self.coordinates2)

        expected = LandscapePosition(self.objective_function, self.coordinates2)
        
        assert expected == (landscape_point1 + landscape_point2)
    def test_distance2_J1(self):
        landscape_point1 = LandscapePosition(self.objective_function,
                                             self.coordinates2)
        landscape_point2 = LandscapePosition(self.objective_function,
                                             self.coordinates1)

        assert_almost_equals(2.0, landscape_point1.distance2(landscape_point2))
    def test_LandscapePosition_with_J1_matrix(self):
        expected_values = [[
            6.671280296717448e-05, 0.004156270134668844, -0.2449540405743497,
            -0.02975999770667323, -5.864187872589536e-06
        ],
                           [
                               -0.0004517936594085711, 0.3265409898164098,
                               -5.680276001896266, -0.4404918548935705,
                               0.003599520786098113
                           ],
                           [
                               -0.03650620461319554, -2.773610019456342,
                               0.9810118431238463, 3.269463326439044,
                               0.03312494992430833
                           ],
                           [
                               -0.003078234252739988, 0.4784411467828077,
                               7.996620241631349, 1.185275846660814,
                               0.0044245231361739
                           ],
                           [
                               3.223535961269275e-05, 0.0311759440751708,
                               0.2998710282262348, 0.03200763718576478,
                               4.102972745826762e-05
                           ]]

        for i in range(5):
            for j in range(5):
                coord = Coordinates([-3.0 + i * 1.5, -3.0 + j * 1.5])
                landscape_point = LandscapePosition(self.objective_function,
                                                    coord)
                assert_almost_equals(
                    expected_values[j][i],
                    landscape_point.evaluate_objective_function())
 def setUp(self):
     self.gso_parameters = GSOParameters()
     self.objective_function = J1()
     self.landscape_position1 = LandscapePosition(self.objective_function,
                                                  Coordinates([0.0, 0.0]))
     self.landscape_position2 = LandscapePosition(self.objective_function,
                                                  Coordinates([1.0, 1.0]))
     self.positions = [[self.landscape_position1, self.landscape_position2]]
    def test_multiply_by_scalar(self):
        landscape_point1 = LandscapePosition(self.objective_function,
                                             self.coordinates2)

        expected = LandscapePosition(self.objective_function,
                                     self.coordinates4)

        assert expected == (landscape_point1 * 2.0)
Exemple #6
0
 def test_self_addition_and_assigment(self):
     landscape_point1 = LandscapePosition(self.objective_function, self.coordinates2)
     
     expected = LandscapePosition(self.objective_function, self.coordinates4)
     
     landscape_point1 += landscape_point1
     
     assert expected == landscape_point1
Exemple #7
0
 def test_subtraction_and_assigment(self):
     landscape_point1 = LandscapePosition(self.objective_function, self.coordinates1)
     landscape_point2 = LandscapePosition(self.objective_function, self.coordinates2)
     
     expected = LandscapePosition(self.objective_function, self.coordinates5)
     
     landscape_point1 -= landscape_point2
     
     assert expected == landscape_point1
Exemple #8
0
 def test_equals(self):
     landscape_point1 = LandscapePosition(self.objective_function, self.coordinates1)
     landscape_point2 = LandscapePosition(self.objective_function, self.coordinates2)
     landscape_point3 = LandscapePosition(self.objective_function, self.coordinates2)
     
     assert not landscape_point1 == landscape_point2
     assert landscape_point2 == landscape_point3
     
     assert landscape_point1 != landscape_point2
     assert not landscape_point2 != landscape_point3
 def setUp(self):
     self.gso_parameters = GSOParameters()
     self.objective_function = J1()
     self.landscape_position1 = [
         LandscapePosition(self.objective_function, Coordinates([0.0, 0.0]))
     ]
     self.landscape_position2 = [
         LandscapePosition(self.objective_function, Coordinates([1.0, 1.0]))
     ]
     self.landscape_position3 = [
         LandscapePosition(self.objective_function, Coordinates([2.0, 2.0]))
     ]
     self.landscape_position4 = [
         LandscapePosition(self.objective_function, Coordinates([0.5, 0.5]))
     ]
     self.landscape_position5 = [
         LandscapePosition(self.objective_function, Coordinates([5.0, 5.0]))
     ]
     self.landscape_position6 = [
         LandscapePosition(self.objective_function, Coordinates([0.0, 1.0]))
     ]
     self.landscape_position7 = [
         LandscapePosition(self.objective_function, Coordinates([0.1, 0.0]))
     ]
     self.landscape_position8 = [
         LandscapePosition(self.objective_function, Coordinates([0.0, 0.1]))
     ]
     self.landscape_position9 = [
         LandscapePosition(self.objective_function, Coordinates([0.2, 0.0]))
     ]
Exemple #10
0
 def test_clone(self):
     landscape_point1 = LandscapePosition(self.objective_function, self.coordinates1)
     
     landscape_point2 = landscape_point1.clone()
     
     expected = LandscapePosition(self.objective_function, self.coordinates1)
     
     assert expected == landscape_point2
     
     landscape_point2.coordinates[0] = 1.0
     
     assert landscape_point1 != landscape_point2
 def test_LandscapePosition_with_J1_matrix(self):
     expected_values = [
         [6.671280296717448e-05, 0.004156270134668844, -0.2449540405743497, -0.02975999770667323, -5.864187872589536e-06],
         [-0.0004517936594085711, 0.3265409898164098, -5.680276001896266, -0.4404918548935705, 0.003599520786098113],
         [-0.03650620461319554, -2.773610019456342, 0.9810118431238463, 3.269463326439044, 0.03312494992430833],
         [-0.003078234252739988, 0.4784411467828077, 7.996620241631349, 1.185275846660814, 0.0044245231361739],
         [3.223535961269275e-05, 0.0311759440751708, 0.2998710282262348, 0.03200763718576478, 4.102972745826762e-05]
         ]
     
     for i in range(5):
         for j in range(5):
             coord = Coordinates([-3.0+i*1.5,-3.0+j*1.5])
             landscape_point = LandscapePosition(self.objective_function, coord)
             assert_almost_equals(expected_values[j][i], landscape_point.evaluate_objective_function())
Exemple #12
0
    def generate_landscape_positions(self):
        """Generates a list of landscape positions that have been read
        from initial_population_file.
        """
        reader = CoordinatesFileReader(self.dimensions)
        coordinates = reader.get_coordinates_from_file(
            self.initial_population_file)

        if not coordinates:
            raise GSOCoordinatesError(
                "No coordinates have been read from %s file" %
                self.initial_population_file)

        if len(coordinates) != self.number_of_glowworms:
            raise GSOCoordinatesError(
                "Number of coordinates read and number of glowworms does not correspond"
            )

        positions = []
        for index in range(self.number_of_glowworms):
            positions.append(
                LandscapePosition(self.objective_functions[0],
                                  coordinates[index]))

        return [positions]
    def test_move_same_coordinates(self):
        landscape_position1 = [
            LandscapePosition(self.objective_function, Coordinates([1.0, 2.0]))
        ]
        landscape_position2 = [
            LandscapePosition(self.objective_function, Coordinates([1.0, 2.0]))
        ]

        glowworm1 = Glowworm(landscape_position1, self.gso_parameters)
        glowworm2 = Glowworm(landscape_position2, self.gso_parameters)
        glowworm1.luciferin = 4.0
        glowworm1.vision_range = 2.0
        glowworm1.max_vision_range = 1.0
        glowworm2.luciferin = 4.0
        glowworm2.vision_range = 2.0
        glowworm2.max_vision_range = 1.0

        glowworm1.move(glowworm2)

        expected = LandscapePosition(self.objective_function,
                                     Coordinates([1.0, 2.0]))

        assert expected == glowworm1.landscape_positions[0]
Exemple #14
0
 def test_move_using_other_step(self):
     landscape_point1 = LandscapePosition(self.objective_function, self.coordinates1, 1.0)
     landscape_point2 = LandscapePosition(self.objective_function, self.coordinates2)
     
     expected = LandscapePosition(self.objective_function, Coordinates([0.70710678118654746,0.70710678118654746]))
     
     landscape_point1.move(landscape_point2)
     
     assert expected == landscape_point1
Exemple #15
0
 def test_move_using_default_step(self):
     landscape_point1 = LandscapePosition(self.objective_function, self.coordinates1)
     landscape_point2 = LandscapePosition(self.objective_function, self.coordinates2)
     
     expected = LandscapePosition(self.objective_function, Coordinates([0.021213203435596423,0.021213203435596423]))
     
     landscape_point1.move(landscape_point2)
     
     assert expected == landscape_point1
Exemple #16
0
 def generate_landscape_positions(self):
     """Generates a list of landscape positions that have been read
     from initial_population_file.
     """
     positions = []
     for index in range(self.number_of_glowworms):
         coordinates = []
         for dimension in range(self.bounding_box.dimension):
             bound = self.bounding_box.get_boundary_of_dimension(dimension)
             coord = self.random_number_generator(bound.lower_limit,
                                                  bound.upper_limit)
             coordinates.append(coord)
         positions.append(
             LandscapePosition(self.objective_functions[0],
                               Coordinates(coordinates)))
     return [positions]
Exemple #17
0
 def test_LandscapePosition_with_J1(self):
     landscape_point = LandscapePosition(self.objective_function, self.coordinates1)
     
     assert_almost_equals(0.9810118431238463, landscape_point.evaluate_objective_function())
Exemple #18
0
 def test_norm(self):
     landscape_point1 = LandscapePosition(self.objective_function, self.coordinates2)
     
     assert_almost_equals(sqrt(2.0), landscape_point1.norm())
 def test_distance2_J1(self):
     landscape_point1 = LandscapePosition(self.objective_function, self.coordinates2)
     landscape_point2 = LandscapePosition(self.objective_function, self.coordinates1)
     
     assert_almost_equals(2.0, landscape_point1.distance2(landscape_point2))