示例#1
0
/*------------------------------------------------------------------*/
int main(int argc, char* argv[]) {
   FILE* digraph_file;
   tour_t tour;
   double start, finish;

   if (argc != 2) Usage(argv[0]);
   digraph_file = fopen(argv[1], "r");
   if (digraph_file == NULL) {
      fprintf(stderr, "Can't open %s\n", argv[1]);
      Usage(argv[0]);
   }
   Read_digraph(digraph_file);
   fclose(digraph_file);
#  ifdef DEBUG
   Print_digraph();
#  endif   
   avail = Init_stack();

   best_tour = Alloc_tour();
   Init_tour(best_tour, INFINITY);
#  ifdef DEBUG
   Print_tour(best_tour, "Best tour");
   printf("City count = %d\n",  City_count(best_tour));
   printf("Cost = %d\n\n", Tour_cost(best_tour));
#  endif
   tour = Alloc_tour();
   Init_tour(tour, 0);
#  ifdef DEBUG
   Print_tour(tour, "Starting tour");
   printf("City count = %d\n",  City_count(tour));
   printf("Cost = %d\n\n", Tour_cost(tour));
#  endif

   GET_TIME(start);
   Iterative_dfs(tour);
   GET_TIME(finish);
   Free_tour(tour);
   
   Print_tour(best_tour, "Best tour");
   printf("Cost = %d\n", best_tour->cost);
   printf("Elapsed time = %e seconds\n", finish-start);

   free(best_tour->cities);
   free(best_tour);
   Free_avail();
   free(digraph);
   return 0;
}  /* main */
示例#2
0
/*------------------------------------------------------------------
 * Function:    Iterative_dfs
 * Purpose:     Use a stack variable to implement an iterative version
 *              of depth-first search
 * In arg:     
 *    tour:     partial tour of cities visited so far (just city 0)
 * Globals in:
 *    n:        total number of cities in the problem
 * Notes:
 * 1  The input tour is modified during execution of search,
 *    but returned to its original state before returning.
 * 2. The Update_best_tour function will modify the global var
 *    best_tour
 */
void Iterative_dfs(tour_t tour) {
   city_t nbr;
   my_stack_t stack;
   tour_t curr_tour;

   stack = Init_stack();
   Push(stack, tour);
   while (!Empty(stack)) {
      curr_tour = Pop(stack);
#     ifdef DEBUG
      printf("Popped tour = %p and %p\n", curr_tour, curr_tour->cities);
      Print_tour(curr_tour, "Popped");
      printf("\n");
#     endif
      if (City_count(curr_tour) == n) {
         if (Best_tour(curr_tour))
            Update_best_tour(curr_tour);
      } else {
         for (nbr = n-1; nbr >= 1; nbr--) 
            if (Feasible(curr_tour, nbr)) {
               Add_city(curr_tour, nbr);
               Push(stack, curr_tour);
               Remove_last_city(curr_tour);
            }
      }
      Free_tour(curr_tour);
   }
   Free_stack(stack);
}  /* Iterative_dfs */
示例#3
0
/*------------------------------------------------------------------
 * Function:    Push_avail
 * Purpose:     Store a tour in the available list
 * In arg:      tour
 * In/out Global:  
 */
void Push_avail(tour_t tour) {
   if (avail->list_sz == n*n) {
      fprintf(stderr, "Available stack overflow!\n");
      free(tour->cities);
      free(tour);
   } else {
#     ifdef DEBUG
      printf("In Push_avail, loc = %d, pushing %p and %p\n",
            avail->list_sz, tour, tour->cities);
      Print_tour(tour, "About to be pushed onto avail");
      printf("\n");
#     endif
      avail->list[avail->list_sz] = tour;
      (avail->list_sz)++;
   }
}  /* Push_avail */
/*------------------------------------------------------------------*/
int main(int argc, char* argv[]) {
    FILE*  mat_file;
	long thread;
	pthread_t* thread_handles;
	
	thread_count = strtol(argv[1], NULL, 10);

    mat_file = fopen(argv[2], "r");
    if (mat_file == NULL) {
       fprintf(stderr, "Can't open %s\n", argv[2]);
       Usage(argv[0]);
    }
    Read_mat(mat_file);
    fclose(mat_file);

    Initialize_tour(&best_tour);
    best_tour.cost = INFINITY;

	thread_handles = malloc(thread_count*sizeof(pthread_t));
	for(thread = 0; thread < thread_count; thread++){
		pthread_create(&thread_handles[thread], NULL, Search, (void*) thread);
	}
	
	for(thread = 0; thread < thread_count; thread++){
		pthread_join(thread_handles[thread], NULL);
	}

    Print_tour(&best_tour, "Best tour");
    printf("Cost = %d\n", best_tour.cost);

	pthread_mutex_unlock(&mutex);
    free(best_tour.cities);
    free(mat);
	free(thread_handles);
    return 0;
}  /* main */