Graph Convolutional Branch and Bound v1.0.0
A TSP solver that combines a graph convolutional network with a 1-Tree branch-and-bound.
Loading...
Searching...
No Matches
tsp_instance_reader.c
Go to the documentation of this file.
1
15#include "tsp_instance_reader.h"
16
17
18void read_tsp_lib_file(Graph *graph, char *filename) {
19 FILE *fp = fopen(filename, "r");
20 if (fp == NULL) {
21 perror("Error while opening the file.\n");
22 printf("\nFile: %s\n", filename);
23 exit(1);
24 }
25
26 char *line = NULL;
27 size_t len = 0;
28 bool check_euc_2d = false;
29 while (getline(&line, &len, fp) != -1 &&
30 strstr(line, "NODE_COORD_SECTION") == NULL) {
31 if (strstr(line, "EDGE_WEIGHT_TYPE : EUC_2D") == NULL) {
32 check_euc_2d = true;
33 }
34 }
35
36 if (!check_euc_2d) {
37 perror("The current TSP file is not an euclidean one.\n");
38 printf("\nFile: %s\n", filename);
39 exit(1);
40 }
41
42 unsigned short i = 0;
43 Node nodes[MAX_VERTEX_NUM];
44 graph->kind = WEIGHTED_GRAPH;
45 List *nodes_list = new_list();
46 bool end_of_file = false;
47 while (getline(&line, &len, fp) != -1 && !end_of_file) {
48 if (strstr(line, "EOF") == NULL) {
49 unsigned short id;
50 float x;
51 float y;
52
53 int result = sscanf(line, "%hu %f %f", &id, &x, &y);
54 if (result != 3) {
55 perror("Error while reading the file.\n");
56 printf("\nFile: %s\n", filename);
57 exit(1);
58 }
59 nodes[i].positionInGraph = i;
60 nodes[i].x = x;
61 nodes[i].y = y;
62 nodes[i].num_neighbours = 0;
63 add_elem_list_bottom(nodes_list, &nodes[i]);
64 i++;
65 } else {
66 end_of_file = true;
67 }
68 }
69 free(line);
70 if (fclose(fp) == EOF) {
71 perror("Error while closing the file.\n");
72 printf("\nFile: %s\n", filename);
73 exit(1);
74 }
75 create_euclidean_graph(graph, nodes_list);
76}
77
78
79void read_tsp_csv_file(Graph *graph, char *filename) {
80 FILE *fp = fopen(filename, "r");
81 if (fp == NULL) {
82 perror("Error while opening the file.\n");
83 printf("\nFile: %s\n", filename);
84 exit(1);
85 }
86 graph->cost = 0;
87 graph->num_edges = 0;
88 graph->num_nodes = 0;
89 graph->kind = WEIGHTED_GRAPH;
90 graph->orderedEdges = false;
91 unsigned short i = 0;
92 unsigned short z = 0;
93 char *line = NULL;
94 size_t len = 0;
95 bool first = true;
96 while (getline(&line, &len, fp) != -1) {
97 if (first) {
98 first = false;
99
100 char *token = strtok(line, ";");
101 unsigned short node_num = 0;
102 while (token != NULL && strcmp(token, "\n") != 0) {
103 double x = 0, y = 0;
104 int result = sscanf(token, "(%lf, %lf)", &x, &y);
105 if (result != 2) {
106 perror("Error while reading the file.\n");
107 printf("\nFile: %s\n", filename);
108 exit(1);
109 }
110 graph->nodes[node_num].positionInGraph = node_num;
111 graph->nodes[node_num].x = x;
112 graph->nodes[node_num].y = y;
113 graph->nodes[node_num].num_neighbours = 0;
114 node_num++;
115 token = strtok(NULL, ";");
116 }
117
118 continue;
119 }
120 char *token = strtok(line, ";");
121 unsigned short j = 0;
122 while (token != NULL && strcmp(token, "\n") != 0) {
123 if (j != i) {
124 double weight = 0, prob = 0;
125
126 int result = sscanf(token, "(%lf, %lf)", &weight, &prob);
127 if (result != 2) {
128 perror("Error while reading the file.\n");
129 printf("\nFile: %s\n", filename);
130 exit(1);
131 }
132
133 if (weight > 0) {
134 if (j > i) {
135 graph->nodes[i].neighbours[graph->nodes[i].num_neighbours] = j;
136 graph->nodes[i].num_neighbours++;
137 graph->num_edges++;
138 graph->edges[z].src = i;
139 graph->edges[z].dest = j;
140 graph->edges[z].prob = HYBRID ? prob : 0;
141 graph->edges[z].symbol = z + 1;
142 graph->edges[z].positionInGraph = z;
143 graph->edges[z].weight = weight;
144 graph->cost += graph->edges[z].weight;
145 graph->nodes[j].positionInGraph = j;
146 graph->edges_matrix[i][j] = graph->edges[z];
147 graph->edges_matrix[j][i] = graph->edges[z];
148 z++;
149 } else {
150 graph->nodes[i].neighbours[graph->nodes[i].num_neighbours] = j;
151 graph->nodes[i].num_neighbours++;
152 }
153 }
154 }
155 token = strtok(NULL, ";");
156 j++;
157 }
158 graph->num_nodes++;
159 i++;
160 }
161 free(line);
162 if (fclose(fp) == EOF) {
163 perror("Error while closing the file.\n");
164 printf("\nFile: %s\n", filename);
165 exit(1);
166 }
167}
void create_euclidean_graph(Graph *graph, List *nodes)
Create a new instance of an euclidean graphs only the Nodes are necessary.
Definition: graph.c:71
@ WEIGHTED_GRAPH
The Graph is weighted.
Definition: graph.h:24
void add_elem_list_bottom(List *list, void *element)
Adds an DllElem to the bottom of the List.
List * new_list(void)
Create a new instance of a List.
unsigned short dest
ID of the destination vertex.
Definition: graph.h:42
unsigned short src
ID of the source vertex.
Definition: graph.h:41
double prob
Probability of the Edge to be in an optimal tour.
Definition: graph.h:45
double weight
Weight of the Edge, 1 if the data_structures is not weighted.
Definition: graph.h:44
unsigned short symbol
Symbol of the Edge, i.e. its unique ID.
Definition: graph.h:43
unsigned short positionInGraph
Position of the Edge in the list of Edges of the Graph.
Definition: graph.h:46
Structure of a Graph.
Definition: graph.h:51
Edge edges_matrix[MAX_VERTEX_NUM][MAX_VERTEX_NUM]
Adjacency matrix of the Graph.
Definition: graph.h:59
unsigned short num_edges
Number of Edges in the Graph.
Definition: graph.h:55
bool orderedEdges
True if the Edges are ordered by weight, false otherwise.
Definition: graph.h:56
Edge edges[MAX_EDGES_NUM]
Array of Edges.
Definition: graph.h:58
GraphKind kind
Type of the Graph.
Definition: graph.h:52
Node nodes[MAX_VERTEX_NUM]
Array of Nodes.
Definition: graph.h:57
double cost
Sum of the weights of the Edges in the Graph.
Definition: graph.h:53
unsigned short num_nodes
Number of Nodes in the Graph.
Definition: graph.h:54
The double linked list.
Definition: linked_list.h:35
Structure of a Node.
Definition: graph.h:30
double x
x coordinate of the Node.
Definition: graph.h:31
double y
y coordinate of the Node.
Definition: graph.h:32
unsigned short num_neighbours
Number of neighbours of the Node.
Definition: graph.h:34
unsigned short positionInGraph
Position of the Node in the list of Nodes of the Graph, i.e. its unique ID.
Definition: graph.h:33
unsigned short neighbours[MAX_VERTEX_NUM - 1]
Array of IDs of the Node's neighbors.
Definition: graph.h:35
void read_tsp_lib_file(Graph *graph, char *filename)
Reads a .tsp file and stores the data in the Graph.
void read_tsp_csv_file(Graph *graph, char *filename)
Reads a .csv file and stores the data in the Graph.
The declaration of the function to read input files.