diff --git a/tsplib.py b/tsplib.py new file mode 100644 index 0000000..49e0b8f --- /dev/null +++ b/tsplib.py @@ -0,0 +1,78 @@ +from cvrp import ProblemInstance +from itertools import cycle +import sys + +def readProblem(filename): + f = open(filename) + cvrp = ProblemInstance() + #read data segment + while True: + line = f.readline() + (var,_, val) = line.partition(":") + var = var.strip() + val = val.strip() + if var == "NAME": + cvrp.name = val + elif var == "TYPE": + cvrp.type = val + elif var == "COMMENT": + cvrp.comment = val + elif var == "DIMENSION": + cvrp.dimension = int(val) + elif var == "CAPACITY": + cvrp.capacity = int(val) + elif var == "EDGE_WEIGHT_TYPE": + cvrp.ewt = val + elif var == "EDGE_WEIGHT_FORMAT": + cvrp.ewf = val + elif var == "EDGE_DATA_FORMAT": + cvrp.edf = val + elif var == "NODE_COORD_TYPE": + cvrp.nct = val + elif var == "DISPLAY_DATA_TYPE": + cvrp.ddt = val + + elif var == "NODE_COORD_SECTION": + cvrp.node_coord = [(lambda x: (float(x[1]), float(x[2])))(f.readline().split()) for _ in xrange(cvrp.dimension)] + elif var == "DEMAND_SECTION": + cvrp.node_demand = [( lambda x: float(x[1]) )(f.readline().split()) for _ in xrange(cvrp.dimension)] + elif var == "EDGE_WEIGHT_SECTION": + cvrp.node_distances=[] + for teller in range(cvrp.dimension) : + a = f.readline().split('\t') # lees een lijn en split per tab + c= [int(e) for e in a] # make integer van string + cvrp.node_distances.append(c) # voeg deze lijst toe aan de matrix + elif var == "EOF": + break + elif var == "": + break + return cvrp.prepare() + +p = open('postenlijst.txt', 'r') +lijst = p.read().split('\n') + +def writeSolution(result,filename = None): + (cost,paths) = result + if filename == None: + f = sys.stdout + else: + f = open(filename) + i = 1 + for path in paths: + string = "Route #" + str(i) + ":" + for node in path: + #string = string + " " + str(node) + string = string + "(" + str(node)+ ')'+ str(lijst[node])+' ' + f.write( string + "\n") + i += 1 + f.write("cost " + str(cost) + " meter.\n") + print " " + for path in paths: + string2= '' + nodeteller = 0 + while nodeteller < len(path)-1: + if nodeteller > 0 : + string2= string2 + ' ,' + string2= string2 +' '+ str(lijst[path[nodeteller]])+' '+ str(lijst[path[nodeteller+1]])+' 1' + nodeteller = nodeteller +1 + f.write( string2 + "\n")