dijkstrabox.cc
author ladanyi
Wed, 02 Jan 2008 21:03:09 +0000
changeset 201 879e47e5b731
parent 194 6b2b718420eb
permissions -rw-r--r--
Merge branches/akos to trunk.
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(
ladanyi@201
    55
      tabcbt.get_active_text()!="" &&
ladanyi@201
    56
      (edgemapcbts[INPUT])->get_active_text()!="" &&
ladanyi@201
    57
      (edgemapcbts[OUTPUT])->get_active_text()!="" &&
ladanyi@201
    58
      source.get_active_text()!="" &&
ladanyi@201
    59
      target.get_active_text()!=""
ladanyi@201
    60
    )
ladanyi@201
    61
  {
ladanyi@201
    62
    const Graph &g=mapstorage->graph;
ladanyi@201
    63
    Node from, to;
ladanyi@201
    64
ladanyi@201
    65
    get_from_to(from, to, (Graph&)g);
ladanyi@201
    66
ladanyi@201
    67
    std::ostringstream o;
ladanyi@201
    68
ladanyi@201
    69
    if(!(from==to))
hegyi@163
    70
    {
ladanyi@201
    71
      std::string inputmapName = edgemapcbts[INPUT]->get_active_text();
ladanyi@201
    72
      std::string outputmapName = edgemapcbts[OUTPUT]->get_active_text();
hegyi@163
    73
ladanyi@201
    74
      MapStorage::NumericEdgeMap& inputmap = mapstorage->getNumericEdgeMap(inputmapName);
ladanyi@201
    75
      MapStorage::NumericEdgeMap& outputmap = mapstorage->getNumericEdgeMap(outputmapName);
hegyi@163
    76
ladanyi@201
    77
      //zero out output map
ladanyi@201
    78
      for (EdgeIt i(g); i!=INVALID; ++i)
ladanyi@201
    79
      {
ladanyi@201
    80
        outputmap[i]=0;
ladanyi@201
    81
      }
hegyi@163
    82
ladanyi@201
    83
      Dijkstra<Graph, MapStorage::NumericEdgeMap > dijkstra(g, inputmap);
ladanyi@201
    84
      dijkstra.run(from, to);
hegyi@165
    85
ladanyi@201
    86
      if(dijkstra.reached(to))
ladanyi@201
    87
      {
ladanyi@201
    88
        Node n=to;
ladanyi@201
    89
        int length=0;
ladanyi@201
    90
        while (n!=INVALID && n!=from)
ladanyi@201
    91
        {
ladanyi@201
    92
          Edge e=dijkstra.predEdge(n);
ladanyi@201
    93
          outputmap[e]=1;
ladanyi@201
    94
          n=dijkstra.predNode(n);
ladanyi@201
    95
          length++;
ladanyi@201
    96
        }
ladanyi@201
    97
        o << "Result: " << length << " long path, with cost " << dijkstra.dist(to);
ladanyi@201
    98
      }
ladanyi@201
    99
      else
ladanyi@201
   100
      {
ladanyi@201
   101
        o << "Result: failed to find shortest path between ";
ladanyi@201
   102
        o << source.get_active_text() << " and " << target.get_active_text();
ladanyi@201
   103
      }
ladanyi@201
   104
      resultlabel.set_text(o.str());
ladanyi@201
   105
ladanyi@201
   106
      mapstorage->mapChanged(true, (edgemapcbts[OUTPUT])->get_active_text());
ladanyi@201
   107
      //   mapstorage->changeActiveMap(true, E_COLOR,
ladanyi@201
   108
      // 			      (edgemapcbts[OUTPUT])->get_active_text());
ladanyi@201
   109
      //   mapstorage->changeActiveMap(true, E_TEXT,
ladanyi@201
   110
      // 			      (edgemapcbts[INPUT])->get_active_text());
hegyi@165
   111
    }
ladanyi@201
   112
  }
hegyi@165
   113
}
hegyi@163
   114
hegyi@165
   115
void SuurballeBox::run()
hegyi@165
   116
{
hegyi@165
   117
  if(
hegyi@165
   118
     tabcbt.get_active_text()!="" &&
hegyi@165
   119
     (edgemapcbts[INPUT])->get_active_text()!="" &&
hegyi@165
   120
     (edgemapcbts[OUTPUT])->get_active_text()!="" &&
hegyi@165
   121
     source.get_active_text()!="" &&
hegyi@165
   122
     target.get_active_text()!=""
hegyi@165
   123
     )
hegyi@165
   124
    {
hegyi@165
   125
      const Graph &g=mapstorage->graph;
hegyi@165
   126
      Node from, to;
hegyi@165
   127
hegyi@165
   128
      get_from_to(from, to, (Graph&)g);
hegyi@165
   129
hegyi@165
   130
      std::ostringstream o;
hegyi@165
   131
hegyi@165
   132
      if(!(from==to))
hegyi@165
   133
	{
ladanyi@201
   134
          MapStorage::NumericEdgeMap& inputmap=
ladanyi@201
   135
	    mapstorage->getNumericEdgeMap(edgemapcbts[INPUT]->get_active_text());
ladanyi@201
   136
          MapStorage::NumericEdgeMap& outputmap=
ladanyi@201
   137
	    mapstorage->getNumericEdgeMap(edgemapcbts[OUTPUT]->get_active_text());
hegyi@165
   138
hegyi@165
   139
	  //zero out output map
hegyi@165
   140
	  for (EdgeIt i(g); i!=INVALID; ++i)
hegyi@165
   141
	    {
ladanyi@201
   142
	      outputmap[i]=0;
hegyi@165
   143
	    }
hegyi@165
   144
	  
ladanyi@201
   145
	  Suurballe<Graph, MapStorage::NumericEdgeMap > sb((Graph&)g, inputmap, from, to);
hegyi@165
   146
	  
hegyi@165
   147
	  int found=sb.run(num_set->get_value_as_int());
hegyi@165
   148
	  if(found)
hegyi@165
   149
	    {
hegyi@165
   150
	      for(int j=0;j<found;j++)
hegyi@165
   151
		{
alpar@188
   152
		  Path<Graph> path;
alpar@188
   153
		  path=sb.path(j);
hegyi@165
   154
		  for(int k=0;k<path.length();k++)
hegyi@165
   155
		    {
ladanyi@201
   156
		      outputmap[path.nth(k)]=j+1;
hegyi@165
   157
		    }
hegyi@165
   158
		}
hegyi@165
   159
	      o << "Result: found " << found << " paths between ";
hegyi@165
   160
	      o << source.get_active_text() << " and " << target.get_active_text();
hegyi@165
   161
	    }
hegyi@165
   162
	  else
hegyi@165
   163
	    {
hegyi@165
   164
	      o << "Result: failed to find shortest path between ";
hegyi@165
   165
	      o << source.get_active_text() << " and " << target.get_active_text();
hegyi@165
   166
	    }
hegyi@165
   167
	  resultlabel.set_text(o.str());
hegyi@165
   168
	  
hegyi@163
   169
	  mapstorage->mapChanged(true, (edgemapcbts[OUTPUT])->get_active_text());
hegyi@165
   170
	  //   mapstorage->changeActiveMap(true, E_COLOR,
hegyi@165
   171
	  // 			      (edgemapcbts[OUTPUT])->get_active_text());
hegyi@165
   172
	  //   mapstorage->changeActiveMap(true, E_TEXT,
hegyi@165
   173
	  // 			      (edgemapcbts[INPUT])->get_active_text());
hegyi@163
   174
	}
hegyi@163
   175
    }
hegyi@163
   176
}
hegyi@163
   177
    
hegyi@163
   178
void DijkstraBox::build_box()
hegyi@163
   179
{
hegyi@163
   180
  //if active tab is changed, labels of graph nodes had to be loaded into comboboxes
hegyi@163
   181
  //this can be done after the maps are loaded into ComboBoxes
hegyi@163
   182
  signal_upon_maplist_updated().connect(sigc::mem_fun(*this, &DijkstraBox::maplists_updated));
hegyi@163
   183
ladanyi@201
   184
  addMapSelector("Cost map: ", true, NUM);
ladanyi@201
   185
  addMapSelector("Edges of path here: ", true, NUM);
hegyi@163
   186
hegyi@163
   187
  Gtk::Label * source_label=new Gtk::Label("Source: ");
hegyi@163
   188
  Gtk::Label * target_label=new Gtk::Label("Target: ");
hegyi@163
   189
hegyi@163
   190
  table.attach(*source_label, 0,1,0,1);
hegyi@163
   191
  table.attach(*target_label, 0,1,1,2);
hegyi@163
   192
  table.attach(source, 1,2,0,1);
hegyi@163
   193
  table.attach(target, 1,2,1,2);
hegyi@163
   194
hegyi@163
   195
hegyi@163
   196
  pack_start(table);
hegyi@163
   197
hegyi@163
   198
  resultlabel.set_text("Result: algorithm is not run yet.");
hegyi@163
   199
  pack_start(resultlabel);
hegyi@163
   200
}
hegyi@163
   201
hegyi@165
   202
void SuurballeBox::build_box()
hegyi@165
   203
{
hegyi@165
   204
}
hegyi@165
   205
hegyi@163
   206
void DijkstraBox::maplists_updated()
hegyi@163
   207
{
hegyi@163
   208
  if(tabcbt.get_active_text()!="")
hegyi@163
   209
    {
hegyi@163
   210
      source.clear();
hegyi@163
   211
      target.clear();
hegyi@163
   212
      const Graph &g=mapstorage->graph;
hegyi@163
   213
      for (NodeIt i(g); i!=INVALID; ++i)
hegyi@163
   214
	{
hegyi@163
   215
	  std::ostringstream text;
ladanyi@201
   216
	  text << mapstorage->getLabel(i);
hegyi@163
   217
	  source.prepend_text(text.str());
hegyi@163
   218
	  target.prepend_text(text.str());
hegyi@163
   219
	}
hegyi@163
   220
    }
hegyi@163
   221
}
hegyi@165
   222
hegyi@165
   223
void DijkstraBox::get_from_to(Node & from, Node & to, Graph & g)
hegyi@165
   224
{
hegyi@165
   225
  int assigned=0;
hegyi@165
   226
  for (NodeIt i(g); (i!=INVALID) && (assigned<2); ++i)
hegyi@165
   227
    {
hegyi@165
   228
      std::ostringstream text;
ladanyi@201
   229
      text << mapstorage->getLabel(i);
hegyi@165
   230
      if(!(text.str().compare(source.get_active_text())))
hegyi@165
   231
	{
hegyi@165
   232
	  from=i;
hegyi@165
   233
	  assigned++;
hegyi@165
   234
	}
hegyi@165
   235
      if(!(text.str().compare(target.get_active_text())))
hegyi@165
   236
	{
hegyi@165
   237
	  to=i;
hegyi@165
   238
	  assigned++;
hegyi@165
   239
	}
hegyi@165
   240
    }
hegyi@165
   241
}