dijkstrabox.cc
changeset 201 879e47e5b731
parent 194 6b2b718420eb
equal deleted inserted replaced
5:896d8068f002 6:91540da10e6a
    48 
    48 
    49 //   pack_start(hbox);
    49 //   pack_start(hbox);
    50 }
    50 }
    51     
    51     
    52 void DijkstraBox::run()
    52 void DijkstraBox::run()
       
    53 {
       
    54   if(
       
    55       tabcbt.get_active_text()!="" &&
       
    56       (edgemapcbts[INPUT])->get_active_text()!="" &&
       
    57       (edgemapcbts[OUTPUT])->get_active_text()!="" &&
       
    58       source.get_active_text()!="" &&
       
    59       target.get_active_text()!=""
       
    60     )
       
    61   {
       
    62     const Graph &g=mapstorage->graph;
       
    63     Node from, to;
       
    64 
       
    65     get_from_to(from, to, (Graph&)g);
       
    66 
       
    67     std::ostringstream o;
       
    68 
       
    69     if(!(from==to))
       
    70     {
       
    71       std::string inputmapName = edgemapcbts[INPUT]->get_active_text();
       
    72       std::string outputmapName = edgemapcbts[OUTPUT]->get_active_text();
       
    73 
       
    74       MapStorage::NumericEdgeMap& inputmap = mapstorage->getNumericEdgeMap(inputmapName);
       
    75       MapStorage::NumericEdgeMap& outputmap = mapstorage->getNumericEdgeMap(outputmapName);
       
    76 
       
    77       //zero out output map
       
    78       for (EdgeIt i(g); i!=INVALID; ++i)
       
    79       {
       
    80         outputmap[i]=0;
       
    81       }
       
    82 
       
    83       Dijkstra<Graph, MapStorage::NumericEdgeMap > dijkstra(g, inputmap);
       
    84       dijkstra.run(from, to);
       
    85 
       
    86       if(dijkstra.reached(to))
       
    87       {
       
    88         Node n=to;
       
    89         int length=0;
       
    90         while (n!=INVALID && n!=from)
       
    91         {
       
    92           Edge e=dijkstra.predEdge(n);
       
    93           outputmap[e]=1;
       
    94           n=dijkstra.predNode(n);
       
    95           length++;
       
    96         }
       
    97         o << "Result: " << length << " long path, with cost " << dijkstra.dist(to);
       
    98       }
       
    99       else
       
   100       {
       
   101         o << "Result: failed to find shortest path between ";
       
   102         o << source.get_active_text() << " and " << target.get_active_text();
       
   103       }
       
   104       resultlabel.set_text(o.str());
       
   105 
       
   106       mapstorage->mapChanged(true, (edgemapcbts[OUTPUT])->get_active_text());
       
   107       //   mapstorage->changeActiveMap(true, E_COLOR,
       
   108       // 			      (edgemapcbts[OUTPUT])->get_active_text());
       
   109       //   mapstorage->changeActiveMap(true, E_TEXT,
       
   110       // 			      (edgemapcbts[INPUT])->get_active_text());
       
   111     }
       
   112   }
       
   113 }
       
   114 
       
   115 void SuurballeBox::run()
    53 {
   116 {
    54   if(
   117   if(
    55      tabcbt.get_active_text()!="" &&
   118      tabcbt.get_active_text()!="" &&
    56      (edgemapcbts[INPUT])->get_active_text()!="" &&
   119      (edgemapcbts[INPUT])->get_active_text()!="" &&
    57      (edgemapcbts[OUTPUT])->get_active_text()!="" &&
   120      (edgemapcbts[OUTPUT])->get_active_text()!="" &&
    66 
   129 
    67       std::ostringstream o;
   130       std::ostringstream o;
    68 
   131 
    69       if(!(from==to))
   132       if(!(from==to))
    70 	{
   133 	{
    71 	  Graph::EdgeMap<double> * inputmap=
   134           MapStorage::NumericEdgeMap& inputmap=
    72 	    (mapstorage->edgemap_storage)[(edgemapcbts[INPUT])->get_active_text()];
   135 	    mapstorage->getNumericEdgeMap(edgemapcbts[INPUT]->get_active_text());
    73 	  Graph::EdgeMap<double> * outputmap=
   136           MapStorage::NumericEdgeMap& outputmap=
    74 	    (mapstorage->edgemap_storage)[(edgemapcbts[OUTPUT])->get_active_text()];
   137 	    mapstorage->getNumericEdgeMap(edgemapcbts[OUTPUT]->get_active_text());
    75 
   138 
    76 	  //zero out output map
   139 	  //zero out output map
    77 	  for (EdgeIt i(g); i!=INVALID; ++i)
   140 	  for (EdgeIt i(g); i!=INVALID; ++i)
    78 	    {
   141 	    {
    79 	      (*outputmap)[i]=0;
   142 	      outputmap[i]=0;
    80 	    }
   143 	    }
    81 	  
   144 	  
    82 	  Dijkstra<Graph, Graph::EdgeMap<double> > dijkstra(g, *inputmap);
   145 	  Suurballe<Graph, MapStorage::NumericEdgeMap > sb((Graph&)g, inputmap, from, to);
    83 	  dijkstra.run(from, to);
       
    84 	  
       
    85 	  if(dijkstra.reached(to))
       
    86 	    {
       
    87 	      Node n=to;
       
    88 	      int length=0;
       
    89 	      while (n!=INVALID && n!=from)
       
    90 		{
       
    91 		  Edge e=dijkstra.predEdge(n);
       
    92 		  (*outputmap)[e]=1;
       
    93 		  n=dijkstra.predNode(n);
       
    94 		  length++;
       
    95 		}
       
    96 	      o << "Result: " << length << " long path, with cost " << dijkstra.dist(to);
       
    97 	    }
       
    98 	  else
       
    99 	    {
       
   100 	      o << "Result: failed to find shortest path between ";
       
   101 	      o << source.get_active_text() << " and " << target.get_active_text();
       
   102 	    }
       
   103 	  resultlabel.set_text(o.str());
       
   104 	  
       
   105 	  mapstorage->mapChanged(true, (edgemapcbts[OUTPUT])->get_active_text());
       
   106 	  //   mapstorage->changeActiveMap(true, E_COLOR,
       
   107 	  // 			      (edgemapcbts[OUTPUT])->get_active_text());
       
   108 	  //   mapstorage->changeActiveMap(true, E_TEXT,
       
   109 	  // 			      (edgemapcbts[INPUT])->get_active_text());
       
   110 	}
       
   111     }
       
   112 }
       
   113 
       
   114 void SuurballeBox::run()
       
   115 {
       
   116   if(
       
   117      tabcbt.get_active_text()!="" &&
       
   118      (edgemapcbts[INPUT])->get_active_text()!="" &&
       
   119      (edgemapcbts[OUTPUT])->get_active_text()!="" &&
       
   120      source.get_active_text()!="" &&
       
   121      target.get_active_text()!=""
       
   122      )
       
   123     {
       
   124       const Graph &g=mapstorage->graph;
       
   125       Node from, to;
       
   126 
       
   127       get_from_to(from, to, (Graph&)g);
       
   128 
       
   129       std::ostringstream o;
       
   130 
       
   131       if(!(from==to))
       
   132 	{
       
   133 	  Graph::EdgeMap<double> * inputmap=
       
   134 	    (mapstorage->edgemap_storage)[(edgemapcbts[INPUT])->get_active_text()];
       
   135 	  Graph::EdgeMap<double> * outputmap=
       
   136 	    (mapstorage->edgemap_storage)[(edgemapcbts[OUTPUT])->get_active_text()];
       
   137 
       
   138 	  //zero out output map
       
   139 	  for (EdgeIt i(g); i!=INVALID; ++i)
       
   140 	    {
       
   141 	      (*outputmap)[i]=0;
       
   142 	    }
       
   143 	  
       
   144 	  Suurballe<Graph, Graph::EdgeMap<double> > sb((Graph&)g, *inputmap, from, to);
       
   145 	  
   146 	  
   146 	  int found=sb.run(num_set->get_value_as_int());
   147 	  int found=sb.run(num_set->get_value_as_int());
   147 	  if(found)
   148 	  if(found)
   148 	    {
   149 	    {
   149 	      for(int j=0;j<found;j++)
   150 	      for(int j=0;j<found;j++)
   150 		{
   151 		{
   151 		  Path<Graph> path;
   152 		  Path<Graph> path;
   152 		  path=sb.path(j);
   153 		  path=sb.path(j);
   153 		  for(int k=0;k<path.length();k++)
   154 		  for(int k=0;k<path.length();k++)
   154 		    {
   155 		    {
   155 		      (*outputmap)[path.nth(k)]=j+1;
   156 		      outputmap[path.nth(k)]=j+1;
   156 		    }
   157 		    }
   157 		}
   158 		}
   158 	      o << "Result: found " << found << " paths between ";
   159 	      o << "Result: found " << found << " paths between ";
   159 	      o << source.get_active_text() << " and " << target.get_active_text();
   160 	      o << source.get_active_text() << " and " << target.get_active_text();
   160 	    }
   161 	    }
   178 {
   179 {
   179   //if active tab is changed, labels of graph nodes had to be loaded into comboboxes
   180   //if active tab is changed, labels of graph nodes had to be loaded into comboboxes
   180   //this can be done after the maps are loaded into ComboBoxes
   181   //this can be done after the maps are loaded into ComboBoxes
   181   signal_upon_maplist_updated().connect(sigc::mem_fun(*this, &DijkstraBox::maplists_updated));
   182   signal_upon_maplist_updated().connect(sigc::mem_fun(*this, &DijkstraBox::maplists_updated));
   182 
   183 
   183   addMapSelector("Cost map: ", true);
   184   addMapSelector("Cost map: ", true, NUM);
   184   addMapSelector("Edges of path here: ", true);
   185   addMapSelector("Edges of path here: ", true, NUM);
   185 
   186 
   186   Gtk::Label * source_label=new Gtk::Label("Source: ");
   187   Gtk::Label * source_label=new Gtk::Label("Source: ");
   187   Gtk::Label * target_label=new Gtk::Label("Target: ");
   188   Gtk::Label * target_label=new Gtk::Label("Target: ");
   188 
   189 
   189   table.attach(*source_label, 0,1,0,1);
   190   table.attach(*source_label, 0,1,0,1);
   210       target.clear();
   211       target.clear();
   211       const Graph &g=mapstorage->graph;
   212       const Graph &g=mapstorage->graph;
   212       for (NodeIt i(g); i!=INVALID; ++i)
   213       for (NodeIt i(g); i!=INVALID; ++i)
   213 	{
   214 	{
   214 	  std::ostringstream text;
   215 	  std::ostringstream text;
   215 	  text << (*((mapstorage->nodemap_storage)["label"]))[i];
   216 	  text << mapstorage->getLabel(i);
   216 	  source.prepend_text(text.str());
   217 	  source.prepend_text(text.str());
   217 	  target.prepend_text(text.str());
   218 	  target.prepend_text(text.str());
   218 	}
   219 	}
   219     }
   220     }
   220 }
   221 }
   223 {
   224 {
   224   int assigned=0;
   225   int assigned=0;
   225   for (NodeIt i(g); (i!=INVALID) && (assigned<2); ++i)
   226   for (NodeIt i(g); (i!=INVALID) && (assigned<2); ++i)
   226     {
   227     {
   227       std::ostringstream text;
   228       std::ostringstream text;
   228       text << (*((mapstorage->nodemap_storage)["label"]))[i];
   229       text << mapstorage->getLabel(i);
   229       if(!(text.str().compare(source.get_active_text())))
   230       if(!(text.str().compare(source.get_active_text())))
   230 	{
   231 	{
   231 	  from=i;
   232 	  from=i;
   232 	  assigned++;
   233 	  assigned++;
   233 	}
   234 	}