models/syntheticmodelgenerator.hxx

Go to the documentation of this file.
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     //typedef typename GM::SparseFunctionType   SparseFunctionType;
00023     //typedef typename GM::ImplicitFunctionType ImplicitFunctionType;
00024     typedef typename GM::FunctionIdentifier   FunctionIdentifier;
00025     typedef typename GM::OperatorType        OperatorType;
00026     //   typedef typename opengm::GraphicalModel<ValueType,Operatort,opengm::DefaultImplicitFunction<ValueType>,GraphicalModelType::isMutable > RebindGmType;
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     //GM buildTree2(size_t var, size_t numStates, unsigned int id, FunktionTypes ft2, ValueType l1, ValueType l2);
00038     //GM buildFull3(size_t var, size_t numStates, unsigned int id,  FunktionTypes ft3, ValueType l1, ValueType l3);
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     //GM buildRandomFull23(size_t numVar, size_t numStates, unsigned int id);
00049     //GM buildRandomFull3(size_t numVar, size_t numStates, unsigned int id);
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;//OperatorType::neutral();
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;//OperatorType::neutral();
00117      else       function(ni,nj) = lambda * v;
00118    }
00119       }
00120       FunctionIdentifier funcId=gm.addFunction(function);
00121       return funcId;
00122     }
00123     else{
00124       //throw exception
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     //UNARY
00151     addUnaries(gm, lambda1);
00152     //PAIRWISE
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     //PAIRWISE
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     //UNARY
00287     addUnaries(gm, lambda1);
00288     //PAIRWISE
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     //PAIRWISE
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     //UNARY
00391     addUnaries(gm, lambda1);
00392     //PAIRWISE
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     template<class GM>
00471     GM ModelGenerator<GM>::buildPottsGrid(size_t height, size_t width, size_t numStates, unsigned int id, bool variableNumStates)
00472     {
00473     srand(id);
00474     size_t N=height*width;
00475     std::vector<size_t>  variableStates(N,numStates);
00476     if(variableNumStates) {
00477     for(size_t i=0; i<N;++i) {
00478     variableStates[i] = (rand() % (numStates-1))+1;
00479     }
00480     }
00481     GraphicalModelType gm(variableStates.begin(),variableStates.end());
00482     //UNARY
00483     {
00484     size_t shape[1];
00485     size_t var[]={0};
00486     for(size_t i=0;i<N;++i) {
00487     shape[0] = variableStates[i];
00488     var[0]   = i;
00489     ExplicitFunctionType function(shape,shape+1);
00490     for(size_t ni=0; ni<variableStates[i]; ++ni) {
00491     function(ni)= (rand() % 1000)*0.001;
00492     }
00493     FunctionIdentifier funcId=gm.addFunction(function);
00494     gm.addFactor(funcId,var,var+1);
00495     }
00496     }
00497     //PAIRWISE
00498     if(variableNumStates==false) {
00499     size_t shape[2];
00500     shape[0] = shape[1] = numStates;
00501     size_t var[]={0,0};
00502     ExplicitFunctionType function(shape,shape+2);
00503     for(size_t ni=0; ni<numStates; ++ni) {
00504     for(size_t nj=0; nj<numStates; ++nj) {
00505     if(ni==nj) function(ni,nj) = 0;
00506     else       function(ni,nj) = 1;
00507     }
00508     }
00509     FunctionIdentifier funcId=gm.addFunction(function);
00510     for(size_t i=0;i<height;++i) {
00511     for(size_t j=0;j<width;++j) {
00512     size_t v = i+height*j;
00513     if(i+1<height) {
00514     var[0] = v;
00515     var[1] = i+1+height*j;
00516     gm.addFactor(funcId,var,var+2);
00517     }
00518     if(j+1<width) {
00519     var[0] = v;
00520     var[1] = i+height*(j+1);
00521     gm.addFactor(funcId,var,var+2);
00522     }
00523     }
00524     }
00525     }
00526     else{
00527     size_t shape[2];
00528     size_t var[]={0,0};
00529     for(size_t i=0;i<height;++i) {
00530     for(size_t j=0;j<width;++j) {
00531     size_t v = i+height*j;
00532     if(i+1<height) {
00533     var[0] = v;
00534     var[1] = i+1+height*j;
00535     shape[0] = variableStates[var[0]];
00536     shape[1] = variableStates[var[1]];
00537     ExplicitFunctionType function(shape,shape+2);
00538     for(size_t ni=0; ni<shape[0]; ++ni) {
00539     for(size_t nj=0; nj<shape[1]; ++nj) {
00540     if(ni==nj) function(ni,nj) = 0;
00541     else       function(ni,nj) = 1;
00542     }
00543     }
00544     FunctionIdentifier funcId=gm.addFunction(function);
00545     gm.addFactor(funcId,var,var+2);
00546     }
00547     if(j+1<width) {
00548     var[0] = v;
00549     var[1] = i+height*(j+1);
00550     shape[0] = variableStates[var[0]];
00551     shape[1] = variableStates[var[1]];
00552     ExplicitFunctionType function(shape,shape+2);
00553     for(size_t ni=0; ni<shape[0]; ++ni) {
00554     for(size_t nj=0; nj<shape[1]; ++nj) {
00555     if(ni==nj) function(ni,nj) = 0;
00556     else       function(ni,nj) = 1;
00557     }
00558     }
00559     FunctionIdentifier funcId=gm.addFunction(function);
00560     gm.addFactor(funcId,var,var+2);
00561     }
00562     }
00563     }
00564     }
00565     return gm;
00566     }
00567     template<class GM>
00568     GM ModelGenerator<GM>::buildPottsFull(size_t numVar, size_t numStates, unsigned int id, bool variableNumStates) {
00569     srand(id);
00570     size_t N=numVar;
00571     std::vector<size_t>  variableStates(N,numStates);
00572     if(variableNumStates) {
00573     for(size_t i=0; i<N;++i) {
00574     variableStates[i] = (rand() % (numStates-1))+1;
00575     }
00576     }
00577     GraphicalModelType gm(variableStates.begin(),variableStates.end());
00578     //UNARY
00579     {
00580     size_t shape[1];
00581     size_t var[]={0};
00582     for(size_t i=0;i<N;++i) {
00583     var[0]   = i;
00584     shape[0] = variableStates[i];
00585     ExplicitFunctionType function(shape,shape+1);
00586     for(size_t ni=0; ni<numStates; ++ni) {
00587     function(ni)= numVar * (rand() % 1000)*0.001;
00588     }
00589     FunctionIdentifier funcId=gm.addFunction(function);
00590     gm.addFactor(funcId,var,var+1);
00591     }
00592     }
00593     //PAIRWISE
00594     if(variableNumStates==false) {
00595     size_t shape[2];
00596     shape[0] = shape[1] = numStates;
00597     size_t var[]={0,0};
00598     ExplicitFunctionType function(shape,shape+2);
00599     for(size_t ni=0; ni<numStates; ++ni) {
00600     for(size_t nj=0; nj<numStates; ++nj) {
00601     if(ni==nj) function(ni,nj) = 0;
00602     else       function(ni,nj) = 1;
00603     }
00604     }
00605     FunctionIdentifier funcId=gm.addFunction(function);
00606     for(size_t i=0;i<N;++i) {
00607     for(size_t j=i+1;j<N;++j) {
00608     var[0] = i;
00609     var[1] = j;
00610     gm.addFactor(funcId,var,var+2);
00611     }
00612     }
00613     }
00614     else{
00615     size_t shape[2];
00616     size_t var[]={0,0};
00617     for(size_t i=0;i<N;++i) {
00618     for(size_t j=i+1;j<N;++j) {
00619     var[0] = i;
00620     var[1] = j;
00621     shape[0] = variableStates[var[0]];
00622     shape[1] = variableStates[var[1]];
00623     ExplicitFunctionType function(shape,shape+2);
00624     for(size_t ni=0; ni<shape[0]; ++ni) {
00625     for(size_t nj=0; nj<shape[1]; ++nj) {
00626     if(ni==nj) function(ni,nj) = 0;
00627     else       function(ni,nj) = 1;
00628     }
00629     }
00630     FunctionIdentifier funcId=gm.addFunction(function);
00631     gm.addFactor(funcId,var,var+2);
00632     }
00633     }
00634     }
00635     return gm;
00636     }
00637     template<class GM>
00638     GM ModelGenerator<GM>::buildRandomGrid(size_t height, size_t width, size_t numStates, unsigned int id, bool variableNumStates) {
00639     srand(id);
00640     size_t N=height*width;
00641     std::vector<size_t>  variableStates(N,numStates);
00642     if(variableNumStates) {
00643     for(size_t i=0; i<N;++i) {
00644     variableStates[i] = (rand() % (numStates-1))+1;
00645     }
00646     }
00647     GraphicalModelType gm(variableStates.begin(),variableStates.end());
00648     //UNARY
00649     {
00650     size_t shape[1];
00651     size_t var[]={0};
00652     for(size_t i=0;i<N;++i) {
00653     var[0]   = i;
00654     shape[0] = variableStates[i];
00655     ExplicitFunctionType function(shape,shape+1);
00656     for(size_t ni=0; ni<shape[0]; ++ni) {
00657     function(ni)= (rand() % 1000)*0.001;
00658     }
00659     FunctionIdentifier funcId=gm.addFunction(function);
00660     gm.addFactor(funcId,var,var+1);
00661     }
00662     }
00663     //PAIRWISE
00664     {
00665     size_t shape[2];
00666     size_t var[]={0,0};
00667     for(size_t i=0;i<height;++i) {
00668     for(size_t j=0;j<width;++j) {
00669     if(i+1<height) {
00670     var[0] = i+height*j;
00671     var[1] = i+1+height*j;
00672     shape[0] = variableStates[var[0]];
00673     shape[1] = variableStates[var[1]];
00674     ExplicitFunctionType function(shape,shape+2);
00675     for(size_t ni=0; ni<shape[0]; ++ni) {
00676     for(size_t nj=0; nj<shape[1]; ++nj) {
00677     function(ni,nj)= (rand() % 1000)*0.001;
00678     }
00679     }
00680     FunctionIdentifier funcId=gm.addFunction(function);
00681     gm.addFactor(funcId,var,var+2);
00682     }
00683     if(j+1<width) {
00684     var[0] = i+height*j;
00685     var[1] = i+height*(j+1);
00686     shape[0] = variableStates[var[0]];
00687     shape[1] = variableStates[var[1]];
00688     ExplicitFunctionType function(shape,shape+2);
00689     for(size_t ni=0; ni<numStates; ++ni) {
00690     for(size_t nj=0; nj<numStates; ++nj) {
00691     function(ni,nj)= (rand() % 1000)*0.001;
00692     }
00693     }
00694     FunctionIdentifier funcId=gm.addFunction(function);
00695     gm.addFactor(funcId,var,var+2);
00696     }
00697     }
00698     }
00699     }
00700     return gm;
00701     }
00702     template<class GM>
00703     GM ModelGenerator<GM>::buildRandomFull(size_t numVar, size_t numStates, unsigned int id, bool variableNumStates) {
00704     srand(id);
00705     size_t N=numVar;
00706     std::vector<size_t>  variableStates(N,numStates);
00707     if(variableNumStates) {
00708     for(size_t i=0; i<N;++i) {
00709     variableStates[i] = (rand() % (numStates-1))+1;
00710     }
00711     }
00712     GraphicalModelType gm(variableStates.begin(),variableStates.end());
00713     //UNARY
00714     {
00715     size_t shape[1];
00716     size_t var[]={0};
00717     for(size_t i=0;i<N;++i) {
00718     shape[0] = variableStates[i];
00719     var[0]   = i;
00720     ExplicitFunctionType function(shape,shape+1);
00721     for(size_t ni=0; ni<shape[0]; ++ni) {
00722     function(ni)= (rand() % 1000)*0.001;
00723     }
00724     FunctionIdentifier funcId=gm.addFunction(function);
00725     gm.addFactor(funcId,var,var+1);
00726     }
00727     }
00728     //PAIRWISE
00729     {
00730     size_t shape[2];
00731     size_t var[]={0,0};
00732     for(size_t i=0;i<N;++i) {
00733     for(size_t j=i+1;j<N;++j) {
00734     var[0] = i;
00735     var[1] = j;
00736     shape[0] = variableStates[i];
00737     shape[1] = variableStates[j];
00738     ExplicitFunctionType function(shape,shape+2);
00739     for(size_t ni=0; ni<numStates; ++ni) {
00740     for(size_t nj=0; nj<numStates; ++nj) {
00741     function(ni,nj)= (rand() % 1000)*0.001;
00742     }
00743     }
00744     FunctionIdentifier funcId=gm.addFunction(function);
00745     gm.addFactor(funcId,var,var+2);
00746     }
00747     }
00748     }
00749     return gm;
00750     }
00751     template<class GM>
00752     GM ModelGenerator<GM>::buildRandomStar(size_t numVar, size_t numStates, unsigned int id, bool variableNumStates) {
00753     srand(id);
00754     size_t N=numVar;
00755     std::vector<size_t>  variableStates(N,numStates);
00756     GraphicalModelType gm(variableStates.begin(),variableStates.end());
00757     if(variableNumStates) {
00758     for(size_t i=0; i<N;++i) {
00759     variableStates[i] = (rand() % (numStates-1))+1;
00760     }
00761     }
00762     size_t root = (rand() % numVar);
00763     //UNARY
00764     {
00765     size_t shape[1];
00766     size_t var[]={0};
00767     for(size_t i=0;i<N;++i) {
00768     var[0]   = i;
00769     shape[0] = variableStates[i];
00770     ExplicitFunctionType function(shape,shape+1);
00771     for(size_t ni=0; ni<shape[0]; ++ni) {
00772     function(ni)= (rand() % 1000)*0.001;
00773     }
00774     FunctionIdentifier funcId=gm.addFunction(function);
00775     gm.addFactor(funcId,var,var+1);
00776     }
00777     }
00778     //PAIRWISE
00779     {
00780     size_t shape[2];
00781     size_t var[]={0,0};
00782     for(size_t i=0;i<N;++i) {
00783     if(i==root)
00784     continue;
00785     else{
00786     var[0] = i;
00787     var[1] = root;
00788     shape[0] = variableStates[var[0]];
00789     shape[1] = variableStates[var[1]];
00790     ExplicitFunctionType function(shape,shape+2);
00791     for(size_t ni=0; ni< shape[0]; ++ni) {
00792     for(size_t nj=0; nj< shape[1]; ++nj) {
00793     function(ni,nj)= (rand() % 1000)*0.001;
00794     }
00795     }
00796     FunctionIdentifier funcId=gm.addFunction(function);
00797     gm.addFactor(funcId,var,var+2);
00798     }
00799     }
00800     }
00801     return gm;
00802     }
00803     template<class GM>
00804     GM ModelGenerator<GM>::buildRandomFull23(size_t numVar, size_t numStates, unsigned int id) {
00805     srand(id);
00806     size_t N=numVar;
00807     std::vector<size_t>  variableStates(N,numStates);
00808     GraphicalModelType gm(variableStates.begin(),variableStates.end());
00809     //UNARY
00810     {
00811     size_t shape[1];
00812     shape[0] = numStates;
00813     size_t var[]={0};
00814     for(size_t v=0;v<N;++v) {
00815     ExplicitFunctionType function(shape,shape+1);
00816     for(size_t ni=0; ni<numStates; ++ni) {
00817     function(ni)= (rand() % 1000)*0.001;
00818     }
00819     FunctionIdentifier funcId=gm.addFunction(function);
00820     var[0] = v;
00821     gm.addFactor(funcId,var,var+1);
00822     }
00823     }
00824     //PAIRWISE
00825     {
00826     size_t shape[2];
00827     shape[0] = shape[1] = numStates;
00828     size_t var[]={0,0};
00829     for(size_t i=0;i<N;++i) {
00830     for(size_t j=i+1;j<N;++j) {
00831     ExplicitFunctionType function(shape,shape+2);
00832     for(size_t ni=0; ni<numStates; ++ni) {
00833     for(size_t nj=0; nj<numStates; ++nj) {
00834     function(ni,nj)= (rand() % 1000)*0.001;
00835     }
00836     }
00837     FunctionIdentifier funcId=gm.addFunction(function);
00838     var[0] = i;
00839     var[1] = j;
00840     gm.addFactor(funcId,var,var+2);
00841     }
00842     }
00843     }
00844     //Tripple
00845     {
00846     size_t shape[3];
00847     shape[0] = shape[1] = shape[2] = numStates;
00848     size_t var[]={0,0,0};
00849     for(size_t i=0;i<N;++i) {
00850     for(size_t j=i+1;j<N;++j) {
00851     for(size_t k=j+1;k<N;++k) {
00852     ExplicitFunctionType function(shape,shape+3);
00853     for(size_t ni=0; ni<numStates; ++ni) {
00854     for(size_t nj=0; nj<numStates; ++nj) {
00855     for(size_t nk=0; nk<numStates; ++nk) {
00856     function(ni,nj,nk)= (rand() % 1000)*0.001;
00857     }
00858     }
00859     }
00860     FunctionIdentifier funcId=gm.addFunction(function);
00861     var[0] = i;
00862     var[1] = j;
00863     var[2] = k;
00864     gm.addFactor(funcId,var,var+3);
00865     }
00866     }
00867     }
00868     }
00869     return gm;
00870     }
00871     template<class GM>
00872     GM ModelGenerator<GM>::buildRandomFull3(size_t numVar, size_t numStates, unsigned int id) {
00873     srand(id);
00874     size_t N=numVar;
00875     std::vector<size_t>  variableStates(N,numStates);
00876     GraphicalModelType gm(variableStates.begin(),variableStates.end());
00877     //UNARY
00878     {
00879     size_t shape[1];
00880     shape[0] = numStates;
00881     size_t var[]={0};
00882     for(size_t v=0;v<N;++v) {
00883     ExplicitFunctionType function(shape,shape+1);
00884     for(size_t ni=0; ni<numStates; ++ni) {
00885     function(ni)= (rand() % 1000)*0.001;
00886     }
00887     FunctionIdentifier funcId=gm.addFunction(function);
00888     var[0] = v;
00889     gm.addFactor(funcId,var,var+1);
00890     }
00891     }
00892     //Tripple
00893     {
00894     size_t shape[3];
00895     shape[0] = shape[1] = shape[2] = numStates;
00896     size_t var[]={0,0,0};
00897     for(size_t i=0;i<N;++i) {
00898     for(size_t j=i+1;j<N;++j) {
00899     for(size_t k=j+1;k<N;++k) {
00900     ExplicitFunctionType function(shape,shape+3);
00901     for(size_t ni=0; ni<numStates; ++ni) {
00902     for(size_t nj=0; nj<numStates; ++nj) {
00903     for(size_t nk=0; nk<numStates; ++nk) {
00904     function(ni,nj,nk)= (rand() % 1000)*0.001;
00905     }
00906     }
00907     }
00908     FunctionIdentifier funcId=gm.addFunction(function);
00909     var[0] = i;
00910     var[1] = j;
00911     var[2] = k;
00912     gm.addFactor(funcId,var,var+3);
00913     }
00914     }
00915     }
00916     }
00917     return gm;
00918     }
00919   */
00920 }
00921 
00923 
00924 #endif
00925 
00926 /*
00927   template<class GM>
00928   typename ModelGenerator<GM>::RebindGmType
00929   ModelGenerator<GM>::buildPottsGridMixedFunctions(size_t height, size_t width, size_t numStates, unsigned int id)
00930   {
00931   srand(id);
00932   size_t N=height*width;
00933   std::vector<size_t>  variableStates(N,numStates);
00934   GraphicalModelType gm(variableStates.begin(),variableStates.end());
00935   //UNARY
00936   {
00937   size_t shape[1];
00938   shape[0] = numStates;
00939   size_t var[]={0};
00940   for(size_t v=0;v<N;++v) {
00941   if(v%2=0) {
00942   SparseFunctionType function(shape,shape+1,(rand() % 1000)*0.001);
00943   for(size_t ni=1; ni<numStates; ++ni) {
00944   function(ni)= 3.0*(rand() % 1000)*0.001;
00945   }
00946   FunctionIdentifier funcId=gm.addFunction(function);
00947   var[0] = v;
00948   gm.addFactor(funcId,var,var+1);
00949   }
00950   if(v%2=1) {
00951   ExplicitFunctionType function(shape,shape+1);
00952   for(size_t ni=0; ni<numStates; ++ni) {
00953   function(ni)= 3.0*(rand() % 1000)*0.001;
00954   }
00955   FunctionIdentifier funcId=gm.addFunction(function);
00956   var[0] = v;
00957   gm.addFactor(funcId,var,var+1);
00958   }
00959   }
00960   }
00961   //PAIRWISE
00962   {
00963   size_t shape[2];
00964   shape[0] = shape[1] = numStates;
00965   size_t var[]={0,0};
00966   ExplicitFunctionType functionE(shape,shape+2);
00967   SparseFunctionType functionS(shape,shape+2,1);
00968   ImplicitFunctionType functionI(ImplicitFunctionType::Potts);
00969   functionI.parameter(0)=0;
00970   functionI.parameter(1)=1;
00971   for(size_t ni=0; ni<numStates; ++ni) {
00972   for(size_t nj=0; nj<numStates; ++nj) {
00973   if(ni==nj) {
00974   functionE(ni,nj) = 0;
00975   functionS(ni,nj) = 0;
00976   }
00977   else{
00978   functionE(ni,nj) = 1;
00979   }
00980   }
00981   }
00982   FunctionIdentifier funcIdE=gm.addFunction(functionE);
00983   FunctionIdentifier funcIdS=gm.addFunction(functionS);
00984   FunctionIdentifier funcIdI=gm.addFunction(functionI);
00985   for(size_t i=0;i<height;++i) {
00986   for(size_t j=0;j<width;++j) {
00987   size_t v = i+height*j;
00988   if(i+1<height) {
00989   var[0] = v;
00990   var[1] = i+1+height*j;
00991   if(j%3==0) {
00992   gm.addFactor(funcIdE,var,var+2);
00993   }
00994   else if(j%3==1) {
00995   gm.addFactor(funcIdS,var,var+2);
00996   }
00997   else if(j%3==2) {
00998   gm.addFactor(funcIdI,var,var+2);
00999   }
01000   }
01001   if(j+1<width) {
01002   var[0] = v;
01003   var[1] = i+height*(j+1);
01004   if(j%3==0) {
01005   gm.addFactor(funcIdE,var,var+2);
01006   }
01007   else if(j%3==1) {
01008   gm.addFactor(funcIdS,var,var+2);
01009   }
01010   else if(j%3==2) {
01011   gm.addFactor(funcIdI,var,var+2);
01012   }
01013   }
01014   }
01015   }
01016   }
01017   return gm;
01018   }
01019 */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Mon Jun 17 16:31:06 2013 for OpenGM by  doxygen 1.6.3