COIN-OR::LEMON - Graph Library

source: glemon-0.x/mapstorage.cc @ 177:40f3006fba2e

Last change on this file since 177:40f3006fba2e was 177:40f3006fba2e, checked in by Hegyi Péter, 17 years ago

Redesign parameters can now be saved and loaded.

File size: 13.0 KB
RevLine 
[174]1/* -*- C++ -*-
2 *
3 * This file is a part of LEMON, a generic C++ optimization library
4 *
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
8 *
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
12 *
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
15 * purpose.
16 *
17 */
18
[53]19#include "mapstorage.h"
[98]20#include "gui_writer.h"
21#include "gui_reader.h"
[128]22#include <limits>
23#include <cmath>
[63]24#include <gtkmm.h>
[77]25
[177]26const double i_d=20;
27const double a_d=0.05;
28const double p_d=40000;
29
30MapStorage::MapStorage() : modified(false), file_name(""), arrow_pos_read_ok(false), iterations(i_d), attraction(a_d), propulsation(p_d)
[6]31{
[53]32  nodemap_storage["coordinates_x"] = new Graph::NodeMap<double>(graph);
33  coords.setXMap(*nodemap_storage["coordinates_x"]);
34  nodemap_storage["coordinates_y"] = new Graph::NodeMap<double>(graph);
35  coords.setYMap(*nodemap_storage["coordinates_y"]);
36
[98]37  edgemap_storage["arrow_pos_x"] = new Graph::EdgeMap<double>(graph);
38  arrow_pos.setXMap(*edgemap_storage["arrow_pos_x"]);
39  edgemap_storage["arrow_pos_y"] = new Graph::EdgeMap<double>(graph);
40  arrow_pos.setYMap(*edgemap_storage["arrow_pos_y"]);
41
[134]42  nodemap_storage["label"] = new Graph::NodeMap<double>(graph);
43  edgemap_storage["label"] = new Graph::EdgeMap<double>(graph);
[64]44
[134]45  nodemap_default["label"] = 1.0;
46  edgemap_default["label"] = 1.0;
[94]47
48  active_nodemaps.resize(NODE_PROPERTY_NUM);
49  for(int i=0;i<NODE_PROPERTY_NUM;i++)
50    {
51      active_nodemaps[i]="";
52    }
53
54  active_edgemaps.resize(EDGE_PROPERTY_NUM);
55  for(int i=0;i<EDGE_PROPERTY_NUM;i++)
56    {
57      active_edgemaps[i]="";
58    }
[53]59}
60
61MapStorage::~MapStorage()
62{
63  for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
64      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
65  {
66    delete it->second;
67  }
68  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
69      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
70  {
71    delete it->second;
72  }
73}
[6]74
[118]75int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap, double default_value)
[6]76{
[46]77  if( nodemap_storage.find(name) == nodemap_storage.end() )
78    {
79      nodemap_storage[name]=nodemap;
[63]80      // set the maps default value
81      nodemap_default[name] = default_value;
[108]82
83      //announce changement in maps
84      signal_node_map.emit(name);
[46]85      return 0;
86    }
87  return 1;
[6]88}
89
[94]90void MapStorage::changeActiveMap(bool itisedge, int prop, std::string mapname)
91{
92  if(itisedge)
93    {
94      active_edgemaps[prop]=mapname;
95    }
96  else
97    {
98      active_nodemaps[prop]=mapname;
99    }
100  signal_prop.emit(itisedge, prop);
101}
102
[172]103void MapStorage::broadcastActiveMaps()
104{
105  for(int i=0;i<NODE_PROPERTY_NUM;i++)
106    {
107      signal_map_win.emit(false, i, active_nodemaps[i]);
108    }
109 
110  for(int i=0;i<EDGE_PROPERTY_NUM;i++)
111    {
112      signal_map_win.emit(true, i, active_edgemaps[i]);
113    }
114}
115
116
[94]117std::string MapStorage::getActiveEdgeMap(int prop)
118{
119  return active_edgemaps[prop];
120}
121
122std::string MapStorage::getActiveNodeMap(int prop)
123{
124  return active_nodemaps[prop];
125}
126
127std::vector<std::string> MapStorage::getEdgeMapList()
128{
129  std::vector<std::string> eml;
130  eml.resize(edgemap_storage.size());
131  int i=0;
132  std::map< std::string,Graph::EdgeMap<double> * >::iterator emsi=beginOfEdgeMaps();
133  for(;emsi!=endOfEdgeMaps();emsi++)
134    {
135      eml[i]=(emsi->first);
136      i++;
137    }
138  return eml;
139}
140
141std::vector<std::string> MapStorage::getNodeMapList()
142{
143  std::vector<std::string> nml;
144  nml.resize(nodemap_storage.size());
145  int i=0;
146  std::map< std::string,Graph::NodeMap<double> * >::iterator nmsi=beginOfNodeMaps();
147  for(;nmsi!=endOfNodeMaps();nmsi++)
148    {
149      nml[i]=(nmsi->first);
150      i++;
151    }
152  return nml;
153}
154
[177]155sigc::signal<void, bool, int> MapStorage::signal_prop_ch()
[94]156{
157  return signal_prop;
158}
159
[118]160int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap, double default_value)
[6]161{
[46]162  if( edgemap_storage.find(name) == edgemap_storage.end() )
163    {
164      edgemap_storage[name]=edgemap;
[63]165      // set the maps default value
166      edgemap_default[name] = default_value;
[108]167
168      //announce changement in maps
169      signal_edge_map.emit(name);
[46]170      return 0;
171    }
172  return 1;
[6]173}
174
175double MapStorage::maxOfNodeMap(const std::string & name)
176{
177  double max=0;
[53]178  for (NodeIt j(graph); j!=INVALID; ++j)
[6]179  {
180    if( (*nodemap_storage[name])[j]>max )
181    {
182      max=(*nodemap_storage[name])[j];
183    }
184  }
185  return max;
186}
187
188double MapStorage::maxOfEdgeMap(const std::string & name)
189{
190  double max=0;
[53]191  for (EdgeIt j(graph); j!=INVALID; ++j)
[6]192  {
193    if( (*edgemap_storage[name])[j]>max )
194    {
195      max=(*edgemap_storage[name])[j];
196    }
197  }
198  return max;
199}
200
201double MapStorage::minOfNodeMap(const std::string & name)
202{
[53]203  NodeIt j(graph);
[58]204  double min;
205  if(j!=INVALID)
206    {
207      min=(*nodemap_storage[name])[j];
208    }
209  else
210    {
211      min=0;
212    }
[6]213  for (; j!=INVALID; ++j)
214  {
215    if( (*nodemap_storage[name])[j]<min )
216    {
217      min=(*nodemap_storage[name])[j];
218    }
219  }
220  return min;
221}
222
223double MapStorage::minOfEdgeMap(const std::string & name)
224{
[53]225  EdgeIt j(graph);
[58]226  double min;
227  if(j!=INVALID)
228    {
229      min=(*edgemap_storage[name])[j];
230    }
231  else
232    {
233      min=0;
234    }
[53]235  for (EdgeIt j(graph); j!=INVALID; ++j)
[6]236  {
237    if( (*edgemap_storage[name])[j]<min )
238    {
239      min=(*edgemap_storage[name])[j];
240    }
241  }
242  return min;
243}
244
[63]245int MapStorage::readFromFile(const std::string &filename)
[53]246{
247  bool read_x = false;
248  bool read_y = false;
[64]249  bool read_edge_id = false;
[53]250
251  try {
252    LemonReader lreader(filename);
253    ContentReader content(lreader);
254    lreader.run();
255
[101]256    if (content.nodeSetNum() < 1)
257    {
258      Gtk::MessageDialog mdialog("No nodeset found in file.");
259      mdialog.run();
260      clear();
261      return 1;
262    }
263
264    if (content.edgeSetNum() < 1)
265    {
266      Gtk::MessageDialog mdialog("No edgeset found in file.");
267      mdialog.run();
268      clear();
269      return 1;
270    }
271
[53]272    const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0);
273    const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0);
274
275    GraphReader<Graph> greader(filename, graph);
276    for (std::vector<std::string>::const_iterator it = nodeMapNames.begin();
277        it != nodeMapNames.end(); ++it)
278    {
279      if (*it == "coordinates_x")
280      {
281        read_x = true;
282        //std::cout << "read X nodemap" << std::endl;
283      }
284      else if (*it == "coordinates_y")
285      {
286        read_y = true;
287        //std::cout << "read Y nodemap" << std::endl;
288      }
[134]289      else if (*it == "label")
[53]290      {
291        //std::cout << "read id nodemap" << std::endl;
292      }
293      else
294      {
295        nodemap_storage[*it] = new Graph::NodeMap<double>(graph);
296        //std::cout << "read " << *it << " nodemap" << std::endl;
297      }
298      greader.readNodeMap(*it, *nodemap_storage[*it]);
299    }
300    for (std::vector<std::string>::const_iterator it = edgeMapNames.begin();
301        it != edgeMapNames.end(); ++it)
302    {
[134]303      if (*it == "label")
[53]304      {
305        //std::cout << "read id edgemap" << std::endl;
[135]306        read_edge_id = true;
[53]307      }
308      else
309      {
310        edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
311        //std::cout << "read " << *it << " edgemap" << std::endl;
312      }
313      greader.readEdgeMap(*it, *edgemap_storage[*it]);
314    }
[98]315    GuiReader gui_reader(greader, this);
[53]316    greader.run();
[101]317  } catch (Exception& error) {
[79]318    Gtk::MessageDialog mdialog(error.what());
[53]319    mdialog.run();
[63]320    clear();
321    return 1;
[53]322  }
323
[64]324  if (!read_edge_id)
325  {
[134]326    edgemap_storage["label"] = new Graph::EdgeMap<double>(graph);
[64]327    int i = 1;
328    for (EdgeIt e(graph); e != INVALID; ++e)
329    {
[134]330      (*edgemap_storage["label"])[e] = i++;
[64]331    }
332  }
333
[53]334  if (!read_x || !read_y)
335  {
336    int node_num = 0;
337    for (NodeIt n(graph); n != INVALID; ++n)
338    {
339      node_num++;
340    }
341    const double pi = 3.142;
342    double step = 2 * pi / (double) node_num;
343    int i = 0;
344    for (NodeIt n(graph); n != INVALID; ++n)
345    {
[77]346      nodemap_storage["coordinates_x"]->set(n, 250.0 * std::cos(i * step));
347      nodemap_storage["coordinates_y"]->set(n, 250.0 * std::sin(i * step));
[53]348      i++;
349    }
350  }
[63]351
[98]352  if (!arrow_pos_read_ok)
353  {
354    arrow_pos_read_ok = false;
355    for (EdgeIt e(graph); e != INVALID; ++e)
356    {
[151]357      if (graph.source(e) == graph.target(e))
358      {
359        arrow_pos.set(e, coords[graph.source(e)] + XY(0.0, 80.0));
360      }
361      else
362      {
363        arrow_pos.set(e, (coords[graph.source(e)] + coords[graph.target(e)]) / 2.0);
364      }
[98]365    }
366  }
367
[63]368  // fill in the default values for the maps
369  for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
370      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
371  {
[134]372    if ((it->first != "label") &&
[63]373        (it->first != "coordiantes_x") &&
374        (it->first != "coordinates_y"))
375    {
376      nodemap_default[it->first] = 0.0;
377    }
[134]378    else if (it->first == "label")
[64]379    {
380      NodeIt n(graph);
[134]381      double max = (*nodemap_storage["label"])[n];
[64]382      for (; n != INVALID; ++n)
383      {
[134]384        if ((*nodemap_storage["label"])[n] > max)
385          max = (*nodemap_storage["label"])[n];
[64]386      }
[134]387      nodemap_default["label"] = max + 1.0;
[64]388    }
[63]389  }
390  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
391      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
392  {
[134]393    if (it->first != "label")
[63]394    {
395      edgemap_default[it->first] = 0.0;
396    }
[64]397    else
398    {
[67]399      double max = std::numeric_limits<double>::min();
400      for (EdgeIt e(graph); e != INVALID; ++e)
[64]401      {
[134]402        if ((*edgemap_storage["label"])[e] > max)
403          max = (*edgemap_storage["label"])[e];
[64]404      }
[67]405      if (max > std::numeric_limits<double>::min())
[134]406        edgemap_default["label"] = max + 1.0;
[67]407      else
[134]408        edgemap_default["label"] = 1.0;
[64]409    }
[63]410  }
411
412  return 0;
[53]413}
414
415void MapStorage::writeToFile(const std::string &filename)
416{
417  GraphWriter<Graph> gwriter(filename, graph);
418
419  for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
420      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
421  {
422    gwriter.writeNodeMap(it->first, *(it->second));
423  }
424  for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
425      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
426  {
[98]427    if ((it->first != "arrow_pos_x") &&
428        (it->first != "arrow_pos_y"))
429    {
430      gwriter.writeEdgeMap(it->first, *(it->second));
431    }
[53]432  }
[98]433
434  GuiWriter gui_writer(gwriter, this);
435
[53]436  gwriter.run();
437}
438
439void MapStorage::clear()
440{
441  for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
442      nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
443  {
444    if ((it->first != "coordinates_x") &&
445        (it->first != "coordinates_y") &&
[134]446        (it->first != "label"))
[53]447    {
448      delete it->second;
449      nodemap_storage.erase(it);
450    }
451  }
452  for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
453      edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
454  {
[134]455    if ((it->first != "label") &&
[98]456        (it->first != "arrow_pos_x") &&
457        (it->first != "arrow_pos_y"))
[53]458    {
459      delete it->second;
460      edgemap_storage.erase(it);
461    }
462  }
[63]463  for (std::map<std::string, double>::iterator it =
464      nodemap_default.begin(); it != nodemap_default.end(); ++it)
465  {
[134]466    if (it->first != "label")
[64]467      nodemap_default.erase(it);
[63]468  }
469  for (std::map<std::string, double>::iterator it =
470      edgemap_default.begin(); it != edgemap_default.end(); ++it)
471  {
[134]472    if (it->first != "label")
[64]473      edgemap_default.erase(it);
[63]474  }
[53]475  graph.clear();
476  file_name = "";
477  modified = false;
[172]478
479  arrow_pos_read_ok = false;
480 
481  for(int i=0;i<NODE_PROPERTY_NUM;i++)
482    {
483      changeActiveMap(false, i, "");
484      signal_map_win.emit(false, i, "");
485    }
486 
487  for(int i=0;i<EDGE_PROPERTY_NUM;i++)
488    {
489      changeActiveMap(true, i, "");
490      signal_map_win.emit(true, i, "");
491    }
[177]492
493  attraction=a_d;
494  propulsation=p_d;
495  iterations=i_d;
496
497  signal_design_win.emit(attraction, propulsation, iterations);
[53]498}
[98]499
500void MapStorage::ArrowPosReadOK()
501{
502  arrow_pos_read_ok = true;
503}
[111]504
505void MapStorage::mapChanged(bool itisedge, std::string mapname)
506{
507  if(itisedge)
508    {
509      for(int i=0;i<EDGE_PROPERTY_NUM;i++)
510        {
511          if(active_edgemaps[i]==mapname)
512            {
513              signal_prop.emit(itisedge, i);
514            }
515        }
516    }
517  else
518    {
519      for(int i=0;i<NODE_PROPERTY_NUM;i++)
520        {
521          if(active_nodemaps[i]==mapname)
522            {
523              signal_prop.emit(itisedge, i);
524            }
525        }
526    }
527}
[177]528
529void MapStorage::get_design_data(double & attraction_p, double & propulsation_p, int & iterations_p)
530{
531  attraction_p=attraction;
532  propulsation_p=propulsation;
533  iterations_p=iterations;
534}
535
536void MapStorage::set_attraction(double attraction_p)
537{
538  attraction=attraction_p;
539}
540
541void MapStorage::set_propulsation(double propulsation_p)
542{
543  propulsation=propulsation_p;
544}
545
546void MapStorage::set_iteration(int iterations_p)
547{
548  iterations=iterations_p;
549}
550
551void MapStorage::redesign_data_changed()
552{
553  signal_design_win.emit(attraction, propulsation, iterations);
554}
Note: See TracBrowser for help on using the repository browser.