/* * clique_max_weight() * * Returns the weight of the maximum weight clique in the graph (or 0 if * the search was aborted). * * g - the graph * opts - time printing options * * Note: As we don't have an algorithm faster than actually finding * a maximum weight clique, we use clique_find_single(). * This incurs only very small overhead. */ int clique_max_weight(graph_t *g,clique_options *opts) { set_t s; int weight; ASSERT((sizeof(setelement)*8)==ELEMENTSIZE); ASSERT(g!=NULL); s=clique_find_single(g,0,0,FALSE,opts); if (s==NULL) { /* Search was aborted. */ return 0; } weight=graph_subgraph_weight(g,s); set_free(s); return weight; }
int main(int argc, char *argv[]) { graph_t *g; set_t s; if (argc!=2) { fprintf(stderr,"%s <dimacs_file>\n",argv[0]); return 1; } g=graph_read_dimacs_file(argv[1]); if (g==NULL) return 1; ASSERT(graph_test(g,stderr)); s=clique_find_single(g,0,0,FALSE,NULL); set_print(s); return 0; }
void multibinpacking(Home home, int n, int m, int k, const IntVarArgs& y, const IntVarArgs& x, const IntSharedArray& D, const IntSharedArray& B, IntConLevel) { /// Check input sizes if (n*k != D.size() ) throw ArgumentSizeMismatch("Int::multibinpacking"); if (k != B.size() ) throw ArgumentSizeMismatch("Int::multibinpacking"); if (n != x.size() ) throw ArgumentSizeMismatch("Int::multibinpacking"); if (m*k != y.size() ) throw ArgumentSizeMismatch("Int::multibinpacking"); for (int i=B.size(); i--; ) Limits::nonnegative(B[i],"Int::multibinpacking"); if (home.failed()) return; /// Post first each single binpacking constraint /// Capacity constraint for each dimension for ( int j = 0; j < m; ++j ) for ( int l = 0; l < k; ++l ) { IntView yi(y[j*k+l]); GECODE_ME_FAIL(yi.lq(home,B[l])); } /// Post a binpacking constraints for each dimension for ( int l = 0; l < k; ++l ) { ViewArray<OffsetView> yv(home,m); for (int j=m; j--; ) yv[j] = OffsetView(y[j*k+l],0); ViewArray<BinPacking::Item> xs(home,x.size()); for (int i=xs.size(); i--; ) xs[i] = BinPacking::Item(x[i],D[i*k+l]); Support::quicksort(&xs[0], xs.size()); GECODE_ES_FAIL(Int::BinPacking::Pack::post(home,yv,xs)); } /// Clique Finding and Alldifferent posting { /// Firt construct the conflict graph graph_t* g = graph_new(n); for ( int a = 0; a < n-1; ++a ) { for ( int b = a+1; b < n; ++b ) { int v = a; /// The variable with smaller domain int w = b; unsigned int nl = 0; if ( x[a].size() > x[b].size() ) { v = b; w = a; } IntVarValues i(x[v]); IntVarValues j(x[w]); while ( i() ) { if ( i.val() != j.val() ) { if ( i.val() < j.val() ) break; else ++i; } else { for ( int l = 0; l < k; ++l ) if ( D[a*k+l] + D[b*k+l] > B[l] ) { nl++; break; } ++i; ++j; } } if ( nl >= x[v].size() ) GRAPH_ADD_EDGE(g,a,b); } } /// Consitency cheking: look for the maximum clique clique_options* opts; opts = (clique_options*) malloc (sizeof(clique_options)); opts->time_function=NULL; opts->reorder_function=reorder_by_default; opts->reorder_map=NULL; opts->user_function=NULL; opts->user_data=NULL; opts->clique_list=NULL; opts->clique_list_length=0; set_t s; s = clique_find_single ( g, 0, 0, TRUE, opts); if ( s != NULL ) { if ( set_size(s) > m ) { set_free(s); free(opts); graph_free(g); GECODE_ES_FAIL(ES_FAILED); } if ( true ) { //option == 1 ) { FIXING for ( int a = 0, j = 0; a < n; ++a ) { if ( SET_CONTAINS_FAST(s,a) ) { assert( x[a].in(j) ); IntView xi(x[a]); GECODE_ME_FAIL(xi.eq(home,j++)); } } } } if ( s!=NULL ) set_free(s); /// List every maximal clique in the conflict graph opts->user_function=record_clique_func; clique_find_all ( g, 2, 0, TRUE, opts); for ( int c = 0; c < clique_count; c++ ) { ViewArray<IntView> xv(home, set_size(clique_list[c])); for ( int a = 0, idx = 0; a < n; ++a ) if ( SET_CONTAINS_FAST(clique_list[c],a) ) xv[idx++] = x[a]; GECODE_ES_FAIL(Distinct::Dom<IntView>::post(home,xv)); set_free(clique_list[c]); } free(opts); graph_free(g); } }
int get_h(C_Graphe &G, char *name, int *UB){ int *som = new int[G.nb_sommets]; for(int i=0 ; i<G.nb_sommets ; i++){som[i] = 0;} int cpt=0; int h=0; int nb; *UB = 0; while (cpt <= G.nb_sommets-1){ graph_t *g; g=(graph_t *)calloc(1,sizeof(graph_t)); g->n = G.nb_sommets; g->edges=(set_t *)calloc(g->n,sizeof(set_t)); for (int i=0; i<g->n; i++){ g->edges[i]=set_new(g->n); } g->weights=(int *)calloc(g->n,sizeof(int)); for(int i=0; i<g->n; i++){ g->weights[i]=1; } for(int i=0; i<g->n; i++){ for(int j=i+1; j<g->n; j++){ if( G.matrice_adjacence[i][j] == 0 && i!=j && som[i]+som[j] == 0 ){ GRAPH_ADD_EDGE(g,i,j); // cout<<"adding edge "<<i<<","<<j<<"\n"; } } } set_t s; s = clique_find_single(g,0,0,FALSE,NULL); nb=0; for(int i=0 ; i<G.nb_sommets ; i++){ if (SET_CONTAINS(s,i)){ som[i] = 1; cpt++; nb++; } } h++; *UB=*UB+nb*h; for (int i=0; i<g->n; i++){ set_free(g->edges[i]); } free(g->edges); free(g->weights); free(g); set_free(s); } for (int i=0; i<G.nb_sommets; i++){ if(som[i] == 0){ h++; break; } } ofstream fichier; ostringstream tmp; //Ouverture fichiers tmp.str("");tmp<<"h.col"; fichier.open(tmp.str().c_str(), ios::out|ios::app); if (!fichier){ cerr << "Erreur à l'ouverture du fichier\n"; return 1; } fichier<<name<<" "<<h<<" "<<*UB<<"\n"; fichier.close(); delete [] som; return h; }
void STABLE_max_heur(C_Graphe &G, int *valeur){ int *som = new int[G.nb_sommets]; for(int i=0 ; i<G.nb_sommets ; i++){som[i] = 0;} int cpt=0; int h=0; int nb; *valeur = 0; while (cpt <= G.nb_sommets-1){ graph_t *g; g=(graph_t *)calloc(1,sizeof(graph_t)); g->n = G.nb_sommets; g->edges=(set_t *)calloc(g->n,sizeof(set_t)); for (int i=0; i<g->n; i++){ g->edges[i]=set_new(g->n); } g->weights=(int *)calloc(g->n,sizeof(int)); for(int i=0; i<g->n; i++){ g->weights[i]=1; } for(int i=0; i<g->n; i++){ for(int j=i+1; j<g->n; j++){ if( G.matrice_adjacence[i][j] == 0 && i!=j && som[i]+som[j] == 0 ){ GRAPH_ADD_EDGE(g,i,j); // cout<<"adding edge "<<i<<","<<j<<"\n"; } } } set_t s; s = clique_find_single(g,0,0,FALSE,NULL); nb=0; for(int i=0 ; i<G.nb_sommets ; i++){ if (SET_CONTAINS(s,i)){ som[i] = 1; cpt++; nb++; } } h++; *valeur=*valeur+nb*h; for (int i=0; i<g->n; i++){ set_free(g->edges[i]); } free(g->edges); free(g->weights); free(g); set_free(s); } for (int i=0; i<G.nb_sommets; i++){ if(som[i] == 0){ h++; *valeur=*valeur+h; break; } } delete [] som; return ; }