00001 #pragma once
00002 #ifndef OPENGM_SYNTHETIC_MODEL_GENERATOR_HXX
00003 #define OPENGM_SYNTHETIC_MODEL_GENERATOR_HXX
00004
00006
00007 #include <cstdlib>
00008 #include <vector>
00009 #include <cstdlib>
00010 #include <set>
00011 #include <functional>
00012
00013 #include "opengm/graphicalmodel/graphicalmodel.hxx"
00014
00015 namespace opengm {
00016
00017 template<class GM> class SyntheticModelGenerator
00018 {
00019 typedef GM GraphicalModelType;
00020 typedef typename GM::ValueType ValueType;
00021 typedef typename GM::ExplicitFunctionType ExplicitFunctionType;
00022
00023
00024 typedef typename GM::FunctionIdentifier FunctionIdentifier;
00025 typedef typename GM::OperatorType OperatorType;
00026
00027 public:
00028 enum FunktionTypes {RANDOM, Potts, GPotts};
00029 bool randomNumberOfStates_;
00030 SyntheticModelGenerator();
00031 SyntheticModelGenerator(bool randomNumberOfStates);
00032 GM buildGrid2(size_t height, size_t width, size_t numStates, unsigned int id, FunktionTypes ft2, ValueType l1, ValueType l2);
00033 GM buildGrid02(size_t height, size_t width, size_t numStates, unsigned int id, FunktionTypes ft2, ValueType l2);
00034 GM buildFull2(size_t var, size_t numStates, unsigned int id, FunktionTypes ft2, ValueType l1, ValueType l2);
00035 GM buildFull02(size_t var, size_t numStates, unsigned int id, FunktionTypes ft2, ValueType l2);
00036 GM buildStar2(size_t var, size_t numStates, unsigned int id, FunktionTypes ft2, ValueType l1, ValueType l2);
00037
00038
00039 GM buildPottsGrid2(size_t height, size_t width, size_t numStates, unsigned int id, ValueType l1=3, ValueType l2=1);
00040 GM buildPottsFull2(size_t numVar, size_t numStates, unsigned int id, ValueType l1=3, ValueType l2=1);
00041 GM buildGPottsFull02(size_t numVar, size_t numStates, unsigned int id, ValueType l2=1);
00042 GM buildGPottsGrid02(size_t height, size_t width, size_t numStates, unsigned int id, ValueType l2=1);
00043 GM buildPottsFull02(size_t numVar, size_t numStates, unsigned int id, ValueType l2=1);
00044 GM buildPottsGrid02(size_t height, size_t width, size_t numStates, unsigned int id, ValueType l2=1);
00045 GM buildRandomFull2(size_t numVar, size_t numStates, unsigned int id, ValueType l1=1, ValueType l2=1);
00046 GM buildRandomGrid2(size_t height, size_t width, size_t numStates, unsigned int id, ValueType l1=3, ValueType l2=1);
00047 GM buildRandomStar2(size_t numVar, size_t numStates, unsigned int id, ValueType l1=3, ValueType l2=1);
00048
00049
00050 private:
00051 void addUnaries(GM& gm, ValueType lambda);
00052 FunctionIdentifier addFunktion(GM& gm, ValueType lambda, FunktionTypes ft, size_t* beginShape, size_t* endShape);
00053 };
00054 template<class GM>
00055 SyntheticModelGenerator<GM>::SyntheticModelGenerator()
00056 {
00057 randomNumberOfStates_ = false;
00058 }
00059 template<class GM>
00060 SyntheticModelGenerator<GM>::SyntheticModelGenerator(bool randomNumberOfStates)
00061 {
00062 randomNumberOfStates_ = randomNumberOfStates;
00063 }
00064 template<class GM>
00065 void SyntheticModelGenerator<GM>::addUnaries(GM& gm, ValueType lambda1)
00066 {
00067 size_t shape[1];
00068 size_t var[]={0};
00069 for(size_t i=0;i<gm.numberOfVariables();++i) {
00070 shape[0] = gm.numberOfLabels(i);
00071 var[0] = i;
00072 ExplicitFunctionType function(shape,shape+1);
00073 for(size_t ni=0; ni<shape[0]; ++ni) {
00074 function(ni)= lambda1 * (rand() % 1000000)*0.000001 + 1;
00075 }
00076 FunctionIdentifier funcId=gm.addFunction(function);
00077 gm.addFactor(funcId,var,var+1);
00078 }
00079 }
00080 template<class GM>
00081 typename GM::FunctionIdentifier SyntheticModelGenerator<GM>::addFunktion
00082 (
00083 GM& gm,
00084 ValueType lambda,
00085 FunktionTypes ft,
00086 size_t* beginShape,
00087 size_t* endShape
00088 )
00089 {
00090 if(ft==RANDOM) {
00091 ExplicitFunctionType function(beginShape,endShape);
00092 for(size_t ni=0; ni<beginShape[0]; ++ni) {
00093 for(size_t nj=0; nj<beginShape[1]; ++nj) {
00094 function(ni,nj) = lambda * (rand() % 1000000)*0.000001 + 1;
00095 }
00096 }
00097 FunctionIdentifier funcId=gm.addFunction(function);
00098 return funcId;
00099 }
00100 else if(ft==Potts) {
00101 ExplicitFunctionType function(beginShape,endShape);
00102 for(size_t ni=0; ni<beginShape[0]; ++ni) {
00103 for(size_t nj=0; nj<beginShape[1]; ++nj) {
00104 if(ni==nj) function(ni,nj) = 0;
00105 else function(ni,nj) = lambda;
00106 }
00107 }
00108 FunctionIdentifier funcId=gm.addFunction(function);
00109 return funcId;
00110 }
00111 else if(ft==GPotts) {
00112 double v = ((rand()%10000)-5000)/5000.0;
00113 ExplicitFunctionType function(beginShape,endShape);
00114 for(size_t ni=0; ni<beginShape[0]; ++ni) {
00115 for(size_t nj=0; nj<beginShape[1]; ++nj) {
00116 if(ni==nj) function(ni,nj) = 0;
00117 else function(ni,nj) = lambda * v;
00118 }
00119 }
00120 FunctionIdentifier funcId=gm.addFunction(function);
00121 return funcId;
00122 }
00123 else{
00124
00125 FunctionIdentifier funcId;
00126 return funcId;
00127 }
00128 }
00129 template<class GM>
00130 GM SyntheticModelGenerator<GM>::buildGrid2
00131 (
00132 size_t height,
00133 size_t width,
00134 size_t numStates,
00135 unsigned int id,
00136 FunktionTypes ft2,
00137 ValueType lambda1,
00138 ValueType lambda2
00139 )
00140 {
00141 srand(id);
00142 size_t N=height*width;
00143 std::vector<size_t> variableStates(N,numStates);
00144 if(randomNumberOfStates_) {
00145 for(size_t i=0; i<N;++i) {
00146 variableStates[i] = (rand() % (numStates-1))+1;
00147 }
00148 }
00149 GraphicalModelType gm(variableStates.begin(),variableStates.end());
00150
00151 addUnaries(gm, lambda1);
00152
00153 size_t shape[2];
00154 size_t var[2];
00155 if(randomNumberOfStates_==false ) {
00156 shape[0] = shape[1] =gm.numberOfLabels(0);
00157 FunctionIdentifier funcId = addFunktion(gm,lambda2,ft2, shape,shape+2);
00158 for(size_t i=0;i<height;++i) {
00159 for(size_t j=0;j<width;++j) {
00160 size_t v = i+height*j;
00161 if(i+1<height) {
00162 var[0] = v;
00163 var[1] = i+1+height*j;
00164 gm.addFactor(funcId,var,var+2);
00165 }
00166 if(j+1<width) {
00167 var[0] = v;
00168 var[1] = i+height*(j+1);
00169 gm.addFactor(funcId,var,var+2);
00170 }
00171 }
00172 }
00173 }
00174 else{
00175 for(size_t i=0;i<height;++i) {
00176 for(size_t j=0;j<width;++j) {
00177 size_t v = i+height*j;
00178 if(i+1<height) {
00179 var[0] = v;
00180 var[1] = i+1+height*j;
00181 shape[0] = gm.numberOfLabels(var[0]);
00182 shape[1] = gm.numberOfLabels(var[1]);
00183 FunctionIdentifier funcId = addFunktion(gm,lambda2,ft2, shape,shape+2);
00184 gm.addFactor(funcId,var,var+2);
00185 }
00186 if(j+1<width) {
00187 var[0] = v;
00188 var[1] = i+height*(j+1);
00189 shape[0] = gm.numberOfLabels(var[0]);
00190 shape[1] = gm.numberOfLabels(var[1]);
00191 FunctionIdentifier funcId = addFunktion(gm,lambda2,ft2, shape,shape+2);
00192 gm.addFactor(funcId,var,var+2);
00193 }
00194 }
00195 }
00196 }
00197 return gm;
00198 }
00199 template<class GM>
00200 GM SyntheticModelGenerator<GM>::buildGrid02
00201 (
00202 size_t height,
00203 size_t width,
00204 size_t numStates,
00205 unsigned int id,
00206 FunktionTypes ft2,
00207 ValueType lambda2
00208 )
00209 {
00210 srand(id);
00211 size_t N=height*width;
00212 std::vector<size_t> variableStates(N,numStates);
00213 if(randomNumberOfStates_) {
00214 for(size_t i=0; i<N;++i) {
00215 variableStates[i] = (rand() % (numStates-1))+1;
00216 }
00217 }
00218 GraphicalModelType gm(variableStates.begin(),variableStates.end());
00219
00220 size_t shape[2];
00221 size_t var[2];
00222 if(randomNumberOfStates_==false ) {
00223 shape[0] = shape[1] =gm.numberOfLabels(0);
00224 for(size_t i=0;i<height;++i) {
00225 for(size_t j=0;j<width;++j) {
00226 size_t v = i+height*j;
00227 if(i+1<height) {
00228 var[0] = v;
00229 var[1] = i+1+height*j;
00230 FunctionIdentifier funcId = addFunktion(gm,lambda2,ft2, shape,shape+2);
00231 gm.addFactor(funcId,var,var+2);
00232 }
00233 if(j+1<width) {
00234 var[0] = v;
00235 var[1] = i+height*(j+1);
00236 FunctionIdentifier funcId = addFunktion(gm,lambda2,ft2, shape,shape+2);
00237 gm.addFactor(funcId,var,var+2);
00238 }
00239 }
00240 }
00241 }
00242 else{
00243 for(size_t i=0;i<height;++i) {
00244 for(size_t j=0;j<width;++j) {
00245 size_t v = i+height*j;
00246 if(i+1<height) {
00247 var[0] = v;
00248 var[1] = i+1+height*j;
00249 shape[0] = gm.numberOfLabels(var[0]);
00250 shape[1] = gm.numberOfLabels(var[1]);
00251 FunctionIdentifier funcId = addFunktion(gm,lambda2,ft2, shape,shape+2);
00252 gm.addFactor(funcId,var,var+2);
00253 }
00254 if(j+1<width) {
00255 var[0] = v;
00256 var[1] = i+height*(j+1);
00257 shape[0] = gm.numberOfLabels(var[0]);
00258 shape[1] = gm.numberOfLabels(var[1]);
00259 FunctionIdentifier funcId = addFunktion(gm,lambda2,ft2, shape,shape+2);
00260 gm.addFactor(funcId,var,var+2);
00261 }
00262 }
00263 }
00264 }
00265 return gm;
00266 }
00267 template<class GM>
00268 GM SyntheticModelGenerator<GM>::buildFull2
00269 (
00270 size_t numberOfVariables,
00271 size_t numberOfLabels,
00272 unsigned int id,
00273 FunktionTypes ft2,
00274 ValueType lambda1,
00275 ValueType lambda2
00276 )
00277 {
00278 srand(id);
00279 std::vector<size_t> variableStates(numberOfVariables,numberOfLabels);
00280 if(randomNumberOfStates_) {
00281 for(size_t i=0; i<numberOfVariables;++i) {
00282 variableStates[i] = (rand() % (numberOfLabels-1))+1;
00283 }
00284 }
00285 GraphicalModelType gm(variableStates.begin(),variableStates.end());
00286
00287 addUnaries(gm, lambda1);
00288
00289 size_t shape[2];
00290 size_t var[2];
00291 if(randomNumberOfStates_==false )
00292 {
00293 shape[0]=gm.numberOfLabels(0);
00294 shape[1]=gm.numberOfLabels(0);
00295 FunctionIdentifier funcId = addFunktion(gm,lambda2,ft2, shape, shape+2);
00296 for(size_t i=0;i<numberOfVariables;++i)
00297 {
00298 for(size_t j=i+1;j<numberOfVariables;++j)
00299 {
00300 var[0] = i;
00301 var[1] = j;
00302 gm.addFactor(funcId,var,var+2);
00303 }
00304 }
00305 }
00306 else{
00307 for(size_t i=0;i<numberOfVariables;++i) {
00308 for(size_t j=i+1;j<numberOfVariables;++j) {
00309 var[0] = i;
00310 var[1] = j;
00311 shape[0] = gm.numberOfLabels(var[0]);
00312 shape[1] = gm.numberOfLabels(var[1]);
00313 FunctionIdentifier funcId = addFunktion(gm,lambda2,ft2, shape,shape+2);
00314 gm.addFactor(funcId,var,var+2);
00315 }
00316 }
00317 }
00318 return gm;
00319 }
00320 template<class GM>
00321 GM SyntheticModelGenerator<GM>::buildFull02
00322 (
00323 size_t numberOfVariables,
00324 size_t numberOfLabels,
00325 unsigned int id,
00326 FunktionTypes ft2,
00327 ValueType lambda2
00328 )
00329 {
00330 srand(id);
00331 std::vector<size_t> variableStates(numberOfVariables,numberOfLabels);
00332 if(randomNumberOfStates_) {
00333 for(size_t i=0; i<numberOfVariables;++i) {
00334 variableStates[i] = (rand() % (numberOfLabels-1))+1;
00335 }
00336 }
00337 GraphicalModelType gm(variableStates.begin(),variableStates.end());
00338
00339 size_t shape[2];
00340 size_t var[2];
00341 if(randomNumberOfStates_==false )
00342 {
00343 shape[0]=gm.numberOfLabels(0);
00344 shape[1]=gm.numberOfLabels(0);
00345 for(size_t i=0;i<numberOfVariables;++i)
00346 {
00347 for(size_t j=i+1;j<numberOfVariables;++j)
00348 {
00349 var[0] = i;
00350 var[1] = j;
00351 FunctionIdentifier funcId = addFunktion(gm,lambda2,ft2, shape, shape+2);
00352 gm.addFactor(funcId,var,var+2);
00353 }
00354 }
00355 }
00356 else{
00357 for(size_t i=0;i<numberOfVariables;++i) {
00358 for(size_t j=i+1;j<numberOfVariables;++j) {
00359 var[0] = i;
00360 var[1] = j;
00361 shape[0] = gm.numberOfLabels(var[0]);
00362 shape[1] = gm.numberOfLabels(var[1]);
00363 FunctionIdentifier funcId = addFunktion(gm,lambda2,ft2, shape,shape+2);
00364 gm.addFactor(funcId,var,var+2);
00365 }
00366 }
00367 }
00368 return gm;
00369 }
00370 template<class GM>
00371 GM SyntheticModelGenerator<GM>::buildStar2
00372 (
00373 size_t numberOfVariables,
00374 size_t numberOfLabels,
00375 unsigned int id,
00376 FunktionTypes ft2,
00377 ValueType lambda1,
00378 ValueType lambda2
00379 )
00380 {
00381 srand(id);
00382 std::vector<size_t> variableStates(numberOfVariables,numberOfLabels);
00383 if(randomNumberOfStates_) {
00384 for(size_t i=0; i<numberOfVariables;++i) {
00385 variableStates[i] = (rand() % (numberOfLabels-1))+1;
00386 }
00387 }
00388 GraphicalModelType gm(variableStates.begin(),variableStates.end());
00389 size_t root = (rand() % numberOfVariables);
00390
00391 addUnaries(gm, lambda1);
00392
00393 size_t shape[2];
00394 size_t var[2];
00395 if(randomNumberOfStates_==false ) {
00396 shape[0] = shape[1] =gm.numberOfLabels(0);
00397 FunctionIdentifier funcId = addFunktion(gm,lambda2,ft2, shape,shape+2);
00398 for(size_t i=0;i<root;++i) {
00399 var[0] = i;
00400 var[1] = root;
00401 gm.addFactor(funcId,var,var+2);
00402 }
00403 for(size_t i=root+1;i<numberOfVariables;++i) {
00404 var[0] = root;
00405 var[1] = i;
00406 gm.addFactor(funcId,var,var+2);
00407 }
00408 }
00409 else{
00410 for(size_t i=0;i<root;++i) {
00411 var[0] = i;
00412 var[1] = root;
00413 shape[0] = gm.numberOfLabels(var[0]);
00414 shape[1] = gm.numberOfLabels(var[1]);
00415 FunctionIdentifier funcId = addFunktion(gm,lambda2,ft2, shape,shape+2);
00416 gm.addFactor(funcId,var,var+2);
00417 }
00418 for(size_t i=root+1;i<numberOfVariables;++i) {
00419 var[0] = root;
00420 var[1] = i;
00421 shape[0] = gm.numberOfLabels(var[0]);
00422 shape[1] = gm.numberOfLabels(var[1]);
00423 FunctionIdentifier funcId = addFunktion(gm,lambda2,ft2, shape,shape+2);
00424 gm.addFactor(funcId,var,var+2);
00425 }
00426 }
00427 return gm;
00428 }
00433 template<class GM>
00434 GM SyntheticModelGenerator<GM>::buildPottsGrid2
00435 (size_t height, size_t width, size_t numStates, unsigned int id, ValueType l1, ValueType l2)
00436 {return buildGrid2(height, width, numStates, id, Potts, l1, l2);}
00437 template<class GM>
00438 GM SyntheticModelGenerator<GM>::buildPottsGrid02
00439 (size_t height, size_t width, size_t numStates, unsigned int id, ValueType l2)
00440 {return buildGrid02(height, width, numStates, id, Potts, l2);}
00441 template<class GM>
00442 GM SyntheticModelGenerator<GM>::buildGPottsGrid02
00443 (size_t height, size_t width, size_t numStates, unsigned int id, ValueType l2)
00444 {return buildGrid02(height, width, numStates, id, GPotts, l2);}
00445 template<class GM>
00446 GM SyntheticModelGenerator<GM>::buildRandomGrid2
00447 (size_t height, size_t width, size_t numStates, unsigned int id, ValueType l1, ValueType l2)
00448 {return buildGrid2(height, width, numStates, id, RANDOM, l1, l2);}
00449 template<class GM>
00450 GM SyntheticModelGenerator<GM>::buildPottsFull2
00451 (size_t numVar, size_t numStates, unsigned int id, ValueType l1, ValueType l2)
00452 {return buildFull2(numVar, numStates, id, Potts, l1, l2);}
00453 template<class GM>
00454 GM SyntheticModelGenerator<GM>::buildPottsFull02
00455 (size_t numVar, size_t numStates, unsigned int id, ValueType l2)
00456 {return buildFull02(numVar, numStates, id, Potts, l2);}
00457 template<class GM>
00458 GM SyntheticModelGenerator<GM>::buildGPottsFull02
00459 (size_t numVar, size_t numStates, unsigned int id, ValueType l2)
00460 {return buildFull02(numVar, numStates, id, GPotts, l2);}
00461 template<class GM>
00462 GM SyntheticModelGenerator<GM>::buildRandomFull2
00463 (size_t numVar, size_t numStates, unsigned int id, ValueType l1, ValueType l2)
00464 {return buildFull2(numVar, numStates, id, RANDOM, l1, l2);}
00465 template<class GM>
00466 GM SyntheticModelGenerator<GM>::buildRandomStar2
00467 (size_t numVar, size_t numStates, unsigned int id, ValueType l1, ValueType l2)
00468 {return buildStar2(numVar, numStates, id, RANDOM, l1, l2);}
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663
00664
00665
00666
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680
00681
00682
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697
00698
00699
00700
00701
00702
00703
00704
00705
00706
00707
00708
00709
00710
00711
00712
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731
00732
00733
00734
00735
00736
00737
00738
00739
00740
00741
00742
00743
00744
00745
00746
00747
00748
00749
00750
00751
00752
00753
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772
00773
00774
00775
00776
00777
00778
00779
00780
00781
00782
00783
00784
00785
00786
00787
00788
00789
00790
00791
00792
00793
00794
00795
00796
00797
00798
00799
00800
00801
00802
00803
00804
00805
00806
00807
00808
00809
00810
00811
00812
00813
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827
00828
00829
00830
00831
00832
00833
00834
00835
00836
00837
00838
00839
00840
00841
00842
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859
00860
00861
00862
00863
00864
00865
00866
00867
00868
00869
00870
00871
00872
00873
00874
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920 }
00921
00923
00924 #endif
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935
00936
00937
00938
00939
00940
00941
00942
00943
00944
00945
00946
00947
00948
00949
00950
00951
00952
00953
00954
00955
00956
00957
00958
00959
00960
00961
00962
00963
00964
00965
00966
00967
00968
00969
00970
00971
00972
00973
00974
00975
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991
00992
00993
00994
00995
00996
00997
00998
00999
01000
01001
01002
01003
01004
01005
01006
01007
01008
01009
01010
01011
01012
01013
01014
01015
01016
01017
01018
01019