int main(int argc, char const *argv[]) { Point coordinates[C]; double distances[C][C]; std::stack<Path> unexplored_paths; // std::priority_queue<Path,std::vector<Path>,ComparePaths> unexplored_paths; Path best_path; best_path.cost = std::numeric_limits<float>::infinity(); Path origin, current_path, new_path; origin.cost = 0.0; origin.order.push_back(0); for(int i = 0; i < C; i++) origin.visited[i] = false; clock_t begin, end; double time_spent; begin = clock(); initialize_coordinates(coordinates); calculate_distances(coordinates,distances); unexplored_paths.push(origin); while(!unexplored_paths.empty()) { current_path = unexplored_paths.top(); unexplored_paths.pop(); if(current_path.cost < best_path.cost) { for(int node = 1 ; node < C ; node++) { if(!current_path.visited[node]) { new_path = current_path; new_path.cost += distances[new_path.order.back()][node]; if(new_path.cost < best_path.cost) { new_path.visited[node] = true; new_path.order.push_back(node); if(new_path.order.size() < C) { unexplored_paths.push(new_path); } else { best_path = new_path; } } } } } } // printf("\n SUM : %lf\n", sumA ); // printf("\n MIN : %lf\n", minA ); end = clock(); time_spent = (double)(end - begin) / CLOCKS_PER_SEC; print_matrix(distances); print_path(best_path); double check_cost = 0.0; for(int i = 0 ; i < C-1 ; i++) { check_cost += distances[best_path.order[i]][best_path.order[i+1]]; } printf("%lf\n",check_cost ); printf("\nTIME : %lf seconds\n", time_spent); return 0; }
int main(void) { int n; FILE* fptr = fopen("input.in", "r"); assert(fptr != NULL); n = count_lines(fptr); printf("\nNumber of cities: %i\n",n); /*Init cities*/ City* cities[n]; read_cities(fptr, cities); printf("Init cities succesful... \n"); /*Init distances*/ double** distances; distances=calculate_distances(distances,cities, n); printf("Init distances succesful... \n"); /*Init GA config*/ Config config; config.populationSize=1000; config.mutationRate=0.2; config.numGenerations=6000; config.numElitism=1; config.mutationSize=1; config.maxBreeding=10; config.numGenes=n; printf("Init GA config succesful... \n"); /*Init random seed*/ srand ( time(NULL) ); printf("Init random seed succesful... \n"); /*Init population*/ Population population; generate_random_population(&population, distances, &config); printf("Init population succesful... \n"); int numGenerations = 0; /*Start evolution*/ while(numGenerations < config.numGenerations){ numGenerations++; if (numGenerations%1000==0) printf("Shortest path of %ith generation: %i\n", numGenerations, population.path[0].fitness); /*Sort population*/ qsort(population.path, population.numPaths, sizeof(Path), compare_population); /*Breed population*/ simple_breed_population(&population, config.numGenes, distances); /*Mutate population*/ mutate_population(&population, distances, &config); } /*Sort population*/ qsort(population.path, population.numPaths, sizeof(Path), compare_population); printf("Shortest path is %i\n", population.path[0].fitness); int i; for(i=0;i<config.numGenes;i++){ print_city(cities[population.path[0].combination[i]]); } return 0; }
int main(int argc, char const *argv[]) { Path best_path; best_path.cost = std::numeric_limits<float>::infinity(); std::queue<Path> global_unexplored_paths; double begin, end; Point coordinates[C]; double distances[C][C]; long busy_workers; int num_threads; int* best_changes; // DEBUG int* paths_explored; // DEBUG int* pushes; // DEBUG #pragma omp parallel shared(best_path, global_unexplored_paths,coordinates,distances,busy_workers,num_threads) { std::stack<Path> unexplored_paths; Path current_path, new_path; #pragma omp master { begin = omp_get_wtime(); num_threads = omp_get_num_threads(); busy_workers = ( 1 << num_threads ) -1; best_changes = new int[num_threads]; //DEBUG paths_explored = new int[num_threads]; //DEBUG pushes = new int[num_threads]; //DEBUG for(int i = 0; i < num_threads; i++)//DEBUG {//DEBUG best_changes[i] = paths_explored[i] = pushes[i] = 0;//DEBUG }//DEBUG } // INIT VARS initialize_coordinates(coordinates); calculate_distances(coordinates,distances); #pragma omp master { Path origin; origin.cost = 0.0; origin.order.push_back(0); for(int i = 0; i < C; i++) origin.visited[i] = false; global_unexplored_paths.push(origin); // master_BFS_search while( global_unexplored_paths.size() < C*(C-1)*(C-2)*(C-3) ) { current_path = global_unexplored_paths.front(); global_unexplored_paths.pop(); for(int node = 1 ; node < C ; node++) { new_path = current_path; if(!new_path.visited[node]) { new_path.cost += distances[new_path.order.back()][node]; new_path.visited[node] = true; new_path.order.push_back(node); if(new_path.cost < best_path.cost) { if(new_path.order.size() < C) { global_unexplored_paths.push(new_path); } else { best_path = new_path; } } } } } } #pragma omp barrier int thread_num = omp_get_thread_num(); long busy_mask = 0x1 << thread_num; long idle_mask = busy_workers ^ busy_mask; long all_working = ( 1 << num_threads ) -1; // worker_DFS_Search while(busy_workers > 0 || !global_unexplored_paths.empty()) { if(!global_unexplored_paths.empty()) { #pragma omp critical(global_unexplored_paths) { if(!global_unexplored_paths.empty()) { unexplored_paths.push(global_unexplored_paths.front()); global_unexplored_paths.pop(); busy_workers |= busy_mask; } } } while(!unexplored_paths.empty()) { current_path = unexplored_paths.top(); unexplored_paths.pop(); if(current_path.cost < best_path.cost) { for(int node = 1 ; node < C ; node++) { new_path = current_path; if(!new_path.visited[node]) { paths_explored[thread_num]++;//DEBUG new_path.cost += distances[new_path.order.back()][node]; new_path.visited[node] = true; new_path.order.push_back(node); if(new_path.cost < best_path.cost) { if(new_path.order.size() < C) { if( busy_workers < all_working ) { #pragma omp critical(global_unexplored_paths) { global_unexplored_paths.push(new_path); } pushes[thread_num]++; //DEBUG } else unexplored_paths.push(new_path); } else { #pragma omp critical(best_path) { if(new_path.cost < best_path.cost) { best_path = new_path; best_changes[thread_num]++;//DEBUG } } } } } } } } #pragma omp atomic busy_workers &= idle_mask; } // master output #pragma omp master { end = omp_get_wtime(); char buffer[80]; FILE *f = fopen("Results.txt", "a"); if(f != NULL) { fprintf(f, "%s\n", gettime(buffer)); fprintf(f,"%d classrooms\n",C); fprintf(f,"%d threads\n",num_threads); fprint_path(f,best_path); fprintf(f,"Proc Changes Paths Pushes\n");//DEBUG for(int i = 0; i < num_threads; i++)//DEBUG {//DEBUG fprintf(f," %02d %03d %8d %03d\n",i,best_changes[i],paths_explored[i],pushes[i]);//DEBUG }//DEBUG fprintf(f,"Time : %f seconds\n", end-begin); fprintf(f,"\n=====================\n"); } } } delete[] best_changes; //DEBUG delete[] paths_explored; //DEBUG delete[] pushes; //DEBUG // print_matrix(distances); return 0; }
int main(int argc, char *argv[]) { int fd, maskfd; CELL *mask; DCELL *dcell; struct GModule *module; struct History history; int row, col; int searchrow, searchcolumn, pointsfound; int *shortlistrows = NULL, *shortlistcolumns = NULL; long ncells = 0; double north, east; double dist; double sum1, sum2, interp_value; int n; double p; struct { struct Option *input, *npoints, *power, *output, *dfield, *col; } parm; struct { struct Flag *noindex; } flag; struct cell_list { int row, column; struct cell_list *next; }; struct cell_list **search_list = NULL, **search_list_start = NULL; int max_radius, radius; int searchallpoints = 0; char *tmpstr1, *tmpstr2; G_gisinit(argv[0]); module = G_define_module(); G_add_keyword(_("vector")); G_add_keyword(_("surface")); G_add_keyword(_("interpolation")); G_add_keyword(_("IDW")); module->description = _("Provides surface interpolation from vector point data by Inverse " "Distance Squared Weighting."); parm.input = G_define_standard_option(G_OPT_V_INPUT); parm.dfield = G_define_standard_option(G_OPT_V_FIELD); parm.col = G_define_standard_option(G_OPT_DB_COLUMN); parm.col->required = NO; parm.col->label = _("Name of attribute column with values to interpolate"); parm.col->description = _("If not given and input is 2D vector map then category values are used. " "If input is 3D vector map then z-coordinates are used."); parm.col->guisection = _("Values"); parm.output = G_define_standard_option(G_OPT_R_OUTPUT); parm.npoints = G_define_option(); parm.npoints->key = "npoints"; parm.npoints->key_desc = "count"; parm.npoints->type = TYPE_INTEGER; parm.npoints->required = NO; parm.npoints->description = _("Number of interpolation points"); parm.npoints->answer = "12"; parm.npoints->guisection = _("Settings"); parm.power = G_define_option(); parm.power->key = "power"; parm.power->type = TYPE_DOUBLE; parm.power->answer = "2.0"; parm.power->label = _("Power parameter"); parm.power->description = _("Greater values assign greater influence to closer points"); parm.power->guisection = _("Settings"); flag.noindex = G_define_flag(); flag.noindex->key = 'n'; flag.noindex->label = _("Don't index points by raster cell"); flag.noindex->description = _("Slower but uses" " less memory and includes points from outside region" " in the interpolation"); flag.noindex->guisection = _("Settings"); if (G_parser(argc, argv)) exit(EXIT_FAILURE); if (sscanf(parm.npoints->answer, "%d", &search_points) != 1 || search_points < 1) G_fatal_error(_("Illegal number (%s) of interpolation points"), parm.npoints->answer); list = (struct list_Point *) G_calloc((size_t) search_points, sizeof(struct list_Point)); p = atof(parm.power->answer); /* get the window, dimension arrays */ G_get_window(&window); if (!flag.noindex->answer) { npoints_currcell = (long **)G_malloc(window.rows * sizeof(long *)); points = (struct Point ***)G_malloc(window.rows * sizeof(struct Point **)); for (row = 0; row < window.rows; row++) { npoints_currcell[row] = (long *)G_malloc(window.cols * sizeof(long)); points[row] = (struct Point **)G_malloc(window.cols * sizeof(struct Point *)); for (col = 0; col < window.cols; col++) { npoints_currcell[row][col] = 0; points[row][col] = NULL; } } } /* read the elevation points from the input sites file */ read_sites(parm.input->answer, parm.dfield->answer, parm.col->answer, flag.noindex->answer); if (npoints == 0) G_fatal_error(_("No points found")); nsearch = npoints < search_points ? npoints : search_points; if (!flag.noindex->answer) { /* Arbitrary point to switch between searching algorithms. Could do * with refinement PK */ if ((window.rows * window.cols) / npoints > 400) { /* Using old algorithm.... */ searchallpoints = 1; ncells = 0; /* Make an array to contain the row and column indices that have * sites in them; later will just search through all these. */ for (searchrow = 0; searchrow < window.rows; searchrow++) for (searchcolumn = 0; searchcolumn < window.cols; searchcolumn++) if (npoints_currcell[searchrow][searchcolumn] > 0) { shortlistrows = (int *)G_realloc(shortlistrows, (1 + ncells) * sizeof(int)); shortlistcolumns = (int *)G_realloc(shortlistcolumns, (1 + ncells) * sizeof(int)); shortlistrows[ncells] = searchrow; shortlistcolumns[ncells] = searchcolumn; ncells++; } } else { /* Fill look-up table of row and column offsets for * doing a circular region growing search looking for sites */ /* Use units of column width */ max_radius = (int)(0.5 + sqrt(window.cols * window.cols + (window.rows * window.ns_res / window.ew_res) * (window.rows * window.ns_res / window.ew_res))); search_list = (struct cell_list **)G_malloc(max_radius * sizeof(struct cell_list *)); search_list_start = (struct cell_list **)G_malloc(max_radius * sizeof(struct cell_list *)); for (radius = 0; radius < max_radius; radius++) search_list[radius] = NULL; for (row = 0; row < window.rows; row++) for (col = 0; col < window.cols; col++) { radius = (int)sqrt(col * col + (row * window.ns_res / window.ew_res) * (row * window.ns_res / window.ew_res)); if (search_list[radius] == NULL) search_list[radius] = search_list_start[radius] = G_malloc(sizeof(struct cell_list)); else search_list[radius] = search_list[radius]->next = G_malloc(sizeof(struct cell_list)); search_list[radius]->row = row; search_list[radius]->column = col; search_list[radius]->next = NULL; } } } /* allocate buffers, etc. */ dcell = Rast_allocate_d_buf(); if ((maskfd = Rast_maskfd()) >= 0) mask = Rast_allocate_c_buf(); else mask = NULL; fd = Rast_open_new(parm.output->answer, DCELL_TYPE); /* GTC Count of window rows */ G_asprintf(&tmpstr1, n_("%d row", "%d rows", window.rows), window.rows); /* GTC Count of window columns */ G_asprintf(&tmpstr2, n_("%d column", "%d columns", window.cols), window.cols); /* GTC First argument is map name, second - message about number of rows, third - columns. */ G_important_message(_("Interpolating raster map <%s> (%s, %s)..."), parm.output->answer, tmpstr1, tmpstr2); G_free(tmpstr1); G_free(tmpstr2); north = window.north + window.ns_res / 2.0; for (row = 0; row < window.rows; row++) { G_percent(row, window.rows, 1); if (mask) Rast_get_c_row(maskfd, mask, row); north -= window.ns_res; east = window.west - window.ew_res / 2.0; for (col = 0; col < window.cols; col++) { east += window.ew_res; /* don't interpolate outside of the mask */ if (mask && mask[col] == 0) { Rast_set_d_null_value(&dcell[col], 1); continue; } /* If current cell contains more than nsearch points just average * all the points in this cell and don't look in any others */ if (!(flag.noindex->answer) && npoints_currcell[row][col] >= nsearch) { sum1 = 0.0; for (i = 0; i < npoints_currcell[row][col]; i++) sum1 += points[row][col][i].z; interp_value = sum1 / npoints_currcell[row][col]; } else { if (flag.noindex->answer) calculate_distances_noindex(north, east); else { pointsfound = 0; i = 0; if (searchallpoints == 1) { /* If there aren't many sites just check them all to find * the nearest */ for (n = 0; n < ncells; n++) calculate_distances(shortlistrows[n], shortlistcolumns[n], north, east, &pointsfound); } else { radius = 0; while (pointsfound < nsearch) { /* Keep widening the search window until we find * enough points */ search_list[radius] = search_list_start[radius]; while (search_list[radius] != NULL) { /* Always */ if (row < (window.rows - search_list[radius]->row) && col < (window.cols - search_list[radius]->column)) { searchrow = row + search_list[radius]->row; searchcolumn = col + search_list[radius]->column; calculate_distances(searchrow, searchcolumn, north, east, &pointsfound); } /* Only if at least one offset is not 0 */ if ((search_list[radius]->row > 0 || search_list[radius]->column > 0) && row >= search_list[radius]->row && col >= search_list[radius]->column) { searchrow = row - search_list[radius]->row; searchcolumn = col - search_list[radius]->column; calculate_distances(searchrow, searchcolumn, north, east, &pointsfound); } /* Only if both offsets are not 0 */ if (search_list[radius]->row > 0 && search_list[radius]->column > 0) { if (row < (window.rows - search_list[radius]->row) && col >= search_list[radius]->column) { searchrow = row + search_list[radius]->row; searchcolumn = col - search_list[radius]->column; calculate_distances(searchrow, searchcolumn, north, east, &pointsfound); } if (row >= search_list[radius]->row && col < (window.cols - search_list[radius]->column)) { searchrow = row - search_list[radius]->row; searchcolumn = col + search_list[radius]->column; calculate_distances(searchrow, searchcolumn, north, east, &pointsfound); } } search_list[radius] = search_list[radius]->next; } radius++; } } } /* interpolate */ sum1 = 0.0; sum2 = 0.0; for (n = 0; n < nsearch; n++) { if ((dist = sqrt(list[n].dist))) { sum1 += list[n].z / pow(dist, p); sum2 += 1.0 / pow(dist, p); } else { /* If one site is dead on the centre of the cell, ignore * all the other sites and just use this value. * (Unlikely when using floating point numbers?) */ sum1 = list[n].z; sum2 = 1.0; break; } } interp_value = sum1 / sum2; } dcell[col] = (DCELL) interp_value; } Rast_put_d_row(fd, dcell); } G_percent(1, 1, 1); Rast_close(fd); /* writing history file */ Rast_short_history(parm.output->answer, "raster", &history); Rast_command_history(&history); Rast_write_history(parm.output->answer, &history); G_done_msg(" "); exit(EXIT_SUCCESS); }