diff --git a/src/test/utility/GraphTest.cpp b/src/test/utility/GraphTest.cpp index 98359a364..026f850e4 100644 --- a/src/test/utility/GraphTest.cpp +++ b/src/test/utility/GraphTest.cpp @@ -268,32 +268,26 @@ TEST(GraphTest, ExplicitProb01MinMax) { TEST(GraphTest, kshortest) { storm::prism::Program program = storm::parser::PrismParser::parse(STORM_CPP_TESTS_BASE_PATH "/functional/builder/brp-16-2.pm"); - //storm::prism::Program program = storm::parser::PrismParser::parse(STORM_CPP_TESTS_BASE_PATH "/../examples/dtmc/die/die.pm"); std::shared_ptr> model = storm::builder::ExplicitPrismModelBuilder().translateProgram(program); ASSERT_TRUE(model->getType() == storm::models::ModelType::Dtmc); - model->printModelInformationToStream(std::cout); + storm::storage::sparse::state_type testState = 300; - std::cout << "Initializing ShortestPathsGenerator ..." << std::endl; storm::utility::shortestPaths::ShortestPathsGenerator shortestPathsGenerator(model); - unsigned int k; - storm::storage::sparse::state_type state; + // the 1-shortest path is computed as a preprocessing step via Dijkstra; + // since there were some bugs here in the past, let's test it separately + double dijkstraSPDistance = shortestPathsGenerator.getKShortest(testState, 1); + std::cout << "Res: " << dijkstraSPDistance << std::endl; + //EXPECT_NEAR(0.0158593, dijkstraSPDistance, 0.0000001); + EXPECT_DOUBLE_EQ(0.015859334652581887, dijkstraSPDistance); - while (true) { - std::cout << std::endl; - std::cout << "Enter state (note: 0-based index) ... "; - std::cin >> state; + // main test + double kSPDistance1 = shortestPathsGenerator.getKShortest(testState, 100); + EXPECT_DOUBLE_EQ(1.5231305000339649e-06, kSPDistance1); - std::cout << "Enter k ... "; - std::cin >> k; - - std::cout << "Computing " << k << "-shortest path to state " << state << std::endl; - shortestPathsGenerator.getKShortest(state, k); - } - - // TODO: actually write tests here - - EXPECT_TRUE(false); + // let's test again to ensure re-entry is no problem + double kSPDistance2 = shortestPathsGenerator.getKShortest(testState, 500); + EXPECT_DOUBLE_EQ(3.0462610000679282e-08, kSPDistance2); } \ No newline at end of file diff --git a/src/utility/shortestPaths.cpp b/src/utility/shortestPaths.cpp index cb053b5e5..8e5aef6b5 100644 --- a/src/utility/shortestPaths.cpp +++ b/src/utility/shortestPaths.cpp @@ -192,26 +192,23 @@ namespace storm { } template - void ShortestPathsGenerator::getKShortest(state_t node, unsigned long k) { + T ShortestPathsGenerator::getKShortest(state_t node, unsigned long k) { unsigned long alreadyComputedK = kShortestPaths[node].size(); -// std::cout << std::endl << "--> DEBUG: Dijkstra SP to " << node << ":" << std::endl; -// printKShortestPath(node, 1); -// std::cout << "---------" << std::endl; - for (unsigned long nextK = alreadyComputedK + 1; nextK <= k; nextK++) { computeNextPath(node, nextK); if (kShortestPaths[node].size() < nextK) { std::cout << std::endl << "--> DEBUG: Last path: k=" << (nextK - 1) << ":" << std::endl; printKShortestPath(node, nextK - 1); std::cout << "---------" << "No other path exists!" << std::endl; - return; + return storm::utility::zero(); // TODO: throw exception or something } } std::cout << std::endl << "--> DEBUG: Finished. " << k << "-shortest path:" << std::endl; printKShortestPath(node, k); std::cout << "---------" << std::endl; + return kShortestPaths[node][k - 1].distance; } template diff --git a/src/utility/shortestPaths.h b/src/utility/shortestPaths.h index 3b65ce22a..9935f3945 100644 --- a/src/utility/shortestPaths.h +++ b/src/utility/shortestPaths.h @@ -71,7 +71,9 @@ namespace storm { ~ShortestPathsGenerator(); // TODO: think about suitable output format - void getKShortest(state_t node, unsigned long k); + T getKShortest(state_t node, unsigned long k); + + // TODO: allow three kinds of target arguments: single state, BitVector, Label private: