コード例 #1
0
ファイル: siman_tsp.c プロジェクト: lemahdi/mglib
void prepare_distance_matrix()
{
  unsigned int i, j;
  double dist;

  for (i = 0; i < N_CITIES; ++i) {
    for (j = 0; j < N_CITIES; ++j) {
      if (i == j) {
        dist = 0;
      } else {
        dist = city_distance(cities[i], cities[j]);
      }
      distance_matrix[i][j] = dist;
    }
  }
}
コード例 #2
0
ファイル: tsp_path_distance.c プロジェクト: ftrimble/ACO_TSP
// after calling this function, distances will be filled
// returns -1 on failure and 0 on success
int parse_input_file(char* input_file_path, int num_cities) {
  int i, j;
  char * this_line;
  char buffer[100];
    
  struct city* cities = (struct city*)calloc(num_cities, sizeof(struct city));
    
  FILE *input_file = fopen(input_file_path, "rb");
  if (input_file == NULL) {
    perror ("The following error occurred");
    return -1;
  }
    
  // skip header information
  this_line = fgets(buffer, 100, input_file); // NAME
  this_line = fgets(buffer, 100, input_file); // TYPE
  this_line = fgets(buffer, 100, input_file); // COMMENT
  this_line = fgets(buffer, 100, input_file); // DIMENSION
  this_line = fgets(buffer, 100, input_file); // EDGE_WEIGHT_TYPE
  this_line = fgets(buffer, 100, input_file); // NODE_COORD_SECTION
    
  // read cities
  for (i=0; i < num_cities; i++) {
    this_line = fgets(buffer, 100, input_file);
        
    sscanf(this_line, " %d %lf %lf", 
	   &(cities[i].id_number), 
	   &(cities[i].x_coord), 
	   &(cities[i].y_coord)
	   );
  }
    
  // calculate and store distances and their inverses
  for (i=0; i < num_cities; i++)
    for (j=0; j < num_cities; j++)
      distances[i][j] = city_distance(cities[i], cities[j]);
    
  free(cities);
  fclose(input_file);
    
  return 0;
}