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@163: #include 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( hegyi@163: tabcbt.get_active_text()!="" && hegyi@163: (edgemapcbts[INPUT])->get_active_text()!="" && hegyi@163: (edgemapcbts[OUTPUT])->get_active_text()!="" && hegyi@163: source.get_active_text()!="" && hegyi@163: target.get_active_text()!="" hegyi@163: ) hegyi@163: { hegyi@163: const Graph &g=mapstorage->graph; hegyi@165: Node from, to; hegyi@163: hegyi@165: get_from_to(from, to, (Graph&)g); hegyi@163: hegyi@163: std::ostringstream o; hegyi@163: hegyi@163: if(!(from==to)) hegyi@163: { hegyi@165: Graph::EdgeMap * inputmap= hegyi@165: (mapstorage->edgemap_storage)[(edgemapcbts[INPUT])->get_active_text()]; hegyi@165: Graph::EdgeMap * outputmap= hegyi@165: (mapstorage->edgemap_storage)[(edgemapcbts[OUTPUT])->get_active_text()]; hegyi@165: hegyi@165: //zero out output map hegyi@165: for (EdgeIt i(g); i!=INVALID; ++i) hegyi@165: { hegyi@165: (*outputmap)[i]=0; hegyi@165: } hegyi@165: hegyi@163: Dijkstra > dijkstra(g, *inputmap); hegyi@163: dijkstra.run(from, to); hegyi@165: hegyi@163: if(dijkstra.reached(to)) hegyi@163: { hegyi@163: Node n=to; hegyi@163: int length=0; hegyi@163: while (n!=INVALID && n!=from) hegyi@163: { hegyi@163: Edge e=dijkstra.predEdge(n); hegyi@163: (*outputmap)[e]=1; hegyi@163: n=dijkstra.predNode(n); hegyi@163: length++; hegyi@163: } hegyi@163: o << "Result: " << length << " long path, with cost " << dijkstra.dist(to); hegyi@163: } hegyi@163: else hegyi@163: { hegyi@163: o << "Result: failed to find shortest path between "; hegyi@163: o << source.get_active_text() << " and " << target.get_active_text(); hegyi@163: } hegyi@163: resultlabel.set_text(o.str()); hegyi@165: hegyi@165: mapstorage->mapChanged(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@165: } hegyi@165: } 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: { hegyi@165: Graph::EdgeMap * inputmap= hegyi@165: (mapstorage->edgemap_storage)[(edgemapcbts[INPUT])->get_active_text()]; hegyi@165: Graph::EdgeMap * outputmap= hegyi@165: (mapstorage->edgemap_storage)[(edgemapcbts[OUTPUT])->get_active_text()]; hegyi@165: hegyi@165: //zero out output map hegyi@165: for (EdgeIt i(g); i!=INVALID; ++i) hegyi@165: { hegyi@165: (*outputmap)[i]=0; hegyi@165: } hegyi@165: hegyi@165: 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(g); hegyi@165: sb.getPath(path, j); hegyi@165: for(int k=0;k::EdgeIt ei; alpar@169: ei=path.nthEdge(k); hegyi@165: (*outputmap)[ei]=j+1; hegyi@165: } hegyi@165: } hegyi@165: o << "Result: found " << found << " paths between "; hegyi@165: o << source.get_active_text() << " and " << target.get_active_text(); hegyi@165: } hegyi@165: else hegyi@165: { hegyi@165: o << "Result: failed to find shortest path between "; hegyi@165: o << source.get_active_text() << " and " << target.get_active_text(); hegyi@165: } hegyi@165: resultlabel.set_text(o.str()); hegyi@165: hegyi@163: mapstorage->mapChanged(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: hegyi@163: addMapSelector("Cost map: ", true); hegyi@163: addMapSelector("Edges of path here: ", true); 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; hegyi@163: text << (*((mapstorage->nodemap_storage)["label"]))[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; hegyi@165: text << (*((mapstorage->nodemap_storage)["label"]))[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: }