/* See English below */ /*********************************************************************/ /* MBMUDs.c, Generador de MBMUDs a base de fixar el nombre d'uns per fila i columna i minimitzar les quadruples amb moviments de tipus (10,01)->(01,10). Busca l'optim o, en el seu defecte, el millor minim local despres d'un numero d'iteracions donat. Mes detalls a l'article mencionat a continuacio. Si us plau, aquest programa es codi lliure, per referenciar-lo citeu: Bofill, P. & Torras, C., "MBMUDs: a combinatorial extension of BIBDs showing good optimality behaviour", Journal of Statistical Planning and Inference 124 (2004) 185-204. Per compilar: cc -o MBMUDs -O MBMUDs.c -lm Pau, Maig 2002 (pau@ac.upc.es) Ultima revisio, 29 de Juliol 2004 (la versio de l'1 de Juliol tenia un error: a cada iteracio no recorria exhaustivament totes les quadruples possibles). */ /*********************************************************************/ /* MBMUDs.c, MBMUD generator based on setting the right number of ones per row and column, and then minimizing the number of quadruples using unit rearrangements of the kind (10,01)->(01,10). Searches the optimal configuration or, in case of failure, the best local minimum within a given number of iterations. See the reference below for details. This program is free code. Please, use the following reference for citation: Bofill, P. & Torras, C., "MBMUDs: a combinatorial extension of BIBDs showing good optimality behaviour", Journal of Statistical Planning and Inference 124 (2004) 185-204. Compile with: cc -o MBMUDs -O MBMUDs.c -lm Pau, May 2002 (pau@ac.upc.es) Last revision, July 29th 2004 (The version of July 1st had a bug: Not all possible quadruples were tested at every iteration). */ /*********************************************************************/ #include #include #include #include #define MAXB 100 #define MAXV 75 typedef struct { int queden; int llista[MAXB]; }sacboles; /* A bag with balls */ typedef struct { int erre,v0; int ka,b0; int lambda,f0; int Qstar; }descrdis; /* Design descriptors */ long seed; void SacNou(int nboles,sacboles *sac){ /* Inicializes a bag with nboles balls */ int i; for (i=0;illista[i]=i; sac->queden=nboles; } int Bola(sacboles *sac){ /* Takes the next ball from the bag, at random */ int i,quina; if (sac->queden==0) return(-1); quina = (rand()%sac->queden); for (i=quina;iqueden-1;i++) sac->llista[i]=sac->llista[i+1]; sac->queden--; return(quina); } LlistaSac(sacboles sac){ /* List the balls in the bag */ int i; printf("%d (",sac.queden); for (i=0;i(01,10) */ matriu[ii1][jj1]=0; matriu[ii1][jj2]=1; matriu[ii2][jj1]=1; matriu[ii2][jj2]=0; } int DeltaQuad(int matriu[][MAXB],int vfils,int bcols, int ii1,int ii2,int jj1,int jj2){ /* Computes the increment in quadruples that *would* take place if the proposed transformation was taken */ int i,j; int dquads=0; for (i=0;ierre=uuns/vfils; des->v0=uuns%vfils; des->ka=uuns/bcols; des->b0=uuns%bcols; efe=(int)(0.5*vfils*(vfils-1)); pvstar=(int)(0.5*bcols*des->ka*(des->ka-1)+des->b0*des->ka); des->lambda=pvstar/efe; des->f0=pvstar%efe; des->Qstar=(int)(0.5*efe*des->lambda*(des->lambda-1)+des->f0*des->lambda); } main() { int vfils,bcols,uns; /* Number of rows, columns and ones */ int MInc[MAXV][MAXB]; /* Incidence Matrix */ int bestMInc[MAXV][MAXB]; /* Thes best Incidence Matrix so far */ descrdis descr; /* Design descriptors */ int Quads,Dquads; /* Absolut and incremetal number of quadruples */ int bestQuads; /* The number of quadruples of the best local minimum so far */ int iter,niters; /* Iteration number, number of iterations */ int i1,i2,j1,j2; int ii1,ii2,jj1,jj2; int updates,nmins; /* Updates per iteration, number of local minima */ sacboles sac1,sac2,sac3,sac4; seed=time(0); srand(seed); printf("\nGENERATION OF MBMUDs. http://people.ac.upc.es/pau/mbmuds \n\n"); printf("Number of rows? v="); scanf("%d%*c",&vfils); printf("Number of columns? b="); scanf("%d%*c",&bcols); printf("Number of ones? u="); scanf("%d%*c",&uns); printf("Maximum number of iterations? Niter="); scanf("%d%*c",&niters); /* Searches until a MBMUD is found or the maximum number of iterations (niters) is exceeded. When a local minimum is found the incidence matrix MInc is randomly reinicialized. At the end, the best configuration found is listed (either the optimal or the best local minimum). */ Descriptors(vfils,bcols,uns,&descr); Quads=InitMatriu(MInc,vfils,bcols,uns); bestQuads=Quads; CopiaMatriu(MInc,bestMInc,vfils,bcols); printf("\ndiss(%d,%d,%d) %d\n",vfils,bcols,uns,descr.Qstar); nmins=0; for (iter=1;iter<=niters && Quads>descr.Qstar;iter++){ updates=0; /* At every iteration tries all possible quadruples */ for (i1=0,SacNou(vfils,&sac1);i1