#include #include #include #include enum {INPUT, OUTPUT, MAP_NUM}; DijkstraBox::DijkstraBox(std::vector t):AlgoBox() { init(t); } SuurballeBox::SuurballeBox(std::vector t):DijkstraBox(t) { Gtk::Adjustment * adjustment=new Gtk::Adjustment(2, 1, 20, 1, 5); num_set = new Gtk::SpinButton(*adjustment, 5,0); Gtk::Label * label=new Gtk::Label("No. of paths to find: "); // hbox.pack_start(*label); // hbox.pack_start(*num_set); // hbox.show_all_children(); table.attach(*label, 0,1,2,3); table.attach(*num_set, 1,2,2,3); // pack_start(hbox); } void DijkstraBox::run() { if( tabcbt.get_active_text()!="" && (edgemapcbts[INPUT])->get_active_text()!="" && (edgemapcbts[OUTPUT])->get_active_text()!="" && source.get_active_text()!="" && target.get_active_text()!="" ) { const Graph &g=mapstorage->graph; Node from, to; get_from_to(from, to, (Graph&)g); std::ostringstream o; if(!(from==to)) { Graph::EdgeMap * inputmap= (mapstorage->edgemap_storage)[(edgemapcbts[INPUT])->get_active_text()]; Graph::EdgeMap * outputmap= (mapstorage->edgemap_storage)[(edgemapcbts[OUTPUT])->get_active_text()]; //zero out output map for (EdgeIt i(g); i!=INVALID; ++i) { (*outputmap)[i]=0; } Dijkstra > dijkstra(g, *inputmap); dijkstra.run(from, to); if(dijkstra.reached(to)) { Node n=to; int length=0; while (n!=INVALID && n!=from) { Edge e=dijkstra.predEdge(n); (*outputmap)[e]=1; n=dijkstra.predNode(n); length++; } o << "Result: " << length << " long path, with cost " << dijkstra.dist(to); } else { o << "Result: failed to find shortest path between "; o << source.get_active_text() << " and " << target.get_active_text(); } resultlabel.set_text(o.str()); mapstorage->mapChanged(true, (edgemapcbts[OUTPUT])->get_active_text()); // mapstorage->changeActiveMap(true, E_COLOR, // (edgemapcbts[OUTPUT])->get_active_text()); // mapstorage->changeActiveMap(true, E_TEXT, // (edgemapcbts[INPUT])->get_active_text()); } } } void SuurballeBox::run() { if( tabcbt.get_active_text()!="" && (edgemapcbts[INPUT])->get_active_text()!="" && (edgemapcbts[OUTPUT])->get_active_text()!="" && source.get_active_text()!="" && target.get_active_text()!="" ) { const Graph &g=mapstorage->graph; Node from, to; get_from_to(from, to, (Graph&)g); std::ostringstream o; if(!(from==to)) { Graph::EdgeMap * inputmap= (mapstorage->edgemap_storage)[(edgemapcbts[INPUT])->get_active_text()]; Graph::EdgeMap * outputmap= (mapstorage->edgemap_storage)[(edgemapcbts[OUTPUT])->get_active_text()]; //zero out output map for (EdgeIt i(g); i!=INVALID; ++i) { (*outputmap)[i]=0; } Suurballe > sb((Graph&)g, *inputmap, from, to); int found=sb.run(num_set->get_value_as_int()); if(found) { for(int j=0;j path(g); sb.getPath(path, j); for(int k=0;k::EdgeIt ei; ei=path.nthEdge(k); (*outputmap)[ei]=j+1; } } o << "Result: found " << found << " paths between "; o << source.get_active_text() << " and " << target.get_active_text(); } else { o << "Result: failed to find shortest path between "; o << source.get_active_text() << " and " << target.get_active_text(); } resultlabel.set_text(o.str()); mapstorage->mapChanged(true, (edgemapcbts[OUTPUT])->get_active_text()); // mapstorage->changeActiveMap(true, E_COLOR, // (edgemapcbts[OUTPUT])->get_active_text()); // mapstorage->changeActiveMap(true, E_TEXT, // (edgemapcbts[INPUT])->get_active_text()); } } } void DijkstraBox::build_box() { //if active tab is changed, labels of graph nodes had to be loaded into comboboxes //this can be done after the maps are loaded into ComboBoxes signal_upon_maplist_updated().connect(sigc::mem_fun(*this, &DijkstraBox::maplists_updated)); addMapSelector("Cost map: ", true); addMapSelector("Edges of path here: ", true); Gtk::Label * source_label=new Gtk::Label("Source: "); Gtk::Label * target_label=new Gtk::Label("Target: "); table.attach(*source_label, 0,1,0,1); table.attach(*target_label, 0,1,1,2); table.attach(source, 1,2,0,1); table.attach(target, 1,2,1,2); pack_start(table); resultlabel.set_text("Result: algorithm is not run yet."); pack_start(resultlabel); } void SuurballeBox::build_box() { } void DijkstraBox::maplists_updated() { if(tabcbt.get_active_text()!="") { source.clear(); target.clear(); const Graph &g=mapstorage->graph; for (NodeIt i(g); i!=INVALID; ++i) { std::ostringstream text; text << (*((mapstorage->nodemap_storage)["label"]))[i]; source.prepend_text(text.str()); target.prepend_text(text.str()); } } } void DijkstraBox::get_from_to(Node & from, Node & to, Graph & g) { int assigned=0; for (NodeIt i(g); (i!=INVALID) && (assigned<2); ++i) { std::ostringstream text; text << (*((mapstorage->nodemap_storage)["label"]))[i]; if(!(text.str().compare(source.get_active_text()))) { from=i; assigned++; } if(!(text.str().compare(target.get_active_text()))) { to=i; assigned++; } } }