def test_that_lastimprovement_is_inf_if_not_enough_scores2(self): p = Particle(Bounds(np.array([1]), np.array([2])), PsoParameters(0.15, 0.15, 0.15)) p._score = [0] self.assertEqual(p.last_improvement(), float('inf'))
def test_that_lastimprovement_is_calculated(self): p = Particle(Bounds(np.array([1]), np.array([2])), PsoParameters(0.15, 0.15, 0.15)) p._score = [0, 5] self.assertEqual(p.last_improvement(), 5)
def test_that_lastimprovement_returns_delta_2(self): p = Particle(Bounds(np.array([1]), np.array([2])), PsoParameters(0.15, 0.15, 0.15)) p._score = [75, 1000, 0] self.assertEqual(p.last_improvement(), -1000)
def test_that_lastimprovement_returns_infinity_if_only_one_score(self): p = Particle(Bounds(np.array([1]), np.array([2])), PsoParameters(0.15, 0.15, 0.15)) p._score = [0] self.assertEqual(p.last_improvement(), float("inf"))
def test_that_last_movement_returns_distance_2d(self): p = Particle(Bounds(np.array([1]), np.array([2])), PsoParameters(0.15, 0.15, 0.15)) p._position = [np.array([0, 0]), np.array([6, 0])] self.assertEqual(p.last_movement(), 6)
def test_that_particle_is_initialized_with_velocity(self, mock): mock.return_value = np.array([666]) p = Particle(Bounds(np.array([1]), np.array([2])), PsoParameters(0.5, 0.5, 0.5)) self.assertTrue(mock.called) self.assertEqual(p.velocity(), np.array([666])) self.assertEqual(len(p._velocity), 1)
def test_that_velocity_returns_the_current_velocity_2(self): p = Particle(Bounds(np.array([1]), np.array([2])), PsoParameters(0.15, 0.15, 0.15)) p._velocity = list(range(10)) self.assertEqual(p.velocity(), p._velocity[-1]) self.assertEqual(p.velocity(), 9) self.assertEqual(len(p._velocity), 10)
def test_that_position_returns_the_current_position_2(self): p = Particle(Bounds(np.array([1]), np.array([2])), PsoParameters(0.15, 0.15, 0.15)) p._position = list(range(10)) self.assertEqual(p.position(), p._position[-1]) self.assertEqual(p.position(), 9) self.assertEqual(len(p._position), 10)
def test_that_update_score_increases_the_amount_of_stored_scores(self): p = Particle(Bounds(np.array([1]), np.array([2])), PsoParameters(0.15, 0.15, 0.15)) self.assertEqual(len(p._score), 0) p.update_score(666) self.assertEqual(len(p._score), 1) self.assertEqual(p.best_score(), 666)
def test_that_best_score_returns_the_highest_score(self): p = Particle(Bounds(np.array([1]), np.array([2])), PsoParameters(0.15, 0.15, 0.15)) p._score = list(range(10)) p._position = list(reversed(range(10))) self.assertEqual(len(p._score), 10) self.assertEqual(len(p._position), 10) self.assertEqual(p.best_score(), 9)
def test_that_parameters_are_correctly_assigned(self): p = Particle(Bounds(np.array([1]), np.array([2])), PsoParameters(0.15, 0.15, 0.15)) self.assertEqual(p._parameters.omega(), 0.15) self.assertEqual(p._parameters.phip(), 0.15) self.assertEqual(p._parameters.phig(), 0.15)
def test_that_best_position_can_be_calculated_if_same_number_of_scores( self): p = Particle(Bounds(np.array([1]), np.array([2])), PsoParameters(0.15, 0.15, 0.15)) p._score = list(range(9)) p._position = list(reversed(range(10))) with self.assertRaises(ValueError): p.best_position()
def test_that_best_score_can_be_calculated_if_scores_not_empty(self): p = Particle(Bounds(np.array([1]), np.array([2])), PsoParameters(0.15, 0.15, 0.15)) with self.assertRaises(ValueError): p.best_score()
def test_that_bounds_are_correctly_assigned(self): p = Particle(Bounds(np.array([1]), np.array([2])), PsoParameters(0.5, 0.5, 0.5)) self.assertEqual(p._bounds.lower(), np.array([1])) self.assertEqual(p._bounds.upper(), np.array([2]))
def test_that_particle_is_initialized_without_score(self): p = Particle(Bounds(np.array([1]), np.array([2])), PsoParameters(0.5, 0.5, 0.5)) self.assertEqual(len(p._score), 0)