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