-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBruteForce.py
252 lines (207 loc) · 8.86 KB
/
BruteForce.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
import networkx as nx
import matplotlib.pyplot as plt
import copy
import time
import creatingGraph as cg
class Node:
def __init__(self,name,feature1,feature2,feature3):
self.name = str(name)
self.feature1 = feature1
self.feature2 = feature2
self.feature3 = feature3
self._covered = False
def __str__(self):
return self.name
class coverageClass:
def __init__(self,nodeList,featureCountList):
self.nodeList = nodeList
self.featureCountList = featureCountList
#print(self.nodeList)
#print(len(self.featureCountList))
def __str__(self):
string = ""
for node in self.nodeList:
string += "Node"+str(node.name)+", "
return string + "feature1:"+str(self.featureCountList["feature1"])\
+", feature2:" + str(self.featureCountList["feature1"])+", feature3:"+str(self.featureCountList["feature2"])
class GraphClass:
def __init__(self,name):
self.name = name
self.G = nx.Graph()
def createNode(self,name,feature1,feature2,feature3):
node = Node(name,feature1,feature2,feature3)
self.G.add_node(node)
return node
def addNode(self,name,feature1,feature2,feature3):
self.G.add_node(Node(name,feature1,feature2,feature3))
def createEdge(self,node1,node2):
self.G.add_edge(node1,node2)
def drawGraph(self):
nx.draw(self.G,with_labels=True)
plt.show()
## Creating a color map for each node for visualisation
def drawGraphColors(self):
node_color =[]
for node in self.G.nodes:
if(node.feature1 == True and node.feature2 == True and node.feature3 == True):
node_color.append(1)
elif(node.feature1 == True and node.feature2 == True):
node_color.append(3)
elif(node.feature1 and node.feature3 == True):
node_color.append(5)
elif (node.feature2 == True and node.feature3 == True) :
node_color.append(7)
elif(node.feature1 == True):
node_color.append(9)
elif(node.feature2== True):
node_color.append(11)
elif (node.feature3 == True):
node_color.append(13)
else:
node_color.append(15)
nx.draw(self.G,with_labels=True,node_color=node_color,pos=nx.circular_layout(self.G))
plt.show()
## For given set of nodes we generate a coverage class which specifies the feature count of the features
def coverageSet(self,nodeList,features):
coveredNodes = []
featureCount = {}
for feature in features:
featureCount[feature] = 0
for node in nodeList:
## Checking the nodes that are considered
if node not in coveredNodes:
for feature in features:
if hasattr(node, feature) and (getattr(node, feature)==True):
featureCount[feature] += 1
coveredNodes.append(node)
## Checking the neighbors of the nodes
for neighbor in self.G.neighbors(node):
## Do not consider the node which has already considered
if neighbor not in coveredNodes:
for feature in features:
if hasattr(neighbor, feature) and (getattr(neighbor, feature) == True):
featureCount[feature] += 1
## Add the neighbor to the negihbor list
coveredNodes.append(neighbor)
coverageObject = coverageClass(nodeList,featureCount)
return coverageObject
## Recursively chosing the nodes depdending on the size to form a nodeSet
def choseRecursiveCoverageSet(self,features,committeSize):
nodeSet = list(self.G.nodes)
presentNodeSet = []
nodeCombinations = []
self.choseRecursiveCoverageSetUtils(features,nodeSet,0,committeSize,presentNodeSet,nodeCombinations)
return nodeCombinations
def choseRecursiveCoverageSetUtils(self,features,nodeSet,start,committeeSize,presentNodeSet,nodeCombinations):
## Number of nodes in the presentNode set is total number of nodes to be considered
if(committeeSize == len(presentNodeSet)):
##nodeCombinations.append(presentNodeSet)
currCoverageSet = self.coverageSet(presentNodeSet,features)
nodeCombinations.append(copy.deepcopy(currCoverageSet))
return
for i in range(start,len(nodeSet)):
presentNodeSet.append(nodeSet[i])
self.choseRecursiveCoverageSetUtils(features,nodeSet,i+1,committeeSize,presentNodeSet,nodeCombinations)
presentNodeSet.remove(presentNodeSet[len(presentNodeSet)-1])
## Chosing the best nodeSet which contains all the feaures and covers maximum features
def choseBestNodes(self,fullCoverageSet,featureList):
maxFeatureCount = 0
maxCoverageNode = fullCoverageSet[0]
containsAllFeatures = True
for coverage in fullCoverageSet:
featureCountList = coverage.featureCountList
featureCount = 0
containsAllFeatures = True
for value in featureCountList.values():
if value == 0:
containsAllFeatures = False
featureCount+=value
if (containsAllFeatures and (featureCount > maxFeatureCount)):
maxFeatureCount = featureCount
maxCoverageNode = coverage
return maxCoverageNode.nodeList
def setNodesCovered(self, nodeList):
for node in nodeList:
node = cg.name2Node[node.name]
for neighbor in self.G.neighbors(node):
neighbor._covered = True
node._covered = True
def measureCoveredNodes(self):
nodeCount = 0
coveredCount = 0
for node in self.G.nodes:
nodeCount = nodeCount + 1
if (node._covered == True):
coveredCount = coveredCount + 1
return nodeCount, coveredCount
def insertSelectedEdges(self, nodeSet):
nodeList = list(nodeSet)
for i in range(0, len(nodeList)):
for j in range(i + 1, len(nodeList)):
node1Name = nodeList[i].name
node2Name = nodeList[j].name
self.createEdge(cg.name2Node[node1Name], cg.name2Node[node2Name])
def createBasicGraph(Graph):
Node1 = Graph.createNode(1, True, True, True)
Node2 = Graph.createNode(2, False, False, False)
Node3 = Graph.createNode(3, True, False, False)
Node4 = Graph.createNode(4, True, True, True)
Node5 = Graph.createNode(5, False, True, True)
Node6 = Graph.createNode(6, False, True, False)
Node7 = Graph.createNode(7, False, False, True)
Node8 = Graph.createNode(8, False, False, False)
Node9 = Graph.createNode(9, True, True, True)
Node10 = Graph.createNode(10, False, True, True)
cg.name2Node['1'] = Node1
cg.name2Node['2'] = Node2
cg.name2Node['3'] = Node3
cg.name2Node['4'] = Node4
cg.name2Node['5'] = Node5
cg.name2Node['6'] = Node6
cg.name2Node['7'] = Node7
cg.name2Node['8'] = Node8
cg.name2Node['9'] = Node9
cg.name2Node['10'] = Node10
## Creating edges between the nodes
Graph.createEdge(Node1, Node2)
Graph.createEdge(Node1, Node3)
Graph.createEdge(Node1, Node4)
Graph.createEdge(Node1, Node5)
Graph.createEdge(Node2, Node3)
Graph.createEdge(Node3, Node4)
Graph.createEdge(Node4, Node5)
Graph.createEdge(Node1, Node6)
Graph.createEdge(Node6, Node7)
Graph.createEdge(Node6, Node8)
Graph.createEdge(Node6, Node9)
Graph.createEdge(Node6, Node10)
Graph.createEdge(Node7, Node8)
Graph.createEdge(Node8, Node9)
Graph.createEdge(Node9, Node10)
Graph.createEdge(Node2, Node7)
if __name__ == "__main__":
start = time.time()
Graph = GraphClass("bruteForce")
##createBasicGraph(Graph)
cg.creatingGraph(Graph,0,100)
##Graph.drawGraphColors()
for i in range(4):
nodeSelected = Graph.choseRecursiveCoverageSet(["feature1", "feature2", "feature3"],3)
bestNodes = Graph.choseBestNodes(nodeSelected, ["feature1", "feature2", "feature3"])
for node in bestNodes:
print(node.name)
##Graph.drawGraphColors()
## Setting the nodes which are covered
Graph.setNodesCovered(bestNodes)
Graph.insertSelectedEdges(bestNodes)
##Graph.drawGraphColors()
try:
diameter = nx.diameter(Graph.G)
except nx.NetworkXError as m :
diameter = "infinity"
print ("diameter of the graph : " + str(diameter))
nodeCount, coveredCount = Graph.measureCoveredNodes()
print("Node Count = %d , Coverage Count = %d" % (nodeCount, coveredCount))
end = time.time()
## Calculating the nodes that are covered
print("Execution Time : " + str(end - start))