dijkstrabox.cc
author alpar
Mon, 30 Oct 2006 12:16:25 +0000
changeset 174 95872af46fc4
parent 169 c223ffdbdb5c
child 188 c1c9fcf03e94
permissions -rw-r--r--
Add copyright headers
alpar@174
     1
/* -*- C++ -*-
alpar@174
     2
 *
alpar@174
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@174
     4
 *
alpar@174
     5
 * Copyright (C) 2003-2006
alpar@174
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@174
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@174
     8
 *
alpar@174
     9
 * Permission to use, modify and distribute this software is granted
alpar@174
    10
 * provided that this copyright notice appears in all copies. For
alpar@174
    11
 * precise terms see the accompanying LICENSE file.
alpar@174
    12
 *
alpar@174
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@174
    14
 * express or implied, and with no claim as to its suitability for any
alpar@174
    15
 * purpose.
alpar@174
    16
 *
alpar@174
    17
 */
alpar@174
    18
hegyi@163
    19
#include <dijkstrabox.h>
hegyi@165
    20
#include <lemon/dijkstra.h>
hegyi@165
    21
#include <lemon/suurballe.h>
hegyi@165
    22
#include <lemon/path.h>
hegyi@163
    23
hegyi@163
    24
enum {INPUT, OUTPUT, MAP_NUM};
hegyi@163
    25
hegyi@163
    26
DijkstraBox::DijkstraBox(std::vector<std::string> t):AlgoBox()
hegyi@163
    27
{
hegyi@163
    28
  init(t);
hegyi@163
    29
}
hegyi@163
    30
    
hegyi@165
    31
SuurballeBox::SuurballeBox(std::vector<std::string> t):DijkstraBox(t)
hegyi@165
    32
{
hegyi@165
    33
  Gtk::Adjustment * adjustment=new Gtk::Adjustment(2, 1, 20, 1, 5);
hegyi@165
    34
  num_set = new Gtk::SpinButton(*adjustment, 5,0);
hegyi@165
    35
hegyi@165
    36
  Gtk::Label * label=new Gtk::Label("No. of paths to find: ");
hegyi@165
    37
//   hbox.pack_start(*label);
hegyi@165
    38
//   hbox.pack_start(*num_set);
hegyi@165
    39
//   hbox.show_all_children();
hegyi@165
    40
hegyi@165
    41
  table.attach(*label, 0,1,2,3);
hegyi@165
    42
  table.attach(*num_set, 1,2,2,3);
hegyi@165
    43
hegyi@165
    44
hegyi@165
    45
//   pack_start(hbox);
hegyi@165
    46
}
hegyi@165
    47
    
hegyi@163
    48
void DijkstraBox::run()
hegyi@163
    49
{
hegyi@163
    50
  if(
hegyi@163
    51
     tabcbt.get_active_text()!="" &&
hegyi@163
    52
     (edgemapcbts[INPUT])->get_active_text()!="" &&
hegyi@163
    53
     (edgemapcbts[OUTPUT])->get_active_text()!="" &&
hegyi@163
    54
     source.get_active_text()!="" &&
hegyi@163
    55
     target.get_active_text()!=""
hegyi@163
    56
     )
hegyi@163
    57
    {
hegyi@163
    58
      const Graph &g=mapstorage->graph;
hegyi@165
    59
      Node from, to;
hegyi@163
    60
hegyi@165
    61
      get_from_to(from, to, (Graph&)g);
hegyi@163
    62
hegyi@163
    63
      std::ostringstream o;
hegyi@163
    64
hegyi@163
    65
      if(!(from==to))
hegyi@163
    66
	{
hegyi@165
    67
	  Graph::EdgeMap<double> * inputmap=
hegyi@165
    68
	    (mapstorage->edgemap_storage)[(edgemapcbts[INPUT])->get_active_text()];
hegyi@165
    69
	  Graph::EdgeMap<double> * outputmap=
hegyi@165
    70
	    (mapstorage->edgemap_storage)[(edgemapcbts[OUTPUT])->get_active_text()];
hegyi@165
    71
hegyi@165
    72
	  //zero out output map
hegyi@165
    73
	  for (EdgeIt i(g); i!=INVALID; ++i)
hegyi@165
    74
	    {
hegyi@165
    75
	      (*outputmap)[i]=0;
hegyi@165
    76
	    }
hegyi@165
    77
	  
hegyi@163
    78
	  Dijkstra<Graph, Graph::EdgeMap<double> > dijkstra(g, *inputmap);
hegyi@163
    79
	  dijkstra.run(from, to);
hegyi@165
    80
	  
hegyi@163
    81
	  if(dijkstra.reached(to))
hegyi@163
    82
	    {
hegyi@163
    83
	      Node n=to;
hegyi@163
    84
	      int length=0;
hegyi@163
    85
	      while (n!=INVALID && n!=from)
hegyi@163
    86
		{
hegyi@163
    87
		  Edge e=dijkstra.predEdge(n);
hegyi@163
    88
		  (*outputmap)[e]=1;
hegyi@163
    89
		  n=dijkstra.predNode(n);
hegyi@163
    90
		  length++;
hegyi@163
    91
		}
hegyi@163
    92
	      o << "Result: " << length << " long path, with cost " << dijkstra.dist(to);
hegyi@163
    93
	    }
hegyi@163
    94
	  else
hegyi@163
    95
	    {
hegyi@163
    96
	      o << "Result: failed to find shortest path between ";
hegyi@163
    97
	      o << source.get_active_text() << " and " << target.get_active_text();
hegyi@163
    98
	    }
hegyi@163
    99
	  resultlabel.set_text(o.str());
hegyi@165
   100
	  
hegyi@165
   101
	  mapstorage->mapChanged(true, (edgemapcbts[OUTPUT])->get_active_text());
hegyi@165
   102
	  //   mapstorage->changeActiveMap(true, E_COLOR,
hegyi@165
   103
	  // 			      (edgemapcbts[OUTPUT])->get_active_text());
hegyi@165
   104
	  //   mapstorage->changeActiveMap(true, E_TEXT,
hegyi@165
   105
	  // 			      (edgemapcbts[INPUT])->get_active_text());
hegyi@165
   106
	}
hegyi@165
   107
    }
hegyi@165
   108
}
hegyi@163
   109
