def rebootPop(self, remakeFile): """ Remakes a previously evolved population from a saved text file. """ print("Rebooting the following population: " + str(remakeFile) + "_RulePop.txt") # *******************Initial file handling********************************************************** try: datasetList = [] f = open(remakeFile + "_RulePop.txt", 'rU') self.headerList = f.readline().rstrip('\n').split( '\t') # strip off first row for line in f: lineList = line.strip('\n').split('\t') datasetList.append(lineList) f.close() except IOError as xxx_todo_changeme: (errno, strerror) = xxx_todo_changeme.args print("Could not Read Remake File!") print(("I/O error(%s): %s" % (errno, strerror))) raise except ValueError: print("Could not convert data to an integer.") raise except: print(("Unexpected error:", sys.exc_info()[0])) raise # ************************************************************************************************** for each in datasetList: cl = Classifier(each) self.popSet.append(cl) # Add classifier to the population numerosityRef = 5 # location of numerosity variable in population file. self.microPopSize += int(each[numerosityRef])
def rebootPop(self, remakeFile): """ Remakes a previously evolved population from a saved text file. """ print("Rebooting the following population: " + str(remakeFile) + "_RulePop.txt") #*******************Initial file handling********************************************************** datasetList = [] try: f = open(remakeFile + "_RulePop.txt", 'rU') except Exception as inst: print(type(inst)) print(inst.args) print(inst) print('cannot open', remakeFile + "_RulePop.txt") raise else: self.headerList = f.readline().rstrip('\n').split( '\t') #strip off first row for line in f: lineList = line.strip('\n').split('\t') datasetList.append(lineList) f.close() #************************************************************************************************** for each in datasetList: cl = Classifier(each) self.popSet.append(cl) #Add classifier to the population numerosityRef = 5 #location of numerosity variable in population file. self.microPopSize += int(each[numerosityRef])
def makeMatchSet(self, state_phenotype, exploreIter): """ Constructs a match set from the population. Covering is initiated if the match set is empty or a rule with the current correct phenotype is absent. """ #Initial values---------------------------------- state = state_phenotype[0] phenotype = state_phenotype[1] doCovering = True # Covering check: Twofold (1)checks that a match is present, and (2) that at least one match dictates the correct phenotype. setNumerositySum = 0 #------------------------------------------------------- # MATCHING #------------------------------------------------------- cons.timer.startTimeMatching() for i in range(len(self.popSet)): # Go through the population cl = self.popSet[i] # One classifier at a time cl.updateEpochStatus( exploreIter ) # Note whether this classifier has seen all training data at this point. if cl.match(state): # Check for match self.matchSet.append( i) # If match - add classifier to match set setNumerositySum += cl.numerosity # Increment the set numerosity sum #Covering Check-------------------------------------------------------- if cons.env.formatData.discretePhenotype: # Discrete phenotype if cl.phenotype == phenotype: # Check for phenotype coverage doCovering = False else: # Continuous phenotype print( "ClassifierSet - Error: ExSTraCS 2.0 can not handle continuous endpoints." ) cons.timer.stopTimeMatching() #------------------------------------------------------- # COVERING #------------------------------------------------------- while doCovering: cons.timer.startTimeCovering() newCl = Classifier(setNumerositySum + 1, exploreIter, state, phenotype) self.addClassifierToPopulation(newCl, True) self.matchSet.append(len(self.popSet) - 1) # Add covered classifier to matchset doCovering = False cons.timer.stopTimeCovering()
def runGA(self, exploreIter, state, phenotype): """ The genetic discovery mechanism in ExSTraCS is controlled here. """ #------------------------------------------------------- # GA RUN REQUIREMENT #------------------------------------------------------- if ( exploreIter - self.getIterStampAverage() ) < cons.theta_GA: #Does the correct set meet the requirements for activating the GA? return self.setIterStamps( exploreIter ) #Updates the iteration time stamp for all rules in the correct set (which the GA operates on). changed = False #------------------------------------------------------- # SELECT PARENTS - Niche GA - selects parents from the correct class #------------------------------------------------------- cons.timer.startTimeSelection() if cons.selectionMethod == "roulette": selectList = self.selectClassifierRW() clP1 = selectList[0] clP2 = selectList[1] elif cons.selectionMethod == "tournament": selectList = self.selectClassifierT() clP1 = selectList[0] clP2 = selectList[1] else: print( "ClassifierSet: Error - requested GA selection method not available." ) cons.timer.stopTimeSelection() #------------------------------------------------------- # INITIALIZE OFFSPRING #------------------------------------------------------- cl1 = Classifier(clP1, exploreIter) if clP2 == None: cl2 = Classifier(clP1, exploreIter) else: cl2 = Classifier(clP2, exploreIter) #------------------------------------------------------- # CROSSOVER OPERATOR - Uniform Crossover Implemented (i.e. all attributes have equal probability of crossing over between two parents) #------------------------------------------------------- if not cl1.equals(cl2) and random.random() < cons.chi: cons.timer.startTimeCrossover() changed = cl1.uniformCrossover(cl2) cons.timer.stopTimeCrossover() #------------------------------------------------------- # INITIALIZE KEY OFFSPRING PARAMETERS #------------------------------------------------------- if changed: cl1.setAccuracy((cl1.accuracy + cl2.accuracy) / 2.0) cl1.setFitness(cons.fitnessReduction * (cl1.fitness + cl2.fitness) / 2.0) cl2.setAccuracy(cl1.accuracy) cl2.setFitness(cl1.fitness) else: cl1.setFitness(cons.fitnessReduction * cl1.fitness) cl2.setFitness(cons.fitnessReduction * cl2.fitness) #------------------------------------------------------- # MUTATION OPERATOR #------------------------------------------------------- cons.timer.startTimeMutation() nowchanged = cl1.Mutation(state, phenotype) howaboutnow = cl2.Mutation(state, phenotype) cons.timer.stopTimeMutation() #Generalize any continuous attributes that span then entire range observed in the dataset. if cons.env.formatData.continuousCount > 0: cl1.rangeCheck() cl2.rangeCheck() #------------------------------------------------------- # ADD OFFSPRING TO POPULATION #------------------------------------------------------- if changed or nowchanged or howaboutnow: self.insertDiscoveredClassifiers( cl1, cl2, clP1, clP2, exploreIter) #Includes subsumption if activated.
def runGA(self, exploreIter, state, phenotype): """ The genetic discovery mechanism in ExSTraCS is controlled here. """ #------------------------------------------------------- # GA RUN REQUIREMENT #------------------------------------------------------- if (exploreIter - self.getIterStampAverage()) < cons.theta_GA: #Does the correct set meet the requirements for activating the GA? return self.setIterStamps(exploreIter) #Updates the iteration time stamp for all rules in the correct set (which the GA operates on). changed = False #------------------------------------------------------- # SELECT PARENTS - Niche GA - selects parents from the correct class #------------------------------------------------------- cons.timer.startTimeSelection() if cons.selectionMethod == "roulette": selectList = self.selectClassifierRW() clP1 = selectList[0] clP2 = selectList[1] elif cons.selectionMethod == "tournament": selectList = self.selectClassifierT() clP1 = selectList[0] clP2 = selectList[1] else: print("ClassifierSet: Error - requested GA selection method not available.") cons.timer.stopTimeSelection() #------------------------------------------------------- # INITIALIZE OFFSPRING #------------------------------------------------------- cl1 = Classifier(clP1, exploreIter) if clP2 == None: cl2 = Classifier(clP1, exploreIter) else: cl2 = Classifier(clP2, exploreIter) #------------------------------------------------------- # CROSSOVER OPERATOR - Uniform Crossover Implemented (i.e. all attributes have equal probability of crossing over between two parents) #------------------------------------------------------- if not cl1.equals(cl2) and random.random() < cons.chi: cons.timer.startTimeCrossover() changed = cl1.uniformCrossover(cl2) cons.timer.stopTimeCrossover() #------------------------------------------------------- # INITIALIZE KEY OFFSPRING PARAMETERS #------------------------------------------------------- if changed: cl1.setAccuracy((cl1.accuracy + cl2.accuracy)/2.0) cl1.setFitness(cons.fitnessReduction * (cl1.fitness + cl2.fitness)/2.0) cl2.setAccuracy(cl1.accuracy) cl2.setFitness(cl1.fitness) else: cl1.setFitness(cons.fitnessReduction * cl1.fitness) cl2.setFitness(cons.fitnessReduction * cl2.fitness) #------------------------------------------------------- # MUTATION OPERATOR #------------------------------------------------------- cons.timer.startTimeMutation() nowchanged = cl1.Mutation(state, phenotype) howaboutnow = cl2.Mutation(state, phenotype) cons.timer.stopTimeMutation() #Generalize any continuous attributes that span then entire range observed in the dataset. if cons.env.formatData.continuousCount > 0: cl1.rangeCheck() cl2.rangeCheck() #------------------------------------------------------- # ADD OFFSPRING TO POPULATION #------------------------------------------------------- if changed or nowchanged or howaboutnow: self.insertDiscoveredClassifiers(cl1, cl2, clP1, clP2, exploreIter) #Includes subsumption if activated.