def test_reset(self): """Test if the reset function allows a new algorithm execution.""" bmda = Bmda() bmda.setup(40) bmda.cost_func = GbCostFunction(OneMax, var_size=10) bmda.search() bmda.reset() self.assertEqual(bmda.iters, 0) self.assertIsNotNone(bmda.distr) self.assertEqual(bmda.cost_func.evals, 0)
def test_max_chisquare_base(self): chi_matrix = np.array([[0.0, 4.0, 0.0], [4, 0.0, 5.0], [0.0, 5.0, 0.0]]) x, y, chi = Bmda.get_max_dependency([0, 1], [2], chi_matrix) self.assertEqual(x, 1) self.assertEqual(y, 2) self.assertEqual(chi, 5.0)
def test_generate_graph_with_dependencies(self): """Test that the algorithm generates a graph with two root nodes""" pop = np.array(([[0, 0, 1, 0], [1, 1, 0, 0], [1, 1, 0, 0], [0, 0, 1, 0], [1, 1, 0, 0]])) roots, _, _ = Bmda.build_graph(pop, np.zeros(4), DependencyMethod.chi2_test, 3.84) self.assertEqual(len(roots), 2) self.assertTrue(np.equal(roots, 3).any())
def test_generate_graph_all_independent(self): """Test that the algorithm generates no graph, all variables independent""" pop = np.array(([[0, 0, 0, 1], [1, 0, 1, 0], [0, 1, 0, 0], [1, 1, 1, 0], [0, 0, 1, 1]])) roots, _, _ = Bmda.build_graph(pop, np.zeros(4), DependencyMethod.chi2_test, 3.84) roots.sort() self.assertTrue(np.equal(roots, [0, 1, 2, 3]).all())
def setup_kiedra(data): train_data, test_data = sample_data(data, train_size) svm_params = { 'svm_type': orngSVM.SVMLearner.C_SVC, 'kernel_type': orngSVM.SVMLearner.RBF, 'C': 100, 'gamma': 10, 'normalization': False } factory = GbFactory(orngSVM.SVMLearner, svm_params) cost_func = GbWrapperCostFunction(train_data, test_data, factory, 0, 2, 2) bmda = Bmda() bmda.setup(pop, gens, dependency_method=DependencyMethod.sim, independence_threshold=0.1) bmda.cost_func = cost_func return bmda
def test_shape_calculate_chisquare_matrix(self): pop = np.array(([[0, 0, 0, 1], [1, 0, 1, 0], [0, 1, 0, 0], [1, 1, 1, 0], [0, 0, 1, 1], [1, 1, 0, 0]])) chi_matrix = Bmda.calc_dependency_matrix(pop, DependencyMethod.chi2_test, 3.84) self.assertEqual(chi_matrix.shape, (4, 4)) expected = np.array([[0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) #Test for a total independent chi matrix. chi_matrix = chi_matrix * (np.eye(4, 4) * -1 + 1) self.assertTrue((chi_matrix == 0.0).all())
def test_basic_search_zero(self): """Test class for the Bmda algorithm""" bmda = Bmda() bmda.setup(40) bmda.cost_func = GbCostFunction(ZeroMax, var_size=10) result = bmda.search() self.assertTrue((result.params + 1).all()) self.assertGreaterEqual(result.cost, 8.0)
def test_basic_search_onemax(self): """Test class for the Bmda algorithm""" bmda = Bmda() bmda.setup(10, 40) bmda.cost_func = GbCostFunction(OneMax) result = bmda.search() self.assertGreaterEqual(result.params.sum(), 8.0) self.assertGreaterEqual(result.cost, 8.0)
def test_basic_search_onemax_mi_method(self): """Test class for the Bmda algorithm""" bmda = Bmda() bmda.setup(40, dependency_method=DependencyMethod.mi) bmda.cost_func = GbCostFunction(OneMax, var_size=10) result = bmda.search() self.assertGreaterEqual(result.params.sum(), 8.0) self.assertGreaterEqual(result.cost, 8.0)
def test_basic_search_onemax(self): """Test class for the Bmda algorithm""" var_size = 10 bmda = Bmda() bmda.setup(40) bmda.cost_func = GbCostFunction(OneMax, var_size=var_size) result = bmda.search() self.assertTrue((result.params + 1).all()) self.assertGreaterEqual(result.cost, var_size * 0.8)