Merge branch 'joris'
[libdai.git] / utils / createfg.cpp
1 /* Copyright (C) 2006-2008 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
2 Radboud University Nijmegen, The Netherlands /
3 Max Planck Institute for Biological Cybernetics, Germany
4
5 This file is part of libDAI.
6
7 libDAI is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 libDAI is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with libDAI; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23 #include <iostream>
24 #include <fstream>
25 #include <vector>
26 #include <iterator>
27 #include <algorithm>
28 #include <boost/program_options.hpp>
29 #include <boost/numeric/ublas/matrix_sparse.hpp>
30 #include <boost/numeric/ublas/io.hpp>
31 #include <dai/factorgraph.h>
32 #include <dai/weightedgraph.h>
33 #include <dai/util.h>
34
35
36 using namespace std;
37 using namespace dai;
38 namespace po = boost::program_options;
39 typedef boost::numeric::ublas::compressed_matrix<double> matrix;
40 typedef matrix::value_array_type::const_iterator matrix_vcit;
41 typedef matrix::index_array_type::const_iterator matrix_icit;
42
43
44 Factor BinaryFactor( const Var &n, double field ) {
45 assert( n.states() == 2 );
46 double buf[2];
47 buf[0] = exp(-field);
48 buf[1] = exp(field);
49 return Factor(n, &buf[0]);
50 }
51
52
53 Factor BinaryFactor( const Var &n1, const Var &n2, double coupling ) {
54 assert( n1.states() == 2 );
55 assert( n2.states() == 2 );
56 assert( n1 != n2 );
57 double buf[4];
58 buf[0] = (buf[3] = exp(coupling));
59 buf[1] = (buf[2] = exp(-coupling));
60 return Factor( VarSet(n1, n2), &buf[0] );
61 }
62
63
64 Factor RandomFactor( const VarSet &ns, double beta ) {
65 Factor fac( ns );
66 for( size_t t = 0; t < fac.states(); t++ )
67 fac[t] = exp(rnd_stdnormal() * beta);
68 return fac;
69 }
70
71
72 Factor PottsFactor( const Var &n1, const Var &n2, double beta ) {
73 Factor fac( VarSet( n1, n2 ), 1.0 );
74 assert( n1.states() == n2.states() );
75 for( size_t s = 0; s < n1.states(); s++ )
76 fac[ s * (n1.states() + 1) ] = exp(beta);
77 return fac;
78 }
79
80
81 void MakeHOIFG( size_t N, size_t M, size_t k, double sigma, FactorGraph &fg ) {
82 vector<Var> vars;
83 vector<Factor> factors;
84
85 vars.reserve(N);
86 for( size_t i = 0; i < N; i++ )
87 vars.push_back(Var(i,2));
88
89 for( size_t I = 0; I < M; I++ ) {
90 VarSet vars;
91 while( vars.size() < k ) {
92 do {
93 size_t newind = (size_t)(N * rnd_uniform());
94 Var newvar = Var(newind, 2);
95 if( !vars.contains( newvar ) ) {
96 vars |= newvar;
97 break;
98 }
99 } while( 1 );
100 }
101 factors.push_back( RandomFactor( vars, sigma ) );
102 }
103
104 fg = FactorGraph( factors.begin(), factors.end(), vars.begin(), vars.end(), factors.size(), vars.size() );
105 }
106
107
108 // w should be upper triangular or lower triangular
109 void WTh2FG( const matrix &w, const vector<double> &th, FactorGraph &fg ) {
110 vector<Var> vars;
111 vector<Factor> factors;
112
113 size_t N = th.size();
114 assert( (w.size1() == N) && (w.size2() == N) );
115
116 vars.reserve(N);
117 for( size_t i = 0; i < N; i++ )
118 vars.push_back(Var(i,2));
119
120 factors.reserve( w.nnz() + N );
121 // walk through the sparse array structure
122 // this is similar to matlab sparse arrays
123 // index2 gives the column index (similar to ir in matlab)
124 // index1 gives the starting indices for each row (similar to jc in matlab)
125 size_t i = 0;
126 for( size_t pos = 0; pos < w.nnz(); pos++ ) {
127 while( pos == w.index1_data()[i+1] )
128 i++;
129 size_t j = w.index2_data()[pos];
130 double w_ij = w.value_data()[pos];
131 factors.push_back( BinaryFactor( vars[i], vars[j], w_ij ) );
132 }
133 for( size_t i = 0; i < N; i++ )
134 factors.push_back( BinaryFactor( vars[i], th[i] ) );
135
136 fg = FactorGraph( factors.begin(), factors.end(), vars.begin(), vars.end(), factors.size(), vars.size() );
137 }
138
139
140 void MakeFullFG( size_t N, double mean_w, double mean_th, double sigma_w, double sigma_th, FactorGraph &fg ) {
141 matrix w(N,N,N*(N-1)/2);
142 vector<double> th(N,0.0);
143
144 for( size_t i = 0; i < N; i++ ) {
145 for( size_t j = i+1; j < N; j++ )
146 w(i,j) = rnd_stdnormal() * sigma_w + mean_w;
147 th[i] = rnd_stdnormal() * sigma_th + mean_th;
148 }
149
150 WTh2FG( w, th, fg );
151 }
152
153
154 void Make3DPotts( size_t n1, size_t n2, size_t n3, size_t states, double beta, FactorGraph &fg ) {
155 vector<Var> vars;
156 vector<Factor> factors;
157
158 for( size_t i1 = 0; i1 < n1; i1++ )
159 for( size_t i2 = 0; i2 < n2; i2++ )
160 for( size_t i3 = 0; i3 < n3; i3++ ) {
161 vars.push_back( Var( i1*n2*n3 + i2*n3 + i3, states ) );
162 if( i1 )
163 factors.push_back( PottsFactor( vars.back(), vars[ (i1-1)*n2*n3 + i2*n3 + i3 ], beta ) );
164 if( i2 )
165 factors.push_back( PottsFactor( vars.back(), vars[ i1*n2*n3 + (i2-1)*n3 + i3 ], beta ) );
166 if( i3 )
167 factors.push_back( PottsFactor( vars.back(), vars[ i1*n2*n3 + i2*n3 + (i3-1) ], beta ) );
168 }
169
170 fg = FactorGraph( factors.begin(), factors.end(), vars.begin(), vars.end(), factors.size(), vars.size() );
171 }
172
173
174 void MakeGridFG( long periodic, size_t n, double mean_w, double mean_th, double sigma_w, double sigma_th, FactorGraph &fg ) {
175 size_t N = n*n;
176
177 matrix w(N,N,2*N);
178 vector<double> th(N,0.0);
179
180 for( size_t i = 0; i < n; i++ )
181 for( size_t j = 0; j < n; j++ ) {
182 if( i+1 < n || periodic )
183 w(i*n+j, ((i+1)%n)*n+j) = rnd_stdnormal() * sigma_w + mean_w;
184 if( j+1 < n || periodic )
185 w(i*n+j, i*n+((j+1)%n)) = rnd_stdnormal() * sigma_w + mean_w;
186 th[i*n+j] = rnd_stdnormal() * sigma_th + mean_th;
187 }
188
189 WTh2FG( w, th, fg );
190 }
191
192
193 void MakeGridNonbinaryFG( bool periodic, size_t n, size_t states, double beta, FactorGraph &fg ) {
194 size_t N = n*n;
195
196 vector<Var> vars;
197 vector<Factor> factors;
198
199 vars.reserve(N);
200 for( size_t i = 0; i < N; i++ )
201 vars.push_back(Var(i, states));
202
203 factors.reserve( 2 * N );
204 for( size_t i = 0; i < n; i++ ) {
205 for( size_t j = 0; j < n; j++ ) {
206 if( i+1 < n || periodic )
207 factors.push_back( RandomFactor( VarSet( vars[i*n+j], vars[((i+1)%n)*n+j] ), beta ) );
208 if( j+1 < n || periodic )
209 factors.push_back( RandomFactor( VarSet( vars[i*n+j], vars[i*n+((j+1)%n)] ), beta ) );
210 }
211 }
212
213 fg = FactorGraph( factors.begin(), factors.end(), vars.begin(), vars.end(), factors.size(), vars.size() );
214 }
215
216
217 void MakeLoopFG( size_t N, double mean_w, double mean_th, double sigma_w, double sigma_th, FactorGraph &fg ) {
218 matrix w(N,N,N);
219 vector<double> th(N,0.0);
220
221 for( size_t i = 0; i < N; i++ ) {
222 w(i, (i+1)%N) = rnd_stdnormal() * sigma_w + mean_w;
223 th[i] = rnd_stdnormal() * sigma_th + mean_th;
224 }
225
226 WTh2FG( w, th, fg );
227 }
228
229
230 void MakeLoopNonbinaryFG( size_t N, size_t states, double beta, FactorGraph &fg ) {
231 vector<Var> vars;
232 vector<Factor> factors;
233
234 vars.reserve(N);
235 for( size_t i = 0; i < N; i++ )
236 vars.push_back(Var(i, states));
237
238 factors.reserve( N );
239 for( size_t i = 0; i < N; i++ ) {
240 factors.push_back( RandomFactor( VarSet( vars[i], vars[(i+1)%N] ), beta ) );
241 }
242
243 fg = FactorGraph( factors.begin(), factors.end(), vars.begin(), vars.end(), factors.size(), vars.size() );
244 }
245
246
247 void MakeTreeFG( size_t N, double mean_w, double mean_th, double sigma_w, double sigma_th, FactorGraph &fg ) {
248 matrix w(N,N,N-1);
249 vector<double> th(N,0.0);
250
251 for( size_t i = 0; i < N; i++ ) {
252 th[i] = rnd_stdnormal() * sigma_th + mean_th;
253 if( i > 0 ) {
254 size_t j = rnd_int( 0, i-1 );
255 w(i,j) = rnd_stdnormal() * sigma_w + mean_w;
256 }
257 }
258
259 WTh2FG( w, th, fg );
260 }
261
262
263 void MakeDRegFG( size_t N, size_t d, double mean_w, double mean_th, double sigma_w, double sigma_th, FactorGraph &fg ) {
264 matrix w(N,N,(d*N)/2);
265 vector<double> th(N,0.0);
266
267 UEdgeVec g = RandomDRegularGraph( N, d );
268 for( size_t i = 0; i < g.size(); i++ )
269 w(g[i].n1, g[i].n2) = rnd_stdnormal() * sigma_w + mean_w;
270
271 for( size_t i = 0; i < N; i++ )
272 th[i] = rnd_stdnormal() * sigma_th + mean_th;
273
274 WTh2FG( w, th, fg );
275 }
276
277
278 // N = number of variables
279 // n = size of variable neighborhoods
280 // K = number of factors
281 // k = size of factor neighborhoods
282 // asserts: N * n == K * k
283 BipartiteGraph CreateRandomBipartiteGraph( size_t N, size_t K, size_t n, size_t k ) {
284 BipartiteGraph G;
285
286 assert( N * n == K * k );
287
288 // build lists of degree-repeated vertex numbers
289 std::vector<size_t> stubs1(N*n,0);
290 for( size_t i = 0; i < N; i++ )
291 for( size_t t = 0; t < n; t++ )
292 stubs1[i*n + t] = i;
293
294 // build lists of degree-repeated vertex numbers
295 std::vector<size_t> stubs2(K*k,0);
296 for( size_t I = 0; I < K; I++ )
297 for( size_t t = 0; t < k; t++ )
298 stubs2[I*k + t] = I;
299
300 // shuffle lists
301 random_shuffle( stubs1.begin(), stubs1.end() );
302 random_shuffle( stubs2.begin(), stubs2.end() );
303
304 // add edges
305 vector<BipartiteGraph::Edge> edges;
306 edges.reserve( N*n );
307 for( size_t e = 0; e < N*n; e++ )
308 edges.push_back( BipartiteGraph::Edge(stubs1[e], stubs2[e]) );
309
310 // finish construction
311 G.construct( N, K, edges.begin(), edges.end() );
312
313 return G;
314 }
315
316
317 // Returns x**n % p, assuming p is prime
318 int powmod (int x, int n, int p) {
319 int y = 1;
320 for( int m = 0; m < n; m++ )
321 y = (x * y) % p;
322 return y;
323 }
324
325
326 // Returns order of x in GF(p) with p prime
327 size_t order (int x, int p) {
328 x = x % p;
329 assert( x != 0 );
330 size_t n = 0;
331 size_t prod = 1;
332 do {
333 prod = (prod * x) % p;
334 n++;
335 } while( prod != 1 );
336 return n;
337 }
338
339
340 // Returns whether n is a prime number
341 bool isPrime (size_t n) {
342 bool result = true;
343 for( size_t k = 2; (k < n) && result; k++ )
344 if( n % k == 0 )
345 result = false;
346 return result;
347 }
348
349
350 // Make a regular LDPC graph with N=6, j=2, K=4, k=3
351 BipartiteGraph CreateSmallLDPCGraph() {
352 BipartiteGraph G;
353 size_t N=4, j=3, K=4; // k=3;
354
355 typedef BipartiteGraph::Edge Edge;
356 vector<Edge> edges;
357 edges.reserve( N*j );
358 edges.push_back( Edge(0,0) ); edges.push_back( Edge(1,0) ); edges.push_back( Edge(2,0) );
359 edges.push_back( Edge(0,1) ); edges.push_back( Edge(1,1) ); edges.push_back( Edge(3,1) );
360 edges.push_back( Edge(0,2) ); edges.push_back( Edge(2,2) ); edges.push_back( Edge(3,2) );
361 edges.push_back( Edge(1,3) ); edges.push_back( Edge(2,3) ); edges.push_back( Edge(3,3) );
362
363 // finish construction
364 G.construct( N, K, edges.begin(), edges.end() );
365
366 return G;
367 }
368
369
370 // Use construction described in "A Class of Group-Structured LDPC Codes"
371 // by R. M. Tanner, D. Sridhara and T. Fuja
372 // Proceedings of ICSTA, 2001
373 //
374 // Example parameters: (p,j,k) = (31,3,5)
375 // j and k must be divisors of p-1
376 BipartiteGraph CreateGroupStructuredLDPCGraph( size_t p, size_t j, size_t k ) {
377 BipartiteGraph G;
378
379 size_t n = j;
380 size_t N = p * k;
381 size_t K = p * j;
382
383 size_t a, b;
384 for( a = 2; a < p; a++ )
385 if( order(a,p) == k )
386 break;
387 assert( a != p );
388 for( b = 2; b < p; b++ )
389 if( order(b,p) == j )
390 break;
391 assert( b != p );
392 // cout << "# order(a=" << a << ") = " << order(a,p) << endl;
393 // cout << "# order(b=" << b << ") = " << order(b,p) << endl;
394
395 assert( N * n == K * k );
396
397 typedef BipartiteGraph::Edge Edge;
398 vector<Edge> edges;
399 edges.reserve( N * n );
400
401 for( size_t s = 0; s < j; s++ )
402 for( size_t t = 0; t < k; t++ ) {
403 size_t P = (powmod(b,s,p) * powmod(a,t,p)) % p;
404 for( size_t m = 0; m < p; m++ )
405 edges.push_back( Edge(t*p + m, s*p + ((m + P) % p)) );
406 }
407
408 // finish construction
409 G.construct( N, K, edges.begin(), edges.end() );
410
411 return G;
412 }
413
414
415 // Make parity check table
416 void MakeParityCheck( double *result, size_t n, double eps ) {
417 size_t N = 1 << n;
418 for( size_t i = 0; i < N; i++ ) {
419 size_t c = 0;
420 for( size_t t = 0; t < n; t++ )
421 if( i & (1 << t) )
422 c ^= 1;
423 if( c )
424 result[i] = eps;
425 else
426 result[i] = 1.0 - eps;
427 }
428 return;
429 }
430
431
432 const char *FULL_TYPE = "full";
433 const char *GRID_TYPE = "grid";
434 const char *GRID_TORUS_TYPE = "grid_torus";
435 const char *DREG_TYPE = "dreg";
436 const char *HOI_TYPE = "hoi";
437 const char *LDPC_RANDOM_TYPE = "ldpc_random";
438 const char *LDPC_GROUP_TYPE = "ldpc_group";
439 const char *LDPC_SMALL_TYPE = "ldpc_small";
440 const char *LOOP_TYPE = "loop";
441 const char *TREE_TYPE = "tree";
442 const char *POTTS3D_TYPE = "potts3d";
443
444
445 int main( int argc, char *argv[] ) {
446 try {
447 size_t N, K, k, d, j, n1, n2, n3;
448 size_t prime;
449 size_t seed;
450 double beta, sigma_w, sigma_th, noise, mean_w, mean_th;
451 string type;
452 size_t states = 2;
453
454 // Declare the supported options.
455 po::options_description desc("Allowed options");
456 desc.add_options()
457 ("help", "produce help message")
458 ("type", po::value<string>(&type), "factor graph type:\n\t'full', 'grid', 'grid_torus', 'dreg', 'loop', 'tree', 'hoi', 'ldpc_random', 'ldpc_group', 'ldpc_small', 'potts3d'")
459 ("seed", po::value<size_t>(&seed), "random number seed (tries to read from /dev/urandom if not specified)")
460 ("N", po::value<size_t>(&N), "number of variables (not for type=='ldpc_small')")
461 ("n1", po::value<size_t>(&n1), "width of 3D grid (only for type=='potts3d')")
462 ("n2", po::value<size_t>(&n2), "height of 3D grid (only for type=='potts3d')")
463 ("n3", po::value<size_t>(&n3), "length of 3D grid (only for type=='potts3d')")
464 ("K", po::value<size_t>(&K), "number of factors\n\t(only for type=='hoi' and 'type=='ldpc_{random,group}')")
465 ("k", po::value<size_t>(&k), "number of variables per factor\n\t(only for type=='hoi' and type=='ldpc_{random,group}')")
466 ("d", po::value<size_t>(&d), "variable connectivity\n\t(only for type=='dreg')")
467 ("j", po::value<size_t>(&j), "number of parity checks per bit\n\t(only for type=='ldpc_{random,group}')")
468 ("prime", po::value<size_t>(&prime), "prime number for construction of LDPC code\n\t(only for type=='ldpc_group')")
469 ("beta", po::value<double>(&beta), "stddev of log-factor entries\n\t(only for type=='hoi', 'potts3d', 'grid' if states>2)")
470 ("mean_w", po::value<double>(&mean_w), "mean of pairwise interactions w_{ij}\n\t(not for type=='hoi', 'ldpc_*', 'potts3d')")
471 ("mean_th", po::value<double>(&mean_th), "mean of singleton interactions th_i\n\t(not for type=='hoi', 'ldpc_*', 'potts3d')")
472 ("sigma_w", po::value<double>(&sigma_w), "stddev of pairwise interactions w_{ij}\n\t(not for type=='hoi', 'ldpc_*', 'potts3d')")
473 ("sigma_th", po::value<double>(&sigma_th), "stddev of singleton interactions th_i\n\t(not for type=='hoi', 'ldpc_*', 'potts3d'")
474 ("noise", po::value<double>(&noise), "bitflip probability for binary symmetric channel (only for type=='ldpc')")
475 ("states", po::value<size_t>(&states), "number of states of each variable (should be 2 for all but type=='grid', 'grid_torus', 'loop', 'potts3d')")
476 ;
477
478 po::variables_map vm;
479 po::store(po::parse_command_line(argc, argv, desc), vm);
480 po::notify(vm);
481
482 if( vm.count("help") || !vm.count("type") ) {
483 if( vm.count("type") ) {
484 if( type == FULL_TYPE ) {
485 cout << "Creates fully connected pairwise graphical model of <N> binary variables;" << endl;
486 } else if( type == GRID_TYPE ) {
487 cout << "Creates (non-periodic) 2D Ising grid of (approx.) <N> variables (which need not be binary);" << endl;
488 } else if( type == GRID_TORUS_TYPE ) {
489 cout << "Creates periodic 2D Ising grid of (approx.) <N> variables (which need not be binary);" << endl;
490 } else if( type == DREG_TYPE ) {
491 cout << "Creates random d-regular graph of <N> binary variables with uniform degree <d>" << endl;
492 cout << "(where <d><N> should be even);" << endl;
493 } else if( type == LOOP_TYPE ) {
494 cout << "Creates a pairwise graphical model consisting of a single loop of" << endl;
495 cout << "<N> variables (which need not be binary);" << endl;
496 } else if( type == TREE_TYPE ) {
497 cout << "Creates a pairwise, connected graphical model without cycles (i.e., a tree)" << endl;
498 cout << "of <N> binary variables;" << endl;
499 } else if( type == HOI_TYPE ) {
500 cout << "Creates a random factor graph of <N> binary variables and" << endl;
501 cout << "<K> factors, each factor being an interaction of <k> variables." << endl;
502 cout << "The entries of the factors are exponentials of i.i.d. Gaussian" << endl;
503 cout << "variables with mean 0 and standard deviation <beta>." << endl;
504 } else if( type == LDPC_RANDOM_TYPE ) {
505 cout << "Simulates LDPC decoding problem, using a LDPC code of <N> bits and <K> parity" << endl;
506 cout << "checks, with <k> bits per check and <j> checks per bit, transmitted on a binary" << endl;
507 cout << "symmetric channel with probability <noise> of flipping a bit. The transmitted" << endl;
508 cout << "codeword has all bits set to zero. The LDPC code is randomly generated." << endl;
509 } else if( type == LDPC_GROUP_TYPE ) {
510 cout << "Simulates LDPC decoding problem, using a LDPC code of <N> bits and <K> parity" << endl;
511 cout << "checks, with <k> bits per check and <j> checks per bit, transmitted on a binary" << endl;
512 cout << "symmetric channel with probability <noise> of flipping a bit. The transmitted" << endl;
513 cout << "codeword has all bits set to zero. The LDPC code is constructed (using group" << endl;
514 cout << "theory) using a parameter <prime>; <j> and <k> should both be divisors of <prime>-1." << endl;
515 } else if( type == LDPC_SMALL_TYPE ) {
516 cout << "Simulates LDPC decoding problem, using a LDPC code of 4 bits and 4 parity" << endl;
517 cout << "checks, with 3 bits per check and 3 checks per bit, transmitted on a binary" << endl;
518 cout << "symmetric channel with probability <noise> of flipping a bit. The transmitted" << endl;
519 cout << "codeword has all bits set to zero. The LDPC code is fixed." << endl;
520 } else if( type == POTTS3D_TYPE ) {
521 cout << "Builds 3D Potts model of size <n1>x<n2>x<n3> with nearest-neighbour Potts" << endl;
522 cout << "interactions with <states> states and inverse temperature <beta>." << endl;
523 } else
524 cerr << "Unknown type (should be one of 'full', 'grid', 'grid_torus', 'dreg', 'loop', 'tree', 'hoi', 'ldpc_random', 'ldpc_group', 'ldpc_small', 'potts3d')" << endl;
525
526 if( type == FULL_TYPE || type == GRID_TYPE || type == GRID_TORUS_TYPE || type == DREG_TYPE || type == LOOP_TYPE || type == TREE_TYPE ) {
527 if( type == GRID_TYPE || type == GRID_TORUS_TYPE || type == LOOP_TYPE ) {
528 cout << "if <states> > 2: factor entries are exponents of Gaussians with mean 0 and standard deviation beta; otherwise," << endl;
529 }
530 cout << "singleton interactions are Gaussian with mean <mean_th> and standard" << endl;
531 cout << "deviation <sigma_th>; pairwise interactions are Gaussian with mean" << endl;
532 cout << "<mean_w> and standard deviation <sigma_w>." << endl;
533 }
534 }
535 cout << endl << desc << endl;
536 return 1;
537 }
538
539 if( !vm.count("states") )
540 states = 2;
541
542 if( !vm.count("seed") ) {
543 ifstream infile;
544 bool success;
545 infile.open( "/dev/urandom" );
546 success = infile.is_open();
547 if( success ) {
548 infile.read( (char *)&seed, sizeof(size_t) / sizeof(char) );
549 success = infile.good();
550 infile.close();
551 }
552 if( !success )
553 throw "Please specify random number seed.";
554 }
555 rnd_seed( seed );
556
557 FactorGraph fg;
558
559 cout << "# Factor graph made by " << argv[0] << endl;
560 cout << "# type = " << type << endl;
561
562 if( type == FULL_TYPE ) {
563 if( !vm.count("N") || !vm.count("mean_w") || !vm.count("mean_th") || !vm.count("sigma_w") || !vm.count("sigma_th") )
564 throw "Please specify all required arguments";
565 MakeFullFG( N, mean_w, mean_th, sigma_w, sigma_th, fg );
566
567 cout << "# N = " << N << endl;
568 cout << "# mean_w = " << mean_w << endl;
569 cout << "# mean_th = " << mean_th << endl;
570 cout << "# sigma_w = " << sigma_w << endl;
571 cout << "# sigma_th = " << sigma_th << endl;
572 } else if( type == GRID_TYPE || type == GRID_TORUS_TYPE ) {
573 if( states > 2 ) {
574 if( !vm.count("N") || !vm.count("beta") )
575 throw "Please specify all required arguments";
576 } else {
577 if( !vm.count("N") || !vm.count("mean_w") || !vm.count("mean_th") || !vm.count("sigma_w") || !vm.count("sigma_th") )
578 throw "Please specify all required arguments";
579 }
580
581 size_t n = (size_t)sqrt((long double)N);
582 N = n * n;
583
584 bool periodic = false;
585 if( type == GRID_TYPE )
586 periodic = false;
587 else
588 periodic = true;
589
590 if( states > 2 )
591 MakeGridNonbinaryFG( periodic, n, states, beta, fg );
592 else
593 MakeGridFG( periodic, n, mean_w, mean_th, sigma_w, sigma_th, fg );
594
595 cout << "# n = " << n << endl;
596 cout << "# N = " << N << endl;
597
598 if( states > 2 )
599 cout << "# beta = " << beta << endl;
600 else {
601 cout << "# mean_w = " << mean_w << endl;
602 cout << "# mean_th = " << mean_th << endl;
603 cout << "# sigma_w = " << sigma_w << endl;
604 cout << "# sigma_th = " << sigma_th << endl;
605 }
606 } else if( type == DREG_TYPE ) {
607 if( !vm.count("N") || !vm.count("mean_w") || !vm.count("mean_th") || !vm.count("sigma_w") || !vm.count("sigma_th") || !vm.count("d") )
608 throw "Please specify all required arguments";
609
610 MakeDRegFG( N, d, mean_w, mean_th, sigma_w, sigma_th, fg );
611
612 cout << "# N = " << N << endl;
613 cout << "# d = " << d << endl;
614 cout << "# mean_w = " << mean_w << endl;
615 cout << "# mean_th = " << mean_th << endl;
616 cout << "# sigma_w = " << sigma_w << endl;
617 cout << "# sigma_th = " << sigma_th << endl;
618 } else if( type == LOOP_TYPE ) {
619 if( states > 2 ) {
620 if( !vm.count("N") || !vm.count("beta") )
621 throw "Please specify all required arguments";
622 } else {
623 if( !vm.count("N") || !vm.count("mean_w") || !vm.count("mean_th") || !vm.count("sigma_w") || !vm.count("sigma_th") )
624 throw "Please specify all required arguments";
625 }
626 if( states > 2 )
627 MakeLoopNonbinaryFG( N, states, beta, fg );
628 else
629 MakeLoopFG( N, mean_w, mean_th, sigma_w, sigma_th, fg );
630
631 cout << "# N = " << N << endl;
632
633 if( states > 2 )
634 cout << "# beta = " << beta << endl;
635 else {
636 cout << "# mean_w = " << mean_w << endl;
637 cout << "# mean_th = " << mean_th << endl;
638 cout << "# sigma_w = " << sigma_w << endl;
639 cout << "# sigma_th = " << sigma_th << endl;
640 }
641 } else if( type == TREE_TYPE ) {
642 if( !vm.count("N") || !vm.count("mean_w") || !vm.count("mean_th") || !vm.count("sigma_w") || !vm.count("sigma_th") )
643 throw "Please specify all required arguments";
644 MakeTreeFG( N, mean_w, mean_th, sigma_w, sigma_th, fg );
645
646 cout << "# N = " << N << endl;
647 cout << "# mean_w = " << mean_w << endl;
648 cout << "# mean_th = " << mean_th << endl;
649 cout << "# sigma_w = " << sigma_w << endl;
650 cout << "# sigma_th = " << sigma_th << endl;
651 } else if( type == HOI_TYPE ) {
652 if( !vm.count("N") || !vm.count("K") || !vm.count("k") || !vm.count("beta") )
653 throw "Please specify all required arguments";
654 do {
655 MakeHOIFG( N, K, k, beta, fg );
656 } while( !fg.isConnected() );
657
658 cout << "# N = " << N << endl;
659 cout << "# K = " << K << endl;
660 cout << "# k = " << k << endl;
661 cout << "# beta = " << beta << endl;
662 } else if( type == LDPC_RANDOM_TYPE || type == LDPC_GROUP_TYPE || type == LDPC_SMALL_TYPE ) {
663 if( !vm.count("noise") )
664 throw "Please specify all required arguments";
665
666 if( type == LDPC_RANDOM_TYPE ) {
667 if( !vm.count("N") || !vm.count("K") || !vm.count("j") || !vm.count("k") )
668 throw "Please specify all required arguments";
669
670 if( N * j != K * k )
671 throw "Parameters should satisfy N * j == K * k";
672 } else if( type == LDPC_GROUP_TYPE ) {
673 if( !vm.count("prime") || !vm.count("j") || !vm.count("k") )
674 throw "Please specify all required arguments";
675
676 if( !isPrime(prime) )
677 throw "Parameter <prime> should be prime";
678 if( !((prime-1) % j == 0 ) )
679 throw "Parameters should satisfy (prime-1) % j == 0";
680 if( !((prime-1) % k == 0 ) )
681 throw "Parameters should satisfy (prime-1) % k == 0";
682
683 N = prime * k;
684 K = prime * j;
685 } else if( type == LDPC_SMALL_TYPE ) {
686 N = 4;
687 K = 4;
688 j = 3;
689 k = 3;
690 }
691
692 cout << "# N = " << N << endl;
693 cout << "# K = " << K << endl;
694 cout << "# j = " << j << endl;
695 cout << "# k = " << k << endl;
696 if( type == LDPC_GROUP_TYPE )
697 cout << "# prime = " << prime << endl;
698 cout << "# noise = " << noise << endl;
699
700 // p = 31, j = 3, k = 5
701 // p = 37, j = 3, k = 4
702 // p = 7 , j = 2, k = 3
703 // p = 29, j = 2, k = 4
704
705 // Construct likelihood and paritycheck factors
706 double likelihood[4] = {1.0 - noise, noise, noise, 1.0 - noise};
707 double *paritycheck = new double[1 << k];
708 MakeParityCheck(paritycheck, k, 0.0);
709
710 // Create LDPC structure
711 BipartiteGraph ldpcG;
712 bool regular;
713 do {
714 if( type == LDPC_GROUP_TYPE )
715 ldpcG = CreateGroupStructuredLDPCGraph( prime, j, k );
716 else if( type == LDPC_RANDOM_TYPE )
717 ldpcG = CreateRandomBipartiteGraph( N, K, j, k );
718 else if( type == LDPC_SMALL_TYPE )
719 ldpcG = CreateSmallLDPCGraph();
720
721 regular = true;
722 for( size_t i = 0; i < N; i++ )
723 if( ldpcG.nb1(i).size() != j )
724 regular = false;
725 for( size_t I = 0; I < K; I++ )
726 if( ldpcG.nb2(I).size() != k )
727 regular = false;
728 } while( !regular && !ldpcG.isConnected() );
729
730 // Convert to FactorGraph
731 vector<Factor> factors;
732 for( size_t I = 0; I < K; I++ ) {
733 VarSet vs;
734 for( size_t _i = 0; _i < k; _i++ ) {
735 size_t i = ldpcG.nb2(I)[_i];
736 vs |= Var( i, 2 );
737 }
738 factors.push_back( Factor( vs, paritycheck ) );
739 }
740 delete paritycheck;
741
742 // Generate noise vector
743 vector<char> noisebits(N,0);
744 size_t bitflips = 0;
745 for( size_t i = 0; i < N; i++ ) {
746 if( rnd_uniform() < noise ) {
747 noisebits[i] = 1;
748 bitflips++;
749 }
750 }
751 cout << "# bitflips = " << bitflips << endl;
752
753 // Simulate transmission of all-zero codeword
754 vector<char> input(N,0);
755 vector<char> output(N,0);
756 for( size_t i = 0; i < N; i++ )
757 output[i] = (input[i] + noisebits[i]) & 1;
758
759 // Add likelihoods
760 for( size_t i = 0; i < N; i++ )
761 factors.push_back( Factor(Var(i,2), likelihood + output[i]*2) );
762
763 // Construct Factor Graph
764 fg = FactorGraph( factors );
765 } else if( type == POTTS3D_TYPE ) {
766 if( !vm.count("n1") || !vm.count("n2") || !vm.count("n3") || !vm.count("beta") || !vm.count("states") )
767 throw "Please specify all required arguments";
768 Make3DPotts( n1, n2, n3, states, beta, fg );
769
770 cout << "# N = " << n1*n2*n3 << endl;
771 cout << "# n1 = " << n1 << endl;
772 cout << "# n2 = " << n2 << endl;
773 cout << "# n3 = " << n3 << endl;
774 cout << "# beta = " << beta << endl;
775 cout << "# states = " << states << endl;
776 } else {
777 throw "Invalid type";
778 }
779
780 cout << "# seed = " << seed << endl;
781 cout << fg;
782 } catch( const char *e ) {
783 cerr << "Error: " << e << endl;
784 return 1;
785 }
786
787 return 0;
788 }