-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathivspSolver.py
449 lines (385 loc) · 17 KB
/
ivspSolver.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
##################################################
# I.ndependent V.ertex S.et P.roblem Solver
# Assignment 4 - CS 440 @ UH Hilo
#
# Professor : Dr. M. Peterson
# Students : Cunnyngham, I.
# : Perkins, J.
# : Wessels, A.
#
# Python implementaion of Dr. Peterson's
# template for this project. Most comments stolen
# verbatum. Statistical functions in Fxrandom are a
# Python implementation of Dr. Peterson's Java
# implementation of a C++ random generator written by
# Dr. Mateen Rizki of the Department of Computer Science
# & Engineering, Wright State University - Dayton, Ohio, U.S.A.
#
##################################################
#!/usr/bin/python
import sys
import time
import datetime
import math
import random
### Vertex Set Class ###
class VSet:
""" A class representing a list of vertices in a graph """
def __init__(self, set=[]):
""" Constructor for VSet, accepts an array of bools as default value of self.set """
self.set = [ i for i in set ]
self.fitness = -1
def toggleVertex(self, i):
""" Toggle whether a vertex at the given index is included in the set or not """
self.set[i] = not self.set[i]
def total(self):
""" Returns the number of vertices in this set """
return self.set.count(True)
def density(self):
""" Returns the percentage of true values in the set """
return self.total()/float(len(self.set))
def pagePrint(self):
""" Prints the members of this set as 1s(included) and 0s(excluded) grouped in sets of 50 """
for i, b in enumerate(self.set):
sys.stdout.write("1") if self.set[i] else sys.stdout.write("0")
if ((i+1)%50==0):
print ""
def __repr__(self):
rep = "VSet(%.3f): "%self.fitness
for i,v in enumerate(self.set):
rep += "%i"%v
return rep+"\n"
def __getitem__(self, key):
return self.set[key]
def __setitem__(self, key, value):
self.set[key] = value
def __len__(self):
return len(self.set)
@classmethod
def emptySet(cls, size):
""" Returns an empty set (all False valued) of [size] """
return cls( [ False for i in range(size) ] )
@classmethod
def randomSet(cls, size, random, density=.2):
""" Generates a random set, where individual vertex inclusion has [density] probability """
return cls( [ random.boolBernoulli(density) for i in range(size) ] )
### Vertex Set Iterator Class ###
class VSetIter:
""" A Class that iterates through all permutations of a vertex set """
def __init__(self, size):
""" Constructor which takes the size of the vertex sets to be generated """
self.sizeN = size
self.vs = VSet.emptySet( size )
def __iter__(self):
""" Make class callable by iterator functions """
return self
def next(self):
""" Find the next perumutation from the current vertex. Covers all possibilties until set is full """
size = k = len( self.vs.set )
if self.vs.set.count(True) == size:
raise StopIteration
while k>=1 and self.vs.set[k-1]:
k-=1
if k>=1:
self.vs.set[k-1]=True
for i in range(k, size ):
self.vs.set[i] = False
return self.vs
### Graph Class ###
class Graph:
""" A class representing a undirected graph with random connectivity """
def __init__(self, size, cnn, seed=0):
""" This is the public constructor for the graph class.
Size is the number
of vertices in the graph, cnn in the connectivity of the graph (the
probability of there being an edge between two arbitrary nodes); cnn must
be between 0.0 * 1.0 inclusive; and seed is the seed value for the
random number generator. By default, use 0 for a random seed based on
the system clock.
size -- Number of vertices in the graph.
cnn -- Connectivity of the graph.
seed -- Random number generator seed (default 0). testcase -- If you want the test case graph, set this to true.
testcase -- Set to true for the 4x4 static test case
fitfunc -- Set the fitness function for the GA and annealing solutions to use
"""
# Perform error checking
if size <= 0:
raise ValueError("Graph Init: size must be > 0.")
if cnn < 0.0 or cnn > 1.0:
raise ValueError("Graph Init: connectivity must be between 0.0 & 1.0.")
if seed < 0:
raise ValueError("Graph Init: seed must be >= 0.")
# Instantiate class variables
self.cnn = cnn
self.sizeN = size
self.rand = Fxrandom(seed)
fitfunc = self.goodFitness #define a global fit function to use by default...
# Instantiate the class's adjacency matrix to all False values
self.adjMatrix = [ [False for j in range(self.sizeN) ] for i in range(self.sizeN) ]
# Initialize edges according to a weighted coin flip.
# if cnn = 0.1, there will be a 10% chance of creating an edge
# between vertices i and j. Note that there will never be an
# edge between a vertex and itself.
for i in range(self.sizeN):
for j in range(i+1, self.sizeN):
self.adjMatrix[i][j] = self.adjMatrix[j][i] = self.rand.boolBernoulli(cnn)
def greedySolution(self):
""" Finds an independent set using a greedy solution """
s = VSet.emptySet( self.sizeN )
# Rank the vertexes by their connectedness
vrank = []
for i, row in enumerate(self.adjMatrix):
vrank.append( [i, row.count(True) ])
vrank = sorted( vrank, key=lambda v: v[1] )
# Try to add each vector from lowest connectedness up.
# If the vector breaks the set, toggle it back.
for v in vrank:
s.toggleVertex( v[0] )
if self.evaluateSet( s ) == -1:
s.toggleVertex( v[0] )
s.fitness = self.fitfunc( s )
return s
def shallowAnnealing(self, coolStep = 0.90):
""" Generate the biggest set using the simulated annealing algorithm """
bestSet = VSet.emptySet( self.sizeN )
bestScore = 0
for j in range(self.sizeN*25):
frozen = False
T = self.sizeN
curSet = VSet.randomSet( self.sizeN, self.rand, self.cnn/2)
newSet = VSet( curSet )
while not frozen:
newSet.toggleVertex( random.randrange(self.sizeN) )
newSet.toggleVertex( random.randrange(self.sizeN) )
dE = self.fitfunc(curSet) - self.fitfunc(newSet)
if dE<0:
curSet = VSet( newSet )
elif self.rand.boolBernoulli( math.exp( - dE / T ) ):
curSet = VSet( newSet )
T *= coolStep
if T < self.sizeN/10:
frozen = True
score = self.evaluateSet( curSet )
if score > bestScore:
bestScore = score
bestSet = VSet( curSet )
bestSet.fitness = self.fitfunc(bestSet)
return bestSet
def exhaustiveSolution(self):
""" Generate the biggest possible independent set of vertices by testing all possibilities """
maxScore = 0
for s in VSetIter( self.sizeN ):
curScore = self.evaluateSet( s )
if curScore > maxScore:
maxScore = curScore
maxSet = VSet( s )
maxSet.fitness = self.fitfunc(maxSet)
return maxSet
def mutate(self, value, rate=None):
""" Mutates a given bit with [rate] liklihood """
if rate == None:
return value
return not value if self.rand.boolBernoulli(rate) else value
def combine(self, group1, group2, mutation):
""" Given two parent vertex sets, produce an offspring vertex set by randomly picking between them and mutating """
#vset = VSet( [self.mutate(group1[i] if self.rand.boolBernoulli(.5) else group2[i], mutation) for i in range(self.sizeN)] )
vset = VSet( [group1[i] if self.rand.boolBernoulli(.5) else group2[i] for i in range(self.sizeN)] )
if mutation and self.rand.boolBernoulli(mutation):
idx = int(self.rand.uniform(0,self.sizeN))
vset[idx] = not vset[idx]
return vset
def rouletteSelection(self, population, popsize, total_fitness):
""" Stochastic Sampling (Roulette wheel) method of selecting parents
This method requires some extra preperation of the data, and fails when fitness is negative...
Source: http://www.cse.unr.edu/~banerjee/selection.htm """
choice = [ ]
choice_fitness = 0
return_population = popsize
#combined_size = popsize*2
k=0
while (k < return_population):
partsum = 0
parent = 0
#print total_fitness
randnum = self.rand.uniform(0.0, total_fitness)
for i in range(0, popsize):
partsum += population[i].fitness
if partsum >= randnum:
parent = i
break
k+=1
fitness = population[parent].fitness
#print "%i minus %f"%(parent, fitness)
choice.append(population[parent])
choice_fitness += fitness
return (choice, choice_fitness)
def GASolution(self, popsize=100, generations=300, density=None, mutation=0.01, preserve=0, fitfunc=None):
""" Finds an independent set using a Genetic Algorithm """
if fitfunc == None:
fitfunc = self.fitfunc
if not density:
""" if no density is set, run greedy and just use a fraction of the density of the set returned"""
density = self.greedySolution().density()*.5
# print "Using density: %.5f"%density
pop_range = range(popsize) #avoid remaking this list a bunch of times
population = [None for x in pop_range] #initialize the list so i don't have to append a million times
children = [None for x in pop_range]
combined = []
population_fitness = 0.0 #stores the current population's fitness total, used in roulette selection
#lastindex = popsize-1
"""initialize the population"""
for i in pop_range:
s = VSet.randomSet(self.sizeN, self.rand, density)
s.fitness = fitfunc(s)
population_fitness += s.fitness
population[i] = s
# print "Average fitness before: %.2f"%(population_fitness/popsize)
for g in range(generations):
children_fitness = 0 #stores the total fitness for the children genomes
for i in pop_range:
rand1 = int(self.rand.uniform(0, popsize))
rand2 = int(self.rand.uniform(0, popsize))
while rand1 == rand2:
rand2 = int(self.rand.uniform(0, popsize))
#set1 = population[rand1]
#set2 = population[rand2]
s = self.combine(population[rand1], population[rand2], mutation=mutation)
s.fitness = fitfunc(s)
children_fitness += s.fitness
children[i] = s
combined = population + children
#this is more shitty!
random.shuffle(combined)
combined_len = popsize*2
for i in pop_range:
other = i+popsize
if combined[i].fitness > combined[other].fitness:
population[i] = combined[i]
else:
population[i] = combined[other]
#(population, population_fitness) = self.rouletteSelection(combined, popsize, population_fitness+children_fitness)
#print population
best = population[0]
for i,t in enumerate(population):
if self.evaluateSet(t) > 0 and (not best or best.fitness < t.fitness):
best = t
break
#print "Average fitness after: %.2f"%(population_fitness/popsize)
#print "Highest fitness: %.2f"%(sorted_population[0].fitness)
#print "Lowest fitness: %.2f"%(sorted_population[-1].fitness)
#print "Best: %s"%best
#print population
return best
def triangleFitness(self, s):
""" Fitness= for i vertexes: sum( [tri] || - 1.5*[tri]). [tri] = triangle number of ith vertex """
# Skip error test and assume len(s) == sizeN for quickness of algorithm
setSize = fitness = 0.0
for i in range(self.sizeN):
if s[i] :
ind = True
for j in range(i+1, self.sizeN):
if s[j] and self.adjMatrix[i][j]:
ind = False
break
if ind:
fitness+=setSize
else:
fitness-=1.5*setSize
setSize+=1
if fitness < 0: #i need the fitness to remain in the positives...
fitness = -1.0/fitness
return fitness
def goodFitness(self, s):
# Skip error test and assume len(s) == sizeN for quickness of algorithm
setSize = fitness = 0.0
valid = invalid = missingValids = 0.0
for i in range(self.sizeN):
if s[i] :
ind = True
for j in range(i+1, self.sizeN):
if s[j] and self.adjMatrix[i][j]:
ind = False
break
if ind:
valid += 1
else:
invalid += 1
setSize+=1
else:
missingValids +=1
return (( setSize )**2) / ( (invalid+1)**2) # works at low numbers
# return ( setSize ) / (invalid+1) # works at low numbers
#return ( valid ) / (invalid+1)
def connFitness(self, s):
# Skip error test and assume len(s) == sizeN for quickness of algorithm
setSize = conn = 0.0
for i in range(self.sizeN):
if s[i] :
for j in range(i+1, self.sizeN):
if s[j] and self.adjMatrix[i][j]:
conn += 1
setSize+=1
return (( setSize )**2) / ( (conn+1)**2)
def evaluateSet(self, vset):
""" Test to see if a passed set is independent, if yes, size of set is returned, -1 elsewise """
# Skip error test and assume len(set) == sizeN for quickness of algorithm
setSize = 0
independent = True
for i in range(self.sizeN):
try:
if vset[i] :
for j in range(i+1, self.sizeN):
if vset[j] and self.adjMatrix[i][j]:
independent = False
break
if not independent:
break
setSize+=1
except TypeError, e:
print "EVALUATE SET BROKEN: {0}".format(e)
# print "Graph: {0}".format(self)
exit(1)
if independent:
return setSize
return -1
def __repr__(self):
ret = "Adjacency Matrix: \n"
for i,l in enumerate(self.adjMatrix):
for j,v in enumerate(self.adjMatrix[i]):
ret += "%i "%self.adjMatrix[i][j]
ret += "\n\n"
return ret #str(self.adjMatrix)
### Statistics class ###
class Fxrandom:
""" A class with some useful statistical functions """
def __init__(self, initSeed=0):
""" Constructor: set class variables, set seed as specified or by system clock if 0(default) """
self.modulus = 0x7fffffff;
self.multiplier = 630360016;
self.setSeed(initSeed)
def setSeed(self, initSeed=0):
""" Reset seed: If initSeed=0 (default) set seed to clock miliseconds, else use passed seed """
if initSeed==0:
self.seed = datetime.datetime.now().microsecond/1000
else:
self.seed = initSeed
def random(self):
""" Returns a random integer 0...0x7fffffff """
self.seed = ((self.multiplier * self.seed) % self.modulus)
return math.fabs(self.seed)
def uniform(self, min, max):
""" Returns a random double between min & max """
val=0.0
if min >= max:
raise ValueError("min must be less than max.")
val = (min + (max - min) * (self.random())/(self.modulus))
return val
def boolBernoulli(self, probability):
""" Return an Bernoulli distributed random bool with given probability """
num = self.uniform(0.0, 1.0)
if(num <= probability):
return True
return False
if __name__=="__main__":
import doctest
doctest.testmod()