def test_that_relaxed_solution_with_some_variables_fixed_is_correctly_calculated(
         self):
     bnb = BranchAndBound(self.input_data_4)
     fixed_vars = [1, 0, np.nan, np.nan]
     solution = bnb.relaxed_solution(fixed_variables=np.array(fixed_vars))
     expected_solution = [1, 0, 7 / 8, 0]
     self.assertEqual(expected_solution, list(solution))
Exemplo n.º 2
0
    def start_indiana_jones(self):
        self.start_input()
        items = self.__get_tuple()
        limit = self.__get_max_weight()

        bagged = None

        if self.algorithm == "dp":
            bagged = dynamic_programming.knapsack_problem_dp(limit, items)
            val, weight = self.total_value_and_weight(bagged)
            print("for a total value of {} and a total weight of {}".format(val, weight))
            # self.print_dynamic_programming(bagged)
        elif self.algorithm == "bb":
            bagged = BranchAndBound.start_branch_and_bound(limit, items)
            self.print_branch_and_bound(bagged)
 def test_that_optimal_solution_is_found_large(self):
     bnb = BranchAndBound(self.input_data_30)
     solution, obj = bnb.solve()
     self.assertEqual(99798, obj)
 def test_that_optimal_solution_is_found(self):
     bnb = BranchAndBound(self.input_data_4)
     solution, obj = bnb.solve()
     expected_solution = [1, 0, 0, 1]
     self.assertEqual(19, obj)
     self.assertListEqual(expected_solution, solution)
 def test_that_knapsack_weight_is_correctly_calculated(self):
     bnb = BranchAndBound(self.input_data_4)
     knapsack = [0, 1, 1, np.nan]
     weight = bnb.calculate_knapsack_weight(np.array(knapsack))
     self.assertEqual(13, weight)
 def test_that_objective_function_value_is_correctly_calculated(self):
     bnb = BranchAndBound(self.input_data_4)
     solution = [0, 0, 1, 1]
     value = bnb.calculate_objective_value(np.array(solution))
     expected = 19
     self.assertEqual(expected, value)
 def test_that_valid_solution_is_identified(self):
     bnb = BranchAndBound(self.input_data_4)
     valid_solution = [1, 1, 0, 0]
     result = bnb.valid(np.array(valid_solution))
     self.assertEqual(result, True)
 def test_that_invalid_solution_is_identified(self):
     bnb = BranchAndBound(self.input_data_4)
     invalid_solution = [1, 0.25, 1, 0]
     result = bnb.valid(np.array(invalid_solution))
     self.assertEqual(result, False)
 def test_that_feasible_solution_is_returned(self):
     bnb = BranchAndBound(self.input_data_4)
     feasible_solution = [1, 0, 0, 1]
     result = bnb.feasible(np.array(feasible_solution))
     self.assertEqual(result, True)
Exemplo n.º 10
0
 def test_that_infeasible_solution_returns_empty_array(self):
     bnb = BranchAndBound(self.input_data_4)
     fixed_vars = [1, 1, 0, 1]
     solution = bnb.relaxed_solution(fixed_variables=np.array(fixed_vars))
     self.assertListEqual([], list(solution))
Exemplo n.º 11
0
 def test_that_relaxed_solution_with_all_variables_fixed_is_correctly_calculated(
         self):
     bnb = BranchAndBound(self.input_data_4)
     fixed_vars = [1, 1, 0, 0]
     solution = bnb.relaxed_solution(fixed_variables=np.array(fixed_vars))
     self.assertListEqual(fixed_vars, list(solution))
Exemplo n.º 12
0
 def bb_time(self, index, iter):
     BBAlgorithm = BranchAndBound(index, self.SymmetricalMatrix)
     start = timer()
     BBAlgorithm.TSP()
     end = timer()
     self.BranchAndBoundTime[iter] = end - start
# Separando features e classes
features_normalized = df_train_normalized.iloc[:, 0:35]
classe_normalized = df_train_normalized.iloc[:, 35]
features_non_normalized = df_train_non_normalized.iloc[:, 0:35]
classe_non_normalized = df_train_non_normalized.iloc[:, 35]
#########################################

######## Feature selection

#### Branch and Bound
# Biblioteca: branch-and-bound-feature-selection
# Parametros: qtd de caracteristicas a ser selecionada (usar raiz quadrada do valor total do conjunto de caracteristicas)
# Função criterio: medida de inconsistência (um subconjunto de caracteristicas é dado como inconsistente se possuir instancias com valores iguais de diferentes classes)
# Observacao: Encontra a melhor solução
# Observacao: Mais apropriado para características discretas (sera necessário fazer a discretização de algumas caracteristicas)
branchAndBound = BranchAndBound(features_non_normalized.values,
                                classe_non_normalized.values)
bestFeaturesBnB = branchAndBound.best_subspace(6)
## Resultado
# Tempo de execução: 55s
# Observação: fez sentido a seleção dessas características como mais relevantes
#for col in range(len(bestFeaturesBnB)):
#    print(features_non_normalized.columns[col])
features_non_normalized.columns[bestFeaturesBnB]
# Caracteristicas selecionadas:
# Age
# Number of sexual partners
# First sexual intercourse
# Num of pregnancies
# Hormonal Contraceptives (years)
# Hinselmann