def test_movement_2(self, mock): s = Swarm(swarm_size=10, bounds=Bounds(np.array([1]), np.array([2])), parameters=PsoParameters(0.15, 0.15, 0.15), minimum_improvement=10e-8, minimum_step=10e-8) mock.return_value = 0 self.assertFalse(s.still_moving())
def test_best_score(self, mock_score): s = Swarm(swarm_size=10, bounds=Bounds(np.array([1]), np.array([2])), parameters=PsoParameters(0.15, 0.15, 0.15), minimum_improvement=10e-8, minimum_step=10e-8) mock_score.side_effect = list(reversed(range(10))) self.assertEqual(s.best_score(), 9)
def test_that_swarm_can_update_the_scores_of_all_particles_if_scores_equal_swarmsize( self): s = Swarm(swarm_size=10, bounds=Bounds(np.array([1]), np.array([2])), parameters=PsoParameters(0.15, 0.15, 0.15), minimum_improvement=10e-8, minimum_step=10e-8) with self.assertRaises(ValueError): s.update_scores(list(range(9)))
def test_that_swarm_can_update_the_scores_of_all_particles(self, mock): s = Swarm(swarm_size=10, bounds=Bounds(np.array([1]), np.array([2])), parameters=PsoParameters(0.15, 0.15, 0.15), minimum_improvement=10e-8, minimum_step=10e-8) s.update_scores(list(range(10))) self.assertTrue(mock.called) self.assertEqual(mock.call_count, 10)
def test_that_update_velocity_is_appleid_to_all_particles( self, mock_update, mock_position): s = Swarm(swarm_size=10, bounds=Bounds(np.array([1]), np.array([2])), parameters=PsoParameters(0.15, 0.15, 0.15), minimum_improvement=10e-8, minimum_step=10e-8) mock_position.return_value = 666 s.update_velocity() self.assertTrue(mock_update.called) self.assertEqual(mock_update.call_count, 10)
def test_that_swarm_has_to_be_created_with_positive_number_of_particles_2( self): with self.assertRaises(ValueError): Swarm(swarm_size=0, bounds=Bounds(np.array([1]), np.array([2])), parameters=PsoParameters(0.15, 0.15, 0.15), minimum_improvement=10e-8, minimum_step=10e-8)
def test_that_swarm_is_iterable(self): s = Swarm(swarm_size=10, bounds=Bounds(np.array([1]), np.array([2])), parameters=PsoParameters(0.15, 0.15, 0.15), minimum_improvement=10e-8, minimum_step=10e-8) particles = [p for p in s] self.assertEqual(len(particles), 10)
def test_that_maximum_number_of_threads_is_limited_by_swarm_size( self, mock_update, mock_size): s = Swarm(swarm_size=1, bounds=Bounds(np.array([1]), np.array([2])), parameters=PsoParameters(0.15, 0.15, 0.15), minimum_improvement=10e-8, minimum_step=10e-8) ex = Executor(DummyObjectiveFunction(), threads=10) mock_size.return_value = 1 ex.calculate_scores(s) self.assertTrue(mock_size.called) self.assertEqual(mock_update.call_count, 1)