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. """ #DEMO 2 CODE------------------------- print( "-----------------------------------------------------------------------------------------------------------------------------------" ) print("Current instance from dataset: " + "State = " + str(state_phenotype[0]) + " Phenotype = " + str(state_phenotype[1])) print( "--------------------------------------------------------------------------------------" ) print("Match Set:") #------------------------------------------ #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 #------------------------------------------------------- for i in range(len(self.popSet)): # Go through the population cl = self.popSet[i] # One classifier at a time if cl.match(state): # Check for match #DEMO 2 CODE------------------------- print("Condition: " + str(cl.reportClassifier()) + " Phenotype: " + str(cl.phenotype) + " Fitness: " + str(cl.fitness)) #------------------------------------------ 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 if float(cl.phenotype[0]) <= float(phenotype) <= float( cl.phenotype[1]): # Check for phenotype coverage doCovering = False #------------------------------------------------------- # COVERING #------------------------------------------------------- while doCovering: newCl = Classifier(setNumerositySum + 1, exploreIter, state, phenotype) #DEMO 2 CODE------------------------- print("Condition: " + str(newCl.reportClassifier()) + " Phenotype: " + str(newCl.phenotype)) #------------------------------------------ self.addClassifierToPopulation(newCl) self.matchSet.append(len(self.popSet) - 1) # Add covered classifier to matchset doCovering = False
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", 'r') 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) numerosityRef = cons.env.formatData.numAttributes + 3 self.microPopSize += int(each[numerosityRef]) print("Rebooted Rule Population has "+str(len(self.popSet))+" Macro Pop Size.")
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. """ #DEMO 2 CODE------------------------- print("-----------------------------------------------------------------------------------------------------------------------------------") print("Current instance from dataset: " + "State = "+ str(state_phenotype[0]) + " Phenotype = "+ str(state_phenotype[1])) print("--------------------------------------------------------------------------------------") print("Match Set:") #------------------------------------------ #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 #------------------------------------------------------- for i in range(len(self.popSet)): # Go through the population cl = self.popSet[i] # One classifier at a time if cl.match(state): # Check for match #DEMO 2 CODE------------------------- print("Condition: "+ str(cl.reportClassifier()) + " Phenotype: "+ str(cl.phenotype) + " Fitness: "+ str(cl.fitness)) #------------------------------------------ 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 if float(cl.phenotype[0]) <= float(phenotype) <= float(cl.phenotype[1]): # Check for phenotype coverage doCovering = False #------------------------------------------------------- # COVERING #------------------------------------------------------- while doCovering: newCl = Classifier(setNumerositySum+1,exploreIter, state, phenotype) #DEMO 2 CODE------------------------- print("Condition: "+ str(newCl.reportClassifier()) + " Phenotype: "+ str(newCl.phenotype)) #------------------------------------------ self.addClassifierToPopulation(newCl) self.matchSet.append(len(self.popSet)-1) # Add covered classifier to matchset doCovering = False
def runGA(self, exploreIter, state, phenotype): """ The genetic discovery mechanism in eLCS 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 opperates in). 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] elif cons.selectionMethod == "lexicase": selectList = self.selectClassifierLex(exploreIter) clP1 = selectList[0] clP2 = selectList[1] elif cons.selectionMethod == "batch-lexicase": selectList = self.selectClassifierLexBatch(exploreIter) 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: changed = cl1.uniformCrossover(cl2) #------------------------------------------------------- # 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 #------------------------------------------------------- nowchanged = cl1.Mutation(state, phenotype) howaboutnow = cl2.Mutation(state, phenotype) #------------------------------------------------------- # ADD OFFSPRING TO POPULATION #------------------------------------------------------- if changed or nowchanged or howaboutnow: self.insertDiscoveredClassifiers(cl1, cl2, clP1, clP2, exploreIter) #Subsumption
def runGA(self, exploreIter, state, phenotype): """ The genetic discovery mechanism in eLCS is controlled here. """ self.setIterStamps( exploreIter ) #Updates the iteration time stamp for all rules in the correct set. changed = False #------------------------------------------------------- # SELECT PARENTS - Panmictic GA - selects parents from the entire rule population #------------------------------------------------------- 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." ) #------------------------------------------------------- # 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: changed = cl1.uniformCrossover(cl2) #------------------------------------------------------- # 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 #------------------------------------------------------- nowchanged = cl1.Mutation(state, phenotype) howaboutnow = cl2.Mutation(state, phenotype) #------------------------------------------------------- # ADD OFFSPRING TO POPULATION #------------------------------------------------------- if changed or nowchanged or howaboutnow: self.insertDiscoveredClassifiers(cl1, cl2, clP1, clP2, exploreIter)
def runGA(self, exploreIter, state, phenotype): """ The genetic discovery mechanism in eLCS is controlled here. """ self.setIterStamps(exploreIter) #Updates the iteration time stamp for all rules in the correct set. changed = False #------------------------------------------------------- # SELECT PARENTS - Panmictic GA - selects parents from the entire rule population #------------------------------------------------------- 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.") #------------------------------------------------------- # 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: changed = cl1.uniformCrossover(cl2) #------------------------------------------------------- # 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 #------------------------------------------------------- nowchanged = cl1.Mutation(state, phenotype) howaboutnow = cl2.Mutation(state, phenotype) #------------------------------------------------------- # ADD OFFSPRING TO POPULATION #------------------------------------------------------- if changed or nowchanged or howaboutnow: self.insertDiscoveredClassifiers(cl1, cl2, clP1, clP2, exploreIter)