File: drl_layout.cpp

package info (click to toggle)
r-cran-igraph 1.0.1-1~bpo8%2B1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-backports
  • size: 18,160 kB
  • sloc: ansic: 173,529; cpp: 19,365; fortran: 4,550; yacc: 1,164; tcl: 931; lex: 484; makefile: 149; sh: 9
file content (472 lines) | stat: -rw-r--r-- 15,104 bytes parent folder | download | duplicates (6)
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
/* 
 * 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.
 */
// Layout
//
// This program implements a parallel force directed graph drawing
// algorithm.  The algorithm used is based upon a random decomposition
// of the graph and simulated shared memory of node position and density.
// In this version, the simulated shared memory is spread among all processors
//
// The structure of the inputs and outputs of this code will be displayed
// if the program is called without parameters, or if an erroneous
// parameter is passed to the program.
//
// S. Martin
// 5/6/2005

// C++ library routines
#include <iostream>
#include <fstream>
#include <map>
#include <set>
#include <string>
#include <deque>
#include <vector>

using namespace std;

// layout routines and constants
#include "drl_layout.h"
#include "drl_parse.h"
#include "drl_graph.h"

// MPI
#ifdef MUSE_MPI
  #include <mpi.h>
#endif

using namespace drl;
#include "igraph_layout.h"
#include "igraph_random.h"
#include "igraph_interface.h"

namespace drl {

// int main(int argc, char **argv) {
  
  
//   // initialize MPI
//   int myid, num_procs;
  
//   #ifdef MUSE_MPI
//     MPI_Init ( &argc, &argv );
//     MPI_Comm_size ( MPI_COMM_WORLD, &num_procs );
//     MPI_Comm_rank ( MPI_COMM_WORLD, &myid );
//   #else
//     myid = 0;
// 	num_procs = 1;
//   #endif
  
//   // parameters that must be broadcast to all processors
//   int rand_seed;
//   float edge_cut;
  
//   char int_file[MAX_FILE_NAME];
//   char coord_file[MAX_FILE_NAME];
//   char real_file[MAX_FILE_NAME];
//   char parms_file[MAX_FILE_NAME];
  
//   int int_out = 0;
//   int edges_out = 0;
//   int parms_in = 0;
//   float real_in = -1.0;
  
//   // user interaction is handled by processor 0
//   if ( myid == 0 )
//   {
//     if ( num_procs > MAX_PROCS )
// 	{
// 		cout << "Error: Maximum number of processors is " << MAX_PROCS << "." << endl;
// 		cout << "Adjust compile time parameter." << endl;
// 		#ifdef MUSE_MPI
// 		  MPI_Abort ( MPI_COMM_WORLD, 1 );
// 		#else
// 		  exit (1);
// 		#endif
// 	}
	
// 	// get user input
//     parse command_line ( argc, argv );
// 	rand_seed = command_line.rand_seed;
// 	edge_cut = command_line.edge_cut;
// 	int_out = command_line.int_out;
// 	edges_out = command_line.edges_out;
// 	parms_in = command_line.parms_in;
// 	real_in = command_line.real_in;
// 	strcpy ( coord_file, command_line.coord_file.c_str() );
// 	strcpy ( int_file, command_line.sim_file.c_str() );
// 	strcpy ( real_file, command_line.real_file.c_str() );
// 	strcpy ( parms_file, command_line.parms_file.c_str() );
	
//   }
  
//   // now we initialize all processors by reading .int file
//   #ifdef MUSE_MPI
//     MPI_Bcast ( &int_file, MAX_FILE_NAME, MPI_CHAR, 0, MPI_COMM_WORLD );
//   #endif
//   graph neighbors ( myid, num_procs, int_file );
  
//   // check for user supplied parameters
//   #ifdef MUSE_MPI
//     MPI_Bcast ( &parms_in, 1, MPI_INT, 0, MPI_COMM_WORLD );
//   #endif
//   if ( parms_in )
//   {
//     #ifdef MUSE_MPI
// 	  MPI_Bcast ( &parms_file, MAX_FILE_NAME, MPI_CHAR, 0, MPI_COMM_WORLD );
// 	#endif
// 	neighbors.read_parms ( parms_file );
//   }

//   // set random seed, edge cutting, and real iterations parameters
//   #ifdef MUSE_MPI
//     MPI_Bcast ( &rand_seed, 1, MPI_INT, 0, MPI_COMM_WORLD );
//     MPI_Bcast ( &edge_cut, 1, MPI_FLOAT, 0, MPI_COMM_WORLD );
// 	MPI_Bcast ( &real_in, 1, MPI_INT, 0, MPI_COMM_WORLD );
//   #endif
//   neighbors.init_parms ( rand_seed, edge_cut, real_in );

//   // check for .real file with existing coordinates
//   if ( real_in >= 0 )
//   {
//     #ifdef MUSE_MPI
// 	  MPI_Bcast ( &real_file, MAX_FILE_NAME, MPI_CHAR, 0, MPI_COMM_WORLD );
// 	#endif
// 	neighbors.read_real ( real_file );
//   }
  
//   neighbors.draw_graph ( int_out, coord_file );

//   // do we have to write out the edges?
//   #ifdef MUSE_MPI
//     MPI_Bcast ( &edges_out, 1, MPI_INT, 0, MPI_COMM_WORLD );
//   #endif
//   if ( edges_out )
//     {
// 	  #ifdef MUSE_MPI
//         MPI_Bcast ( &coord_file, MAX_FILE_NAME, MPI_CHAR, 0, MPI_COMM_WORLD );
// 	  #endif
//       for ( int i = 0; i < num_procs; i++ )
// 	  {
// 	    if ( myid == i )
// 	      neighbors.write_sim ( coord_file );
// 	    #ifdef MUSE_MPI
//   	      MPI_Barrier ( MPI_COMM_WORLD );
// 	    #endif
// 	  }
//     }
  
//   // finally we output file and quit
//   float tot_energy;
//   tot_energy = neighbors.get_tot_energy ();
//   if ( myid == 0 )
//   {
// 	neighbors.write_coord ( coord_file );
// 	cout << "Total Energy: " << tot_energy << "." << endl
// 	     << "Program terminated successfully." << endl;
//   }

//   // MPI finalize
//   #ifdef MUSE_MPI
//     MPI_Finalize ();
//   #endif

//   return 0;
// }

} // namespace drl

