I/O for the EO format, remove commented test
This commit is contained in:
parent
749cedb8d7
commit
77725b9597
1 changed files with 60 additions and 47 deletions
107
tsplib.py
107
tsplib.py
|
|
@ -1,13 +1,8 @@
|
|||
import sys
|
||||
import scipy
|
||||
|
||||
def write_segments( segments, size, depth, rounding, fd = sys.stdout,
|
||||
node_coord_section=False,
|
||||
edge_data_section=False,
|
||||
edge_weight_section=True,
|
||||
display_data_section=True ):
|
||||
|
||||
# construct a {coords:id} dictionary
|
||||
def segments_to_nodes( segments ):
|
||||
"""construct a {coords:id} dictionary and returns (nodes,nb)"""
|
||||
nodes = {}
|
||||
nb = 0
|
||||
for segment in segments:
|
||||
|
|
@ -15,6 +10,27 @@ def write_segments( segments, size, depth, rounding, fd = sys.stdout,
|
|||
if not nodes.has_key(coords):
|
||||
nodes[coords] = nb
|
||||
nb += 1
|
||||
return nodes,nb
|
||||
|
||||
|
||||
def write_nodes_simple( nodes, fd = sys.stdout ):
|
||||
"""write only the coordinates"""
|
||||
fd.write("%i\n" % len(nodes))
|
||||
nodes_index = nodes.keys()
|
||||
nodes_index.sort()
|
||||
for i in nodes_index:
|
||||
x,y = nodes[i]
|
||||
fd.write( "%f %f\n" % (x,y))
|
||||
|
||||
|
||||
|
||||
def write_segments( segments, size, depth, rounding, fd = sys.stdout,
|
||||
node_coord_section=False,
|
||||
edge_data_section=False,
|
||||
edge_weight_section=True,
|
||||
display_data_section=True ):
|
||||
|
||||
nodes,nb = segments_to_nodes(segments)
|
||||
|
||||
fd.write( "NAME : penrose3_%i\n" % depth)
|
||||
fd.write("COMMENT : Rhombus Penrose tiling (type P3) as generated by a L-system, at depth %i\n" % depth)
|
||||
|
|
@ -62,8 +78,13 @@ def write_segments( segments, size, depth, rounding, fd = sys.stdout,
|
|||
if display_data_section:
|
||||
fd.write("DISPLAY_DATA_SECTION\n")
|
||||
fmt = "%"+str(len(str(nb)))+"i %"+str(rounding)+"f %"+str(rounding)+"f\n"
|
||||
sorted_nodes = [0] * len(nodes)
|
||||
for x,y in nodes:
|
||||
fd.write(fmt % (nodes[(x,y)],x,y))
|
||||
sorted_nodes[nodes[(x,y)]] = (x,y)
|
||||
for i in xrange(len(sorted_nodes)):
|
||||
x,y = sorted_nodes[i]
|
||||
fd.write(fmt % (i,x,y))
|
||||
|
||||
|
||||
fd.write("EOF\n")
|
||||
|
||||
|
|
@ -76,6 +97,14 @@ def read_tour_index( fd ):
|
|||
|
||||
return map(int, tour)
|
||||
|
||||
def read_tour_EO( fd ):
|
||||
line = fd.readline()
|
||||
sol = line.split()
|
||||
tour = sol[2:] # 0=fitness, 1=size, 2+=tour
|
||||
|
||||
return map(int, tour)
|
||||
|
||||
|
||||
|
||||
def read_nodes( fd ):
|
||||
"""Parse a .tsp file and returns a dictionary of nodes, of the form {id:(x,y)}"""
|
||||
|
|
@ -180,71 +209,55 @@ def plot_segments_tour( segments_1, segments_2 ):
|
|||
if __name__=="__main__":
|
||||
import sys
|
||||
|
||||
# segments = [
|
||||
# ( (0,0),(0,2) ),
|
||||
# ( (0,2),(2,2) ),
|
||||
# ( (2,2),(2,0) ),
|
||||
# ( (2,0),(0,0) )
|
||||
# ]
|
||||
#
|
||||
# filename = "test.tsp"
|
||||
# with open(filename,"w") as fd:
|
||||
# write_segments( segments, fd=fd, size=1, depth=0, rounding=10 )
|
||||
# write_segments( segments, fd=sys.stdout, size=1, depth=0, rounding=10 )
|
||||
#
|
||||
# with open(filename,"r") as fd:
|
||||
# nodes = read_nodes( fd )
|
||||
#
|
||||
# print "Nodes: id (x, y)"
|
||||
# for idx,node in nodes.items():
|
||||
# print idx,node
|
||||
#
|
||||
# with open(filename,"r") as fd:
|
||||
# vertices = read_vertices( fd )
|
||||
#
|
||||
# print "Segments: (x1,y1) (x2,y2)"
|
||||
# segments = []
|
||||
# for i1,i2 in vertices:
|
||||
# print nodes[i1],nodes[i2]
|
||||
# segments.append( (nodes[i1],nodes[i2]) )
|
||||
#
|
||||
# plot_segments( segments )
|
||||
|
||||
finstance = sys.argv[1]
|
||||
ftour = sys.argv[2]
|
||||
|
||||
print "Read nodes"
|
||||
print "Read nodes",
|
||||
sys.stdout.flush()
|
||||
with open(finstance,"r") as fd:
|
||||
nodes = read_nodes( fd )
|
||||
print len(nodes)
|
||||
|
||||
print "Read vertices"
|
||||
with open(finstance+".nodes","w") as fd:
|
||||
write_nodes_simple(nodes,fd)
|
||||
|
||||
print "Read vertices",
|
||||
sys.stdout.flush()
|
||||
with open(finstance,"r") as fd:
|
||||
vertices = read_vertices( fd )
|
||||
print len(vertices)
|
||||
|
||||
print "Build segments"
|
||||
print "Build segments",
|
||||
sys.stdout.flush()
|
||||
segments = []
|
||||
for i1,i2 in vertices:
|
||||
#print nodes[i1],nodes[i2]
|
||||
segments.append( (nodes[i1],nodes[i2]) )
|
||||
print len(segments)
|
||||
|
||||
# print "Plot segments"
|
||||
# plot_segments( segments )
|
||||
|
||||
|
||||
print "Read tour"
|
||||
print "Read tour",
|
||||
sys.stdout.flush()
|
||||
with open(ftour,"r") as fd:
|
||||
tour = read_tour_index( fd )
|
||||
#tour = read_tour_index( fd )
|
||||
tour = read_tour_EO( fd )
|
||||
print len(tour)
|
||||
|
||||
print tour
|
||||
#print tour
|
||||
|
||||
print "Build tour segments"
|
||||
print "Build tour segments",
|
||||
sys.stdout.flush()
|
||||
tour_segments = []
|
||||
for i in xrange(0,len(tour)-1):
|
||||
tour_segments.append( ( nodes[tour[i]],nodes[tour[i+1]] ) )
|
||||
print tour_segments[-1]
|
||||
#print tour_segments[-1]
|
||||
|
||||
tour_segments.append( ( nodes[tour[i+1]], nodes[tour[0]] ) )
|
||||
print tour_segments[-1]
|
||||
#print tour_segments[-1]
|
||||
print len(tour_segments)
|
||||
|
||||
print "Plot tour segments"
|
||||
plot_segments_tour( segments, tour_segments )
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue