File: drl_graph.cpp

package info (click to toggle)
r-cran-igraph 1.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 14,984 kB
  • sloc: ansic: 117,319; cpp: 22,287; fortran: 4,551; yacc: 1,150; tcl: 931; lex: 478; makefile: 149; sh: 9
file content (1286 lines) | stat: -rw-r--r-- 37,671 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
/* 
 * Copyright 2007 Sandia Corporation. Under the terms of Contract
 * DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government retains
 * certain rights in this software.
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without 
 * modification, are permitted provided that the following conditions are 
 * met:
 * 
 *     * Redistributions of source code must retain the above copyright 
 * notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright 
 * notice, this list of conditions and the following disclaimer in the 
 * documentation and/or other materials provided with the distribution.
 *     * Neither the name of Sandia National Laboratories nor the names of 
 * its contributors may be used to endorse or promote products derived from 
 * this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
// This file contains the member definitions of the master class

#include <iostream>
#include <fstream>
#include <map>
#include <vector>
#include <cstdlib>
#include <cmath>
#include <cstring>

using namespace std;

#include "drl_graph.h"
#include "igraph_random.h"
#include "igraph_interface.h"
#include "igraph_progress.h"
#include "igraph_interrupt_internal.h"
#ifdef MUSE_MPI
  #include <mpi.h>
#endif

namespace drl {

// constructor -- initializes the schedule variables (as in 
// graph constructor)

// graph::graph ( int proc_id, int tot_procs, char *int_file )
// {
		  
// 		  // MPI parameters
// 		  myid = proc_id;
// 		  num_procs = tot_procs;

// 		  // initial annealing parameters
// 		  STAGE = 0;
// 		  iterations = 0;
// 		  temperature = 2000;
// 		  attraction = 10;
// 		  damping_mult = 1.0;
// 		  min_edges = 20;
// 		  first_add = fine_first_add = true;
// 		  fineDensity = false;

// 		  // Brian's original Vx schedule
// 		  liquid.iterations = 200;
// 		  liquid.temperature = 2000;
// 		  liquid.attraction = 2;
// 		  liquid.damping_mult = 1.0;
// 		  liquid.time_elapsed = 0;

// 		  expansion.iterations = 200;
// 		  expansion.temperature = 2000;
// 		  expansion.attraction = 10;
// 		  expansion.damping_mult = 1.0;
// 		  expansion.time_elapsed = 0;

// 		  cooldown.iterations = 200;
// 		  cooldown.temperature = 2000;
// 		  cooldown.attraction = 1;
// 		  cooldown.damping_mult = .1;
// 		  cooldown.time_elapsed = 0;

// 		  crunch.iterations = 50;
// 		  crunch.temperature = 250;
// 		  crunch.attraction = 1;
// 		  crunch. damping_mult = .25;
// 		  crunch.time_elapsed = 0;

// 		  simmer.iterations = 100;
// 		  simmer.temperature = 250;
// 		  simmer.attraction = .5;
// 		  simmer.damping_mult = 0.0;
// 		  simmer.time_elapsed = 0;

// 		  // scan .int file for node info
// 		  scan_int ( int_file );
		  
// 		  // populate node positions and ids
// 		  positions.reserve ( num_nodes );
// 		  map < int, int >::iterator cat_iter;
// 		  for ( cat_iter = id_catalog.begin();
// 			    cat_iter != id_catalog.end();
// 				cat_iter++ )
// 			positions.push_back ( Node( cat_iter->first ) );
		  
// 		  /*
// 		  // output positions .ids for debugging
// 		  for ( int id = 0; id < num_nodes; id++ )
// 			cout << positions[id].id << endl;
// 		  */
		  
// 		  // read .int file for graph info
// 		  read_int ( int_file );
		  
// 		  // initialize density server
// 		  density_server.Init();
		  
// }

graph::graph(const igraph_t *igraph, 
	     const igraph_layout_drl_options_t *options,
	     const igraph_vector_t *weights) {
  myid = 0;
  num_procs = 1;
  
  STAGE = 0;
  iterations = options->init_iterations;
  temperature = options->init_temperature;
  attraction = options->init_attraction;
  damping_mult = options->init_damping_mult;
  min_edges = 20;
  first_add = fine_first_add = true;
  fineDensity = false;
  
  // Brian's original Vx schedule
  liquid.iterations = options->liquid_iterations;
  liquid.temperature = options->liquid_temperature;
  liquid.attraction = options->liquid_attraction;
  liquid.damping_mult = options->liquid_damping_mult;
  liquid.time_elapsed = 0;
  
  expansion.iterations = options->expansion_iterations;
  expansion.temperature = options->expansion_temperature;
  expansion.attraction = options->expansion_attraction;
  expansion.damping_mult = options->expansion_damping_mult;
  expansion.time_elapsed = 0;
  
  cooldown.iterations = options->cooldown_iterations;
  cooldown.temperature = options->cooldown_temperature;
  cooldown.attraction = options->cooldown_attraction;
  cooldown.damping_mult = options->cooldown_damping_mult;
  cooldown.time_elapsed = 0;
  
  crunch.iterations = options->crunch_iterations;
  crunch.temperature = options->crunch_temperature;
  crunch.attraction = options->crunch_attraction;
  crunch.damping_mult = options->crunch_damping_mult;
  crunch.time_elapsed = 0;
  
  simmer.iterations = options->simmer_iterations;
  simmer.temperature = options->simmer_temperature;
  simmer.attraction = options->simmer_attraction;
  simmer.damping_mult = options->simmer_damping_mult;
  simmer.time_elapsed = 0;
  
  // scan .int file for node info
  highest_sim = 1.0;
  num_nodes=igraph_vcount(igraph);
  long int no_of_edges=igraph_ecount(igraph);
  for (long int i=0; i<num_nodes; i++) {
    id_catalog[i] = 1;
  }
  map< int, int>::iterator cat_iter;
  for ( cat_iter = id_catalog.begin();
	cat_iter != id_catalog.end(); cat_iter++) {
    cat_iter->second = cat_iter->first;
  }
  
  // populate node positions and ids  
  positions.reserve ( num_nodes );
  for ( cat_iter = id_catalog.begin();
	cat_iter != id_catalog.end();
	cat_iter++ ) {
    positions.push_back ( Node( cat_iter->first ) );
  }
  
  // read .int file for graph info
  long int node_1, node_2;
  double weight;
  for (long int i=0; i<no_of_edges; i++) {
    node_1=IGRAPH_FROM(igraph, i);
    node_2=IGRAPH_TO(igraph,i);
    weight = weights ? VECTOR(*weights)[i] : 1.0 ;
    (neighbors[id_catalog[node_1]])[id_catalog[node_2]] = weight;    
    (neighbors[id_catalog[node_2]])[id_catalog[node_1]] = weight;  
  }
  
  // initialize density server
  density_server.Init();
    
}

// The following subroutine scans the .int file for the following
// information: number nodes, node ids, and highest similarity.  The
// corresponding graph globals are populated: num_nodes, id_catalog,
// and highest_sim.

// void graph::scan_int ( char *filename )
// {

//   cout << "Proc. " << myid << " scanning .int file ..." << endl;
  
//   // Open (sim) File
//   ifstream fp ( filename );
//   if ( !fp )
//   {
// 	cout << "Error: could not open " << filename << ".  Program terminated." << endl;
// 	#ifdef MUSE_MPI
// 	  MPI_Abort ( MPI_COMM_WORLD, 1 );
// 	#else
// 	  exit (1);
//     #endif
//   }	
  
//   // Read file, parse, and add into data structure
//   int id1, id2;
//   float edge_weight;
//   highest_sim = -1.0;
//   while ( !fp.eof () )
// 	{
// 	  fp >> id1 >> id2 >> edge_weight;
	  
// 	  // ignore negative weights!
// 	  if ( edge_weight <= 0 )
// 	  {
// 	     cout << "Error: found negative edge weight in " << filename << ".  Program stopped." << endl;
// 		 #ifdef MUSE_MPI
// 	       MPI_Abort ( MPI_COMM_WORLD, 1 );
// 	     #else
// 	       exit (1);
//          #endif
// 	   }

// 	   if ( highest_sim < edge_weight )
// 	      highest_sim = edge_weight;
	
// 	   id_catalog[id1] = 1;
// 	   id_catalog[id2] = 1;
// 	}

//   fp.close();

//   if ( id_catalog.size() == 0 )
//   {
//     cout << "Error: Proc. " << myid << ": " << filename << " is empty.  Program terminated." << endl;
// 	#ifdef MUSE_MPI
// 	  MPI_Abort ( MPI_COMM_WORLD, 1 );
// 	#else
// 	  exit (1);
// 	#endif
//   }
  
//   // label nodes with sequential integers starting at 0
//   map< int, int>::iterator cat_iter;
//   int id_label;
//   for ( cat_iter = id_catalog.begin(), id_label = 0;
// 	    cat_iter != id_catalog.end(); cat_iter++, id_label++ )
//     cat_iter->second = id_label;

//   /*
//   // output id_catalog for debugging:
//   for ( cat_iter = id_catalog.begin();
// 		cat_iter != id_catalog.end();
// 		cat_iter++ )
// 	cout << cat_iter->first << "\t" << cat_iter->second << endl;
//   */
  
//   num_nodes = id_catalog.size();  
// }

// read in .parms file, if present

/*
void graph::read_parms ( char *parms_file )
{

		  // read from .parms file
		  ifstream parms_in ( parms_file );
		  if ( !parms_in )
		  {
		    cout << "Error: could not open .parms file!  Program stopped." << endl;
			#ifdef MUSE_MPI
			  MPI_Abort ( MPI_COMM_WORLD, 1 );
			#else
			  exit (1);
			#endif
		  }
		  
		  cout << "Processor " << myid << " reading .parms file." << endl;
		  
		  // read in stage parameters
		  string parm_label;	// this is ignored in the .parms file
		  
		  // initial parameters
		  parms_in >> parm_label >> iterations;
		  parms_in >> parm_label >> temperature;
		  parms_in >> parm_label >> attraction;
		  parms_in >> parm_label >> damping_mult;
		  
		  // liquid stage
		  parms_in >> parm_label >> liquid.iterations;
		  parms_in >> parm_label >> liquid.temperature;
		  parms_in >> parm_label >> liquid.attraction;
		  parms_in >> parm_label >> liquid.damping_mult;
		  
		  // expansion stage
		  parms_in >> parm_label >> expansion.iterations;
		  parms_in >> parm_label >> expansion.temperature;
		  parms_in >> parm_label >> expansion.attraction;
		  parms_in >> parm_label >> expansion.damping_mult;
		  
		  // cooldown stage
		  parms_in >> parm_label >> cooldown.iterations;
		  parms_in >> parm_label >> cooldown.temperature;
		  parms_in >> parm_label >> cooldown.attraction;
		  parms_in >> parm_label >> cooldown.damping_mult;
		  
		  // crunch stage
		  parms_in >> parm_label >> crunch.iterations;
		  parms_in >> parm_label >> crunch.temperature;
		  parms_in >> parm_label >> crunch.attraction;
		  parms_in >> parm_label >> crunch.damping_mult;
		  
		  // simmer stage
		  parms_in >> parm_label >> simmer.iterations;
		  parms_in >> parm_label >> simmer.temperature;
		  parms_in >> parm_label >> simmer.attraction;
		  parms_in >> parm_label >> simmer.damping_mult;
		  
		  parms_in.close();

		  // print out parameters for double checking
		  if ( myid == 0 )
		  {
		    cout << "Processor 0 reports the following inputs:" << endl;
			cout << "inital.iterations = " << iterations << endl;
			cout << "initial.temperature = " << temperature << endl;
			cout << "initial.attraction = " << attraction << endl;
			cout << "initial.damping_mult = " << damping_mult << endl;
			cout << " ..." << endl;
			cout << "liquid.iterations = " << liquid.iterations << endl;
			cout << "liquid.temperature = " << liquid.temperature << endl;
			cout << "liquid.attraction = " << liquid.attraction << endl;
			cout << "liquid.damping_mult = " << liquid.damping_mult << endl;
			cout << " ..." << endl;
			cout << "simmer.iterations = " << simmer.iterations << endl;
			cout << "simmer.temperature = " << simmer.temperature << endl;
			cout << "simmer.attraction = " << simmer.attraction << endl;
			cout << "simmer.damping_mult = " << simmer.damping_mult << endl;
		  }

}
*/

// init_parms -- this subroutine initializes the edge_cut variables
// used in the original VxOrd starting with the edge_cut parameter.
// In our version, edge_cut = 0 means no cutting, 1 = maximum cut.
// We also set the random seed here.

void graph::init_parms ( int rand_seed, float edge_cut, float real_parm )
{
        IGRAPH_UNUSED(rand_seed);

	// first we translate edge_cut the former tcl sliding scale
	//CUT_END = cut_length_end = 39000.0 * (1.0 - edge_cut) + 1000.0;
	CUT_END = cut_length_end = 40000.0 * (1.0 - edge_cut);
	
	// cut_length_end cannot actually be 0
	if ( cut_length_end <= 1.0 )
	  cut_length_end = 1.0;
	  
	float cut_length_start = 4.0 * cut_length_end;
	
	// now we set the parameters used by ReCompute
	cut_off_length = cut_length_start;
	cut_rate = ( cut_length_start - cut_length_end ) / 400.0;
	
	// finally set the number of iterations to leave .real coords fixed
	int full_comp_iters;
	full_comp_iters = liquid.iterations + expansion.iterations +
					  cooldown.iterations + crunch.iterations + 3;
    
	// adjust real parm to iterations (do not enter simmer halfway)
	if ( real_parm < 0 )
		real_iterations = (int)real_parm;
	else if ( real_parm == 1)
		real_iterations = full_comp_iters + simmer.iterations + 100;
	else
		real_iterations = (int)(real_parm*full_comp_iters);
		
	tot_iterations = 0;
	if ( real_iterations > 0 )
		real_fixed = true;
	else
		real_fixed = false;

    // calculate total expected iterations (for progress bar display)
    tot_expected_iterations = liquid.iterations +
                              expansion.iterations + cooldown.iterations +
                              crunch.iterations + simmer.iterations;

	/*
	// output edge_cutting parms (for debugging)
	cout << "Processor " << myid << ": "
	     << "cut_length_end = CUT_END = " << cut_length_end
	     << ", cut_length_start = " << cut_length_start
	     << ", cut_rate = " << cut_rate << endl;
	*/
	
	// set random seed
	// srand ( rand_seed ); // Don't need this in igraph
	
}

void graph::init_parms(const igraph_layout_drl_options_t *options) {
  double rand_seed = 0.0;
  double real_in = -1.0;
  init_parms(rand_seed, options->edge_cut, real_in);
}

// The following subroutine reads a .real file to obtain initial
// coordinates.  If a node is missing coordinates the coordinates
// are computed 

// void graph::read_real ( char *real_file )
// {
//   cout << "Processor " << myid << " reading .real file ..." << endl;
  
//   // read in .real file and mark as fixed
//   ifstream real_in ( real_file );
//   if ( !real_in )
//   {
//     cout << "Error: proc. " << myid << " could not open .real file." << endl;
//     #ifdef MUSE_MPI
// 	  MPI_Abort ( MPI_COMM_WORLD, 1 );
// 	#else
// 	  exit (1);
// 	#endif
//   }
  
//   int real_id;
//   float real_x, real_y;
//   while ( !real_in.eof () )
//   {
//     real_id = -1;
//     real_in >> real_id >> real_x >> real_y;
// 	if ( real_id >= 0 )
// 	{
// 	  positions[id_catalog[real_id]].x = real_x;
// 	  positions[id_catalog[real_id]].y = real_y;
// 	  positions[id_catalog[real_id]].fixed = true;
	  
// 	  /*
// 	  // output positions read (for debugging)
//       cout << id_catalog[real_id] << " (" << positions[id_catalog[real_id]].x
// 		   << ", " << positions[id_catalog[real_id]].y << ") " 
// 		   << positions[id_catalog[real_id]].fixed << endl;
// 	  */
	  
// 	  // add node to density grid
// 	  if ( real_iterations > 0 )
// 	    density_server.Add ( positions[id_catalog[real_id]], fineDensity );
// 	}
		 
//   }
  
//   real_in.close();
// }

int graph::read_real ( const igraph_matrix_t *real_mat, 
		       const igraph_vector_bool_t *fixed) {
  long int n=igraph_matrix_nrow(real_mat);
  for (long int i=0; i<n; i++) {
    positions[id_catalog[i]].x = MATRIX(*real_mat, i, 0);
    positions[id_catalog[i]].y = MATRIX(*real_mat, i, 1);
    positions[id_catalog[i]].fixed = fixed ? VECTOR(*fixed)[i] : false;
    
    if ( real_iterations > 0 ) {
      density_server.Add ( positions[id_catalog[i]], fineDensity );
    }
  }

  return 0;
}

// The read_part_int subroutine reads the .int
// file produced by convert_sim and gathers the nodes and their
// neighbors in the range start_ind to end_ind.

// void graph::read_int ( char *file_name )
// {

// 	ifstream int_file;
	
// 	int_file.open ( file_name );
// 	if ( !int_file )
// 	{
// 		cout << "Error (worker process " << myid << "): could not open .int file." << endl;
// 		#ifdef MUSE_MPI
// 		  MPI_Abort ( MPI_COMM_WORLD, 1 );
// 		#else
// 		  exit (1);
// 		#endif
// 	}
	
// 	cout << "Processor " << myid << " reading .int file ..." << endl;
	
// 	int node_1, node_2;
// 	float weight;
	
//     while ( !int_file.eof() )
// 	{
// 		weight = 0;		// all weights should be >= 0
// 		int_file >> node_1 >> node_2 >> weight;
// 		if ( weight )		// otherwise we are at end of file
// 								// or it is a self-connected node
// 		{
// 			    // normalization from original vxord
// 			    weight /= highest_sim;
// 				weight = weight*fabs(weight);
				
// 				// initialize graph
// 				if ( ( node_1 % num_procs ) == myid )
// 					(neighbors[id_catalog[node_1]])[id_catalog[node_2]] = weight;
// 				if ( ( node_2 % num_procs ) == myid )
// 					(neighbors[id_catalog[node_2]])[id_catalog[node_1]] = weight;
// 		}
// 	}
// 	int_file.close();
	
// 	/*
// 	// the following code outputs the contents of the neighbors structure
// 	// (to be used for debugging)
	
// 	map<int, map<int,float> >::iterator i;
// 	map<int,float>::iterator j;
	
// 	for ( i = neighbors.begin(); i != neighbors.end(); i++ ) {
// 	  cout << myid << ": " << i->first << " ";
// 		for (j = (i->second).begin(); j != (i->second).end(); j++ )
// 			cout << j->first << " (" << j->second << ") ";
// 		cout << endl;
// 		}
// 	*/
	
// }

/*********************************************
 * Function: ReCompute				         *
 * Description: Compute the graph locations	 *
 * Modified from original code by B. Wylie   *
 ********************************************/

