int main(int argc,char *argv[]) { int *g; int *new_g; int gsize; int count; int i; int j; int best_count; int best_i; int best_j; int best_x; int best_y; void *taboo_list; srand (time(NULL)); /* * start with graph of size 8 */ gsize = 54; g = (int *)malloc(gsize*gsize*sizeof(int)); if(g == NULL) { exit(1); } /* * make a fifo to use as the taboo list */ taboo_list = FIFOInitEdge(TABOOSIZE); if(taboo_list == NULL) { exit(1); } /* * start out with all zeros */ memset(g,0,gsize*gsize*sizeof(int)); ReadGraph(g,gsize); /* int ns=gsize+10; new_g = (int *)malloc((ns)*(ns)*sizeof(int)); //CopyGraph(g,gsize,new_g,ns); GoodCopy(g,gsize,new_g,ns); PrintGraph(new_g,ns); count = CliqueCount(new_g,ns); printf("%d\n",count); return ; */ /* * while we do not have a publishable result */ int flag=1; int lastcount=IMAX; while(gsize < 102) { /* * find out how we are doing */ count = CliqueCount(g,gsize); /* * if we have a counter example */ if(count == 0) { printf("Eureka! Counter-example found!\n"); PrintGraph(g,gsize); WriteGraph(g,gsize); /* * make a new graph one size bigger */ new_g = (int *)malloc((gsize+1)*(gsize+1)*sizeof(int)); if(new_g == NULL) exit(1); /* * copy the old graph into the new graph leaving the * last row and last column alone */ //CopyGraph(g,gsize,new_g,gsize+1); GoodCopy(g,gsize,new_g,gsize+1); /* * zero out the last column and last row for(i=0; i < (gsize+1); i++) { new_g[i*(gsize+1) + gsize] = 0; // last column new_g[gsize*(gsize+1) + i] = 0; // last row } */ /* * throw away the old graph and make new one the * graph */ free(g); g = new_g; gsize = gsize+1; /* * reset the taboo list for the new graph */ taboo_list = FIFOResetEdge(taboo_list); /* * keep going */ continue; } /* * otherwise, we need to consider flipping an edge * * let's speculative flip each edge, record the new count, * and unflip the edge. We'll then remember the best flip and * keep it next time around * * only need to work with upper triangle of matrix => * notice the indices */ int x,y; best_count = BIGCOUNT; for(x=0; x < gsize*gsize; x++) { for(y=x; y < gsize*gsize; y++) { if(y%gsize < x%gsize) continue; // swap if(g[x]==g[y] && x!=y) continue; swap(g,x,y); count = CliqueCount(g,gsize); // * is it better and the i,j,count not taboo? if((count < best_count) && !FIFOFindEdge(taboo_list,x,y)) // !FIFOFindEdgeCount(taboo_list,i,j,count)) { best_count = count; best_x = x; best_y = y; } // * flip it back swap(g,x,y); } } /* best_count = BIGCOUNT; for(i=0; i < gsize; i++) { for(j=i+1; j < gsize; j++) { // flip it g[i*gsize+j] = 1 - g[i*gsize+j]; count = CliqueCount(g,gsize); // * is it better and the i,j,count not taboo? if((count < best_count) && !FIFOFindEdge(taboo_list,i,j)) // !FIFOFindEdgeCount(taboo_list,i,j,count)) { best_count = count; best_i = i; best_j = j; } // * flip it back g[i*gsize+j] = 1 - g[i*gsize+j]; } } */ if(best_count == BIGCOUNT) { printf("no best edge found, terminating\n"); exit(1); } /* * keep the best flip we saw */ //g[best_i*gsize+best_j] = 1 - g[best_i*gsize+best_j]; swap(g,best_x,best_y); /* * taboo this graph configuration so that we don't visit * it again */ count = CliqueCount(g,gsize); // FIFOInsertEdge(taboo_list,best_i,best_j); FIFOInsertEdgeCount(taboo_list,best_i,best_j,count); // FIFOInsertEdge(taboo_list,best_x,best_y); /* printf("ce size: %d, best_count: %d, swap: (%d,%d)\n", gsize, best_count, best_x, best_y ); */ /* printf("ce size: %d, best_count: %d, best edge: (%d,%d), new color: %d\n", gsize, best_count, best_i, best_j, g[best_i*gsize+best_j]); */ /* * rinse and repeat */ } FIFODeleteGraph(taboo_list); return(0); }
int main(int argc,char *argv[]) { printf("Setting up signals \n"); signal(SIGINT, sig_handler); signal(SIGTERM, sig_handler); int *g; int *new_g; int gsize; int count; int i; int j; int best_count; int best_i; int best_j; void *taboo_list; int val,iter,jter; int gt[2]; int t=INITEM; int sock = -1; char Fname[255]; char c; int ilim = 8; while((c = getopt(argc,argv,ARGS)) != EOF) { switch(c) { case 'f': strncpy(Fname,optarg,sizeof(Fname)); break; case 'l': printf("optarg: %s", optarg); ilim = atoi(optarg); break; default: fprintf(stderr, "test_clique_count unrecognized argument: %c\n",c); fflush(stderr); break; } } if(!ReadGraph(Fname,&g,&gsize)) { gsize = ilim; g = PaleyGraph(ilim); } /* * make a fifo to use as the taboo list */ taboo_list = FIFOInitEdge(TABOOSIZE); if(taboo_list == NULL) { exit(1); } int best_clique = INITEM; int flag = 0; int min_count = BIGCOUNT; //int term = atoi(argv[1]); /* * while we do not have a publishable result */ while(gsize <= ilim) { printf("Graph size is %d\n", gsize); /* * find out how we are doing */ count = CliqueCount(g,gsize); int * cliques = (int*)malloc( (gsize)* (gsize) *sizeof(int)); memset (cliques,INICLI,(gsize)* (gsize)); /* * if we have a counter example */ if(count == 0) { printf("Eureka! Counter-example found!\n"); min_count = BIGCOUNT; FILE *fp; //printf("Filename: %s", buf); fp = fopen("result.state", "w"); PrintGraphToFile(g, gsize, fp); fclose(fp); //socket_upload_2(sock, -1, -1, count, gsize, g); /* * make a new graph one size bigger */ new_g = (int *)malloc((gsize+1)*(gsize+1)*sizeof(int)); if(new_g == NULL) exit(1); /* * copy the old graph into the new graph leaving the * last row and last column alone */ CopyGraph(g,gsize,new_g,gsize+1); /* * zero out the last column and last row */ for(i=0; i < (gsize+1); i++) { if(drand48() > 0.5) { new_g[i*(gsize+1) + gsize] = 0; // last column new_g[gsize*(gsize+1) + i] = 0; // last row } else { new_g[i*(gsize+1) + gsize] = 1; // last column new_g[gsize*(gsize+1) + i] = 1; // last row } } /* * throw away the old graph and make new one the * graph */ free(g); g = new_g; gsize = gsize+1; /* * reset the taboo list for the new graph */ taboo_list = FIFOResetEdge(taboo_list); /* * keep going */ continue; } /* * otherwise, we need to consider flipping an edge * * let's speculative flip each edge, record the new count, * and unflip the edge. We'll then remember the best flip and * keep it next time around * * only need to work with upper triangle of matrix => * notice the indices */ best_clique = INICLI; time_t tim; srand((unsigned) time(&tim)); // Introducing randomization into system int choice = rand() % 10; printf("choice: %d \n", choice); if (choice == 0){ i=rand()%gsize; j=rand()%gsize; while (FIFOFindEdge(taboo_list,i,j)){ i=rand()%gsize; j=rand()%gsize; } gt[0]=i; gt[1]=j; g[gt[0]*gsize+gt[1]] = 1 - g[gt[0]*gsize+gt[1]]; // Getting clique count of edges on flipping - assuming random move is best best_clique=CliqueCount_D(g,gsize,gt[0],gt[1], 0); g[gt[0]*gsize+gt[1]] = 1 - g[gt[0]*gsize+gt[1]]; } else{ for(i=0; i < gsize; i++) { for(j=i+1; j < gsize; j++) { if(!FIFOFindEdge(taboo_list,i,j)){ // Clique Count without flip int old_clique = CliqueCount_D(g,gsize,i,j, 0); // Clique Count with flip int new_clique = CliqueCount_D(g,gsize,i,j, 1); // Improvement with flip cliques[i*gsize+j] = count + new_clique - old_clique; } } } // Finding edges in clique for(i=0; i < gsize; i++) { for(j=i+1; j < gsize; j++) { if(!FIFOFindEdge(taboo_list,i,j) ) { if(cliques[i*gsize+j] < best_clique ) { best_clique = cliques[i*gsize+j]; gt[0] = i; gt[1] = j; } } } } } printf("2\n"); if(min_count > count) { min_count = count; } if (best_clique <= count){ g[gt[0]*gsize+gt[1]] = 1 - g[gt[0]*gsize+gt[1]]; if( CliqueCount(g, gsize) > count) { g[gt[0]*gsize+gt[1]] = 1 - g[gt[0]*gsize+gt[1]]; } } printf("3\n"); FIFOInsertEdge(taboo_list,gt[0],gt[1]); printf("4\n"); t-=DTEM; if (t==0){ t=INITEM; } printf("5\n"); //socket_upload_2(sock, gt[0], gt[1], count, gsize, g); printf("size: %d, temperature: %d, count: %d, best_clique: %d\n", gsize, t, count, best_clique); free(cliques); g_latest = g; g_size_latest = gsize; g_count_latest = count; } FIFODeleteGraph(taboo_list); return(0); }