def TestHeightHeightUp(): for _ in range(100): X = [] Y = [] alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k'] X, Y = RandomNew(alphabet) A = induce(X, Y) alphabet = [ 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'w', 'x', 'y', 'z' ] X, Y = RandomNew(alphabet) B = induce(X, Y) AExp = A.__str__() BExp = B.__str__() (first, firstPar) = randomWalk(A, 2) (second, secondPar) = randomWalk(B, 2) theoreticalHeightA = heightUp(A, first, firstPar) + height(second) theoreticalHeightB = heightUp(B, second, secondPar) + height(first) switch(first, firstPar, second, secondPar) sizeAAfter = heightUp(A, second, firstPar) + height(second) sizeBAfter = heightUp(B, first, secondPar) + height(first) assert (theoreticalHeightA == sizeAAfter) assert (theoreticalHeightB == sizeBAfter) assert (AExp != A.__str__()) assert (BExp != B.__str__())
def evolving(self, interations: int, debugOn: bool = False, statOn: bool = False) -> Individual: """Main optimization function.""" self.initPopulation() if debugOn: self.dump() self.initNextPopulation() best: Individual = self.bestIndividual(self.population) for i in range(interations): self.nextPopulation[0] = best for k in range(1, self.populationSize): child1, child2 = self.crossOver(self.selection(), self.selection()) self.nextPopulation[k] = self.succession(child1, child2) self.population = copy(self.nextPopulation) if debugOn: self.dump() if statOn: fitnesses = list(map(lambda ind: ind.fitness, self.population)) print( "fitness at {0}: min={1:.4f}; median={2:.4f}; max={3:.4f}". format(i, min(fitnesses), median(fitnesses), max(fitnesses))) treeHeights = list( map(lambda ind: height(ind.expression), self.population)) print( "exp. heights at {0}: min={1:.4f}; median={2:.4f}; max={3:.4f}" .format(i, min(treeHeights), median(treeHeights), max(treeHeights))) treeNodeCounts = list( map(lambda ind: count(ind.expression), self.population)) print( "exp. node count at {0}: min={1:.4f}; median={2:.4f}; max={3:.4f}" .format(i, min(treeNodeCounts), median(treeNodeCounts), max(treeNodeCounts))) divers = set() [divers.add(ind.expression) for ind in self.population] print("exp. unique at {0}: {1}/{2}".format( i, len(divers), self.populationSize)) itbest = self.bestIndividual(self.population) if itbest.fitness > best.fitness: best = deepcopy(itbest) return best
def random(cls, X: set, Y: set, sampleSize, depthSize: int, fitness: Callable[[Expression], float]): tryCount: int = 0 while (tryCount < 30): exp = induce(sample(X, sampleSize), sample(Y, sampleSize)) if height(exp) > depthSize: continue fit: float = fitness(exp) return cls(exp, fit) raise NameError( "Try 30 times to create expression with depth less then {0}". format(depthSize))
def TestHeight1(): A: Expression = induce(['aba', 'abb', 'abc'], ['aa', 'ab', 'ac', 'abcd']) h = height(A) assert (h in (5, 6))
def TestHeight3(): a = Terminal('a') B = Expression('B') B = a >> a assert (height(B) == 1)
def TestHeight2(): a = Terminal('a') b = Terminal('b') B = Expression('B') B = a | ~b >> a | ~b assert (height(B) == 4)