int graph::ReCompute( ) 
{

  // carryover from original VxOrd
  int MIN = 1;
  
  /*
  // output parameters (for debugging)
  cout << "ReCompute is using the following parameters: "<< endl;
  cout << "STAGE: " << STAGE << ", iter: " << iterations << ", temp = " << temperature
       << ", attract = " << attraction << ", damping_mult = " << damping_mult
	   << ", min_edges = " << min_edges << ", cut_off_length = " << cut_off_length
	   << ", fineDensity = " << fineDensity << endl; 
  */

  /* igraph progress report */
  float progress = (tot_iterations * 100.0 / tot_expected_iterations);

  switch (STAGE) {
    case 0:
      if (iterations == 0)
        IGRAPH_PROGRESS("DrL layout (initialization stage)", progress, 0);
      else
        IGRAPH_PROGRESS("DrL layout (liquid stage)", progress, 0);
      break;
    case 1:
      IGRAPH_PROGRESS("DrL layout (expansion stage)", progress, 0); break;
    case 2:
      IGRAPH_PROGRESS("DrL layout (cooldown and cluster phase)", progress, 0); break;
    case 3:
      IGRAPH_PROGRESS("DrL layout (crunch phase)", progress, 0); break;
    case 5:
      IGRAPH_PROGRESS("DrL layout (simmer phase)", progress, 0); break;
    case 6:
      IGRAPH_PROGRESS("DrL layout (final phase)", 100.0, 0); break;
    default:
      IGRAPH_PROGRESS("DrL layout (unknown phase)", 0.0, 0); break;
  }

  /* Compute Energies for individual nodes */
  update_nodes ();
  
  // check to see if we need to free fixed nodes
  tot_iterations++;
  if ( tot_iterations >= real_iterations )
	 real_fixed = false;


  	// ****************************************
	// AUTOMATIC CONTROL SECTION
	// ****************************************

	// STAGE 0: LIQUID
	if (STAGE == 0) {

		if ( iterations == 0 )
		{
			start_time = time( NULL );
// 			if ( myid == 0 )
// 				cout << "Entering liquid stage ...";
		}

		if (iterations < liquid.iterations) {
			temperature = liquid.temperature;
			attraction = liquid.attraction;
			damping_mult = liquid.damping_mult;
			iterations++;
// 			if ( myid == 0 )
// 				cout << "." << flush;
		
		} else {

			stop_time = time( NULL );
			liquid.time_elapsed = liquid.time_elapsed + (stop_time - start_time);
			temperature = expansion.temperature;
			attraction = expansion.attraction;
			damping_mult = expansion.damping_mult;
			iterations = 0;

			// go to next stage
			STAGE = 1;
			start_time = time( NULL );
			
// 			if ( myid == 0 )
// 				cout << "Entering expansion stage ...";
		}
	}

	// STAGE 1: EXPANSION
	if (STAGE == 1) {

		if (iterations < expansion.iterations) {
				
			// Play with vars
			if (attraction > 1) attraction -= .05;
			if (min_edges > 12) min_edges -= .05;
			cut_off_length -= cut_rate;
			if (damping_mult > .1) damping_mult -= .005;
			iterations++;
// 			if ( myid == 0 ) cout << "." << flush;
	
		} else {

			stop_time = time( NULL );
			expansion.time_elapsed = expansion.time_elapsed + (stop_time - start_time);
		  	min_edges = 12;
			damping_mult = cooldown.damping_mult;
			
			STAGE = 2;
			attraction = cooldown.attraction;
			temperature = cooldown.temperature;
			iterations = 0;
			start_time = time( NULL );
			
// 			if ( myid == 0 )
// 				cout << "Entering cool-down stage ...";
		}
	}

	// STAGE 2: Cool down and cluster
	else if(STAGE==2) {

		if (iterations < cooldown.iterations) {

			// Reduce temperature
			if (temperature > 50) temperature -= 10;

			// Reduce cut length
			if (cut_off_length > cut_length_end) cut_off_length -= cut_rate*2;
			if (min_edges > MIN) min_edges -= .2;
			//min_edges = 99;
			iterations++;
// 			if ( myid == 0 )
// 				cout << "." << flush;
			
		} else {

			stop_time = time( NULL );
			cooldown.time_elapsed = cooldown.time_elapsed + (stop_time - start_time);
			cut_off_length = cut_length_end;
			temperature = crunch.temperature;
			damping_mult = crunch.damping_mult;
			min_edges = MIN;
			//min_edges = 99; // In other words: no more cutting
			
			STAGE = 3;
			iterations = 0;
			attraction = crunch.attraction;
			start_time = time( NULL );
			
// 			if ( myid == 0 )
// 				cout << "Entering crunch stage ...";
		}  
	}

	// STAGE 3: Crunch
	else if(STAGE==3) {
	
		if (iterations < crunch.iterations)
		{
			iterations++;
// 			if ( myid == 0 ) cout << "." << flush;
		}
		else {
		
			stop_time = time( NULL );
			crunch.time_elapsed = crunch.time_elapsed + (stop_time - start_time);
			iterations = 0;
			temperature = simmer.temperature;
			attraction = simmer.attraction;
			damping_mult = simmer.damping_mult;
			min_edges = 99;
			fineDensity = true;
			
			STAGE = 5;
			start_time = time( NULL );
				
// 			if ( myid == 0 )
// 				cout << "Entering simmer stage ...";
		}  
	}

	// STAGE 5: Simmer
	else if( STAGE==5 ) {

		if (iterations < simmer.iterations) {
			if (temperature > 50) temperature -= 2;
			iterations++;
// 			if ( myid == 0 ) cout << "." << flush;
		} else {
			stop_time = time( NULL );
			simmer.time_elapsed = simmer.time_elapsed + (stop_time - start_time);
			
			STAGE = 6;
			
// 			if ( myid == 0 )
// 				cout << "Layout calculation completed in " <<
// 				  ( liquid.time_elapsed + expansion.time_elapsed +
// 				    cooldown.time_elapsed + crunch.time_elapsed +
// 				    simmer.time_elapsed )
// 				     << " seconds (not including I/O)." 
// 				     << endl;
		}
	}

	// STAGE 6: All Done!
	else if ( STAGE == 6)
	{
	  
	  /*
	  // output parameters (for debugging)
	  cout << "ReCompute is using the following parameters: "<< endl;
      cout << "STAGE: " << STAGE << ", iter: " << iterations << ", temp = " << temperature
           << ", attract = " << attraction << ", damping_mult = " << damping_mult
	       << ", min_edges = " << min_edges << ", cut_off_length = " << cut_off_length
	       << ", fineDensity = " << fineDensity << endl; 
	  */
	  
	  return 0;
	}
	
	// ****************************************
	// END AUTOMATIC CONTROL SECTION
	// ****************************************

	// Still need more recomputation
	return 1;

}

