[new] add tsp2json

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

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)