[go: up one dir, main page]

Menu

[046f16]: / graph / tsplib.py  Maximize  Restore  History

Download this file

176 lines (133 with data), 5.5 kB

  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
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# -*- coding: utf-8 -*-
#
# Author: Marcelo Costa Toyama - mctoyama@gmail.com
#
# License GPL v 3.0
#
# Description:
# TSPLIB problems
#
# TSPLIB
# http://comopt.ifi.uni-heidelberg.de/software/TSPLIB95/
import re
import math
from graph.distance_matrix import *
# -------------------------------------------------------------------------
# TSP LIB distance matrix -
#
# -------------------------------------------------------------------------
class TSPLIB_matrix(Distance_matrix):
"""Traveling Salesman problem matrix"""
def __init__(self, p_filename):
"""Construct matrix from file"""
self._matrix = list()
self._NAME = ""
self._COMMENT = ""
self._TYPE = ""
self._EDGE_WEIGHT_TYPE = ""
self._DIMENSION = ""
self._NODE_COORD_SECTION = False
self._coord = {}
# reading TSPLIB file
v_fd = open(p_filename, "r")
for v_line in v_fd:
v_pr_line = v_line.split(":")
# removendo espaços duplos
v_match = re.search(" ", v_pr_line[0])
while v_match:
v_match = re.search(" ", v_pr_line[0])
v_pr_line[0] = v_pr_line[0].replace(" ", " ")
# removendo \s\n - > ""
v_match = re.search(" \n", v_pr_line[0])
while v_match:
v_match = re.search(" \n", v_pr_line[0])
v_pr_line[0] = v_pr_line[0].replace(" \n", "")
# removendo \n
v_match = re.search("\n", v_pr_line[0])
while v_match:
v_match = re.search("\n", v_pr_line[0])
v_pr_line[0] = v_pr_line[0].replace("\n", "")
if not self._NODE_COORD_SECTION:
if re.search("^NAME", v_pr_line[0]):
self._NAME = v_pr_line[1]
elif re.search("^COMMENT", v_pr_line[0]):
self._COMMENT = v_pr_line[1]
elif re.search("^TYPE", v_pr_line[0]):
if not re.search("TSP", v_pr_line[1]):
raise Exception("ERROR: NOT A TSP PROBLEM")
else:
self._TYPE = v_pr_line[1]
elif re.search("EDGE_WEIGHT_TYPE", v_pr_line[0]) :
if not re.search("EUC_2D", v_pr_line[1]):
raise Exception("ERROR: NOT A EUC_2D TYPE")
else:
self._EDGE_WEIGHT_TYPE = v_pr_line[1]
elif re.search("^DIMENSION", v_pr_line[0]):
self._DIMENSION = v_pr_line[1]
elif re.search("^NODE_COORD_SECTION", v_pr_line[0]):
self._NODE_COORD_SECTION = True
else:
if re.search("^EOF", v_line):
break
v_line_node = v_line
# cleaning double \s\s -> \s
v_match = re.search(" ", v_line_node)
while v_match:
v_match = re.search(" ", v_line_node)
v_line_node = v_line_node.replace(" ", " ")
# cleaning double \s\n -> \s
v_match = re.search(" \n", v_line_node)
while v_match:
v_match = re.search(" \n", v_line_node)
v_line_node = v_line_node.replace(" \n", "")
# cleaning double \n -> \s
v_match = re.search("\n", v_line_node)
while v_match:
v_match = re.search("\n", v_line_node)
v_line_node = v_line_node.replace("\n", "")
v_pr_line = v_line_node.split(" ")
# registering node
v_node = int(v_pr_line[-3]) - 1
v_x = float(v_pr_line[-2])
v_y = float(v_pr_line[-1])
self._coord[v_node] = (v_x,v_y)
v_fd.close()
# creating distance matrix
for v_row in range( len( self._coord ) ):
v_line_tsp = list()
for v_column in range( len( self._coord ) ):
v_line_tsp.append(0.0)
self._matrix.append(v_line_tsp)
for v_i, (v_xi, v_yi) in self._coord.items():
for v_j, (v_xj, v_yj) in self._coord.items():
dx = v_xi - v_xj
dy = v_yi - v_yj
dij = math.sqrt( dx*dx + dy*dy )
dij = float( dij )
self._matrix[v_i][v_j] = dij
if v_i != v_j and dij == 0:
self._matrix[v_i][v_j] = 0.001
# -------------------------------------------------------------------------
# TSPLIB tour
# -------------------------------------------------------------------------
class TSPLIB_tour:
"""Represents a TSPLIB tour"""
def __init__(self, p_filename):
"""It reads a TSPLIB opt.tour"""
self._path = list()
v_fd = open(p_filename, "r")
v_tour_section = False
for v_line in v_fd:
if re.search("^EOF", v_line):
break
if re.search("^TOUR_SECTION", v_line):
v_tour_section = True
continue
if v_tour_section:
v_value = int( v_line )
if v_value > 0:
v_value -= 1
self._path.append( v_value )
v_fd.close()
def path(self):
return( self._path )