/
biggroup.py
162 lines (137 loc) · 5.59 KB
/
biggroup.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import math
import random
import sys
import ConfigParser
from work import Work
# BigGroup class initilize the Group of Ants which move in the graph
# The big group is controlled by number of parametrs and it take input
# (graph, num_ants, num_iterations)
# Graph can be gentrated by graph class
class BigGroup:
def __init__(self, graph, num_ants, num_iterations):
self.graph = graph
self.num_ants = num_ants
self.num_iterations = num_iterations
self.initConfigParser()
self.Alpha=self.config.getfloat('BigGroup', 'Alpha')
self.reset()
# initlize the ConfigParser
def initConfigParser(self):
self.config = ConfigParser.ConfigParser()
self.config.readfp(open('config.ini'))
# Resets the ant poition
def reset(self):
#self.optimalCostArray = sys.maxint
self.optimalCostArray = sys.maxsize
self.optimalPath = None
self.bestPathArray = None
self.lastpathiter = 0
# Starts the ants similations
def start(self):
self.ants = self.c_workers()
self.iter_counter = 0
while self.iter_counter < self.num_iterations:
self.iteration()
# Note that this will help refine the results future iterations.
self.global_updating_rule()
# Iterate over the ants and run them.
def iteration(self):
self.avg_path_cost = 0
self.ant_counter = 0
self.iter_counter += 1
for ant in self.ants:
ant.run()
def num_ants(self):
return len(self.ants)
def num_iterations(self):
return self.num_iterations
def iteration_counter(self):
return self.iter_counter
# Update the optimal Path and optimalCostArray after each ents
# moved over the nodes in Graph
def update(self, ant):
print ("Update called by %s" % (ant.ID,))
self.ant_counter += 1
self.avg_path_cost += ant.path_cost
if ant.path_cost < self.optimalCostArray:
self.optimalCostArray = ant.path_cost
self.bestPathArray = ant.path_mat
self.optimalPath = ant.path_vec
self.lastpathiter = self.iter_counter
if self.ant_counter == len(self.ants):
self.avg_path_cost /= len(self.ants)
print ("Best: optimalPath %s \n optimalCostArray %s \n iter_counter %s \n avg_path_cost %s" % (
self.optimalPath, self.optimalCostArray, self.iter_counter, self.avg_path_cost,))
def done(self):
return self.iter_counter == self.num_iterations
# Initlize the Ants at random location n Graph and Returns the Ants array array
def c_workers(self):
self.reset()
ants = []
for i in range(0, self.num_ants):
ant = Work(i, random.randint(0, self.graph.num_nodes - 1), self)
ants.append(ant)
return ants
# Test Funtion works similar to but initlize the fix position
def c_workers_test(self,placeSeed):
self.reset()
ants = []
for i in range(0, self.num_ants):
self.ant = Work(i, placeSeed , self)
ants.append(self.ant)
return ants
# Update the evaporation and Deposition (pheromones)after movement at each iteration
def global_updating_rule(self):
#can someone explain this
evaporation = 0
deposition = 0
for r in range(0, self.graph.num_nodes):
for s in range(0, self.graph.num_nodes):
if r != s:
delt_tau = self.bestPathArray[r][s] / self.optimalCostArray
evaporation = (1 - self.Alpha) * self.graph.tau(r, s)
deposition = self.Alpha * delt_tau
self.graph.update_tau(r, s, evaporation + deposition)
# Graph Class is ised to initlize the graph on which the ants will be move
# This class take the construtor values (num_nodes, cost_matix, tau_mat=None)
# This class genrates the delta , tau and etha matrix
# (Distance matrix , Phermones level , inverse of Distance matrix )
class GraphBit:
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
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]
# Phermones deposited by the ants
def tau(self, r, s):
return self.tau_mat[r][s]
def etha(self, r, s):
return 1.0 / self.delta(r, s)
def update_tau(self, r, s, val):
self.tau_mat[r][s] = val
def reset_tau(self):
avg = self.average_delta()
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
def average_delta(self):
return self.average(self.delta_mat)
def average_tau(self):
return self.average(self.tau_mat)
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