def setUp(self): """Sets up test fixtures""" self.optimizer = LocalBestPSO self.n_particles = 40 self.dimensions = 20 self.options = { 'c1': [1, 5], 'c2': [6, 10], 'k': [11, 15], 'w': [0.4, 0.9], 'p': 1 } self.bounds = (np.array([-5, -5]), np.array([5, 5])) self.iters = 10 self.n_selection_iters = 100 self.objective_func = sphere_func self.g = RandomSearch(self.optimizer, self.n_particles, self.dimensions, self.options, self.objective_func, self.iters, self.n_selection_iters, self.bounds, velocity_clamp=None) self.g_unbound = RandomSearch(self.optimizer, self.n_particles, self.dimensions, self.options, self.objective_func, self.iters, self.n_selection_iters, bounds=None, velocity_clamp=None)
def test_optimizer_type_fail(self): """Test that :code:`optimizer` of type :code:`string` raises :code:`TypeError`""" bad_optimizer = 'LocalBestPSO' # a string instead of a class object with self.assertRaises(TypeError): g = RandomSearch(bad_optimizer, self.n_particles, self.dimensions, self.options, self.objective_func, self.iters, self.n_selection_iters, bounds=None, velocity_clamp=None)
def test_n_selection_iters_type_fail(self): """Test that :code:`n_selection_iters` of type :code:`float` raises :code:`TypeError`""" bad_n_selection_iters = 100.0 # should be an int with self.assertRaises(TypeError): g = RandomSearch(self.optimizer, self.n_particles, self.dimensions, self.options, self.objective_func, self.iters, bad_n_selection_iters, self.bounds, velocity_clamp=None)
def main(image, data): ''' Parametes: image: path to image file data: path to file that contain the message p: QKrawtchoukMatrix p parameter q: QKrawtchoukMatrix q parameter ''' with open(data, 'r') as file: message = file.read() file.close() cover_work = imageio.imread(image) options = { 'c1': [0.1, 3], 'c2': [0.1, 3], 'w': [0.6, 2], 'k': [0, 10], 'p': 1 } g = RandomSearch( GlobalBestPSO, n_particles=20, dimensions=2, options=options, objective_func=lambda x: qKrawtchouk_hide(x, cover_work, message), iters=10, n_selection_iters=10, bounds=([0, 0], [1, 1])) best_score, best_options = g.search() dct_score = dct_insert(cover_work, message) logging.info('Score: {}'.format(best_score)) logging.info('DCT score {}'.format(dct_score)) logging.info(best_options)
def random_unbounded(): """Returns a RandomSearch instance without bounds""" options = { 'c1': [1, 5], 'c2': [6, 10], 'k': [11, 15], 'w': [0.4, 0.9], 'p': 1 } return RandomSearch(LocalBestPSO, n_particles=40, dimensions=20, options=options, objective_func=sphere_func, iters=10, n_selection_iters=100, bounds=None)
def random_bounded(): """Returns a RandomSearch instance with bounds""" bounds = (np.array([-5, -5]), np.array([5, 5])) options = { 'c1': [1, 5], 'c2': [6, 10], 'k': [11, 15], 'w': [0.4, 0.9], 'p': 1 } return RandomSearch(LocalBestPSO, n_particles=40, dimensions=20, options=options, objective_func=sphere_func, iters=10, n_selection_iters=100, bounds=bounds)
def random_unbounded(): """Returns a RandomSearch instance without bounds""" options = { "c1": [1, 5], "c2": [6, 10], "k": [11, 15], "w": [0.4, 0.9], "p": 1, } return RandomSearch( LocalBestPSO, n_particles=40, dimensions=20, options=options, objective_func=sphere_func, iters=10, n_selection_iters=100, bounds=None, )
def random_bounded(): """Returns a RandomSearch instance with bounds""" bounds = (np.array([-5, -5]), np.array([5, 5])) options = { "c1": [1, 5], "c2": [6, 10], "k": [11, 15], "w": [0.4, 0.9], "p": 1, } return RandomSearch( LocalBestPSO, n_particles=40, dimensions=20, options=options, objective_func=sphere, iters=10, n_selection_iters=100, bounds=bounds, )