[new] add tsp2json

This commit is contained in:
Wu Han 2021-01-08 19:19:07 +00:00
parent 3a4ab95699
commit a7dbaffe06
2 changed files with 31 additions and 10 deletions

View File

@ -97,15 +97,9 @@ def TSP(tsp_file, model, timeout=60):
data['edges'] = []
for i in range(len(best_solution)):
if i == len(best_solution)-1:
data['edges'].append({
'source': best_solution[i],
'target': best_solution[0]
})
else:
data['edges'].append({
'source': best_solution[i],
'target': best_solution[i+1]
'source': best_solution[i % len(best_solution)],
'target': best_solution[(i+1) % len(best_solution)]
})
with open('output/' + os.path.splitext(os.path.basename(tsp_file))[0] + '.json', 'w') as outfile:

View File

@ -0,0 +1,27 @@
import os
import json
from utils.load_data import load_data
def write_json(path):
for root, _, files in os.walk(path):
if(files):
for f in files:
# Get input file name
tsp_file = os.path.join(root, str(f))
print(tsp_file)
nodes = load_data(tsp_file)
# Write to JSON
data = {}
data['nodes'] = []
for i in range(len(nodes)):
data['nodes'].append({
'title': str(i),
'id': i,
'x': int(nodes[i][0]),
'y': int(nodes[i][1])
})
with open('./' + os.path.splitext(os.path.basename(tsp_file))[0] + '.json', 'w') as outfile:
json.dump(data, outfile)