You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
1.7 KiB

import elkai
from algorithm.dataset import dmatrix, dmatrixLabel
INF=100000
#get subset matrix
def getSubset(dataset = [], sub=[]):
subset = []
for ridx in sub:
subset.append( [dataset[ridx][x] for x in sub] )
return subset
# #system input lables
# lables = ['C1', 'E2', 'E8', 'E4', 'C4', 'F2', 'D0', 'E9']
def getLabelIndex(lables, dmatrixLabel):
sub = []
for label in lables:
if label in dmatrixLabel:
sub.append(dmatrixLabel.index(label))
else:
raise Exception("Lable is not in data label matrix")
return sub
#get two points distance
def getPoint2PointDistance(lables = []):
#get distance matrix index
sub = getLabelIndex(lables, dmatrixLabel)
#get sub matrix
subm = getSubset(dmatrix, sub)
#return distance
return subm[0][1]
def getItinerarys(lables = []):
#get all matrix
# sub = [ x for x in range(len(dmatrix)) ]
lables.sort()
# print("getItinerarys: ", lables)
#sub matrix index
# sub = [1, 5, 7, 9, 10, 13, 15, 17]
sub = getLabelIndex(lables, dmatrixLabel)
# print("sub: ", sub)
# #need to sort to the order same as dmatrix
# sub.sort()
# print("sub sort: ", sub)
#get sub matrix
subm = getSubset(dmatrix, sub)
# print("sub matrix: ", subm)
#init data matrix
cities = elkai.DistanceMatrix(subm)
#get tsp solved
itinerarys = cities.solve_tsp()
# print("itinerarys: ", itinerarys)
tripNum = [ x for x in itinerarys ]
# print("tripNum: ", tripNum)
#get index
trip = [ lables[x] for x in itinerarys ]
#print result
# print("trip: ", trip) # Output: [0, 2, 1, 0]
return trip