hegyi@165
   110
void SuurballeBox::run()
hegyi@165
   111
{
hegyi@165
   112
  if(
hegyi@165
   113
     tabcbt.get_active_text()!="" &&
hegyi@165
   114
     (edgemapcbts[INPUT])->get_active_text()!="" &&
hegyi@165
   115
     (edgemapcbts[OUTPUT])->get_active_text()!="" &&
hegyi@165
   116
     source.get_active_text()!="" &&
hegyi@165
   117
     target.get_active_text()!=""
hegyi@165
   118
     )
hegyi@165
   119
    {
hegyi@165
   120
      const Graph &g=mapstorage->graph;
hegyi@165
   121
      Node from, to;
hegyi@165
   122
hegyi@165
   123
      get_from_to(from, to, (Graph&)g);
hegyi@165
   124
hegyi@165
   125
      std::ostringstream o;
hegyi@165
   126
hegyi@165
   127
      if(!(from==to))
hegyi@165
   128
	{
hegyi@165
   129
	  Graph::EdgeMap<double> * inputmap=
hegyi@165
   130
	    (mapstorage->edgemap_storage)[(edgemapcbts[INPUT])->get_active_text()];
hegyi@165
   131
	  Graph::EdgeMap<double> * outputmap=
hegyi@165
   132
	    (mapstorage->edgemap_storage)[(edgemapcbts[OUTPUT])->get_active_text()];
hegyi@165
   133
hegyi@165
   134
	  //zero out output map
hegyi@165
   135
	  for (EdgeIt i(g); i!=INVALID; ++i)
hegyi@165
   136
	    {
hegyi@165
   137
	      (*outputmap)[i]=0;
hegyi@165
   138
	    }
hegyi@165
   139
	  
hegyi@165
   140
	  Suurballe<Graph, Graph::EdgeMap<double> > sb((Graph&)g, *inputmap, from, to);
hegyi@165
   141
	  
hegyi@165
   142
	  int found=sb.run(num_set->get_value_as_int());
hegyi@165
   143
	  if(found)
hegyi@165
   144
	    {
hegyi@165
   145
	      for(int j=0;j<found;j++)
hegyi@165
   146
		{
alpar@169
   147
		  Path<Graph> path(g);
hegyi@165
   148
		  sb.getPath(path, j);
hegyi@165
   149
		  for(int k=0;k<path.length();k++)
hegyi@165
   150
		    {
alpar@169
   151
		      Path<Graph>::EdgeIt ei;
alpar@169
   152
		      ei=path.nthEdge(k);
hegyi@165
   153
		      (*outputmap)[ei]=j+1;
hegyi@165
   154
		    }
hegyi@165
   155
		}
hegyi@165
   156
	      o << "Result: found " << found << " paths between ";
hegyi@165
   157
	      o << source.get_active_text() << " and " << target.get_active_text();
hegyi@165
   158
	    }
hegyi@165
   159
	  else
hegyi@165
   160
	    {
hegyi@165
   161
	      o << "Result: failed to find shortest path between ";
hegyi@165
   162
	      o << source.get_active_text() << " and " << target.get_active_text();
hegyi@165
   163
	    }
hegyi@165
   164
	  resultlabel.set_text(o.str());
hegyi@165
   165
	  
hegyi@163
   166
	  mapstorage->mapChanged(true, (edgemapcbts[OUTPUT])->get_active_text());
hegyi@165
   167
	  //   mapstorage->changeActiveMap(true, E_COLOR,
hegyi@165
   168
	  // 			      (edgemapcbts[OUTPUT])->get_active_text());
hegyi@165
   169
	  //   mapstorage->changeActiveMap(true, E_TEXT,
hegyi@165
   170
	  // 			      (edgemapcbts[INPUT])->get_active_text());
hegyi@163
   171
	}
hegyi@163
   172
    }
hegyi@163
   173
}
hegyi@163
   174
    