/**
 * \section about_drl
 * 
 * <para>
 * DrL is a sophisticated layout generator developed and implemented by 
 * Shawn Martin et al. As of October 2012 the original DrL homepage is
 * unfortunately not available. You can read more about this algorithm
 * in the following technical report: Martin, S., Brown, W.M.,
 * Klavans, R., Boyack, K.W., DrL: Distributed Recursive (Graph)
 * Layout. SAND Reports, 2008. 2936: p. 1-10.
 * </para>
 * 
 * <para>
 * Only a subset of the complete DrL functionality is 
 * included in igraph, parallel runs and recursive, multi-level
 * layouting is not supported.
 * </para>
 * 
 * <para>
 * The parameters of the layout are stored in an \ref
 * igraph_layout_drl_options_t structure, this can be initialized by
 * calling the function \ref igraph_layout_drl_options_init(). 
 * The fields of this structure can then be adjusted by hand if needed.
 * The layout is calculated by an \ref igraph_layout_drl() call.
 * </para>
 */

/**
 * \function igraph_layout_drl_options_init
 * Initialize parameters for the DrL layout generator
 * 
 * This function can be used to initialize the struct holding the
 * parameters for the DrL layout generator. There are a number of
 * predefined templates available, it is a good idea to start from one
 * of these by modifying some parameters.
 * \param options The struct to initialize.
 * \param templ The template to use. Currently the following templates
 *     are supplied: \c IGRAPH_LAYOUT_DRL_DEFAULT, \c
 *     IGRAPH_LAYOUT_DRL_COARSEN, \c IGRAPH_LAYOUT_DRL_COARSEST, 
 *     \c IGRAPH_LAYOUT_DRL_REFINE and \c IGRAPH_LAYOUT_DRL_FINAL.
 * \return Error code.
 * 
 * Time complexity: O(1).
 */

int igraph_layout_drl_options_init(igraph_layout_drl_options_t *options,
				   igraph_layout_drl_default_t templ) {

  options->edge_cut=32.0/40.0;
    
  switch (templ) {
  case IGRAPH_LAYOUT_DRL_DEFAULT:
    options->init_iterations   = 0;
    options->init_temperature  = 2000;
    options->init_attraction   = 10;
    options->init_damping_mult = 1.0;

    options->liquid_iterations   = 200;
    options->liquid_temperature  = 2000;
    options->liquid_attraction   = 10;
    options->liquid_damping_mult = 1.0;

    options->expansion_iterations   = 200;
    options->expansion_temperature  = 2000;
    options->expansion_attraction   = 2;
    options->expansion_damping_mult = 1.0;

    options->cooldown_iterations   = 200;
    options->cooldown_temperature  = 2000;
    options->cooldown_attraction   = 1;
    options->cooldown_damping_mult = .1;

    options->crunch_iterations   = 50;
    options->crunch_temperature  = 250;
    options->crunch_attraction   = 1;
    options->crunch_damping_mult = 0.25;

    options->simmer_iterations   = 100;
    options->simmer_temperature  = 250;
    options->simmer_attraction   = .5;
    options->simmer_damping_mult = 0;
    
    break;
  case IGRAPH_LAYOUT_DRL_COARSEN:
    options->init_iterations   = 0;
    options->init_temperature  = 2000;
    options->init_attraction   = 10;
    options->init_damping_mult = 1.0;

    options->liquid_iterations   = 200;
    options->liquid_temperature  = 2000;
    options->liquid_attraction   = 2;
    options->liquid_damping_mult = 1.0;

    options->expansion_iterations   = 200;
    options->expansion_temperature  = 2000;
    options->expansion_attraction   = 10;
    options->expansion_damping_mult = 1.0;

    options->cooldown_iterations   = 200;
    options->cooldown_temperature  = 2000;
    options->cooldown_attraction   = 1;
    options->cooldown_damping_mult = .1;

    options->crunch_iterations   = 50;
    options->crunch_temperature  = 250;
    options->crunch_attraction   = 1;
    options->crunch_damping_mult = 0.25;

    options->simmer_iterations   = 100;
    options->simmer_temperature  = 250;
    options->simmer_attraction   = .5;
    options->simmer_damping_mult = 0;

    break;
  case IGRAPH_LAYOUT_DRL_COARSEST:
    options->init_iterations   = 0;
    options->init_temperature  = 2000;
    options->init_attraction   = 10;
    options->init_damping_mult = 1.0;

    options->liquid_iterations   = 200;
    options->liquid_temperature  = 2000;
    options->liquid_attraction   = 2;
    options->liquid_damping_mult = 1.0;

    options->expansion_iterations   = 200;
    options->expansion_temperature  = 2000;
    options->expansion_attraction   = 10;
    options->expansion_damping_mult = 1.0;

    options->cooldown_iterations   = 200;
    options->cooldown_temperature  = 2000;
    options->cooldown_attraction   = 1;
    options->cooldown_damping_mult = .1;

    options->crunch_iterations   = 200;
    options->crunch_temperature  = 250;
    options->crunch_attraction   = 1;
    options->crunch_damping_mult = 0.25;

    options->simmer_iterations   = 100;
    options->simmer_temperature  = 250;
    options->simmer_attraction   = .5;
    options->simmer_damping_mult = 0;

    break;
  case IGRAPH_LAYOUT_DRL_REFINE:
    options->init_iterations   = 0;
    options->init_temperature  = 50;
    options->init_attraction   = .5;
    options->init_damping_mult = 0;

    options->liquid_iterations   = 0;
    options->liquid_temperature  = 2000;
    options->liquid_attraction   = 2;
    options->liquid_damping_mult = 1.0;

    options->expansion_iterations   = 50;
    options->expansion_temperature  = 500;
    options->expansion_attraction   = .1;
    options->expansion_damping_mult = .25;

    options->cooldown_iterations   = 50;
    options->cooldown_temperature  = 200;
    options->cooldown_attraction   = 1;
    options->cooldown_damping_mult = .1;

    options->crunch_iterations   = 50;
    options->crunch_temperature  = 250;
    options->crunch_attraction   = 1;
    options->crunch_damping_mult = 0.25;

    options->simmer_iterations   = 0;
    options->simmer_temperature  = 250;
    options->simmer_attraction   = .5;
    options->simmer_damping_mult = 0;

    break;
  case IGRAPH_LAYOUT_DRL_FINAL:
    options->init_iterations   = 0;
    options->init_temperature  = 50;
    options->init_attraction   = .5;
    options->init_damping_mult = 0;

    options->liquid_iterations   = 0;
    options->liquid_temperature  = 2000;
    options->liquid_attraction   = 2;
    options->liquid_damping_mult = 1.0;

    options->expansion_iterations   = 50;
    options->expansion_temperature  = 50;
    options->expansion_attraction   = .1;
    options->expansion_damping_mult = .25;

    options->cooldown_iterations   = 50;
    options->cooldown_temperature  = 200;
    options->cooldown_attraction   = 1;
    options->cooldown_damping_mult = .1;

    options->crunch_iterations   = 50;
    options->crunch_temperature  = 250;
    options->crunch_attraction   = 1;
    options->crunch_damping_mult = 0.25;

    options->simmer_iterations   = 25;
    options->simmer_temperature  = 250;
    options->simmer_attraction   = .5;
    options->simmer_damping_mult = 0;
    
  default:
    IGRAPH_ERROR("Unknown DrL template", IGRAPH_EINVAL);
    break;
  }

  return 0;
}

/**
 * \function igraph_layout_drl
 * The DrL layout generator
 * 
 * This function implements the force-directed DrL layout generator.
 * Please see more in the following technical report: Martin, S.,
 * Brown, W.M., Klavans, R., Boyack, K.W., DrL: Distributed Recursive
 * (Graph) Layout. SAND Reports, 2008. 2936: p. 1-10. 
 * \param graph The input graph.
 * \param use_seed Logical scalar, if true, then the coordinates
 *    supplied in the \p res argument are used as starting points.
 * \param res Pointer to a matrix, the result layout is stored
 *    here. It will be resized as needed.
 * \param options The parameters to pass to the layout generator.
 * \param weights Edge weights, pointer to a vector. If this is a null
 *    pointer then every edge will have the same weight.
 * \param fixed Pointer to a logical vector, or a null pointer. This
 *    can be used to fix the position of some vertices. Vertices for
 *    which it is true will not be moved, but stay at the coordinates
 *    given in the \p res matrix. This argument is ignored if it is a
 *    null pointer or if use_seed is false.
 * \return Error code.
 * 
 * Time complexity: ???.
 */

int igraph_layout_drl(const igraph_t *graph, igraph_matrix_t *res, 
		      igraph_bool_t use_seed,
		      igraph_layout_drl_options_t *options,
		      const igraph_vector_t *weights,
		      const igraph_vector_bool_t *fixed) {
  
  RNG_BEGIN();

  drl::graph neighbors(graph, options, weights);
  neighbors.init_parms(options);
  if (use_seed) {
    IGRAPH_CHECK(igraph_matrix_resize(res, igraph_vcount(graph), 2));
    neighbors.read_real(res, fixed);
  }
  neighbors.draw_graph(res);

  RNG_END();
  
  return 0;
}