-
Notifications
You must be signed in to change notification settings - Fork 49
/
antgraph.py
69 lines (53 loc) · 1.85 KB
/
antgraph.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
from threading import Lock
class AntGraph:
def __init__(self, num_nodes, delta_mat, tau_mat=None):
print (len(delta_mat))
if len(delta_mat) != num_nodes:
raise Exception("len(delta) != num_nodes")
self.num_nodes = num_nodes
self.delta_mat = delta_mat # matrix of node distance deltas
self.lock = Lock()
# tau mat contains the amount of phermone at node x,y
if tau_mat is None:
self.tau_mat = []
for i in range(0, num_nodes):
self.tau_mat.append([0]*num_nodes)
def delta(self, r, s):
return self.delta_mat[r][s]
def tau(self, r, s):
return self.tau_mat[r][s]
# 1 / delta = eta or etha
def etha(self, r, s):
return 1.0 / self.delta(r, s)
# inner locks most likely not necessary
def update_tau(self, r, s, val):
lock = Lock()
lock.acquire()
self.tau_mat[r][s] = val
lock.release()
def reset_tau(self):
lock = Lock()
lock.acquire()
avg = self.average_delta()
# initial tau
self.tau0 = 1.0 / (self.num_nodes * 0.5 * avg)
print ("Average = %s" % (avg,))
print ("Tau0 = %s" % (self.tau0))
for r in range(0, self.num_nodes):
for s in range(0, self.num_nodes):
self.tau_mat[r][s] = self.tau0
lock.release()
# average delta in delta matrix
def average_delta(self):
return self.average(self.delta_mat)
# average tau in tau matrix
def average_tau(self):
return self.average(self.tau_mat)
# average val of a matrix
def average(self, matrix):
sum = 0
for r in range(0, self.num_nodes):
for s in range(0, self.num_nodes):
sum += matrix[r][s]
avg = sum / (self.num_nodes * self.num_nodes)
return avg