// update_nodes -- this function will complete the primary node update
// loop in layout's recompute routine.  It follows exactly the same
// sequence to ensure similarity of parallel layout to the standard layout

void graph::update_nodes ( )
{
	
	vector<int> node_indices;			// node list of nodes currently being updated
	float old_positions[2*MAX_PROCS];	// positions before update
	float new_positions[2*MAX_PROCS];	// positions after update
    
	bool all_fixed;						// check if all nodes are fixed
	
	// initial node list consists of 0,1,...,num_procs
	for ( int i = 0; i < num_procs; i++ )
	  node_indices.push_back( i );

	// next we calculate the number of nodes there would be if the
	// num_nodes by num_procs schedule grid were perfectly square
	int square_num_nodes = (int)(num_procs + num_procs*floor ((float)(num_nodes-1)/(float)num_procs ));

	for ( int i = myid; i < square_num_nodes; i += num_procs )
	{
	
		// get old positions
		get_positions ( node_indices, old_positions );
		
		// default new position is old position
		get_positions ( node_indices, new_positions );
		
		if ( i < num_nodes )
		{

		  // advance random sequence according to myid
		  for ( int j = 0; j < 2*myid; j++ )
		    RNG_UNIF01();
		    // rand();

		  // calculate node energy possibilities
		  if ( !(positions[i].fixed && real_fixed) )
			update_node_pos ( i, old_positions, new_positions );

		  // advance random sequence for next iteration
		  for ( unsigned int j = 2*myid; j < 2*(node_indices.size()-1); j++ )
		    RNG_UNIF01();
		    // rand();

		}
		else
		{
		  // advance random sequence according to use by
		  // the other processors
		  for ( unsigned int j = 0; j < 2*(node_indices.size()); j++ )
		    RNG_UNIF01();
		    //rand();
		}
		
		// check if anything was actually updated (e.g. everything was fixed)
		all_fixed = true;
		for ( unsigned int j = 0; j < node_indices.size (); j++ )
		  if ( !(positions [ node_indices[j] ].fixed && real_fixed) )
		    all_fixed = false;
		  
		// update positions across processors (if not all fixed)
		if ( !all_fixed )
		{
		  #ifdef MUSE_MPI
  		    MPI_Allgather ( &new_positions[2*myid], 2, MPI_FLOAT,
			  	            new_positions, 2, MPI_FLOAT, MPI_COMM_WORLD ); 
		  #endif
		
		  // update positions (old to new)
		  update_density ( node_indices, old_positions, new_positions );
		}
		
		/*
		if ( myid == 0 )
		  {
		    // output node list (for debugging)
		    for ( unsigned int j = 0; j < node_indices.size(); j++ )
		      cout << node_indices[j] << " ";
		    cout << endl;
		  }
		*/

		// compute node list for next update
		for ( unsigned int j = 0; j < node_indices.size(); j++ )
		  node_indices [j] += num_procs;
	
		while ( !node_indices.empty() && node_indices.back() >= num_nodes )
		  node_indices.pop_back ( );
			
	}
	
	// update first_add and fine_first_add
	first_add = false;
	if ( fineDensity ) fine_first_add = false;
	
}

// The get_positions function takes the node_indices list
// and returns the corresponding positions in an array.

void graph::get_positions ( vector<int> &node_indices,
			    float return_positions[2*MAX_PROCS]  )
{
	
	// fill positions
	for(unsigned int i=0; i < node_indices.size(); i++)
	{
		return_positions[2*i] = positions[ node_indices[i] ].x;
		return_positions[2*i+1] = positions[ node_indices[i] ].y;
	}
	
}

// update_node_pos -- this subroutine does the actual work of computing
// the new position of a given node.  num_act_proc gives the number
// of active processes at this level for use by the random number
// generators.

