OpenGM  2.3.x
Discrete Graphical Model Library
models/syntheticmodelgenerator.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_SYNTHETIC_MODEL_GENERATOR_HXX
3 #define OPENGM_SYNTHETIC_MODEL_GENERATOR_HXX
4 
6 
7 #include <cstdlib>
8 #include <vector>
9 #include <cstdlib>
10 #include <set>
11 #include <functional>
12 
14 
15 namespace opengm {
16 
17  template<class GM> class SyntheticModelGenerator
18  {
19  typedef GM GraphicalModelType;
20  typedef typename GM::ValueType ValueType;
21  typedef typename GM::ExplicitFunctionType ExplicitFunctionType;
22  //typedef typename GM::SparseFunctionType SparseFunctionType;
23  //typedef typename GM::ImplicitFunctionType ImplicitFunctionType;
24  typedef typename GM::FunctionIdentifier FunctionIdentifier;
25  typedef typename GM::OperatorType OperatorType;
26  // typedef typename opengm::GraphicalModel<ValueType,Operatort,opengm::DefaultImplicitFunction<ValueType>,GraphicalModelType::isMutable > RebindGmType;
27  public:
28  enum FunktionTypes {RANDOM, Potts, GPotts};
29  bool randomNumberOfStates_;
30  SyntheticModelGenerator();
31  SyntheticModelGenerator(bool randomNumberOfStates);
32  GM buildGrid2(size_t height, size_t width, size_t numStates, unsigned int id, FunktionTypes ft2, ValueType l1, ValueType l2);
33  GM buildGrid02(size_t height, size_t width, size_t numStates, unsigned int id, FunktionTypes ft2, ValueType l2);
34  GM buildFull2(size_t var, size_t numStates, unsigned int id, FunktionTypes ft2, ValueType l1, ValueType l2);
35  GM buildFull02(size_t var, size_t numStates, unsigned int id, FunktionTypes ft2, ValueType l2);
36  GM buildStar2(size_t var, size_t numStates, unsigned int id, FunktionTypes ft2, ValueType l1, ValueType l2);
37  //GM buildTree2(size_t var, size_t numStates, unsigned int id, FunktionTypes ft2, ValueType l1, ValueType l2);
38  //GM buildFull3(size_t var, size_t numStates, unsigned int id, FunktionTypes ft3, ValueType l1, ValueType l3);
39  GM buildPottsGrid2(size_t height, size_t width, size_t numStates, unsigned int id, ValueType l1=3, ValueType l2=1);
40  GM buildPottsFull2(size_t numVar, size_t numStates, unsigned int id, ValueType l1=3, ValueType l2=1);
41  GM buildGPottsFull02(size_t numVar, size_t numStates, unsigned int id, ValueType l2=1);
42  GM buildGPottsGrid02(size_t height, size_t width, size_t numStates, unsigned int id, ValueType l2=1);
43  GM buildPottsFull02(size_t numVar, size_t numStates, unsigned int id, ValueType l2=1);
44  GM buildPottsGrid02(size_t height, size_t width, size_t numStates, unsigned int id, ValueType l2=1);
45  GM buildRandomFull2(size_t numVar, size_t numStates, unsigned int id, ValueType l1=1, ValueType l2=1);
46  GM buildRandomGrid2(size_t height, size_t width, size_t numStates, unsigned int id, ValueType l1=3, ValueType l2=1);
47  GM buildRandomStar2(size_t numVar, size_t numStates, unsigned int id, ValueType l1=3, ValueType l2=1);
48  //GM buildRandomFull23(size_t numVar, size_t numStates, unsigned int id);
49  //GM buildRandomFull3(size_t numVar, size_t numStates, unsigned int id);
50  private:
51  void addUnaries(GM& gm, ValueType lambda);
52  FunctionIdentifier addFunktion(GM& gm, ValueType lambda, FunktionTypes ft, size_t* beginShape, size_t* endShape);
53  };
54  template<class GM>
55  SyntheticModelGenerator<GM>::SyntheticModelGenerator()
56  {
57  randomNumberOfStates_ = false;
58  }
59  template<class GM>
60  SyntheticModelGenerator<GM>::SyntheticModelGenerator(bool randomNumberOfStates)
61  {
62  randomNumberOfStates_ = randomNumberOfStates;
63  }
64  template<class GM>
65  void SyntheticModelGenerator<GM>::addUnaries(GM& gm, ValueType lambda1)
66  {
67  size_t shape[1];
68  size_t var[]={0};
69  for(size_t i=0;i<gm.numberOfVariables();++i) {
70  shape[0] = gm.numberOfLabels(i);
71  var[0] = i;
72  ExplicitFunctionType function(shape,shape+1);
73  for(size_t ni=0; ni<shape[0]; ++ni) {
74  function(ni)= lambda1 * (rand() % 1000000)*0.000001 + 1;
75  }
76  FunctionIdentifier funcId=gm.addFunction(function);
77  gm.addFactor(funcId,var,var+1);
78  }
79  }
80  template<class GM>
81  typename GM::FunctionIdentifier SyntheticModelGenerator<GM>::addFunktion
82  (
83  GM& gm,
84  ValueType lambda,
85  FunktionTypes ft,
86  size_t* beginShape,
87  size_t* endShape
88  )
89  {
90  if(ft==RANDOM) {
91  ExplicitFunctionType function(beginShape,endShape);
92  for(size_t ni=0; ni<beginShape[0]; ++ni) {
93  for(size_t nj=0; nj<beginShape[1]; ++nj) {
94  function(ni,nj) = lambda * (rand() % 1000000)*0.000001 + 1;
95  }
96  }
97  FunctionIdentifier funcId=gm.addFunction(function);
98  return funcId;
99  }
100  else if(ft==Potts) {
101  ExplicitFunctionType function(beginShape,endShape);
102  for(size_t ni=0; ni<beginShape[0]; ++ni) {
103  for(size_t nj=0; nj<beginShape[1]; ++nj) {
104  if(ni==nj) function(ni,nj) = 0;//OperatorType::neutral();
105  else function(ni,nj) = lambda;
106  }
107  }
108  FunctionIdentifier funcId=gm.addFunction(function);
109  return funcId;
110  }
111  else if(ft==GPotts) {
112  double v = ((rand()%10000)-5000)/5000.0;
113  ExplicitFunctionType function(beginShape,endShape);
114  for(size_t ni=0; ni<beginShape[0]; ++ni) {
115  for(size_t nj=0; nj<beginShape[1]; ++nj) {
116  if(ni==nj) function(ni,nj) = 0;//OperatorType::neutral();
117  else function(ni,nj) = lambda * v;
118  }
119  }
120  FunctionIdentifier funcId=gm.addFunction(function);
121  return funcId;
122  }
123  else{
124  //throw exception
125  FunctionIdentifier funcId;
126  return funcId;
127  }
128  }
129  template<class GM>
130  GM SyntheticModelGenerator<GM>::buildGrid2
131  (
132  size_t height,
133  size_t width,
134  size_t numStates,
135  unsigned int id,
136  FunktionTypes ft2,
137  ValueType lambda1,
138  ValueType lambda2
139  )
140  {
141  srand(id);
142  size_t N=height*width;
143  std::vector<size_t> variableStates(N,numStates);
144  if(randomNumberOfStates_) {
145  for(size_t i=0; i<N;++i) {
146  variableStates[i] = (rand() % (numStates-1))+1;
147  }
148  }
149  GraphicalModelType gm(variableStates.begin(),variableStates.end());
150  //UNARY
151  addUnaries(gm, lambda1);
152  //PAIRWISE
153  size_t shape[2];
154  size_t var[2];
155  if(randomNumberOfStates_==false ) {
156  shape[0] = shape[1] =gm.numberOfLabels(0);
157  FunctionIdentifier funcId = addFunktion(gm,lambda2,ft2, shape,shape+2);
158  for(size_t i=0;i<height;++i) {
159  for(size_t j=0;j<width;++j) {
160  size_t v = i+height*j;
161  if(i+1<height) {
162  var[0] = v;
163  var[1] = i+1+height*j;
164  gm.addFactor(funcId,var,var+2);
165  }
166  if(j+1<width) {
167  var[0] = v;
168  var[1] = i+height*(j+1);
169  gm.addFactor(funcId,var,var+2);
170  }
171  }
172  }
173  }
174  else{
175  for(size_t i=0;i<height;++i) {
176  for(size_t j=0;j<width;++j) {
177  size_t v = i+height*j;
178  if(i+1<height) {
179  var[0] = v;
180  var[1] = i+1+height*j;
181  shape[0] = gm.numberOfLabels(var[0]);
182  shape[1] = gm.numberOfLabels(var[1]);
183  FunctionIdentifier funcId = addFunktion(gm,lambda2,ft2, shape,shape+2);
184  gm.addFactor(funcId,var,var+2);
185  }
186  if(j+1<width) {
187  var[0] = v;
188  var[1] = i+height*(j+1);
189  shape[0] = gm.numberOfLabels(var[0]);
190  shape[1] = gm.numberOfLabels(var[1]);
191  FunctionIdentifier funcId = addFunktion(gm,lambda2,ft2, shape,shape+2);
192  gm.addFactor(funcId,var,var+2);
193  }
194  }
195  }
196  }
197  return gm;
198  }
199  template<class GM>
200  GM SyntheticModelGenerator<GM>::buildGrid02
201  (
202  size_t height,
203  size_t width,
204  size_t numStates,
205  unsigned int id,
206  FunktionTypes ft2,
207  ValueType lambda2
208  )
209  {
210  srand(id);
211  size_t N=height*width;
212  std::vector<size_t> variableStates(N,numStates);
213  if(randomNumberOfStates_) {
214  for(size_t i=0; i<N;++i) {
215  variableStates[i] = (rand() % (numStates-1))+1;
216  }
217  }
218  GraphicalModelType gm(variableStates.begin(),variableStates.end());
219  //PAIRWISE
220  size_t shape[2];
221  size_t var[2];
222  if(randomNumberOfStates_==false ) {
223  shape[0] = shape[1] =gm.numberOfLabels(0);
224  for(size_t i=0;i<height;++i) {
225  for(size_t j=0;j<width;++j) {
226  size_t v = i+height*j;
227  if(i+1<height) {
228  var[0] = v;
229  var[1] = i+1+height*j;
230  FunctionIdentifier funcId = addFunktion(gm,lambda2,ft2, shape,shape+2);
231  gm.addFactor(funcId,var,var+2);
232  }
233  if(j+1<width) {
234  var[0] = v;
235  var[1] = i+height*(j+1);
236  FunctionIdentifier funcId = addFunktion(gm,lambda2,ft2, shape,shape+2);
237  gm.addFactor(funcId,var,var+2);
238  }
239  }
240  }
241  }
242  else{
243  for(size_t i=0;i<height;++i) {
244  for(size_t j=0;j<width;++j) {
245  size_t v = i+height*j;
246  if(i+1<height) {
247  var[0] = v;
248  var[1] = i+1+height*j;
249  shape[0] = gm.numberOfLabels(var[0]);
250  shape[1] = gm.numberOfLabels(var[1]);
251  FunctionIdentifier funcId = addFunktion(gm,lambda2,ft2, shape,shape+2);
252  gm.addFactor(funcId,var,var+2);
253  }
254  if(j+1<width) {
255  var[0] = v;
256  var[1] = i+height*(j+1);
257  shape[0] = gm.numberOfLabels(var[0]);
258  shape[1] = gm.numberOfLabels(var[1]);
259  FunctionIdentifier funcId = addFunktion(gm,lambda2,ft2, shape,shape+2);
260  gm.addFactor(funcId,var,var+2);
261  }
262  }
263  }
264  }
265  return gm;
266  }
267  template<class GM>
268  GM SyntheticModelGenerator<GM>::buildFull2
269  (
270  size_t numberOfVariables,
271  size_t numberOfLabels,
272  unsigned int id,
273  FunktionTypes ft2,
274  ValueType lambda1,
275  ValueType lambda2
276  )
277  {
278  srand(id);
279  std::vector<size_t> variableStates(numberOfVariables,numberOfLabels);
280  if(randomNumberOfStates_) {
281  for(size_t i=0; i<numberOfVariables;++i) {
282  variableStates[i] = (rand() % (numberOfLabels-1))+1;
283  }
284  }
285  GraphicalModelType gm(variableStates.begin(),variableStates.end());
286  //UNARY
287  addUnaries(gm, lambda1);
288  //PAIRWISE
289  size_t shape[2];
290  size_t var[2];
291  if(randomNumberOfStates_==false )
292  {
293  shape[0]=gm.numberOfLabels(0);
294  shape[1]=gm.numberOfLabels(0);
295  FunctionIdentifier funcId = addFunktion(gm,lambda2,ft2, shape, shape+2);
296  for(size_t i=0;i<numberOfVariables;++i)
297  {
298  for(size_t j=i+1;j<numberOfVariables;++j)
299  {
300  var[0] = i;
301  var[1] = j;
302  gm.addFactor(funcId,var,var+2);
303  }
304  }
305  }
306  else{
307  for(size_t i=0;i<numberOfVariables;++i) {
308  for(size_t j=i+1;j<numberOfVariables;++j) {
309  var[0] = i;
310  var[1] = j;
311  shape[0] = gm.numberOfLabels(var[0]);
312  shape[1] = gm.numberOfLabels(var[1]);
313  FunctionIdentifier funcId = addFunktion(gm,lambda2,ft2, shape,shape+2);
314  gm.addFactor(funcId,var,var+2);
315  }
316  }
317  }
318  return gm;
319  }
320  template<class GM>
321  GM SyntheticModelGenerator<GM>::buildFull02
322  (
323  size_t numberOfVariables,
324  size_t numberOfLabels,
325  unsigned int id,
326  FunktionTypes ft2,
327  ValueType lambda2
328  )
329  {
330  srand(id);
331  std::vector<size_t> variableStates(numberOfVariables,numberOfLabels);
332  if(randomNumberOfStates_) {
333  for(size_t i=0; i<numberOfVariables;++i) {
334  variableStates[i] = (rand() % (numberOfLabels-1))+1;
335  }
336  }
337  GraphicalModelType gm(variableStates.begin(),variableStates.end());
338  //PAIRWISE
339  size_t shape[2];
340  size_t var[2];
341  if(randomNumberOfStates_==false )
342  {
343  shape[0]=gm.numberOfLabels(0);
344  shape[1]=gm.numberOfLabels(0);
345  for(size_t i=0;i<numberOfVariables;++i)
346  {
347  for(size_t j=i+1;j<numberOfVariables;++j)
348  {
349  var[0] = i;
350  var[1] = j;
351  FunctionIdentifier funcId = addFunktion(gm,lambda2,ft2, shape, shape+2);
352  gm.addFactor(funcId,var,var+2);
353  }
354  }
355  }
356  else{
357  for(size_t i=0;i<numberOfVariables;++i) {
358  for(size_t j=i+1;j<numberOfVariables;++j) {
359  var[0] = i;
360  var[1] = j;
361  shape[0] = gm.numberOfLabels(var[0]);
362  shape[1] = gm.numberOfLabels(var[1]);
363  FunctionIdentifier funcId = addFunktion(gm,lambda2,ft2, shape,shape+2);
364  gm.addFactor(funcId,var,var+2);
365  }
366  }
367  }
368  return gm;
369  }
370  template<class GM>
371  GM SyntheticModelGenerator<GM>::buildStar2
372  (
373  size_t numberOfVariables,
374  size_t numberOfLabels,
375  unsigned int id,
376  FunktionTypes ft2,
377  ValueType lambda1,
378  ValueType lambda2
379  )
380  {
381  srand(id);
382  std::vector<size_t> variableStates(numberOfVariables,numberOfLabels);
383  if(randomNumberOfStates_) {
384  for(size_t i=0; i<numberOfVariables;++i) {
385  variableStates[i] = (rand() % (numberOfLabels-1))+1;
386  }
387  }
388  GraphicalModelType gm(variableStates.begin(),variableStates.end());
389  size_t root = (rand() % numberOfVariables);
390  //UNARY
391  addUnaries(gm, lambda1);
392  //PAIRWISE
393  size_t shape[2];
394  size_t var[2];
395  if(randomNumberOfStates_==false ) {
396  shape[0] = shape[1] =gm.numberOfLabels(0);
397  FunctionIdentifier funcId = addFunktion(gm,lambda2,ft2, shape,shape+2);
398  for(size_t i=0;i<root;++i) {
399  var[0] = i;
400  var[1] = root;
401  gm.addFactor(funcId,var,var+2);
402  }
403  for(size_t i=root+1;i<numberOfVariables;++i) {
404  var[0] = root;
405  var[1] = i;
406  gm.addFactor(funcId,var,var+2);
407  }
408  }
409  else{
410  for(size_t i=0;i<root;++i) {
411  var[0] = i;
412  var[1] = root;
413  shape[0] = gm.numberOfLabels(var[0]);
414  shape[1] = gm.numberOfLabels(var[1]);
415  FunctionIdentifier funcId = addFunktion(gm,lambda2,ft2, shape,shape+2);
416  gm.addFactor(funcId,var,var+2);
417  }
418  for(size_t i=root+1;i<numberOfVariables;++i) {
419  var[0] = root;
420  var[1] = i;
421  shape[0] = gm.numberOfLabels(var[0]);
422  shape[1] = gm.numberOfLabels(var[1]);
423  FunctionIdentifier funcId = addFunktion(gm,lambda2,ft2, shape,shape+2);
424  gm.addFactor(funcId,var,var+2);
425  }
426  }
427  return gm;
428  }
433  template<class GM>
434  GM SyntheticModelGenerator<GM>::buildPottsGrid2
435  (size_t height, size_t width, size_t numStates, unsigned int id, ValueType l1, ValueType l2)
436  {return buildGrid2(height, width, numStates, id, Potts, l1, l2);}
437  template<class GM>
438  GM SyntheticModelGenerator<GM>::buildPottsGrid02
439  (size_t height, size_t width, size_t numStates, unsigned int id, ValueType l2)
440  {return buildGrid02(height, width, numStates, id, Potts, l2);}
441  template<class GM>
442  GM SyntheticModelGenerator<GM>::buildGPottsGrid02
443  (size_t height, size_t width, size_t numStates, unsigned int id, ValueType l2)
444  {return buildGrid02(height, width, numStates, id, GPotts, l2);}
445  template<class GM>
446  GM SyntheticModelGenerator<GM>::buildRandomGrid2
447  (size_t height, size_t width, size_t numStates, unsigned int id, ValueType l1, ValueType l2)
448  {return buildGrid2(height, width, numStates, id, RANDOM, l1, l2);}
449  template<class GM>
450  GM SyntheticModelGenerator<GM>::buildPottsFull2
451  (size_t numVar, size_t numStates, unsigned int id, ValueType l1, ValueType l2)
452  {return buildFull2(numVar, numStates, id, Potts, l1, l2);}
453  template<class GM>
454  GM SyntheticModelGenerator<GM>::buildPottsFull02
455  (size_t numVar, size_t numStates, unsigned int id, ValueType l2)
456  {return buildFull02(numVar, numStates, id, Potts, l2);}
457  template<class GM>
458  GM SyntheticModelGenerator<GM>::buildGPottsFull02
459  (size_t numVar, size_t numStates, unsigned int id, ValueType l2)
460  {return buildFull02(numVar, numStates, id, GPotts, l2);}
461  template<class GM>
462  GM SyntheticModelGenerator<GM>::buildRandomFull2
463  (size_t numVar, size_t numStates, unsigned int id, ValueType l1, ValueType l2)
464  {return buildFull2(numVar, numStates, id, RANDOM, l1, l2);}
465  template<class GM>
466  GM SyntheticModelGenerator<GM>::buildRandomStar2
467  (size_t numVar, size_t numStates, unsigned int id, ValueType l1, ValueType l2)
468  {return buildStar2(numVar, numStates, id, RANDOM, l1, l2);}
469  /*
470  template<class GM>
471  GM ModelGenerator<GM>::buildPottsGrid(size_t height, size_t width, size_t numStates, unsigned int id, bool variableNumStates)
472  {
473  srand(id);
474  size_t N=height*width;
475  std::vector<size_t> variableStates(N,numStates);
476  if(variableNumStates) {
477  for(size_t i=0; i<N;++i) {
478  variableStates[i] = (rand() % (numStates-1))+1;
479  }
480  }
481  GraphicalModelType gm(variableStates.begin(),variableStates.end());
482  //UNARY
483  {
484  size_t shape[1];
485  size_t var[]={0};
486  for(size_t i=0;i<N;++i) {
487  shape[0] = variableStates[i];
488  var[0] = i;
489  ExplicitFunctionType function(shape,shape+1);
490  for(size_t ni=0; ni<variableStates[i]; ++ni) {
491  function(ni)= (rand() % 1000)*0.001;
492  }
493  FunctionIdentifier funcId=gm.addFunction(function);
494  gm.addFactor(funcId,var,var+1);
495  }
496  }
497  //PAIRWISE
498  if(variableNumStates==false) {
499  size_t shape[2];
500  shape[0] = shape[1] = numStates;
501  size_t var[]={0,0};
502  ExplicitFunctionType function(shape,shape+2);
503  for(size_t ni=0; ni<numStates; ++ni) {
504  for(size_t nj=0; nj<numStates; ++nj) {
505  if(ni==nj) function(ni,nj) = 0;
506  else function(ni,nj) = 1;
507  }
508  }
509  FunctionIdentifier funcId=gm.addFunction(function);
510  for(size_t i=0;i<height;++i) {
511  for(size_t j=0;j<width;++j) {
512  size_t v = i+height*j;
513  if(i+1<height) {
514  var[0] = v;
515  var[1] = i+1+height*j;
516  gm.addFactor(funcId,var,var+2);
517  }
518  if(j+1<width) {
519  var[0] = v;
520  var[1] = i+height*(j+1);
521  gm.addFactor(funcId,var,var+2);
522  }
523  }
524  }
525  }
526  else{
527  size_t shape[2];
528  size_t var[]={0,0};
529  for(size_t i=0;i<height;++i) {
530  for(size_t j=0;j<width;++j) {
531  size_t v = i+height*j;
532  if(i+1<height) {
533  var[0] = v;
534  var[1] = i+1+height*j;
535  shape[0] = variableStates[var[0]];
536  shape[1] = variableStates[var[1]];
537  ExplicitFunctionType function(shape,shape+2);
538  for(size_t ni=0; ni<shape[0]; ++ni) {
539  for(size_t nj=0; nj<shape[1]; ++nj) {
540  if(ni==nj) function(ni,nj) = 0;
541  else function(ni,nj) = 1;
542  }
543  }
544  FunctionIdentifier funcId=gm.addFunction(function);
545  gm.addFactor(funcId,var,var+2);
546  }
547  if(j+1<width) {
548  var[0] = v;
549  var[1] = i+height*(j+1);
550  shape[0] = variableStates[var[0]];
551  shape[1] = variableStates[var[1]];
552  ExplicitFunctionType function(shape,shape+2);
553  for(size_t ni=0; ni<shape[0]; ++ni) {
554  for(size_t nj=0; nj<shape[1]; ++nj) {
555  if(ni==nj) function(ni,nj) = 0;
556  else function(ni,nj) = 1;
557  }
558  }
559  FunctionIdentifier funcId=gm.addFunction(function);
560  gm.addFactor(funcId,var,var+2);
561  }
562  }
563  }
564  }
565  return gm;
566  }
567  template<class GM>
568  GM ModelGenerator<GM>::buildPottsFull(size_t numVar, size_t numStates, unsigned int id, bool variableNumStates) {
569  srand(id);
570  size_t N=numVar;
571  std::vector<size_t> variableStates(N,numStates);
572  if(variableNumStates) {
573  for(size_t i=0; i<N;++i) {
574  variableStates[i] = (rand() % (numStates-1))+1;
575  }
576  }
577  GraphicalModelType gm(variableStates.begin(),variableStates.end());
578  //UNARY
579  {
580  size_t shape[1];
581  size_t var[]={0};
582  for(size_t i=0;i<N;++i) {
583  var[0] = i;
584  shape[0] = variableStates[i];
585  ExplicitFunctionType function(shape,shape+1);
586  for(size_t ni=0; ni<numStates; ++ni) {
587  function(ni)= numVar * (rand() % 1000)*0.001;
588  }
589  FunctionIdentifier funcId=gm.addFunction(function);
590  gm.addFactor(funcId,var,var+1);
591  }
592  }
593  //PAIRWISE
594  if(variableNumStates==false) {
595  size_t shape[2];
596  shape[0] = shape[1] = numStates;
597  size_t var[]={0,0};
598  ExplicitFunctionType function(shape,shape+2);
599  for(size_t ni=0; ni<numStates; ++ni) {
600  for(size_t nj=0; nj<numStates; ++nj) {
601  if(ni==nj) function(ni,nj) = 0;
602  else function(ni,nj) = 1;
603  }
604  }
605  FunctionIdentifier funcId=gm.addFunction(function);
606  for(size_t i=0;i<N;++i) {
607  for(size_t j=i+1;j<N;++j) {
608  var[0] = i;
609  var[1] = j;
610  gm.addFactor(funcId,var,var+2);
611  }
612  }
613  }
614  else{
615  size_t shape[2];
616  size_t var[]={0,0};
617  for(size_t i=0;i<N;++i) {
618  for(size_t j=i+1;j<N;++j) {
619  var[0] = i;
620  var[1] = j;
621  shape[0] = variableStates[var[0]];
622  shape[1] = variableStates[var[1]];
623  ExplicitFunctionType function(shape,shape+2);
624  for(size_t ni=0; ni<shape[0]; ++ni) {
625  for(size_t nj=0; nj<shape[1]; ++nj) {
626  if(ni==nj) function(ni,nj) = 0;
627  else function(ni,nj) = 1;
628  }
629  }
630  FunctionIdentifier funcId=gm.addFunction(function);
631  gm.addFactor(funcId,var,var+2);
632  }
633  }
634  }
635  return gm;
636  }
637  template<class GM>
638  GM ModelGenerator<GM>::buildRandomGrid(size_t height, size_t width, size_t numStates, unsigned int id, bool variableNumStates) {
639  srand(id);
640  size_t N=height*width;
641  std::vector<size_t> variableStates(N,numStates);
642  if(variableNumStates) {
643  for(size_t i=0; i<N;++i) {
644  variableStates[i] = (rand() % (numStates-1))+1;
645  }
646  }
647  GraphicalModelType gm(variableStates.begin(),variableStates.end());
648  //UNARY
649  {
650  size_t shape[1];
651  size_t var[]={0};
652  for(size_t i=0;i<N;++i) {
653  var[0] = i;
654  shape[0] = variableStates[i];
655  ExplicitFunctionType function(shape,shape+1);
656  for(size_t ni=0; ni<shape[0]; ++ni) {
657  function(ni)= (rand() % 1000)*0.001;
658  }
659  FunctionIdentifier funcId=gm.addFunction(function);
660  gm.addFactor(funcId,var,var+1);
661  }
662  }
663  //PAIRWISE
664  {
665  size_t shape[2];
666  size_t var[]={0,0};
667  for(size_t i=0;i<height;++i) {
668  for(size_t j=0;j<width;++j) {
669  if(i+1<height) {
670  var[0] = i+height*j;
671  var[1] = i+1+height*j;
672  shape[0] = variableStates[var[0]];
673  shape[1] = variableStates[var[1]];
674  ExplicitFunctionType function(shape,shape+2);
675  for(size_t ni=0; ni<shape[0]; ++ni) {
676  for(size_t nj=0; nj<shape[1]; ++nj) {
677  function(ni,nj)= (rand() % 1000)*0.001;
678  }
679  }
680  FunctionIdentifier funcId=gm.addFunction(function);
681  gm.addFactor(funcId,var,var+2);
682  }
683  if(j+1<width) {
684  var[0] = i+height*j;
685  var[1] = i+height*(j+1);
686  shape[0] = variableStates[var[0]];
687  shape[1] = variableStates[var[1]];
688  ExplicitFunctionType function(shape,shape+2);
689  for(size_t ni=0; ni<numStates; ++ni) {
690  for(size_t nj=0; nj<numStates; ++nj) {
691  function(ni,nj)= (rand() % 1000)*0.001;
692  }
693  }
694  FunctionIdentifier funcId=gm.addFunction(function);
695  gm.addFactor(funcId,var,var+2);
696  }
697  }
698  }
699  }
700  return gm;
701  }
702  template<class GM>
703  GM ModelGenerator<GM>::buildRandomFull(size_t numVar, size_t numStates, unsigned int id, bool variableNumStates) {
704  srand(id);
705  size_t N=numVar;
706  std::vector<size_t> variableStates(N,numStates);
707  if(variableNumStates) {
708  for(size_t i=0; i<N;++i) {
709  variableStates[i] = (rand() % (numStates-1))+1;
710  }
711  }
712  GraphicalModelType gm(variableStates.begin(),variableStates.end());
713  //UNARY
714  {
715  size_t shape[1];
716  size_t var[]={0};
717  for(size_t i=0;i<N;++i) {
718  shape[0] = variableStates[i];
719  var[0] = i;
720  ExplicitFunctionType function(shape,shape+1);
721  for(size_t ni=0; ni<shape[0]; ++ni) {
722  function(ni)= (rand() % 1000)*0.001;
723  }
724  FunctionIdentifier funcId=gm.addFunction(function);
725  gm.addFactor(funcId,var,var+1);
726  }
727  }
728  //PAIRWISE
729  {
730  size_t shape[2];
731  size_t var[]={0,0};
732  for(size_t i=0;i<N;++i) {
733  for(size_t j=i+1;j<N;++j) {
734  var[0] = i;
735  var[1] = j;
736  shape[0] = variableStates[i];
737  shape[1] = variableStates[j];
738  ExplicitFunctionType function(shape,shape+2);
739  for(size_t ni=0; ni<numStates; ++ni) {
740  for(size_t nj=0; nj<numStates; ++nj) {
741  function(ni,nj)= (rand() % 1000)*0.001;
742  }
743  }
744  FunctionIdentifier funcId=gm.addFunction(function);
745  gm.addFactor(funcId,var,var+2);
746  }
747  }
748  }
749  return gm;
750  }
751  template<class GM>
752  GM ModelGenerator<GM>::buildRandomStar(size_t numVar, size_t numStates, unsigned int id, bool variableNumStates) {
753  srand(id);
754  size_t N=numVar;
755  std::vector<size_t> variableStates(N,numStates);
756  GraphicalModelType gm(variableStates.begin(),variableStates.end());
757  if(variableNumStates) {
758  for(size_t i=0; i<N;++i) {
759  variableStates[i] = (rand() % (numStates-1))+1;
760  }
761  }
762  size_t root = (rand() % numVar);
763  //UNARY
764  {
765  size_t shape[1];
766  size_t var[]={0};
767  for(size_t i=0;i<N;++i) {
768  var[0] = i;
769  shape[0] = variableStates[i];
770  ExplicitFunctionType function(shape,shape+1);
771  for(size_t ni=0; ni<shape[0]; ++ni) {
772  function(ni)= (rand() % 1000)*0.001;
773  }
774  FunctionIdentifier funcId=gm.addFunction(function);
775  gm.addFactor(funcId,var,var+1);
776  }
777  }
778  //PAIRWISE
779  {
780  size_t shape[2];
781  size_t var[]={0,0};
782  for(size_t i=0;i<N;++i) {
783  if(i==root)
784  continue;
785  else{
786  var[0] = i;
787  var[1] = root;
788  shape[0] = variableStates[var[0]];
789  shape[1] = variableStates[var[1]];
790  ExplicitFunctionType function(shape,shape+2);
791  for(size_t ni=0; ni< shape[0]; ++ni) {
792  for(size_t nj=0; nj< shape[1]; ++nj) {
793  function(ni,nj)= (rand() % 1000)*0.001;
794  }
795  }
796  FunctionIdentifier funcId=gm.addFunction(function);
797  gm.addFactor(funcId,var,var+2);
798  }
799  }
800  }
801  return gm;
802  }
803  template<class GM>
804  GM ModelGenerator<GM>::buildRandomFull23(size_t numVar, size_t numStates, unsigned int id) {
805  srand(id);
806  size_t N=numVar;
807  std::vector<size_t> variableStates(N,numStates);
808  GraphicalModelType gm(variableStates.begin(),variableStates.end());
809  //UNARY
810  {
811  size_t shape[1];
812  shape[0] = numStates;
813  size_t var[]={0};
814  for(size_t v=0;v<N;++v) {
815  ExplicitFunctionType function(shape,shape+1);
816  for(size_t ni=0; ni<numStates; ++ni) {
817  function(ni)= (rand() % 1000)*0.001;
818  }
819  FunctionIdentifier funcId=gm.addFunction(function);
820  var[0] = v;
821  gm.addFactor(funcId,var,var+1);
822  }
823  }
824  //PAIRWISE
825  {
826  size_t shape[2];
827  shape[0] = shape[1] = numStates;
828  size_t var[]={0,0};
829  for(size_t i=0;i<N;++i) {
830  for(size_t j=i+1;j<N;++j) {
831  ExplicitFunctionType function(shape,shape+2);
832  for(size_t ni=0; ni<numStates; ++ni) {
833  for(size_t nj=0; nj<numStates; ++nj) {
834  function(ni,nj)= (rand() % 1000)*0.001;
835  }
836  }
837  FunctionIdentifier funcId=gm.addFunction(function);
838  var[0] = i;
839  var[1] = j;
840  gm.addFactor(funcId,var,var+2);
841  }
842  }
843  }
844  //Tripple
845  {
846  size_t shape[3];
847  shape[0] = shape[1] = shape[2] = numStates;
848  size_t var[]={0,0,0};
849  for(size_t i=0;i<N;++i) {
850  for(size_t j=i+1;j<N;++j) {
851  for(size_t k=j+1;k<N;++k) {
852  ExplicitFunctionType function(shape,shape+3);
853  for(size_t ni=0; ni<numStates; ++ni) {
854  for(size_t nj=0; nj<numStates; ++nj) {
855  for(size_t nk=0; nk<numStates; ++nk) {
856  function(ni,nj,nk)= (rand() % 1000)*0.001;
857  }
858  }
859  }
860  FunctionIdentifier funcId=gm.addFunction(function);
861  var[0] = i;
862  var[1] = j;
863  var[2] = k;
864  gm.addFactor(funcId,var,var+3);
865  }
866  }
867  }
868  }
869  return gm;
870  }
871  template<class GM>
872  GM ModelGenerator<GM>::buildRandomFull3(size_t numVar, size_t numStates, unsigned int id) {
873  srand(id);
874  size_t N=numVar;
875  std::vector<size_t> variableStates(N,numStates);
876  GraphicalModelType gm(variableStates.begin(),variableStates.end());
877  //UNARY
878  {
879  size_t shape[1];
880  shape[0] = numStates;
881  size_t var[]={0};
882  for(size_t v=0;v<N;++v) {
883  ExplicitFunctionType function(shape,shape+1);
884  for(size_t ni=0; ni<numStates; ++ni) {
885  function(ni)= (rand() % 1000)*0.001;
886  }
887  FunctionIdentifier funcId=gm.addFunction(function);
888  var[0] = v;
889  gm.addFactor(funcId,var,var+1);
890  }
891  }
892  //Tripple
893  {
894  size_t shape[3];
895  shape[0] = shape[1] = shape[2] = numStates;
896  size_t var[]={0,0,0};
897  for(size_t i=0;i<N;++i) {
898  for(size_t j=i+1;j<N;++j) {
899  for(size_t k=j+1;k<N;++k) {
900  ExplicitFunctionType function(shape,shape+3);
901  for(size_t ni=0; ni<numStates; ++ni) {
902  for(size_t nj=0; nj<numStates; ++nj) {
903  for(size_t nk=0; nk<numStates; ++nk) {
904  function(ni,nj,nk)= (rand() % 1000)*0.001;
905  }
906  }
907  }
908  FunctionIdentifier funcId=gm.addFunction(function);
909  var[0] = i;
910  var[1] = j;
911  var[2] = k;
912  gm.addFactor(funcId,var,var+3);
913  }
914  }
915  }
916  }
917  return gm;
918  }
919  */
920 }
921 
923 
924 #endif
925 
926 /*
927  template<class GM>
928  typename ModelGenerator<GM>::RebindGmType
929  ModelGenerator<GM>::buildPottsGridMixedFunctions(size_t height, size_t width, size_t numStates, unsigned int id)
930  {
931  srand(id);
932  size_t N=height*width;
933  std::vector<size_t> variableStates(N,numStates);
934  GraphicalModelType gm(variableStates.begin(),variableStates.end());
935  //UNARY
936  {
937  size_t shape[1];
938  shape[0] = numStates;
939  size_t var[]={0};
940  for(size_t v=0;v<N;++v) {
941  if(v%2=0) {
942  SparseFunctionType function(shape,shape+1,(rand() % 1000)*0.001);
943  for(size_t ni=1; ni<numStates; ++ni) {
944  function(ni)= 3.0*(rand() % 1000)*0.001;
945  }
946  FunctionIdentifier funcId=gm.addFunction(function);
947  var[0] = v;
948  gm.addFactor(funcId,var,var+1);
949  }
950  if(v%2=1) {
951  ExplicitFunctionType function(shape,shape+1);
952  for(size_t ni=0; ni<numStates; ++ni) {
953  function(ni)= 3.0*(rand() % 1000)*0.001;
954  }
955  FunctionIdentifier funcId=gm.addFunction(function);
956  var[0] = v;
957  gm.addFactor(funcId,var,var+1);
958  }
959  }
960  }
961  //PAIRWISE
962  {
963  size_t shape[2];
964  shape[0] = shape[1] = numStates;
965  size_t var[]={0,0};
966  ExplicitFunctionType functionE(shape,shape+2);
967  SparseFunctionType functionS(shape,shape+2,1);
968  ImplicitFunctionType functionI(ImplicitFunctionType::Potts);
969  functionI.parameter(0)=0;
970  functionI.parameter(1)=1;
971  for(size_t ni=0; ni<numStates; ++ni) {
972  for(size_t nj=0; nj<numStates; ++nj) {
973  if(ni==nj) {
974  functionE(ni,nj) = 0;
975  functionS(ni,nj) = 0;
976  }
977  else{
978  functionE(ni,nj) = 1;
979  }
980  }
981  }
982  FunctionIdentifier funcIdE=gm.addFunction(functionE);
983  FunctionIdentifier funcIdS=gm.addFunction(functionS);
984  FunctionIdentifier funcIdI=gm.addFunction(functionI);
985  for(size_t i=0;i<height;++i) {
986  for(size_t j=0;j<width;++j) {
987  size_t v = i+height*j;
988  if(i+1<height) {
989  var[0] = v;
990  var[1] = i+1+height*j;
991  if(j%3==0) {
992  gm.addFactor(funcIdE,var,var+2);
993  }
994  else if(j%3==1) {
995  gm.addFactor(funcIdS,var,var+2);
996  }
997  else if(j%3==2) {
998  gm.addFactor(funcIdI,var,var+2);
999  }
1000  }
1001  if(j+1<width) {
1002  var[0] = v;
1003  var[1] = i+height*(j+1);
1004  if(j%3==0) {
1005  gm.addFactor(funcIdE,var,var+2);
1006  }
1007  else if(j%3==1) {
1008  gm.addFactor(funcIdS,var,var+2);
1009  }
1010  else if(j%3==2) {
1011  gm.addFactor(funcIdI,var,var+2);
1012  }
1013  }
1014  }
1015  }
1016  }
1017  return gm;
1018  }
1019 */
The OpenGM namespace.
Definition: config.hxx:43