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 | |
---|
19 | #include "mapstorage.h" |
---|
20 | #include "gui_writer.h" |
---|
21 | #include "gui_reader.h" |
---|
22 | #include <limits> |
---|
23 | #include <cmath> |
---|
24 | #include <gtkmm.h> |
---|
25 | |
---|
26 | const double i_d=20; |
---|
27 | const double a_d=0.05; |
---|
28 | const double p_d=40000; |
---|
29 | |
---|
30 | MapStorage::MapStorage() : modified(false), file_name(""), arrow_pos_read_ok(false), iterations(i_d), attraction(a_d), propulsation(p_d) |
---|
31 | { |
---|
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 | |
---|
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 | |
---|
42 | nodemap_storage["label"] = new Graph::NodeMap<double>(graph); |
---|
43 | edgemap_storage["label"] = new Graph::EdgeMap<double>(graph); |
---|
44 | |
---|
45 | nodemap_default["label"] = 1.0; |
---|
46 | edgemap_default["label"] = 1.0; |
---|
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 | } |
---|
59 | } |
---|
60 | |
---|
61 | MapStorage::~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 | } |
---|
74 | |
---|
75 | int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap, double default_value) |
---|
76 | { |
---|
77 | if( nodemap_storage.find(name) == nodemap_storage.end() ) |
---|
78 | { |
---|
79 | nodemap_storage[name]=nodemap; |
---|
80 | // set the maps default value |
---|
81 | nodemap_default[name] = default_value; |
---|
82 | |
---|
83 | //announce changement in maps |
---|
84 | signal_node_map.emit(name); |
---|
85 | return 0; |
---|
86 | } |
---|
87 | return 1; |
---|
88 | } |
---|
89 | |
---|
90 | void 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 | |
---|
103 | void 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 | |
---|
117 | std::string MapStorage::getActiveEdgeMap(int prop) |
---|
118 | { |
---|
119 | return active_edgemaps[prop]; |
---|
120 | } |
---|
121 | |
---|
122 | std::string MapStorage::getActiveNodeMap(int prop) |
---|
123 | { |
---|
124 | return active_nodemaps[prop]; |
---|
125 | } |
---|
126 | |
---|
127 | std::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 | |
---|
141 | std::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 | |
---|
155 | sigc::signal<void, bool, int> MapStorage::signal_prop_ch() |
---|
156 | { |
---|
157 | return signal_prop; |
---|
158 | } |
---|
159 | |
---|
160 | int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap, double default_value) |
---|
161 | { |
---|
162 | if( edgemap_storage.find(name) == edgemap_storage.end() ) |
---|
163 | { |
---|
164 | edgemap_storage[name]=edgemap; |
---|
165 | // set the maps default value |
---|
166 | edgemap_default[name] = default_value; |
---|
167 | |
---|
168 | //announce changement in maps |
---|
169 | signal_edge_map.emit(name); |
---|
170 | return 0; |
---|
171 | } |
---|
172 | return 1; |
---|
173 | } |
---|
174 | |
---|
175 | double MapStorage::maxOfNodeMap(const std::string & name) |
---|
176 | { |
---|
177 | double max=0; |
---|
178 | for (NodeIt j(graph); j!=INVALID; ++j) |
---|
179 | { |
---|
180 | if( (*nodemap_storage[name])[j]>max ) |
---|
181 | { |
---|
182 | max=(*nodemap_storage[name])[j]; |
---|
183 | } |
---|
184 | } |
---|
185 | return max; |
---|
186 | } |
---|
187 | |
---|
188 | double MapStorage::maxOfEdgeMap(const std::string & name) |
---|
189 | { |
---|
190 | double max=0; |
---|
191 | for (EdgeIt j(graph); j!=INVALID; ++j) |
---|
192 | { |
---|
193 | if( (*edgemap_storage[name])[j]>max ) |
---|
194 | { |
---|
195 | max=(*edgemap_storage[name])[j]; |
---|
196 | } |
---|
197 | } |
---|
198 | return max; |
---|
199 | } |
---|
200 | |
---|
201 | double MapStorage::minOfNodeMap(const std::string & name) |
---|
202 | { |
---|
203 | NodeIt j(graph); |
---|
204 | double min; |
---|
205 | if(j!=INVALID) |
---|
206 | { |
---|
207 | min=(*nodemap_storage[name])[j]; |
---|
208 | } |
---|
209 | else |
---|
210 | { |
---|
211 | min=0; |
---|
212 | } |
---|
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 | |
---|
223 | double MapStorage::minOfEdgeMap(const std::string & name) |
---|
224 | { |
---|
225 | EdgeIt j(graph); |
---|
226 | double min; |
---|
227 | if(j!=INVALID) |
---|
228 | { |
---|
229 | min=(*edgemap_storage[name])[j]; |
---|
230 | } |
---|
231 | else |
---|
232 | { |
---|
233 | min=0; |
---|
234 | } |
---|
235 | for (EdgeIt j(graph); j!=INVALID; ++j) |
---|
236 | { |
---|
237 | if( (*edgemap_storage[name])[j]<min ) |
---|
238 | { |
---|
239 | min=(*edgemap_storage[name])[j]; |
---|
240 | } |
---|
241 | } |
---|
242 | return min; |
---|
243 | } |
---|
244 | |
---|
245 | int MapStorage::readFromFile(const std::string &filename) |
---|
246 | { |
---|
247 | bool read_x = false; |
---|
248 | bool read_y = false; |
---|
249 | bool read_edge_id = false; |
---|
250 | |
---|
251 | try { |
---|
252 | LemonReader lreader(filename); |
---|
253 | ContentReader content(lreader); |
---|
254 | lreader.run(); |
---|
255 | |
---|
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 | |
---|
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 | } |
---|
289 | else if (*it == "label") |
---|
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 | { |
---|
303 | if (*it == "label") |
---|
304 | { |
---|
305 | //std::cout << "read id edgemap" << std::endl; |
---|
306 | read_edge_id = true; |
---|
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 | } |
---|
315 | GuiReader gui_reader(greader, this); |
---|
316 | greader.run(); |
---|
317 | } catch (Exception& error) { |
---|
318 | Gtk::MessageDialog mdialog(error.what()); |
---|
319 | mdialog.run(); |
---|
320 | clear(); |
---|
321 | return 1; |
---|
322 | } |
---|
323 | |
---|
324 | if (!read_edge_id) |
---|
325 | { |
---|
326 | edgemap_storage["label"] = new Graph::EdgeMap<double>(graph); |
---|
327 | int i = 1; |
---|
328 | for (EdgeIt e(graph); e != INVALID; ++e) |
---|
329 | { |
---|
330 | (*edgemap_storage["label"])[e] = i++; |
---|
331 | } |
---|
332 | } |
---|
333 | |
---|
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 | { |
---|
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)); |
---|
348 | i++; |
---|
349 | } |
---|
350 | } |
---|
351 | |
---|
352 | if (!arrow_pos_read_ok) |
---|
353 | { |
---|
354 | arrow_pos_read_ok = false; |
---|
355 | for (EdgeIt e(graph); e != INVALID; ++e) |
---|
356 | { |
---|
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 | } |
---|
365 | } |
---|
366 | } |
---|
367 | |
---|
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 | { |
---|
372 | if ((it->first != "label") && |
---|
373 | (it->first != "coordiantes_x") && |
---|
374 | (it->first != "coordinates_y")) |
---|
375 | { |
---|
376 | nodemap_default[it->first] = 0.0; |
---|
377 | } |
---|
378 | else if (it->first == "label") |
---|
379 | { |
---|
380 | NodeIt n(graph); |
---|
381 | double max = (*nodemap_storage["label"])[n]; |
---|
382 | for (; n != INVALID; ++n) |
---|
383 | { |
---|
384 | if ((*nodemap_storage["label"])[n] > max) |
---|
385 | max = (*nodemap_storage["label"])[n]; |
---|
386 | } |
---|
387 | nodemap_default["label"] = max + 1.0; |
---|
388 | } |
---|
389 | } |
---|
390 | for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it = |
---|
391 | edgemap_storage.begin(); it != edgemap_storage.end(); ++it) |
---|
392 | { |
---|
393 | if (it->first != "label") |
---|
394 | { |
---|
395 | edgemap_default[it->first] = 0.0; |
---|
396 | } |
---|
397 | else |
---|
398 | { |
---|
399 | double max = std::numeric_limits<double>::min(); |
---|
400 | for (EdgeIt e(graph); e != INVALID; ++e) |
---|
401 | { |
---|
402 | if ((*edgemap_storage["label"])[e] > max) |
---|
403 | max = (*edgemap_storage["label"])[e]; |
---|
404 | } |
---|
405 | if (max > std::numeric_limits<double>::min()) |
---|
406 | edgemap_default["label"] = max + 1.0; |
---|
407 | else |
---|
408 | edgemap_default["label"] = 1.0; |
---|
409 | } |
---|
410 | } |
---|
411 | |
---|
412 | return 0; |
---|
413 | } |
---|
414 | |
---|
415 | void 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 | { |
---|
427 | if ((it->first != "arrow_pos_x") && |
---|
428 | (it->first != "arrow_pos_y")) |
---|
429 | { |
---|
430 | gwriter.writeEdgeMap(it->first, *(it->second)); |
---|
431 | } |
---|
432 | } |
---|
433 | |
---|
434 | GuiWriter gui_writer(gwriter, this); |
---|
435 | |
---|
436 | gwriter.run(); |
---|
437 | } |
---|
438 | |
---|
439 | void 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") && |
---|
446 | (it->first != "label")) |
---|
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 | { |
---|
455 | if ((it->first != "label") && |
---|
456 | (it->first != "arrow_pos_x") && |
---|
457 | (it->first != "arrow_pos_y")) |
---|
458 | { |
---|
459 | delete it->second; |
---|
460 | edgemap_storage.erase(it); |
---|
461 | } |
---|
462 | } |
---|
463 | for (std::map<std::string, double>::iterator it = |
---|
464 | nodemap_default.begin(); it != nodemap_default.end(); ++it) |
---|
465 | { |
---|
466 | if (it->first != "label") |
---|
467 | nodemap_default.erase(it); |
---|
468 | } |
---|
469 | for (std::map<std::string, double>::iterator it = |
---|
470 | edgemap_default.begin(); it != edgemap_default.end(); ++it) |
---|
471 | { |
---|
472 | if (it->first != "label") |
---|
473 | edgemap_default.erase(it); |
---|
474 | } |
---|
475 | graph.clear(); |
---|
476 | file_name = ""; |
---|
477 | modified = false; |
---|
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 | } |
---|
492 | |
---|
493 | attraction=a_d; |
---|
494 | propulsation=p_d; |
---|
495 | iterations=i_d; |
---|
496 | |
---|
497 | signal_design_win.emit(attraction, propulsation, iterations); |
---|
498 | } |
---|
499 | |
---|
500 | void MapStorage::ArrowPosReadOK() |
---|
501 | { |
---|
502 | arrow_pos_read_ok = true; |
---|
503 | } |
---|
504 | |
---|
505 | void 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 | } |
---|
528 | |
---|
529 | void 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 | |
---|
536 | void MapStorage::set_attraction(double attraction_p) |
---|
537 | { |
---|
538 | attraction=attraction_p; |
---|
539 | } |
---|
540 | |
---|
541 | void MapStorage::set_propulsation(double propulsation_p) |
---|
542 | { |
---|
543 | propulsation=propulsation_p; |
---|
544 | } |
---|
545 | |
---|
546 | void MapStorage::set_iteration(int iterations_p) |
---|
547 | { |
---|
548 | iterations=iterations_p; |
---|
549 | } |
---|
550 | |
---|
551 | void MapStorage::redesign_data_changed() |
---|
552 | { |
---|
553 | signal_design_win.emit(attraction, propulsation, iterations); |
---|
554 | } |
---|