alpar@174: /* -*- C++ -*- alpar@174: * alpar@174: * This file is a part of LEMON, a generic C++ optimization library alpar@174: * alpar@174: * Copyright (C) 2003-2006 alpar@174: * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport alpar@174: * (Egervary Research Group on Combinatorial Optimization, EGRES). alpar@174: * alpar@174: * Permission to use, modify and distribute this software is granted alpar@174: * provided that this copyright notice appears in all copies. For alpar@174: * precise terms see the accompanying LICENSE file. alpar@174: * alpar@174: * This software is provided "AS IS" with no warranty of any kind, alpar@174: * express or implied, and with no claim as to its suitability for any alpar@174: * purpose. alpar@174: * alpar@174: */ alpar@174: hegyi@194: #include hegyi@194: #include hegyi@194: #include hegyi@163: #include hegyi@194: hegyi@165: #include hegyi@165: #include hegyi@165: #include hegyi@163: hegyi@163: enum {INPUT, OUTPUT, MAP_NUM}; hegyi@163: hegyi@163: DijkstraBox::DijkstraBox(std::vector t):AlgoBox() hegyi@163: { hegyi@163: init(t); hegyi@163: } hegyi@163: hegyi@165: SuurballeBox::SuurballeBox(std::vector t):DijkstraBox(t) hegyi@165: { hegyi@165: Gtk::Adjustment * adjustment=new Gtk::Adjustment(2, 1, 20, 1, 5); hegyi@165: num_set = new Gtk::SpinButton(*adjustment, 5,0); hegyi@165: hegyi@165: Gtk::Label * label=new Gtk::Label("No. of paths to find: "); hegyi@165: // hbox.pack_start(*label); hegyi@165: // hbox.pack_start(*num_set); hegyi@165: // hbox.show_all_children(); hegyi@165: hegyi@165: table.attach(*label, 0,1,2,3); hegyi@165: table.attach(*num_set, 1,2,2,3); hegyi@165: hegyi@165: hegyi@165: // pack_start(hbox); hegyi@165: } hegyi@165: hegyi@163: void DijkstraBox::run() hegyi@163: { hegyi@163: if( ladanyi@201: tabcbt.get_active_text()!="" && ladanyi@201: (edgemapcbts[INPUT])->get_active_text()!="" && ladanyi@201: (edgemapcbts[OUTPUT])->get_active_text()!="" && ladanyi@201: source.get_active_text()!="" && ladanyi@201: target.get_active_text()!="" ladanyi@201: ) ladanyi@201: { ladanyi@201: const Graph &g=mapstorage->graph; ladanyi@201: Node from, to; ladanyi@201: ladanyi@201: get_from_to(from, to, (Graph&)g); ladanyi@201: ladanyi@201: std::ostringstream o; ladanyi@201: ladanyi@201: if(!(from==to)) hegyi@163: { ladanyi@201: std::string inputmapName = edgemapcbts[INPUT]->get_active_text(); ladanyi@201: std::string outputmapName = edgemapcbts[OUTPUT]->get_active_text(); hegyi@163: ladanyi@201: MapStorage::NumericEdgeMap& inputmap = mapstorage->getNumericEdgeMap(inputmapName); ladanyi@201: MapStorage::NumericEdgeMap& outputmap = mapstorage->getNumericEdgeMap(outputmapName); hegyi@163: ladanyi@201: //zero out output map ladanyi@201: for (EdgeIt i(g); i!=INVALID; ++i) ladanyi@201: { ladanyi@201: outputmap[i]=0; ladanyi@201: } hegyi@163: ladanyi@201: Dijkstra dijkstra(g, inputmap); ladanyi@201: dijkstra.run(from, to); hegyi@165: ladanyi@201: if(dijkstra.reached(to)) ladanyi@201: { ladanyi@201: Node n=to; ladanyi@201: int length=0; ladanyi@201: while (n!=INVALID && n!=from) ladanyi@201: { ladanyi@201: Edge e=dijkstra.predEdge(n); ladanyi@201: outputmap[e]=1; ladanyi@201: n=dijkstra.predNode(n); ladanyi@201: length++; ladanyi@201: } ladanyi@201: o << "Result: " << length << " long path, with cost " << dijkstra.dist(to); ladanyi@201: } ladanyi@201: else ladanyi@201: { ladanyi@201: o << "Result: failed to find shortest path between "; ladanyi@201: o << source.get_active_text() << " and " << target.get_active_text(); ladanyi@201: } ladanyi@201: resultlabel.set_text(o.str()); ladanyi@201: ladanyi@201: mapstorage->mapChanged(true, (edgemapcbts[OUTPUT])->get_active_text()); ladanyi@201: // mapstorage->changeActiveMap(true, E_COLOR, ladanyi@201: // (edgemapcbts[OUTPUT])->get_active_text()); ladanyi@201: // mapstorage->changeActiveMap(true, E_TEXT, ladanyi@201: // (edgemapcbts[INPUT])->get_active_text()); hegyi@165: } ladanyi@201: } hegyi@165: } hegyi@163: hegyi@165: void SuurballeBox::run() hegyi@165: { hegyi@165: if( hegyi@165: tabcbt.get_active_text()!="" && hegyi@165: (edgemapcbts[INPUT])->get_active_text()!="" && hegyi@165: (edgemapcbts[OUTPUT])->get_active_text()!="" && hegyi@165: source.get_active_text()!="" && hegyi@165: target.get_active_text()!="" hegyi@165: ) hegyi@165: { hegyi@165: const Graph &g=mapstorage->graph; hegyi@165: Node from, to; hegyi@165: hegyi@165: get_from_to(from, to, (Graph&)g); hegyi@165: hegyi@165: std::ostringstream o; hegyi@165: hegyi@165: if(!(from==to)) hegyi@165: { ladanyi@201: MapStorage::NumericEdgeMap& inputmap= ladanyi@201: mapstorage->getNumericEdgeMap(edgemapcbts[INPUT]->get_active_text()); ladanyi@201: MapStorage::NumericEdgeMap& outputmap= ladanyi@201: mapstorage->getNumericEdgeMap(edgemapcbts[OUTPUT]->get_active_text()); hegyi@165: hegyi@165: //zero out output map hegyi@165: for (EdgeIt i(g); i!=INVALID; ++i) hegyi@165: { ladanyi@201: outputmap[i]=0; hegyi@165: } hegyi@165: ladanyi@201: Suurballe sb((Graph&)g, inputmap, from, to); hegyi@165: hegyi@165: int found=sb.run(num_set->get_value_as_int()); hegyi@165: if(found) hegyi@165: { hegyi@165: for(int j=0;j path; alpar@188: path=sb.path(j); hegyi@165: for(int k=0;kmapChanged(true, (edgemapcbts[OUTPUT])->get_active_text()); hegyi@165: // mapstorage->changeActiveMap(true, E_COLOR, hegyi@165: // (edgemapcbts[OUTPUT])->get_active_text()); hegyi@165: // mapstorage->changeActiveMap(true, E_TEXT, hegyi@165: // (edgemapcbts[INPUT])->get_active_text()); hegyi@163: } hegyi@163: } hegyi@163: } hegyi@163: hegyi@163: void DijkstraBox::build_box() hegyi@163: { hegyi@163: //if active tab is changed, labels of graph nodes had to be loaded into comboboxes hegyi@163: //this can be done after the maps are loaded into ComboBoxes hegyi@163: signal_upon_maplist_updated().connect(sigc::mem_fun(*this, &DijkstraBox::maplists_updated)); hegyi@163: ladanyi@201: addMapSelector("Cost map: ", true, NUM); ladanyi@201: addMapSelector("Edges of path here: ", true, NUM); hegyi@163: hegyi@163: Gtk::Label * source_label=new Gtk::Label("Source: "); hegyi@163: Gtk::Label * target_label=new Gtk::Label("Target: "); hegyi@163: hegyi@163: table.attach(*source_label, 0,1,0,1); hegyi@163: table.attach(*target_label, 0,1,1,2); hegyi@163: table.attach(source, 1,2,0,1); hegyi@163: table.attach(target, 1,2,1,2); hegyi@163: hegyi@163: hegyi@163: pack_start(table); hegyi@163: hegyi@163: resultlabel.set_text("Result: algorithm is not run yet."); hegyi@163: pack_start(resultlabel); hegyi@163: } hegyi@163: hegyi@165: void SuurballeBox::build_box() hegyi@165: { hegyi@165: } hegyi@165: hegyi@163: void DijkstraBox::maplists_updated() hegyi@163: { hegyi@163: if(tabcbt.get_active_text()!="") hegyi@163: { hegyi@163: source.clear(); hegyi@163: target.clear(); hegyi@163: const Graph &g=mapstorage->graph; hegyi@163: for (NodeIt i(g); i!=INVALID; ++i) hegyi@163: { hegyi@163: std::ostringstream text; ladanyi@201: text << mapstorage->getLabel(i); hegyi@163: source.prepend_text(text.str()); hegyi@163: target.prepend_text(text.str()); hegyi@163: } hegyi@163: } hegyi@163: } hegyi@165: hegyi@165: void DijkstraBox::get_from_to(Node & from, Node & to, Graph & g) hegyi@165: { hegyi@165: int assigned=0; hegyi@165: for (NodeIt i(g); (i!=INVALID) && (assigned<2); ++i) hegyi@165: { hegyi@165: std::ostringstream text; ladanyi@201: text << mapstorage->getLabel(i); hegyi@165: if(!(text.str().compare(source.get_active_text()))) hegyi@165: { hegyi@165: from=i; hegyi@165: assigned++; hegyi@165: } hegyi@165: if(!(text.str().compare(target.get_active_text()))) hegyi@165: { hegyi@165: to=i; hegyi@165: assigned++; hegyi@165: } hegyi@165: } hegyi@165: }