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