From 97e3a59888804970df3f8627ec5f54c5744f5807 Mon Sep 17 00:00:00 2001 From: Patrick O'Hara Date: Wed, 27 Mar 2024 13:18:55 +0000 Subject: [PATCH] Bug fixing in tests --- include/pctsp/graph.hh | 12 ++++++++++++ pctsp/apps/dataset_app.py | 3 +-- src/algorithms.cpp | 6 +++--- src/graph.cpp | 6 ++++++ tests/test_algorithms.cpp | 14 +++++++++++--- tests/test_subtour_elimination.cpp | 16 +++++++++------- 6 files changed, 42 insertions(+), 15 deletions(-) diff --git a/include/pctsp/graph.hh b/include/pctsp/graph.hh index 43c6a13..014c299 100644 --- a/include/pctsp/graph.hh +++ b/include/pctsp/graph.hh @@ -2,6 +2,7 @@ #ifndef __PCTSP_GRAPH__ #define __PCTSP_GRAPH__ +#include #include #include #include @@ -150,6 +151,17 @@ std::vector getEdgeVariables( std::vector& edges ); +template +void printEdges(TGraph& graph, EdgeIt& first, EdgeIt& last) { + // typedef typename boost::graph_traits< TGraph >::edge_descriptor TEdge; + for (; first != last; first++) { + auto edge = *first; + std::cout << boost::source(edge, graph) << ", " << boost::target(edge, graph) << std::endl; + } +}; + +void printEdges(std::vector>& edges); + template std::vector::vertex_descriptor> getVerticesOfEdges( TGraph& graph, diff --git a/pctsp/apps/dataset_app.py b/pctsp/apps/dataset_app.py index 75cbe70..6d8e81c 100644 --- a/pctsp/apps/dataset_app.py +++ b/pctsp/apps/dataset_app.py @@ -10,7 +10,6 @@ EdgeWeightType, Generation, ProfitsProblem, - NotConnectedException, asymmetric_from_undirected, biggest_vertex_id_from_graph, build_path_to_londonaq_yaml, @@ -109,7 +108,7 @@ def get_graph_stats(graph: nx.Graph, root_vertex: int) -> Dict[str, float]: instance_stats["total_prize"] = og_prize try: instance_stats["metricness"] = metricness(graph) - except NotConnectedException: + except nx.exception.NetworkXException: # FIXME change to NotConnectedException largest_component_graph = graph.subgraph(max(nx.connected_components(graph), key=len)) instance_stats["metricness"] = metricness(largest_component_graph) diff --git a/src/algorithms.cpp b/src/algorithms.cpp index 1918a03..c3a8a28 100644 --- a/src/algorithms.cpp +++ b/src/algorithms.cpp @@ -37,7 +37,7 @@ std::vector> solvePrizeCollectingTSP( ) { auto edge_var_map = modelPrizeCollectingTSP(scip, graph, heuristic_edges, cost_map, prize_map, quota, root_vertex, name); SCIPsolve(scip); - if (SCIPgetStatus(scip) != SCIP_INFEASIBLE) { + if (SCIPgetStatus(scip) != SCIP_STATUS_INFEASIBLE) { SCIP_SOL* sol = SCIPgetBestSol(scip); auto solution_edges = getSolutionEdges(scip, graph, sol, edge_var_map); return getVertexPairVectorFromEdgeSubset(graph, solution_edges); @@ -58,7 +58,7 @@ std::vector> solvePrizeCollectingTSP( ) { auto edge_var_map = modelPrizeCollectingTSP(scip, graph, edge_list, heuristic_edges, cost_dict, prize_dict, quota, root_vertex, name); SCIPsolve(scip); - if (SCIPgetStatus(scip) != SCIP_INFEASIBLE) { + if (SCIPgetStatus(scip) != SCIP_STATUS_INFEASIBLE) { SCIP_SOL* sol = SCIPgetBestSol(scip); auto solution_edges = getSolutionEdges(scip, graph, sol, edge_var_map); return getVertexPairVectorFromEdgeSubset(graph, solution_edges); @@ -211,7 +211,7 @@ std::vector> solvePrizeCollectingTSP( std::vector solution_edges = std::vector(); if (SCIPgetNSols(scip) > 0) { SCIP_SOL* sol = SCIPgetBestSol(scip); - auto solution_edges = getSolutionEdges(scip, graph, sol, edge_var_map); + solution_edges = getSolutionEdges(scip, graph, sol, edge_var_map); } // get the node stats of the solver diff --git a/src/graph.cpp b/src/graph.cpp index d11b71c..d9dccac 100644 --- a/src/graph.cpp +++ b/src/graph.cpp @@ -38,6 +38,12 @@ std::vector getEdgeVariables( return getEdgeVariables(scip, graph, edge_variable_map, first, last); } +void printEdges(std::vector>& edges){ + for (auto& edge : edges) { + std::cout << edge.first << ", " << edge.second << std::endl; + } +} + std::vector getEdgesInducedByVertices(PCTSPgraph& graph, std::vector& vertices) { auto first = vertices.begin(); auto last = vertices.end(); diff --git a/tests/test_algorithms.cpp b/tests/test_algorithms.cpp index 5e8ee18..3ac4388 100644 --- a/tests/test_algorithms.cpp +++ b/tests/test_algorithms.cpp @@ -3,6 +3,7 @@ #include "fixtures.hh" #include "pctsp/graph.hh" #include "pctsp/algorithms.hh" +#include "pctsp/node_selection.hh" typedef GraphFixture AlgorithmsFixture; typedef GraphFixture SuurballeGraphFixture; @@ -159,9 +160,16 @@ TEST_P(AlgorithmsFixture, testAddHeuristicVarsToSolver) { // initialise and create the model without subtour elimiation constraints SCIP* scip_model = NULL; SCIPcreate(&scip_model); - includeBranchRules(scip_model); - // SCIPincludeDefaultPlugins(scip_model); - SCIPcreateProbBasic(scip_model, "test-add-heuristic"); + SCIPincludeDefaultPlugins(scip_model); + SCIPcreateProbBasic(scip_model, "testAddHeuristicVarsToSolver"); + + SCIP_MESSAGEHDLR* handler; + std::filesystem::path solver_dir = ".logs"; + std::filesystem::create_directory(solver_dir); + std::string filename = "testAddHeuristicVarsToSolver_" + getParamName() + ".txt"; + std::filesystem::path logs_txt = solver_dir / filename; + SCIPcreateMessagehdlrDefault(&handler, false, logs_txt.c_str(), true); + SCIPsetMessagehdlr(scip_model, handler); // add variables and constraints SCIP_RETCODE code = diff --git a/tests/test_subtour_elimination.cpp b/tests/test_subtour_elimination.cpp index b2f353b..e9eb464 100644 --- a/tests/test_subtour_elimination.cpp +++ b/tests/test_subtour_elimination.cpp @@ -154,7 +154,7 @@ TEST_P(SubtourGraphFixture, testSubtourParams) { int quota; switch (GetParam()) { case GraphType::COMPLETE25: quota = totalPrizeOfGraph(graph, prize_map); break; - default: quota = 3; break; + default: quota = 4; break; } addSelfLoopsToGraph(graph); @@ -204,17 +204,17 @@ TEST_P(SubtourGraphFixture, testSubtourParams) { break; } case GraphType::SUURBALLE: { - expected_cost = 15; + expected_cost = 16; expected_nnodes = 3; + expected_num_sec_maxflow_mincut = 4; break; } case GraphType::COMPLETE4: { - expected_cost = 4; - expected_num_sec_maxflow_mincut = 3; + expected_cost = 6; break; } case GraphType::COMPLETE5: { - expected_cost = 5; + expected_cost = 7; break; } case GraphType::COMPLETE25: { @@ -231,8 +231,8 @@ TEST_P(SubtourGraphFixture, testSubtourParams) { EXPECT_EQ(expected_cost, actual_cost); auto summary_yaml = logger_dir / PCTSP_SUMMARY_STATS_YAML; auto stats = readSummaryStatsFromYaml(summary_yaml); - // EXPECT_EQ(stats.num_sec_maxflow_mincut, expected_num_sec_maxflow_mincut); - // EXPECT_EQ(stats.num_sec_disjoint_tour, expected_num_sec_disjoint_tour); + EXPECT_EQ(stats.num_sec_maxflow_mincut, expected_num_sec_maxflow_mincut); + EXPECT_EQ(stats.num_sec_disjoint_tour, expected_num_sec_disjoint_tour); // EXPECT_EQ(SCIPgetNNodes(scip), expected_nnodes); SCIPfree(&scip); } @@ -282,6 +282,7 @@ TEST(TestSubtourElimination, testPushIntoRollingLpGapList) { EXPECT_EQ(rolling_gaps.back(), gap); } + TEST_P(SubtourGraphFixture, testTailingOff) { PCTSPinitLogging(logging::trivial::warning); bool sec_disjoint_tour = true; @@ -303,6 +304,7 @@ TEST_P(SubtourGraphFixture, testTailingOff) { SCIPcreate(&scip); std::string name = "testTailingOff"; + auto solution_edges = solvePrizeCollectingTSP( scip, graph,