/* Usually called once every second, this function updates the * uptime.yaku TXT CHAOS RR in the local table. */ int uptime_refresh(void) { struct RRentry *uptimerr = NULL; char buffer[1024]; byte *encoded_uptime; int encoded_uptime_size; time_t uptime; uptimerr = local_search(YAKU_UPTIME_RR_NAME".", T_TXT, C_CHAOS, 0); if (uptimerr == NULL) { uptimerr = alloc_rr(YAKU_UPTIME_RR_NAME, T_TXT, C_CHAOS, 1); if (uptimerr == NULL) return -1; uptimerr->data[0] = 0; uptimerr->ttl = 0; local_add_entry(uptimerr); } uptime = get_sec() - ens_start; snprintf(buffer, 1024, "%ld days,%ld hours,%ld minutes,%ld seconds", uptime/86400, (uptime%86400)/3600, ((uptime%86400)%3600)/60, ((uptime%86400)%3600)%60); encoded_uptime = name_encode(buffer, &encoded_uptime_size, ','); if (encoded_uptime == NULL) return encoded_uptime_size; encoded_uptime_size--; free(uptimerr->data); uptimerr->data = encoded_uptime; uptimerr->size = encoded_uptime_size; return 0; }
Solution *Solver::solve(){ std::ofstream out_file (file_out.c_str()); if (!out_file.is_open()) { std::cout << "Unable to open file"; } init(); // ------ print solution ------- print_solution(current_solution); //print_file_solution(0, current_solution, out_file); // ------ print solution ------- clock_t start, end, total; start = clock(); for(unsigned int i=0;i<max_iterations;i++){ iter_cost_time = 0; std::cout << "----- iteration "<< i << " ------" << std::endl; local_search(); // ------ print solution ------- std::cout << "local search "; print_solution(current_solution); print_file_solution(i, current_solution, out_file); // ------ print solution ------- global_search(); // ------ print solution ------- std::cout << "global search "; print_solution(current_solution); //print_file_solution(i, current_solution, out_file); // ------ print solution ------- tabu_list->update_tabu(); total_cost_time += iter_cost_time; if(global_best_cost == 0) break; } end = clock(); total = end - start; printf("\nTotal OpenCL Kernel time in milliseconds = %0.3f ms\n", (total_cost_time / 1000000.0) ); printf("Total CPU time in milliseconds = %0.3f ms\n", total /1000.0); return global_best; }
void remove_empty_clusters_l1(CtrlType *ctrl, GraphType *graph, int nparts, idxtype *w, float *tpwgts, float ubfactor){ int *clustersize=imalloc(nparts, "remove_empty_clusters: clusterSize"); int number_of_empty_cluster=0, i, s; for(i=0; i<nparts; i++) clustersize[i] =0; clusterSize(graph, clustersize); for(i=0; i<nparts; i++) if(clustersize[i] ==0) number_of_empty_cluster ++; if(number_of_empty_cluster>0) local_search(ctrl, graph, nparts, 1, w, tpwgts, ubfactor); free(clustersize); }
void pingpong(CtrlType *ctrl, GraphType *graph, int nparts, int chain_length, float *tpwgts, float ubfactor, int toplevel) // do batch-local search; chain_length is the search length { int nvtxs, nedges, moves, iter; idxtype *w; //float *m_adjwgt; nedges = graph->nedges; nvtxs = graph->nvtxs; w = idxsmalloc(nvtxs, 0, "pingpong: weight"); Compute_Weights(ctrl, graph, w); //m_adjwgt = fmalloc(nedges, "pingpong: normalized matrix"); //transform_matrix(ctrl, graph, w, m_adjwgt); //printf("Chain length is %d.\n", chain_length); moves =0; iter =0; //printf("Number of boundary points is %d\n", graph->nbnd); do{ //Weighted_kernel_k_means(ctrl, graph, nparts, w, m_adjwgt, tpwgts, ubfactor); Weighted_kernel_k_means(ctrl, graph, nparts, w, tpwgts, ubfactor); if (chain_length>0){ //moves = local_search(ctrl, graph, nparts, chain_length, w, m_adjwgt, tpwgts, ubfactor); moves = local_search(ctrl, graph, nparts, chain_length, w, tpwgts, ubfactor); //printf("Number of local search moves is %d\n", moves); //printf("Number of boundary points is %d\n", graph->nbnd); } iter ++; if (iter > MAXITERATIONS) break; }while(moves >0) ; if(memory_saving ==0){ remove_empty_clusters_l1(ctrl, graph, nparts, w, tpwgts, ubfactor); if(toplevel>0) remove_empty_clusters_l2(ctrl, graph, nparts, w, tpwgts, ubfactor); } free(w); //free(m_adjwgt); }
gcp_solution_t* antcol(gcp_t *problem) {/*{{{*/ int c = 0; gcp_solution_t *result; result = malloc(sizeof(gcp_solution_t)); initialize_data(problem); best_ant->ncolors = INT_MAX; int terminate = FALSE; while (!terminate) { initialize_var_phero(problem); construct_solutions(problem, c); local_search(problem); update_pheromone_trails(problem); c++; if (problem->flags & FLAG_VERBOSE) { printf("Cycle %d - Colors used: %d\n", c, best_ant->ncolors); } else { printf("*"); fflush(stdout); } /* test stop conditions */ if ((problem->flags & FLAG_CYCLE) && problem->ncycles <= c) terminate = TRUE; if ((problem->flags & FLAG_TIME) && problem->time <= current_usertime_secs()) terminate = TRUE; } *result = *best_ant; result->ncycles = c; return result; }/*}}}*/
int main(int argc, char **argv) { int num_trials; double step_size; double best_minimum; struct timeval start, finish; int i; if (argc != 3) { fprintf(stderr,"Usage: %d <num trials> <step size>\n",argc); exit(1); } if ((sscanf(argv[1],"%d",&num_trials) != 1) || (sscanf(argv[2],"%lf",&step_size) != 1)) { fprintf(stderr,"Usage: %d <num trials> <step size>\n"); exit(1); } gettimeofday(&start,NULL); best_minimum = -1.0; for (i=0; i<num_trials; i++) { double local_minimum; local_minimum = local_search(step_size, i); //fprintf(stdout,"Local minimum found for trial #%d: %.4f\n",i,local_minimum); if ((best_minimum < 0) || (local_minimum < best_minimum)) { best_minimum = local_minimum; } } gettimeofday(&finish,NULL); fprintf(stdout,"Time to solution: %.4f\n", ((finish.tv_sec*1000000.0+finish.tv_usec) - (start.tv_sec*1000000.0+start.tv_usec)) / 1000000.00); fprintf(stdout,"Best found minimum: %.4f\n",best_minimum); }
void Solver::run( const Instance & instance, const TerminationCondition & termination, int population_size ) { // generate random population for(int i = 0; i < population_size; i++){ Individual * next = new Individual(instance.num_words()); state.inc_processed(); next->randomize(); next->set_cost(instance.evaluate(next)); state.population().add(next); } // evaluate individuals, sort them etc. update_population(instance); while( !termination(state) ){ // select parents std::vector<const Individual *> parents = parent_selector(state); // crossover std::vector<Individual *> children = crossover_strategy(state, parents); // mutation mutation_strategy(state, children); // evaluate children for(unsigned int i = 0; i < children.size(); i++){ int c = instance.evaluate(children[i]); state.inc_processed(); children[i]->set_cost(c); } local_search(state, children); // replacement replacement_strategy(state, children); update_population(instance); immigration(state, instance); update_population(instance); // one more iteration... state.inc_iteration(); printf("current iteration : %5d best: %d diversity : %lf processed : %llu \n", state.iteration(), state.population().best(), state.population().diversity(), state.processed()); fflush(stdout); save_iteration_info(); } }
void initialize() { coarse_search(); communication(); local_search(); }
int main(int argc, char *argv[]) {/*{{{*/ extern char *optarg; char op, *namefilein; int it; ALPHA = 2; BETA = 1; RHO = 0.4; M = 40; it = 1000; /* Usando getopt para tratamento dos argumentos */ struct option longopts[] = {/*{{{*/ {"alpha", 1, NULL, 'a'}, {"beta", 1, NULL, 'b'}, {"rho", 1, NULL, 'r'}, {"ants", 1, NULL, 'm'}, {"iterations", 1, NULL, 'i'}, {"help", 0, NULL, 'h'}, };/*}}}*/ while ((op = getopt_long(argc, argv, "a:b:r:m:i:h", longopts, NULL)) != -1) {/*{{{*/ switch (op) { case 'a': ALPHA = atoi(optarg); break; case 'b': BETA = atoi(optarg); break; case 'r': RHO = atoi(optarg); break; case 'm': M = atoi(optarg); break; case 'i': it = atoi(optarg); break; case 'h': print_help(argv[0]); return 0; // case ':': // /* a opção anterior precisa de especificação mas não recebeu */ // printf("Erro: arquivo de saida nao especificado. Use '-h' para ajuda.\n"); // return 0; // case '?': // /* opção não existe */ // printf("Erro: opcao '-%c' nao existente. Use '-h' para ajuda.\n", optopt); // return 0; } }/*}}}*/ /*{{{*/ /* O único argumento não capturado acima é o nome do arquivo de entrada, * se existir */ if (optind < argc) { namefilein = malloc(sizeof(char) * strlen(argv[optind])); strcpy(namefilein, argv[optind++]); /* verificar se foi passado algum argumento a mais */ if (optind < argc) { printf("Erro: foram passados muitos argumentos. Use '-h' para ajuda.\n"); return 0; } } else { printf("Erro: falta nome do arquivo de entrada. Use '-h' para ajuda.\n"); return 0; } if (!fopen(namefilein, "r")) { printf("Erro: arquivo ou diretorio inexistente.\n"); return 0; } /*}}}*/ int terminate = 0; int k = 0; initialize_data(namefilein); while (!terminate && k != it) { construct_solutions(); local_search(); //update_statistics(); update_pheromone_trails(); k++; } int menor = 0; for (k = 1; k < M; k++) { if (ant[menor].tour_length > ant[k].tour_length) { menor = k; } } printf("%d: %d\n", menor, ant[menor].tour_length); return 0; }/*}}}*/
Solution* oLocalMove::local_search(Problem* prob, Solution* ind) throw(exception){ Codification* neighbor = ind->codif()->Clone(prob); local_search(prob, neighbor); return prob->NewSolution(neighbor); }