boost::graph metric_tsp_approx with bundled properties fails at runtime












0














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);
}









share|improve this question


















  • 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 '18 at 20:31












  • Thank you very much rafix07, it indeed solved the problem!
    – Nicola Gastaldon
    Nov 19 '18 at 22:27










  • @rafix07 why not post your valuable comments as answers :)
    – sehe
    Nov 21 '18 at 0:13
















0














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);
}









share|improve this question


















  • 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 '18 at 20:31












  • Thank you very much rafix07, it indeed solved the problem!
    – Nicola Gastaldon
    Nov 19 '18 at 22:27










  • @rafix07 why not post your valuable comments as answers :)
    – sehe
    Nov 21 '18 at 0:13














0












0








0







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);
}









share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 19 '18 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 '18 at 20:31












  • Thank you very much rafix07, it indeed solved the problem!
    – Nicola Gastaldon
    Nov 19 '18 at 22:27










  • @rafix07 why not post your valuable comments as answers :)
    – sehe
    Nov 21 '18 at 0:13














  • 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 '18 at 20:31












  • Thank you very much rafix07, it indeed solved the problem!
    – Nicola Gastaldon
    Nov 19 '18 at 22:27










  • @rafix07 why not post your valuable comments as answers :)
    – sehe
    Nov 21 '18 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 '18 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 '18 at 20:31














Thank you very much rafix07, it indeed solved the problem!
– Nicola Gastaldon
Nov 19 '18 at 22:27




Thank you very much rafix07, it indeed solved the problem!
– Nicola Gastaldon
Nov 19 '18 at 22:27












@rafix07 why not post your valuable comments as answers :)
– sehe
Nov 21 '18 at 0:13




@rafix07 why not post your valuable comments as answers :)
– sehe
Nov 21 '18 at 0:13












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

'app-layout' is not a known element: how to share Component with different Modules

android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

WPF add header to Image with URL pettitions [duplicate]