I’m working on a project that attempts to attack classical cryptography schemes using a genetic algorithm. Outside of very simplistic GAs using only strings/swapping characters for crossover, I’m not sure how to attack the problem I am facing. I would to crossover two object instances of the same type that have crypto parameters and other attributes walking these objects/their nested objects instead of encoding as a string; there are validations for variations and don’t want to waste CPU cycles on invalid encodings or splitting the wrong part of the string/serialization and deserialization.
I was thinking of doing something like the following:
# select most fit
parent1 = self.genepool[0]
parent2 = self.genepool[1]
# find max depth - should create a dict for other objects as to not traverse too deep on the wrong object
depth = lambda L: isinstance(parent1, object) and max(map(depth, parent1))+1
walk_depth = random.int(0, depth)
# walk into objects...
# select random attribute
attr, v = random.choice([attr, v for attr, v in vars(self).items())
offspring = p2
offspring[attr] = v