boost::graph metric_tsp_approx with bundled properties fails at runtime
up vote
0
down vote
favorite
I am trying to run the metric_tsp_approx algorithm from boost BGL to solve a simple Traveling Salesman Problem (here the documentation and the related example). I use bundled properties to create my graph. The code compiles fine, but at runtime, when executing the function metric_tsp_approx(...)
, it crashes and gives the assertion vector subscript out of range. I cannot understand what I am missing here, below I put some code that you can run to reproduce the error:
#include <string>
#include <vector>
#include <string>
#include <iostream>
#include <boost/graph/adjacency_matrix.hpp>
#include <boost/graph/metric_tsp_approx.hpp>
using std::cout;
using std::endl;
using std::string;
using std::vector;
using namespace boost;
struct Trip
{
double km;
int travel_minutes;
};
struct Location
{
string name;
std::pair<double, double> coordinates;
//the following function returns the euclidian distance between locations
static double distance(const Location & l1, const Location & l2);
};
typedef boost::adjacency_matrix<boost::undirectedS, Location, Trip> Graph;
typedef typename Graph::vertex_descriptor Node;
typedef typename Graph::edge_descriptor Arc;
int main()
{
//num of nodes in graph
int N = 4;
Graph g = Graph(N);
//building a complete graph of 4 nodes on a 2x2 grid: coordinates of such nodes are (0,0), (0,1), (1,0), (1,1)
//the member variable *km* of each arc is computed as the
//Euclidian distance between the source and target node
Graph::vertex_iterator v, v_end;
for (tie(v, v_end) = vertices(g); v != v_end; ++v)
{
Location loc;
int idx_vertex = N - (int)std::distance(v, v_end);
loc.name = "loc_" + std::to_string(idx_vertex);
loc.coordinates.first = idx_vertex < 2 ? 0 : 1;
loc.coordinates.second = idx_vertex % 2 == 0 ? 0 : 1;
g[*v] = loc;
}
Graph::vertex_iterator i, i_end, j, j_end;
for (tie(i, i_end) = vertices(g); i != i_end; ++i)
{
for (tie(j, j_end) = vertices(g); j != j_end; ++j)
{
Trip trip;
trip.km = Location::distance(g[*i], g[*j]);
trip.travel_minutes = (int)floor(trip.km);
Arc a;
bool inserted;
tie(a,inserted) = add_edge(*i, *j, g);
g[a] = trip;
}
}
/*TSP*/
vector<Node> tour;//solution of TSP
double len = 0;//length of the tour
auto vertex_indices = get(vertex_index, g);
auto w_map = get(&Trip::km, g);//property map of Trip::km that I want to pass to metric_tsp_approx
auto tour_visitor = make_tsp_tour_len_visitor(g, std::back_inserter(tour), len, w_map);
//run
metric_tsp_approx(g, w_map, vertex_indices, tour_visitor);//ERR: vector subscript out of range
for (vector<Node>::iterator itr = tour.begin(); itr != tour.end(); ++itr)
cout << g[*itr].name << " ";
return 0;
}
double Location::distance(const Location & l1, const Location & l2)
{
double lat_delta = l1.coordinates.first - l2.coordinates.first;
double lng_delta = l1.coordinates.second - l2.coordinates.second;
return sqrt(lat_delta*lat_delta + lng_delta*lng_delta);
}
c++ boost runtime-error boost-graph
add a comment |
up vote
0
down vote
favorite
I am trying to run the metric_tsp_approx algorithm from boost BGL to solve a simple Traveling Salesman Problem (here the documentation and the related example). I use bundled properties to create my graph. The code compiles fine, but at runtime, when executing the function metric_tsp_approx(...)
, it crashes and gives the assertion vector subscript out of range. I cannot understand what I am missing here, below I put some code that you can run to reproduce the error:
#include <string>
#include <vector>
#include <string>
#include <iostream>
#include <boost/graph/adjacency_matrix.hpp>
#include <boost/graph/metric_tsp_approx.hpp>
using std::cout;
using std::endl;
using std::string;
using std::vector;
using namespace boost;
struct Trip
{
double km;
int travel_minutes;
};
struct Location
{
string name;
std::pair<double, double> coordinates;
//the following function returns the euclidian distance between locations
static double distance(const Location & l1, const Location & l2);
};
typedef boost::adjacency_matrix<boost::undirectedS, Location, Trip> Graph;
typedef typename Graph::vertex_descriptor Node;
typedef typename Graph::edge_descriptor Arc;
int main()
{
//num of nodes in graph
int N = 4;
Graph g = Graph(N);
//building a complete graph of 4 nodes on a 2x2 grid: coordinates of such nodes are (0,0), (0,1), (1,0), (1,1)
//the member variable *km* of each arc is computed as the
//Euclidian distance between the source and target node
Graph::vertex_iterator v, v_end;
for (tie(v, v_end) = vertices(g); v != v_end; ++v)
{
Location loc;
int idx_vertex = N - (int)std::distance(v, v_end);
loc.name = "loc_" + std::to_string(idx_vertex);
loc.coordinates.first = idx_vertex < 2 ? 0 : 1;
loc.coordinates.second = idx_vertex % 2 == 0 ? 0 : 1;
g[*v] = loc;
}
Graph::vertex_iterator i, i_end, j, j_end;
for (tie(i, i_end) = vertices(g); i != i_end; ++i)
{
for (tie(j, j_end) = vertices(g); j != j_end; ++j)
{
Trip trip;
trip.km = Location::distance(g[*i], g[*j]);
trip.travel_minutes = (int)floor(trip.km);
Arc a;
bool inserted;
tie(a,inserted) = add_edge(*i, *j, g);
g[a] = trip;
}
}
/*TSP*/
vector<Node> tour;//solution of TSP
double len = 0;//length of the tour
auto vertex_indices = get(vertex_index, g);
auto w_map = get(&Trip::km, g);//property map of Trip::km that I want to pass to metric_tsp_approx
auto tour_visitor = make_tsp_tour_len_visitor(g, std::back_inserter(tour), len, w_map);
//run
metric_tsp_approx(g, w_map, vertex_indices, tour_visitor);//ERR: vector subscript out of range
for (vector<Node>::iterator itr = tour.begin(); itr != tour.end(); ++itr)
cout << g[*itr].name << " ";
return 0;
}
double Location::distance(const Location & l1, const Location & l2)
{
double lat_delta = l1.coordinates.first - l2.coordinates.first;
double lng_delta = l1.coordinates.second - l2.coordinates.second;
return sqrt(lat_delta*lat_delta + lng_delta*lng_delta);
}
c++ boost runtime-error boost-graph
2
Difference between your code and example is you add edges where source and target vertices are the same, for example(1,1)
,(2,2)
with 0.0 as weight. Without adding such edges your code works.
– rafix07
Nov 19 at 20:31
Thank you very much rafix07, it indeed solved the problem!
– Nicola Gastaldon
Nov 19 at 22:27
@rafix07 why not post your valuable comments as answers :)
– sehe
Nov 21 at 0:13
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I am trying to run the metric_tsp_approx algorithm from boost BGL to solve a simple Traveling Salesman Problem (here the documentation and the related example). I use bundled properties to create my graph. The code compiles fine, but at runtime, when executing the function metric_tsp_approx(...)
, it crashes and gives the assertion vector subscript out of range. I cannot understand what I am missing here, below I put some code that you can run to reproduce the error:
#include <string>
#include <vector>
#include <string>
#include <iostream>
#include <boost/graph/adjacency_matrix.hpp>
#include <boost/graph/metric_tsp_approx.hpp>
using std::cout;
using std::endl;
using std::string;
using std::vector;
using namespace boost;
struct Trip
{
double km;
int travel_minutes;
};
struct Location
{
string name;
std::pair<double, double> coordinates;
//the following function returns the euclidian distance between locations
static double distance(const Location & l1, const Location & l2);
};
typedef boost::adjacency_matrix<boost::undirectedS, Location, Trip> Graph;
typedef typename Graph::vertex_descriptor Node;
typedef typename Graph::edge_descriptor Arc;
int main()
{
//num of nodes in graph
int N = 4;
Graph g = Graph(N);
//building a complete graph of 4 nodes on a 2x2 grid: coordinates of such nodes are (0,0), (0,1), (1,0), (1,1)
//the member variable *km* of each arc is computed as the
//Euclidian distance between the source and target node
Graph::vertex_iterator v, v_end;
for (tie(v, v_end) = vertices(g); v != v_end; ++v)
{
Location loc;
int idx_vertex = N - (int)std::distance(v, v_end);
loc.name = "loc_" + std::to_string(idx_vertex);
loc.coordinates.first = idx_vertex < 2 ? 0 : 1;
loc.coordinates.second = idx_vertex % 2 == 0 ? 0 : 1;
g[*v] = loc;
}
Graph::vertex_iterator i, i_end, j, j_end;
for (tie(i, i_end) = vertices(g); i != i_end; ++i)
{
for (tie(j, j_end) = vertices(g); j != j_end; ++j)
{
Trip trip;
trip.km = Location::distance(g[*i], g[*j]);
trip.travel_minutes = (int)floor(trip.km);
Arc a;
bool inserted;
tie(a,inserted) = add_edge(*i, *j, g);
g[a] = trip;
}
}
/*TSP*/
vector<Node> tour;//solution of TSP
double len = 0;//length of the tour
auto vertex_indices = get(vertex_index, g);
auto w_map = get(&Trip::km, g);//property map of Trip::km that I want to pass to metric_tsp_approx
auto tour_visitor = make_tsp_tour_len_visitor(g, std::back_inserter(tour), len, w_map);
//run
metric_tsp_approx(g, w_map, vertex_indices, tour_visitor);//ERR: vector subscript out of range
for (vector<Node>::iterator itr = tour.begin(); itr != tour.end(); ++itr)
cout << g[*itr].name << " ";
return 0;
}
double Location::distance(const Location & l1, const Location & l2)
{
double lat_delta = l1.coordinates.first - l2.coordinates.first;
double lng_delta = l1.coordinates.second - l2.coordinates.second;
return sqrt(lat_delta*lat_delta + lng_delta*lng_delta);
}
c++ boost runtime-error boost-graph
I am trying to run the metric_tsp_approx algorithm from boost BGL to solve a simple Traveling Salesman Problem (here the documentation and the related example). I use bundled properties to create my graph. The code compiles fine, but at runtime, when executing the function metric_tsp_approx(...)
, it crashes and gives the assertion vector subscript out of range. I cannot understand what I am missing here, below I put some code that you can run to reproduce the error:
#include <string>
#include <vector>
#include <string>
#include <iostream>
#include <boost/graph/adjacency_matrix.hpp>
#include <boost/graph/metric_tsp_approx.hpp>
using std::cout;
using std::endl;
using std::string;
using std::vector;
using namespace boost;
struct Trip
{
double km;
int travel_minutes;
};
struct Location
{
string name;
std::pair<double, double> coordinates;
//the following function returns the euclidian distance between locations
static double distance(const Location & l1, const Location & l2);
};
typedef boost::adjacency_matrix<boost::undirectedS, Location, Trip> Graph;
typedef typename Graph::vertex_descriptor Node;
typedef typename Graph::edge_descriptor Arc;
int main()
{
//num of nodes in graph
int N = 4;
Graph g = Graph(N);
//building a complete graph of 4 nodes on a 2x2 grid: coordinates of such nodes are (0,0), (0,1), (1,0), (1,1)
//the member variable *km* of each arc is computed as the
//Euclidian distance between the source and target node
Graph::vertex_iterator v, v_end;
for (tie(v, v_end) = vertices(g); v != v_end; ++v)
{
Location loc;
int idx_vertex = N - (int)std::distance(v, v_end);
loc.name = "loc_" + std::to_string(idx_vertex);
loc.coordinates.first = idx_vertex < 2 ? 0 : 1;
loc.coordinates.second = idx_vertex % 2 == 0 ? 0 : 1;
g[*v] = loc;
}
Graph::vertex_iterator i, i_end, j, j_end;
for (tie(i, i_end) = vertices(g); i != i_end; ++i)
{
for (tie(j, j_end) = vertices(g); j != j_end; ++j)
{
Trip trip;
trip.km = Location::distance(g[*i], g[*j]);
trip.travel_minutes = (int)floor(trip.km);
Arc a;
bool inserted;
tie(a,inserted) = add_edge(*i, *j, g);
g[a] = trip;
}
}
/*TSP*/
vector<Node> tour;//solution of TSP
double len = 0;//length of the tour
auto vertex_indices = get(vertex_index, g);
auto w_map = get(&Trip::km, g);//property map of Trip::km that I want to pass to metric_tsp_approx
auto tour_visitor = make_tsp_tour_len_visitor(g, std::back_inserter(tour), len, w_map);
//run
metric_tsp_approx(g, w_map, vertex_indices, tour_visitor);//ERR: vector subscript out of range
for (vector<Node>::iterator itr = tour.begin(); itr != tour.end(); ++itr)
cout << g[*itr].name << " ";
return 0;
}
double Location::distance(const Location & l1, const Location & l2)
{
double lat_delta = l1.coordinates.first - l2.coordinates.first;
double lng_delta = l1.coordinates.second - l2.coordinates.second;
return sqrt(lat_delta*lat_delta + lng_delta*lng_delta);
}
c++ boost runtime-error boost-graph
c++ boost runtime-error boost-graph
asked Nov 19 at 14:11
Nicola Gastaldon
212
212
2
Difference between your code and example is you add edges where source and target vertices are the same, for example(1,1)
,(2,2)
with 0.0 as weight. Without adding such edges your code works.
– rafix07
Nov 19 at 20:31
Thank you very much rafix07, it indeed solved the problem!
– Nicola Gastaldon
Nov 19 at 22:27
@rafix07 why not post your valuable comments as answers :)
– sehe
Nov 21 at 0:13
add a comment |
2
Difference between your code and example is you add edges where source and target vertices are the same, for example(1,1)
,(2,2)
with 0.0 as weight. Without adding such edges your code works.
– rafix07
Nov 19 at 20:31
Thank you very much rafix07, it indeed solved the problem!
– Nicola Gastaldon
Nov 19 at 22:27
@rafix07 why not post your valuable comments as answers :)
– sehe
Nov 21 at 0:13
2
2
Difference between your code and example is you add edges where source and target vertices are the same, for example
(1,1)
, (2,2)
with 0.0 as weight. Without adding such edges your code works.– rafix07
Nov 19 at 20:31
Difference between your code and example is you add edges where source and target vertices are the same, for example
(1,1)
, (2,2)
with 0.0 as weight. Without adding such edges your code works.– rafix07
Nov 19 at 20:31
Thank you very much rafix07, it indeed solved the problem!
– Nicola Gastaldon
Nov 19 at 22:27
Thank you very much rafix07, it indeed solved the problem!
– Nicola Gastaldon
Nov 19 at 22:27
@rafix07 why not post your valuable comments as answers :)
– sehe
Nov 21 at 0:13
@rafix07 why not post your valuable comments as answers :)
– sehe
Nov 21 at 0:13
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53376465%2fboostgraph-metric-tsp-approx-with-bundled-properties-fails-at-runtime%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Difference between your code and example is you add edges where source and target vertices are the same, for example
(1,1)
,(2,2)
with 0.0 as weight. Without adding such edges your code works.– rafix07
Nov 19 at 20:31
Thank you very much rafix07, it indeed solved the problem!
– Nicola Gastaldon
Nov 19 at 22:27
@rafix07 why not post your valuable comments as answers :)
– sehe
Nov 21 at 0:13