void graph::update_node_pos ( int node_ind,
			      float old_positions[2*MAX_PROCS],
			      float new_positions[2*MAX_PROCS] )
{	

		float energies[2];			// node energies for possible positions
		float updated_pos[2][2];	// possible positions
		float pos_x, pos_y;
		
		// old VxOrd parameter
		float jump_length = .010 * temperature;
		
		// subtract old node
		density_server.Subtract ( positions[node_ind], first_add, fine_first_add, fineDensity );

		// compute node energy for old solution
		energies[0] = Compute_Node_Energy ( node_ind );

	        // move node to centroid position
		Solve_Analytic ( node_ind, pos_x, pos_y );
		positions[node_ind].x = updated_pos[0][0] = pos_x;
		positions[node_ind].y = updated_pos[0][1] = pos_y;

		/*
		// ouput random numbers (for debugging)
		int rand_0, rand_1;
		rand_0 = rand();
		rand_1 = rand();
		cout << myid << ": " << rand_0 << ", " << rand_1 << endl;
		*/

		// Do random method (RAND_MAX is C++ maximum random number)
		updated_pos[1][0] = updated_pos[0][0] + (.5 - RNG_UNIF01()) * jump_length;
		updated_pos[1][1] = updated_pos[0][1] + (.5 - RNG_UNIF01()) * jump_length;
		
		// compute node energy for random position
		positions[node_ind].x = updated_pos[1][0];
		positions[node_ind].y = updated_pos[1][1];
		energies[1] = Compute_Node_Energy ( node_ind );
		
		/*
		// output update possiblities (debugging):
		cout << node_ind << ": (" << updated_pos[0][0] << "," << updated_pos[0][1]
			 << "), " << energies[0] << "; (" << updated_pos[1][0] << ","
			 << updated_pos[1][1] << "), " << energies[1] << endl;
		*/
			 
		// add back old position
		positions[node_ind].x = old_positions[2*myid];
		positions[node_ind].y = old_positions[2*myid+1];
		if ( !fineDensity && !first_add )
			density_server.Add ( positions[node_ind], fineDensity );
		else if ( !fine_first_add )
			density_server.Add ( positions[node_ind], fineDensity );
		
		// choose updated node position with lowest energy
		if ( energies[0] < energies[1] )
		{
			new_positions[2*myid] = updated_pos[0][0];
			new_positions[2*myid+1] = updated_pos[0][1];
			positions[node_ind].energy = energies[0];
		}
		else
		{
			new_positions[2*myid] = updated_pos[1][0];
			new_positions[2*myid+1] = updated_pos[1][1];
			positions[node_ind].energy = energies[1];
		}
		
}

// update_density takes a sequence of node_indices and their positions and
// updates the positions by subtracting the old positions and adding the
// new positions to the density grid.

void graph::update_density ( vector<int> &node_indices,
			     float old_positions[2*MAX_PROCS],
			     float new_positions[2*MAX_PROCS] )
{
	
	// go through each node and subtract old position from
	// density grid before adding new position
	for ( unsigned int i = 0; i < node_indices.size(); i++ )
	{
		positions[node_indices[i]].x = old_positions[2*i];
		positions[node_indices[i]].y = old_positions[2*i+1];
		density_server.Subtract ( positions[node_indices[i]],
					  first_add, fine_first_add, fineDensity );
		
		positions[node_indices[i]].x = new_positions[2*i];
		positions[node_indices[i]].y = new_positions[2*i+1];
		density_server.Add ( positions[node_indices[i]], fineDensity );
	}	

}

/********************************************
* Function: Compute_Node_Energy			    *
* Description: Compute the node energy		*
* This code has been modified from the      *
* original code by B. Wylie.                *
*********************************************/

float graph::Compute_Node_Energy( int node_ind )
{
	
	/* Want to expand 4th power range of attraction */
	float attraction_factor = attraction*attraction*
			attraction*attraction*2e-2;
	
	map <int,float>::iterator EI;
	float x_dis,y_dis;
	float energy_distance, weight;
	float node_energy=0;
	
	// Add up all connection energies
	for(EI = neighbors[node_ind].begin(); EI != neighbors[node_ind].end(); ++EI) {

		// Get edge weight
		weight = EI->second;
				
		// Compute x,y distance
		x_dis = positions[ node_ind ].x - positions[ EI->first ].x;
		y_dis = positions[ node_ind ].y - positions[ EI->first ].y;
		
		// Energy Distance
		energy_distance = x_dis*x_dis + y_dis*y_dis;
		if (STAGE<2) energy_distance *= energy_distance;

		// In the liquid phase we want to discourage long link distances
		if (STAGE==0) energy_distance *= energy_distance;

		node_energy += weight * attraction_factor * energy_distance;
	}

	// output effect of density (debugging)
	//cout << "[before: " << node_energy;
	
	// add density
	node_energy += density_server.GetDensity ( positions[ node_ind ].x, positions[ node_ind ].y,
											   fineDensity );

	// after calling density server (debugging)
	//cout << ", after: " << node_energy << "]" << endl;
	
	// return computated energy
	return node_energy;
}


/*********************************************
* Function: Solve_Analytic			         *
* Description: Compute the node position     *
* This is a modified version of the function *
* originally written by B. Wylie		     *
*********************************************/

void graph::Solve_Analytic( int node_ind, float &pos_x, float &pos_y )
{

   map <int,float>::iterator EI;
   float total_weight = 0;
   float x_dis, y_dis,x_cen=0, y_cen=0;
   float x=0,y=0,dis;
   float damping,weight;

   // Sum up all connections
   for(EI = neighbors[node_ind].begin(); EI != neighbors[node_ind].end(); ++EI) {
		weight = EI->second;
		total_weight += weight;
		x +=  weight * positions[ EI->first ].x;  
		y +=  weight * positions[ EI->first ].y;
   }

   // Now set node position
   if (total_weight > 0) {

		// Compute centriod
		x_cen = x/total_weight;
		y_cen = y/total_weight;
		damping = 1.0 - damping_mult;
		pos_x = damping*positions[ node_ind ].x + (1.0-damping) * x_cen;
		pos_y = damping*positions[ node_ind ].y + (1.0-damping) * y_cen;
   } else {
		pos_x = positions[ node_ind ].x;
		pos_y = positions[ node_ind ].y;
   }
   
   // No cut edge flag (?)
   if (min_edges == 99) return;

   // Don't cut at end of scale
   if ( CUT_END >= 39500 ) return;

   float num_connections = sqrt((double)neighbors[node_ind].size());
   float maxLength = 0;

   map<int, float>::iterator maxIndex;

   // Go through nodes edges... cutting if necessary
   for(EI = maxIndex = neighbors[node_ind].begin();
	   EI !=neighbors[node_ind].end(); ++EI) {

		// Check for at least min edges
		if (neighbors[node_ind].size() < min_edges) continue;

		x_dis = x_cen - positions[ EI->first ].x;
		y_dis = y_cen - positions[ EI->first ].y;
		dis = x_dis*x_dis+y_dis*y_dis;
		dis *= num_connections;

		// Store maximum edge
		if (dis > maxLength) {maxLength = dis; maxIndex=EI;}
   }

   // If max length greater than cut_length then cut
   if (maxLength > cut_off_length) neighbors[ node_ind ].erase( maxIndex ); 
   
}


