[pgrouting-dev] TRSP test utility

Stephen Woodbridge woodbri at swoodbridge.com
Sat Jun 30 12:23:18 PDT 2012


Hi all,

Since I rarely throw anything out, I was about to find the TRSP 
testmain.cpp code. This might be used as an example of how to test the 
core algorithm outside of postgresql. Oh and another benefit of this is 
that you can run the code under valgrind to identify memory leaks and 
potential array indexing issues.

At some point I will add these to my git branch so these and some of the 
other test files are available.

Obviously, you would need to modify this for your code but I can see 
each algorithm as having a similar tool to load and run files for testing.

Thanks,
   -Steve

$ cat testmain.cpp
#include "GraphDefinition.h"
#include "utils.h"
#include <string.h>


std::vector<edge_t> vecEdges;
std::vector<PDVI> ruleTable;

int main()
{
         edge_t *edges;
         int edge_count;
         char buff[1024];
         std::string edgeFile = "edges-b1.txt";
         std::string ruleFile = "rules-b1.txt";
         freopen(edgeFile.c_str(), "rt", stdin);

         edge_count = 0;
         vecEdges.clear();
         while(gets(buff))
         {
                 if(strlen(buff) == 0)
                         break;
                 edge_count++;
                 StringTokenizer token;
                 token.parse(buff, ",");
                 std::vector<std::string> vecToken;
                 token.getTokens(vecToken);
                 if(vecToken.size() < 5)
                         fprintf(stderr, "Error in %d edge\n", edge_count);

                 edge_t tempEdge;
                 tempEdge.id = atoi(vecToken[0].c_str());
                 tempEdge.source = atoi(vecToken[1].c_str());
                 tempEdge.target = atoi(vecToken[2].c_str());
                 tempEdge.cost = atof(vecToken[3].c_str());
                 tempEdge.reverse_cost = atof(vecToken[4].c_str());

                 vecEdges.push_back(tempEdge);
         }

         edges = new edge_t[edge_count];
         int i;

         for(i = 0; i < edge_count; i++)
         {
                 edges[i] = vecEdges[i];
         }

         freopen(ruleFile.c_str(), "rt", stdin);

         ruleTable.clear();
         while(gets(buff))
         {
                 if(strlen(buff) == 0)
                         break;
                 StringTokenizer token;
                 token.parse(buff, ",");
                 std::vector<std::string> vecToken;
                 token.getTokens(vecToken);
                 int totalTokens = vecToken.size();
                 std::vector<int> seq;
                 seq.clear();
                 for(i = 1; i < totalTokens; i++)
                 {
                         seq.push_back(atoi(vecToken[i].c_str()));
                 }
 
ruleTable.push_back(make_pair(atof(vecToken[0].c_str()), seq));
         }
         path_element_t *path;
         char *err_msg;
         int path_count;
         GraphDefinition gdef;
         int res = gdef.my_dijkstra(edges, edge_count, 32966, 33353, 
&path, &path_count, &err_msg, ruleTable);

         freopen("result-nt.txt", "wt", stdout);
         if(res < 0)
                 printf("%s\n", err_msg);
         else
         {
                 for(i = 0; i < path_count; i++)
                 {
                         printf("%d\t|%d\t|%.6lf\n", path[i].vertex_id, 
path[i].edge_id, path[i].cost);
                 }
         }
     free(path);
         delete [] edges;
         return 0;
}

Here is an example input file:

$ cat edge2.txt
1,1,6,1,1
2,2,3,1,1
3,3,4,1,1
4,4,5,1,1
5,5,6,1,1
6,2,7,1,1
7,3,8,1,1
8,4,9,1,1
9,5,10,1,1
10,6,11,1,1
11,7,8,1,1
12,8,9,1,1
13,9,10,1,1
14,10,11,1,1
15,7,12,1,1
16,8,13,1,1
17,9,14,1,1
18,10,15,1,1
19,11,16,1,1
20,12,13,1,1
21,13,14,1,1
22,14,15,1,1
23,15,16,1,1
24,12,17,1,1
25,13,18,1,1
26,14,19,1,1
27,15,20,1,1
28,16,21,1,1
29,17,18,1,1
30,18,19,1,1
31,19,20,1,1
32,20,21,1,1
33,17,22,1,1
34,18,23,1,1
35,19,24,1,1
36,20,25,1,1
37,21,26,1,1
38,22,23,1,1
39,23,24,1,1
40,24,25,1,1
41,25,26,1,1
42,22,27,1,1

And a sample restrictions table:

$ cat rules2.txt
1.1,5,1
1.1,14,10
1.1,23,19
1.1,32,28
1.1,13,9
1.1,5,9
1.1,22,18
1.1,14,18
1.1,31,27
1.1,23,27
1.1,40,36
1.1,32,36
1.1,12,8
1.1,4,8
1.1,21,17
1.1,13,17
1.1,30,26
1.1,22,26
1.1,39,35
1.1,31,35
1.1,11,7
1.1,3,7
1.1,20,16
1.1,12,16
1.1,29,25
1.1,21,25
1.1,38,34
1.1,30,34
1.1,38,42
1.1,29,33
1.1,20,24
1.1,11,15
1.1,10,5
1.1,9,4
1.1,8,3
1.1,7,2
1.1,9,14
1.1,19,14
1.1,8,13
1.1,18,13
1.1,7,12
1.1,17,12
1.1,6,11
1.1,16,11
1.1,18,23
1.1,28,23
1.1,17,22
1.1,27,22
1.1,16,21
1.1,26,21
1.1,15,20
1.1,25,20
1.1,27,32
1.1,37,32
1.1,26,31
1.1,36,31
1.1,25,30
1.1,35,30
1.1,24,29
1.1,34,29
1.1,36,41
1.1,35,40
1.1,34,39
1.1,33,38


More information about the pgrouting-dev mailing list