forked from rtinos/gpx2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_man.cpp
76 lines (69 loc) · 2.17 KB
/
file_man.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/******************************************************************************\
* Files Manipulation *
\******************************************************************************/
#include "defs.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<string>
#include<cstring>
#include<fstream>
#define CHAR_LEN 100
/****************************************************************/
/* Read the problem instance (adapted from DTSP generator - Yang et al.) */
/****************************************************************/
void read_problem(char* filename){
int i, j, id;
char line[CHAR_LEN], * keywords,Delimiters[] = " :=\n\t\r\f\v", * tempChar;
ifstream fin(filename);
while((fin.getline(line, CHAR_LEN-1))){
if(!(keywords = strtok(line, Delimiters)))
continue;
if(!strcmp(keywords, "DIMENSION")){
if(!sscanf(strtok(NULL, Delimiters), "%d", &n_cities)){
cerr << "DIMENSION error" << endl;
exit(EXIT_FAILURE);
}
coord_x=aloc_vectord (n_cities);
coord_y=aloc_vectord (n_cities);
}
else if(!strcmp(keywords, "EDGE_WEIGHT_TYPE")){
if(!(tempChar=strtok(NULL, Delimiters))){
cerr << "EDGE_WEIGHT_TYPE error" << endl;
exit(EXIT_FAILURE);
}
if(strcmp(tempChar, "EUC_2D")){
cerr << "not EUC_2D" << endl;
exit(EXIT_FAILURE);
}
}
else if(!strcmp(keywords, "NODE_COORD_SECTION")){
if(n_cities!=0){
for(i=0; i<n_cities; i++){
//coordinates
fin>>id;
fin>>coord_x[i]>>coord_y[i];
}
}
}
}
fin.close();
// building the weight matrix
if (n_cities <= max_dm_size) {
W = aloc_matrixi(n_cities,n_cities);
for(i=0; i<n_cities; i++){
for(j=i; j<n_cities; j++){
if (i==j)
W[i][j]=0;
else {
W[i][j]=round( sqrt( pow(coord_x[i]-coord_x[j],2) +pow(coord_y[i]-coord_y[j],2) ) ); // compute the distance between the cities i and j
W[j][i]=W[i][j];
}
}
}
} else {
W = NULL;
}
srand (time(NULL));
cout << "GPX2: Components, (Infeasible, Feasible, Fusions), Parent A, Parent B, Best offspring, Complementary offspring, Time\n";
}