Skip to content

Commit

Permalink
Bug fixing in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickOHara committed Mar 27, 2024
1 parent 545ab9b commit 97e3a59
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 15 deletions.
12 changes: 12 additions & 0 deletions include/pctsp/graph.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#ifndef __PCTSP_GRAPH__
#define __PCTSP_GRAPH__
#include <iostream>
#include <boost/bimap.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/filtered_graph.hpp>
Expand Down Expand Up @@ -150,6 +151,17 @@ std::vector<SCIP_VAR*> getEdgeVariables(
std::vector<PCTSPedge>& edges
);

template <typename TGraph, typename EdgeIt>
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<std::pair<PCTSPvertex, PCTSPvertex>>& edges);

template <typename TGraph, typename EdgeIt>
std::vector<typename boost::graph_traits< TGraph >::vertex_descriptor> getVerticesOfEdges(
TGraph& graph,
Expand Down
3 changes: 1 addition & 2 deletions pctsp/apps/dataset_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
EdgeWeightType,
Generation,
ProfitsProblem,
NotConnectedException,
asymmetric_from_undirected,
biggest_vertex_id_from_graph,
build_path_to_londonaq_yaml,
Expand Down Expand Up @@ -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)

Expand Down
6 changes: 3 additions & 3 deletions src/algorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ std::vector<std::pair<PCTSPvertex, PCTSPvertex>> 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);
Expand All @@ -58,7 +58,7 @@ std::vector<std::pair<PCTSPvertex, PCTSPvertex>> 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);
Expand Down Expand Up @@ -211,7 +211,7 @@ std::vector<std::pair<PCTSPvertex, PCTSPvertex>> solvePrizeCollectingTSP(
std::vector<PCTSPedge> solution_edges = std::vector<PCTSPedge>();
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
Expand Down
6 changes: 6 additions & 0 deletions src/graph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ std::vector<SCIP_VAR*> getEdgeVariables(
return getEdgeVariables(scip, graph, edge_variable_map, first, last);
}

void printEdges(std::vector<std::pair<PCTSPvertex, PCTSPvertex>>& edges){
for (auto& edge : edges) {
std::cout << edge.first << ", " << edge.second << std::endl;
}
}

std::vector<PCTSPedge> getEdgesInducedByVertices(PCTSPgraph& graph, std::vector<PCTSPvertex>& vertices) {
auto first = vertices.begin();
auto last = vertices.end();
Expand Down
14 changes: 11 additions & 3 deletions tests/test_algorithms.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 =
Expand Down
16 changes: 9 additions & 7 deletions tests/test_subtour_elimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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: {
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
Expand All @@ -303,6 +304,7 @@ TEST_P(SubtourGraphFixture, testTailingOff) {
SCIPcreate(&scip);
std::string name = "testTailingOff";


auto solution_edges = solvePrizeCollectingTSP(
scip,
graph,
Expand Down

0 comments on commit 97e3a59

Please sign in to comment.