// write_coord writes out the coordinate file of the final solutions

// void graph::write_coord( const char *file_name )
// {

//   ofstream coordOUT( file_name );
//   if ( !coordOUT )
//   {
// 	cout << "Could not open " << file_name << ".  Program terminated." << endl;
// 	#ifdef MUSE_MPI
// 	  MPI_Abort ( MPI_COMM_WORLD, 1 );
// 	#else
// 	  exit (1);
// 	#endif
//   }
  
//   cout << "Writing out solution to " << file_name << " ..." << endl;
  
//   for (unsigned int i = 0; i < positions.size(); i++) {
//     coordOUT << positions[i].id << "\t" << positions[i].x << "\t" << positions[i].y <<endl;
//   }
//   coordOUT.close();
  
// }

// write_sim -- outputs .edges file, takes as input .coord filename,
// with .coord extension

/*
void graph::write_sim ( const char *file_name )
{

  string prefix_name ( file_name, strlen(file_name)-7 );
  prefix_name = prefix_name + ".iedges";

  // first we overwrite, then we append
  ofstream simOUT;
  if ( myid == 0 )
    simOUT.open ( prefix_name.c_str() );
  else
    simOUT.open ( prefix_name.c_str(), ios::app );

  if ( !simOUT )
    {
      cout << "Could not open " << prefix_name << ". Program terminated." << endl;
	  #ifdef MUSE_MPI
	    MPI_Abort ( MPI_COMM_WORLD, 1 );
	  #else
	    exit (1);
	  #endif
    }
  

  cout << "Proc. " << myid << " writing to " << prefix_name << " ..." << endl;

      
  // the following code outputs the contents of the neighbors structure

  map<int, map<int,float> >::iterator i;
  map<int,float>::iterator j;
  
  for ( i = neighbors.begin(); i != neighbors.end(); i++ )
    for (j = (i->second).begin(); j != (i->second).end(); j++ )
	simOUT << positions[i->first].id << "\t"
	       << positions[j->first].id << "\t"
	       << j->second << endl;

  simOUT.close();

}
*/

// get_tot_energy adds up the energy for each node to give an estimate of the
// quality of the minimization.

float graph::get_tot_energy ( )
{

	float my_tot_energy, tot_energy;
	my_tot_energy = 0;
	for ( int i = myid; i < num_nodes; i += num_procs )
	  my_tot_energy += positions[i].energy;
	  
	//vector<Node>::iterator i;
    //for ( i = positions.begin(); i != positions.end(); i++ )
	//	tot_energy += i->energy;
	
	#ifdef MUSE_MPI
		MPI_Reduce ( &my_tot_energy, &tot_energy, 1, MPI_FLOAT, MPI_SUM, 0, MPI_COMM_WORLD );
	#else
		tot_energy = my_tot_energy;
	#endif
	
	return tot_energy;
	
}


// The following subroutine draws the graph with possible intermediate
// output (int_out is set to 0 if not proc. 0).  int_out is the parameter
// passed by the user, and coord_file is the .coord file.

// void graph::draw_graph ( int int_out, char *coord_file )
// {
	
// 	// layout graph (with possible intermediate output)
// 	int count_iter = 0, count_file = 1;
// 	char int_coord_file [MAX_FILE_NAME + MAX_INT_LENGTH];
// 	while ( ReCompute( ) )
// 		if ( (int_out > 0) && (count_iter == int_out) )
// 		{
// 			// output intermediate solution
// 			sprintf ( int_coord_file, "%s.%d", coord_file, count_file );
// 			write_coord ( int_coord_file );
			
// 			count_iter = 0;
// 			count_file++;
// 		}
// 		else
// 			count_iter++;
	
// }

int graph::draw_graph(igraph_matrix_t *res) {
  int count_iter=0;
  while (ReCompute()) {
    IGRAPH_ALLOW_INTERRUPTION();
    count_iter++;
  }
  long int n=positions.size();
  IGRAPH_CHECK(igraph_matrix_resize(res, n, 2));
  for (long int i=0; i<n; i++) {
    MATRIX(*res, i, 0) = positions[i].x;
    MATRIX(*res, i, 1) = positions[i].y;
  }
  return 0;
}

} // namespace drl