diff --git a/src/machine_learning/search_algorithms/best_first/best_first.h b/src/machine_learning/search_algorithms/best_first/best_first.h index 3677a991..5618e376 100644 --- a/src/machine_learning/search_algorithms/best_first/best_first.h +++ b/src/machine_learning/search_algorithms/best_first/best_first.h @@ -1,5 +1,5 @@ -#ifndef BEST_FIRST_H -#define BEST_FIRST_H +#ifndef BEST_FIRST_H +#define BEST_FIRST_H #ifdef __cplusplus #include @@ -13,9 +13,9 @@ */ template class best_first{ private: - std::unordered_map > > adj; + std::unordered_map > > adj; std::unordered_map nodes; - + public: /** @@ -39,7 +39,7 @@ template class best_first{ std::cerr << e.what() << '\n'; } } - + /** * @brief insert_node function * @param u: the node ID @@ -67,7 +67,7 @@ template class best_first{ } return false; } - + /** * @brief add_edge function * @param u: the first node @@ -86,7 +86,7 @@ template class best_first{ std::cerr << e.what() << '\n'; } } - + /** * @brief search function * @param start: starting node @@ -124,4 +124,3 @@ template class best_first{ #endif - diff --git a/src/machine_learning/search_algorithms/hill_climbing/hill_climbing.h b/src/machine_learning/search_algorithms/hill_climbing/hill_climbing.h index f890acf8..bf40d46a 100644 --- a/src/machine_learning/search_algorithms/hill_climbing/hill_climbing.h +++ b/src/machine_learning/search_algorithms/hill_climbing/hill_climbing.h @@ -14,22 +14,26 @@ */ template class hill_climbing{ private: - std::unordered_map > > adj; + std::unordered_map > > adj; std::unordered_map nodes; - + public: /** * @brief hill_climbing constructor - * @param v: unordered_map > initializer vector. Default = {} - * + * @param v: unordered_map > initializer vector. Default = {} + * @param nodes: unordered_map initializer values for the nodes. Default = {} */ - explicit hill_climbing(std::unordered_map > > v = {}){ - if(!v.empty()){ + explicit hill_climbing(std::unordered_map > > v = {}, + std::unordered_map nodes = {}){ + if(!v.empty()) { this->adj = v; } + if(!nodes.empty()) { + this->nodes = nodes; + } } - + /** * @brief insert_node function * @param u: the node ID @@ -57,7 +61,7 @@ template class hill_climbing{ } return false; } - + /** * @brief add_edge function * @param u: the first node @@ -76,7 +80,7 @@ template class hill_climbing{ std::cerr << e.what() << '\n'; } } - + /** * @brief search function * @param start: starting node diff --git a/tests/machine_learning/search_algorithms/best_first.cc b/tests/machine_learning/search_algorithms/best_first.cc index e59fc011..e282ecb5 100644 --- a/tests/machine_learning/search_algorithms/best_first.cc +++ b/tests/machine_learning/search_algorithms/best_first.cc @@ -13,6 +13,22 @@ TEST_CASE("testing edges with insertion for best first class"){ REQUIRE(h.has_edge(0, 3) == false); } +TEST_CASE("Testing default constructor of best first with passed adjacency list") { + std::unordered_map > > adj; + std::unordered_map nodes; + + nodes[0] = INT_MAX; + nodes[1] = 10; + nodes[2] = 20; + nodes[3] = 10; + + adj[0].push_back({1, nodes[1]}); + + best_first h(adj, nodes); + REQUIRE(h.has_edge(0, 1) == true); + REQUIRE(h.has_edge(0, 3) == false); + REQUIRE(h.has_edge(1, 0) == false); +} TEST_CASE("testing search function for best_first class"){ best_first h; @@ -52,3 +68,19 @@ TEST_CASE("testing search function for best_first class"){ } +TEST_CASE("Testing throws on add_edge for best first function") { + std::unordered_map > > adj; + std::unordered_map nodes; + + nodes[0] = INT_MAX; + nodes[1] = 10; + nodes[2] = 20; + nodes[3] = 10; + + adj[0].push_back({1, nodes[1]}); + + best_first h(adj, nodes); + + CHECK_NOTHROW(h.add_edge(10, 20)); + CHECK_NOTHROW(h.add_edge(0, 1)); +} diff --git a/tests/machine_learning/search_algorithms/hill_climbing.cc b/tests/machine_learning/search_algorithms/hill_climbing.cc index 42afcdf0..19ee08e7 100644 --- a/tests/machine_learning/search_algorithms/hill_climbing.cc +++ b/tests/machine_learning/search_algorithms/hill_climbing.cc @@ -13,6 +13,23 @@ TEST_CASE("testing edges with insertion for hill climbing class"){ REQUIRE(h.has_edge(0, 3) == false); } +TEST_CASE("Testing default constructor of hill climbing") { + std::unordered_map node; + std::unordered_map > > adj; + + node[0] = INT_MAX; + node[1] = 10; + node[2] = 20; + node[3] = 10; + + adj[0].push_back({1, node[1]}); + + hill_climbing h(adj, node); + + REQUIRE(h.has_edge(0, 1) == true); + REQUIRE(h.has_edge(0, 3) == false); +} + TEST_CASE("testing search function for hill climbing class"){ hill_climbing h; h.insert_node('s', INT_MAX); @@ -49,5 +66,3 @@ TEST_CASE("testing search function for hill climbing class"){ REQUIRE(h2.search('s', 'G') == false); } - -