def generateChromo(self,numNodes,numSamples,numStatements,fitness= 1): newCh = chromo(self, set(helpers.proportionalrandom(self.samplesPool.keys(),lambda x:1,min(numSamples,len(self.samplesPool)))) , self.boolStatements, self.numStatements, 1) newCh.removeWorstStates(max([newCh.lenStatements() - numStatements,0])) for i in range(numNodes): if not newCh.expandBestNode(): break if newCh.numNodes ==0: return False newCh.fitness = fitness return newCh
def recombine(self,mother,father): '''adressing parential nodes as mather and father is no more stupid than calling your own parents "parent #" ''' minNodesCount = 1 #alarma might need to alter recombination here, ESPECIALLY statements newCh = chromo(self,set(list(mother.samples)+list(father.samples)), set(list(mother.boolStatements)+list(father.boolStatements)), set(list(mother.numStatements)+list(father.numStatements)), 1)#(mother.fitness+father.fitness)/2) ncount = max(int((mother.numNodes+father.numNodes)/2),minNodesCount) for i in range(ncount): if not newCh.expandBestNode(): if newCh.numNodes < minNodesCount: return False break return newCh
def initialise(self): '''create initial pool of trees''' rateTrees = .3 baseFitnessForIdealTree = 1 if self.writeLog: self.logWriter.writeLine('starting initialisation') self.logWriter.timeMarker('init',False) samplesPerTree = int(round(self.maxSamplesCount/(rateTrees*self.maxNodesCount)+0.49)) nodesPerTree = int(1/rateTrees) countTrees = int(self.maxNodesCount*rateTrees) self.maxAttrsCount= countTrees #ALARMA!! may need some details for samples/statements pools once they're implemented for i in range(countTrees): newCh = chromo(self, set(helpers.proportionalrandom(self.samplesPool.keys(),lambda x:1,min(samplesPerTree,len(self.samplesPool)))) , self.boolStatements, self.numStatements, 1) for i in range(nodesPerTree): if not newCh.expandBestNode(): break if newCh.numNodes ==0: continue newCh.fitness = helpers.getSuccessProbability(newCh,self.samplesPool.keys())*baseFitnessForIdealTree self.currentNodesCount+= newCh.numNodes self.currentSamplesCount+=len(newCh.samples) self.currentAttrsCount += len(newCh.boolStatements)+len(newCh.numStatements) self.treesPool.add(newCh) for sample in newCh.samples: self.samplesPool[sample].add(newCh) for sample in self.samplesPool.keys(): if len(self.samplesPool[sample])==0: del self.samplesPool[sample] if self.writeLog: self.logWriter.timeMarker('init') self.logWriter.writeLine('init phase end.', len(self.treesPool),'trees created with', nodesPerTree,'nodes and',samplesPerTree,'samples each')
def recombine(self,mother,father): '''adressing parential nodes as mather and father is no more stupid than calling your own parents "parent #" ''' minNodesCount = 1 #alarma might need to alter recombination here, ESPECIALLY statements newCh = chromo(self,set(list(mother.samples)+list(father.samples)), set(list(mother.boolStatements)+list(father.boolStatements)), set(list(mother.numStatements)+list(father.numStatements)), 1)#(mother.fitness+father.fitness)/2) for i in range(max(int((mother.numNodes+father.numNodes)/2),minNodesCount)): if not newCh.expandBestNode(): if newCh.numNodes < minNodesCount: return False break self.treesPool.add(newCh) for sample in newCh.samples: self.samplesPool[sample].add(newCh) self.currentNodesCount+= newCh.numNodes self.currentSamplesCount += len(newCh.samples) self.currentAttrsCount += len(newCh.boolStatements) + len (newCh.numStatements) return True
lengC=len(pop[selChrom]) print pop[selChrom] selGene=randint(1,lengC-1) for j in range(0,selGene): mutated.append(pop[selChrom][j]) mutate = FindWay(net, pop[selChrom][selGene], pop[selChrom][lengC-1], pop[selChrom]) mutated = mutated + mutate return mutated if __name__=='__main__': start = timeit.default_timer() rede = chromo.waxman(10) populacao=chromo.chromo(rede, 20) print populacao pesos=pesoCaminho(populacao,rede) print pesos fitness=fitness(pesos) print fitness for i in range(0, len(fitness)): print 'a fitness do %d cromossomo e %f' %(i, fitness[i]) popF=torneio(fitness,populacao,75, 2) popS=crossiover(popF, populacao, fitness) print popS