-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnn.py
110 lines (82 loc) · 2.66 KB
/
nn.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# Generates a key array using nearest neighbour algorithm
# Only function to be called external is nearestNeighbourKeygen(),
# this functio is to be called with a proccessed benchmark logfile
# as the sole argument.
import config
import time
import numpy as np
# borrowed code
# def convXYtoNode(logFile):
# benchmark = []
# with open(logFile,"r") as bmread:
# for line in bmread:
# benchmarkLine = []
# benchmarkItems = line.split()
# for i in benchmarkItems:
# benchmarkLine.append(int(i))
# benchmark.append(benchmarkLine)
# newBenchmark = []
# for row in benchmark:
# if (row[0] != 0) & (row[2] != 0):
# newRow = [(row[0] - 1) * 8 + row[1], (row[2] - 1) * 8 + row[3]]
# newRow.extend(row[4:])
# newBenchmark.append(newRow)
# return newBenchmark
# def keyMapping(key):
# for row in benchmark:
# row[1] = key[row[1]]
# row[2] = key[row[2]]
# startTime = time.time()
# nodeBenchmarkList = convXYtoNode(config.logFile)
# borrowed code
def rank():
n = 0
for row in trafficto:
for i in range(0,n):
if ~(row[4] == 0):
trafpositions.append((n,i,row[i]))
n += 1
def getsortkey(item):
return item[2]
def NN(A, start):
"""Nearest neighbor algorithm.
A is an NxN array indicating distance between N locations
start is the index of the starting location
Returns the path and cost of the found solution
"""
path = [start]
cost = 0
N = A.shape[0]
mask = np.ones(N, dtype=bool) # boolean values indicating which
# locations have not been visited
mask[start] = False
for i in range(N-1):
last = path[-1]
next_ind = np.argmax(A[last][mask]) # find minimum of remaining locations
next_loc = np.arange(N)[mask][next_ind] # convert to original location
path.append(next_loc)
mask[next_loc] = False
return path
def nearestNeighbourKeygen(benchmark):
trafficto = np.zeros((16,16))
trafpositions = []
for row in benchmark:
print row[4]
trafficto[row[0]][row[1]] += row[4]
trafficto[row[1]][row[0]] += row[4]
# for row in trafficto:
# print row
#used to find highest trafic node pair to serve as a starting position
rank()
ntraffic = sorted(trafpositions, key=getsortkey)
key = NN(trafficto, ntraffic[0][1])
print "This is the neARST NEIGHBOUR key"
print key
return key
# print ntraffic
# for row in ntraffic:
# s = row[1]
# d = row[2]
# #check key
# if s in key:
# if d in key: