def initialise(self): '''create initial pool of trees''' rateTrees = .3 statementsPerTree = self.maxAvgAttrsPerTree 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 i=0 #while i< countTrees: for i in range(countTrees): newCh = self.generateChromo(nodesPerTree,samplesPerTree,statementsPerTree,1) if newCh: newCh.fitness = helpers.getSuccessProbability(newCh,self.samplesPool.keys()) self.addChromo(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 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 manageTrees(self): ''' update the pool''' expandedPoolCapacityRate = 1.2 targetTreesCount = 0.3*self.maxNodesCount minTreesCount = 10 if self.writeLog: self.logWriter.writeLine( '\nManaging trees pool. Initial nodes count = ', self.currentNodesCount,'/',self.maxNodesCount, ', trees count = ',len(self.treesPool)) self.logWriter.timeMarker('manageTrees',False) self.logWriter.timeMarker('negative fitness removal',False) _t = 0 _r = 0 for chromo in copy.copy(self.treesPool): if chromo.fitness <=0: self.removeTree(chromo) _t +=1 while len(self.treesPool) < minTreesCount: newCh = self.generateChromo(self.currentNodesCount/len(self.treesPool),len(self.treesPool),self.currentSamplesCount/len(self.treesPool),self.currentAttrsCount/len(self.treesPool)) if newCh: newCh.fitness = helpers.getSuccessProbability(newCh,self.samplesPool.keys()) self.addChromo(newCh) _r+=1 if self.writeLog: self.logWriter.timeMarker('negative fitness removal') self.logWriter.writeLine(_t,'trees removed for negative fitness, ',_r,' replaced') self.logWriter.writeLine('\nRecombination phase') self.logWriter.timeMarker('recombination',False) treeGrowthRate = 5 _s = 0 # add some new trees mutationSequence = helpers.proportionalrandom(self.treesPool,lambda x:(x.fitness),2*treeGrowthRate) if self.writeLog: self.logWriter.writeLine('Recombination phase: sequence len =',len(mutationSequence)) random.shuffle(mutationSequence) #ALARME!!actual mutation for trees may be implemented either here or separately. or not implemented due to it's simulated by all the alterations with samples and nodes. while len(mutationSequence)>1: newChromo = self.recombine(mutationSequence.pop(),mutationSequence.pop()); if newChromo: self.addChromo(newChromo) _s+=1 if self.writeLog: self.logWriter.timeMarker('recombination') self.logWriter.writeLine('End of recombination phase') self.logWriter.writeLine('\nExpansion phase') self.logWriter.timeMarker('expansion',False) #expand nodes up to maximal nodescount times expandedPoolCapacityRate (actually less due to absense of repetitions), # prioritised by tree fitness times expected benefit from expansion #this NoRep is recalculating priorities every round. expandees = helpers.proportionalrandomRounds(self.treesPool,lambda x:x.fitness*x.getBestReplacementPotential(),max(int((self.maxNodesCount*expandedPoolCapacityRate-self.currentNodesCount)), 0))#that one was set(random) just a sec ago if self.writeLog: self.logWriter.writeLine( 'Expansion phase: sequence len =',len(expandees)) for tree in expandees: if tree.expandBestNode(): self.currentNodesCount+=1 if self.writeLog: self.logWriter.timeMarker('expansion') self.logWriter.writeLine('End of expansion phase') self.logWriter.writeLine('\nPruning phase') self.logWriter.timeMarker('pruning',False) #remove up to all nodes above nodescount(actually less due to possibility that trees have less nodes than tis demanded to remove) # of least effective nodes in terms of nodes per fitness, remove empty trees if self.writeLog: self.logWriter.writeLine('Pruning phase: initial nodes count =',self.currentNodesCount,'/',self.maxNodesCount) _t = 0 while self.currentNodesCount > self.maxNodesCount: reducees = helpers.proportionalrandom(self.treesPool,lambda x:x.numNodes/(x.fitness),max(self.currentNodesCount - self.maxNodesCount,0)) for tree in reducees: if tree in self.treesPool: if tree.pruneWorstNode(): self.currentNodesCount-=1 #if pruning fails, -1 node is done in else: _t+=1 self.removeTree(tree) if self.writeLog: self.logWriter.writeLine( _t,' trees removed in pruning phase') self.logWriter.timeMarker('pruning') self.logWriter.writeLine('End of pruning phase') if self.writeLog: self.logWriter.timeMarker('manageTrees') self.logWriter.writeLine( 'OnAfterStep nodes count = ', self.currentNodesCount,', trees count = ',len(self.treesPool)) self.logWriter.writeLine( 'End of tree management.\n')