hegyi@163
   175
void DijkstraBox::build_box()
hegyi@163
   176
{
hegyi@163
   177
  //if active tab is changed, labels of graph nodes had to be loaded into comboboxes
hegyi@163
   178
  //this can be done after the maps are loaded into ComboBoxes
hegyi@163
   179
  signal_upon_maplist_updated().connect(sigc::mem_fun(*this, &DijkstraBox::maplists_updated));
hegyi@163
   180
hegyi@163
   181
  addMapSelector("Cost map: ", true);
hegyi@163
   182
  addMapSelector("Edges of path here: ", true);
hegyi@163
   183
hegyi@163
   184
  Gtk::Label * source_label=new Gtk::Label("Source: ");
hegyi@163
   185
  Gtk::Label * target_label=new Gtk::Label("Target: ");
hegyi@163
   186
hegyi@163
   187
  table.attach(*source_label, 0,1,0,1);
hegyi@163
   188
  table.attach(*target_label, 0,1,1,2);
hegyi@163
   189
  table.attach(source, 1,2,0,1);
hegyi@163
   190
  table.attach(target, 1,2,1,2);
hegyi@163
   191
hegyi@163
   192
hegyi@163
   193
  pack_start(table);
hegyi@163
   194
hegyi@163
   195
  resultlabel.set_text("Result: algorithm is not run yet.");
hegyi@163
   196
  pack_start(resultlabel);
hegyi@163
   197
}
hegyi@163
   198
hegyi@165
   199
void SuurballeBox::build_box()
hegyi@165
   200
{
hegyi@165
   201
}
hegyi@165
   202
hegyi@163
   203
void DijkstraBox::maplists_updated()
hegyi@163
   204
{
hegyi@163
   205
  if(tabcbt.get_active_text()!="")
hegyi@163
   206
    {
hegyi@163
   207
      source.clear();
hegyi@163
   208
      target.clear();
hegyi@163
   209
      const Graph &g=mapstorage->graph;
hegyi@163
   210
      for (NodeIt i(g); i!=INVALID; ++i)
hegyi@163
   211
	{
hegyi@163
   212
	  std::ostringstream text;
hegyi@163
   213
	  text << (*((mapstorage->nodemap_storage)["label"]))[i];
hegyi@163
   214
	  source.prepend_text(text.str());
hegyi@163
   215
	  target.prepend_text(text.str());
hegyi@163
   216
	}
hegyi@163
   217
    }
hegyi@163
   218
}
hegyi@165
   219
hegyi@165
   220
void DijkstraBox::get_from_to(Node & from, Node & to, Graph & g)
hegyi@165
   221
{
hegyi@165
   222
  int assigned=0;
hegyi@165
   223
  for (NodeIt i(g); (i!=INVALID) && (assigned<2); ++i)
hegyi@165
   224
    {
hegyi@165
   225
      std::ostringstream text;
hegyi@165
   226
      text << (*((mapstorage->nodemap_storage)["label"]))[i];
hegyi@165
   227
      if(!(text.str().compare(source.get_active_text())))
hegyi@165
   228
	{
hegyi@165
   229
	  from=i;
hegyi@165
   230
	  assigned++;
hegyi@165
   231
	}
hegyi@165
   232
      if(!(text.str().compare(target.get_active_text())))
hegyi@165
   233
	{
hegyi@165
   234
	  to=i;
hegyi@165
   235
	  assigned++;
hegyi@165
   236
	}
hegyi@165
   237
    }
hegyi@165
   238
}