-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.py
261 lines (224 loc) · 6.61 KB
/
tests.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
252
253
254
255
256
257
258
259
260
261
import os
import networkx as nx
import pylab as plt
import numpy as np
from classes import Graph, Edge, Vertex, MatchPoints, findPaths, findAllPaths, CrossOver
from random import randint
from crossover import read_graph_from_file
from bmp_to_graphs import produce_graphs
def format_graph_output_arr(graph):
vlist = []
vertices = graph.getVertexes()
for vertex in vertices:
vlist.append([vertex.get_y(), vertex.get_x()])
adjMatrix = graph.getAdjMatrix()
newMatrix = []
for row in adjMatrix:
newRow = []
for item in row:
if item == None:
newRow.append(0)
else:
newRow.append(1)
newMatrix.append(newRow)
return vlist, newMatrix
def format_graph_output_str(graph):
vstr = ""
vertices = graph.getVertexes()
for i, vertex in enumerate(vertices):
vstr += "(" + str(vertex.get_x()) + "," + str(vertex.get_y()) + ")"
if i != len(vertices)-1:
vstr += ","
print vstr
adjMatrix = graph.getAdjMatrix()
res = ""
for j, row in enumerate(adjMatrix):
res += "{"
for i, item in enumerate(row):
if item == None:
res += str(0)
else:
res += str(1)
if i != len(row)-1:
res += ","
res += "}"
if j != len(adjMatrix)-1:
res += ","
print res
def draw_graph_networks(chars, persons):
num_persons = len(persons)
directory = "lao_graphs"
if not os.path.exists(directory):
os.makedirs(directory)
graphs = produce_graphs(chars, persons, "coords")
for char in graphs:
for person in graphs[char]:
outfile = directory+"/"+str(char)+"_"+str(person)+".jpg"
if os.path.isfile(outfile):
continue
graph = graphs[char][person]
vlist, adjMatrix = format_graph_output_arr(graph)
A = np.array(adjMatrix)
G = nx.Graph(A)
fig = plt.figure()
ax = plt.gca()
ax.invert_yaxis()
pos = vlist
nx.draw(G,pos,node_size=20)
fig.savefig(outfile)
plt.close()
def test_read_crossovers():
chars = [9]
persons = range(1,8)
for char in chars:
for person in persons:
graph = read_graph_from_file("000" + str(person) + "_" + str(char) + ".pkl")
graph.print_graph()
def tests():
v1 = Vertex(2,3)
v2 = Vertex(0,0)
v1.print_out()
v2.print_out()
print "\n"
dist = v1.EuclidDist(v2)
e = Edge(v1, v2, dist, False)
graph = Graph()
graph.addVertex(v1)
graph.addVertex(v2)
graph.addEdge(e)
graph.print_adjmatrix()
graph.print_vertexlst()
v3 = Vertex(4,5)
graph.addVertex(v3)
e2 = Edge(v1, v3, v3.EuclidDist(v1), True)
graph.addEdge(e2)
print("Real Print starts here ")
graph.print_graph()
def makeSpecificGraph():
# Set up graph
graph = Graph()
v1 = Vertex(2,3)
graph.addVertex(v1)
v2 = Vertex(0,0)
graph.addVertex(v2)
dist = v1.EuclidDist(v2)
e = Edge(v1, v2, dist, False)
graph.addEdge(e)
v3 = Vertex(4,5)
graph.addVertex(v3)
e2 = Edge(v1, v3, v3.EuclidDist(v1), True)
graph.addEdge(e2)
v4 = Vertex(10,20)
graph.addVertex(v4)
e3 = Edge(v3, v4, v3.EuclidDist(v4), False)
graph.addEdge(e3)
return graph
def testGetNeighbors():
graph = makeSpecificGraph()
vertexes = graph.getVertexes()
for index in range(len(vertexes)):
print "For vertex " + str(index)
neighbors = graph.getNeighborVertexes(vertexes[index])
for vertex in neighbors:
print vertex.print_out()
# returns graph with numPoints vertexes; random number of vertexes if None specified
def makeTestGraph(numPoints=None):
graph = Graph()
# Pixels always between 33 x 48
if not numPoints:
numPoints = randint(5,10)
for i in range(numPoints):
v = Vertex(int(randint(0,33)), int(randint(0,48)))
graph.addVertex(v)
return graph
def testMatch(g1=None, g2=None):
if g1 == None:
g1 = makeTestGraph(45)
if g2 == None:
g2 = makeTestGraph(35)
print "Graph 1:"
g1.print_vertexlst()
print "Graph 2:"
g2.print_vertexlst()
matches = MatchPoints(g1, g2, threshold = 4)
print matches
for v1,v2 in matches:
print "(", v1.print_out(), v2.print_out(), ")",
return matches
def testRemoveVertex():
graph = makeSpecificGraph()
graph2 = copy.deepcopy(graph)
print "Before: "
graph.print_graph()
vertexToRemove = graph.getVertexes() [1]
graph.removeVertex(vertexToRemove)
print "After: "
graph.print_graph()
print "Copyed Graph "
graph2.print_graph()
def testCopy():
graph
def testFindPaths():
# Set up graph
graph = makeSpecificGraph()
vertexes = graph.getVertexes()
v1 = vertexes[0]
v2 = vertexes[1]
v3 = vertexes[2]
v4 = vertexes[3]
path = []
findPaths(v2, v3, 1, graph, path)
for v in path:
print v.print_out()
print "New one: "
path = []
findPaths(v2, v4, 2, graph, path)
for v in path:
print v.print_out()
allPaths = findAllPaths(graph)
# print allPaths
# index = 0
# for path in allPaths:
# print "Path " + str(index)
# index += 1
# # print "start: "
# # print path[0].print_out()
# # print "end: "
# # print path[len(path) - 1].print_out()
# print "Path list: "
# for v in path:
# print v.print_out(),
# #print allPaths[path]
def testCrossover():
g1 = makeTestGraph(35)
print "g1 numvertices = ", len(g1.getVertexes())
g1.print_vertexlst()
g2 = makeTestGraph(25)
print "g2 numvertices = ", len(g2.getVertexes())
g2.print_vertexlst()
# Since this makes the same graph, CrossOver should return same thing
newGraph = CrossOver(g1, g2)
print "New Graph = ", len(newGraph.getVertexes())
newGraph.print_vertexlst()
print "end New Graph"
# Extensive testing of CrossOver generation
def testCrossovers():
for i in range(50):
print "This is test ", i
testCrossover()
def graph_readings_tests():
#graph = makeSpecificGraph()
#save_graph_to_file(graph, "test_graph")
graph = read_graph_from_file("pkl_files/test_graph.pkl")
graph.print_graph()
def main():
chars = [5, 9, 11]
people = range(1,10)
draw_graph_networks(chars, people)
# testMatch()
# testCrossover()
# testRemoveVertex()
# testFindPaths()
# testGetNeighbors()
if __name__ == "__main__":
main()