def test_swap_strongest_two_better_colonies_swap_first(self): # given test_function = CostFunction(lambda x: tf.reduce_sum(tf.square(x), 1), 10.0, -10.0, 2) ica = ICA(test_function, 5, 2, 1, log=True, seed=42) empires = tf.constant([[3., 3., ], [3., 3., ], [2., 4., ] ], tf.float64) colonies = tf.constant([[1., 1., ], [1., 1., ], [1., 7., ] ], tf.float64) empires_numbers = tf.constant([1, 1, 0]) # when new_colonies, new_empires, _, _ = ica.swap_strongest(empires=empires, colonies=colonies, empires_numbers=empires_numbers ) # then assert_equal(new_empires.numpy(), [[1., 1., ], [1., 1., ], [2., 4., ]]) assert_equal(new_colonies.numpy(), [[3., 3., ], [1., 1., ], [1., 7., ]])
def test_competition_only_one_empire_no_changes(self): # given test_function = CostFunction(lambda x: tf.reduce_sum(tf.square(x), 1), 10.0, -10.0, 2) ica = ICA(test_function, 6, 2, 1, log=True, seed=42) empires = tf.constant([[3., 3., ], [3., 3., ], [3., 3., ], [3., 3., ] ], tf.float64) colonies = tf.constant([[4., 4., ], [5., 5., ], [1., 7., ], [9., 9., ] ], tf.float64) empires_numbers = tf.constant([1, 1, 1, 1]) empires_power = helpers.evaluate_countries_power(empires, test_function.function) colonies_power = helpers.evaluate_countries_power(colonies, test_function.function) # when new_empires, new_empire_numbers, _, _, _ = ica.competition(empires=empires, colonies=colonies, empires_numbers=empires_numbers, empires_power=empires_power, colonies_power=colonies_power ) # then assert_equal(new_empire_numbers.numpy(), empires_numbers.numpy()) assert_equal(new_empires.numpy(), empires.numpy())
def test_initialize_countries(self): # given test_function = CostFunction(lambda x: tf.reduce_sum(tf.square(x), 1), 10.0, -10.0, 2) ica = ICA(test_function, 10, 3, 1, log=True, seed=42) # when countries = ica.initialize_countries() # then self.assertTrue(tf.reduce_all(tf.greater(countries, -10.0).numpy())) self.assertTrue(tf.reduce_all(tf.less(countries, 10.0).numpy()))
def test_create_empires(self): # given test_function = CostFunction(lambda x: tf.reduce_sum(tf.square(x), 1), 10.0, -10.0, 2) ica = ICA(test_function, 10, 3, 1, log=True, seed=42) countries = ica.initialize_countries() # when empires, colonies, empires_numbers = ica.create_empires(countries) # then unique, _ = tf.unique(empires_numbers) self.assertEqual(3, tf.size(unique)) self.assertEqual([7, 2], colonies.shape) self.assertEqual([7, 2], empires.shape)
def test_assimilation(self): # given test_function = CostFunction(lambda x: tf.reduce_sum(tf.square(x), 1), 10.0, -10.0, 2) ica = ICA(test_function, 5, 2, 1, log=True, seed=42) empires = tf.constant([[3., 3., ], [3., 3., ], [2., 4., ] ], tf.float64) colonies = tf.constant([[1., 9., ], [5., 3., ], [1., 7., ] ], tf.float64) empires_numbers = tf.constant([1, 1, 0]) # when new_colonies = ica.assimilation(empires, colonies) # then distance_before = tf.map_fn(tf.norm, tf.subtract(empires, colonies)) distance_after = tf.map_fn(tf.norm, tf.subtract(empires, new_colonies)) self.assertTrue(tf.reduce_all(tf.less(distance_after, distance_before)).numpy())
def gridsearch(function, params, results_path): param_grid = ParameterGrid(params) iterations_results = [] for index, params in enumerate(param_grid): print(params) ica = ICA(cost_function=function, num_of_countries=params["num_of_countries"], num_of_imperialist=params["num_of_imperialist"], max_iterations=params["max_iterations"], direct_assimilation=params["direct_assimilation"], avg_colonies_power=params["avg_colonies_power"], revolution_rate=params["revolution_rate"], seed=params["seed"] ) ica.eval() metadata = ica.get_evaluation_data() iterations_results.append((index, metadata)) print(index, metadata, sep=" ") save_metadata_per_iteration(index, metadata, results_path) save_result(index, ica.result.numpy(), results_path) create_and_save_plots_to_file(iterations_results, results_path, str(index)) return iterations_results