def run(): """ run the pipeline """ global google_api_key google_api_key = get_key() start_logging() genome = GoogleQueryGenome(seq_length) print genome genome.evaluator.set(count_results_by_query) # genome.setParams(rangemin=rangemin, rangemax=rangemax) genome.initialize() ga = GSimpleGA.GSimpleGA(genome) ga.setGenerations(ngenerations) # ga.setMultiProcessing() csv_adapter = DBAdapters.DBFileCSV(identify="testrun", filename='results/testrun.csv') ga.setDBAdapter(csv_adapter) # ga.StepCallback.set(pickle_generation) ga.evolve(freq_stats=10) print "*"*5 + "best individual:" print ga.bestIndividual()
def run_main(): # Genome instance genome = G1DBinaryString.G1DBinaryString(bitstrlen) # The evaluator function (objective function) # genome.evaluator.set(eval_func) genome.evaluator.set(eval_func_onhardware) genome.mutator.set(Mutators.G1DBinaryStringMutatorFlip) # Genetic Algorithm Instance ga = GSimpleGA.GSimpleGA(genome) #ga.setElitism(True) ga.setMutationRate(0.02) # for use with long strings 0.01, default: 0.02 ga.selector.set(Selectors.GTournamentSelector) ga.setPopulationSize(10) ga.setGenerations(30) # ga.setInteractiveGeneration(10) csv_adapter = DBAdapters.DBFileCSV(identify="run1", filename="stats.csv") ga.setDBAdapter(csv_adapter) # Do the evolution, with stats dump # frequency of 10 generations ga.evolve(freq_stats=1) # Best individual print ga.bestIndividual() print "target:", target.bin print "date:" # do timings of all components: bitgen, eval, f = open("best-ind.txt", "w") f.write(str(ga.bestIndividual())) f.write("\n") f.write(str(target.bin)) f.write("\n") f.close()
def ga_layout(dim, objfunc, initfunc, statsfile, coordx, coordy, coordz, **kwargs): """ Run Genetic Algorithm Runs the Genetic Algorithm using the Pyevolve module, an implementation of the Simple GA is used to minimize the objective function. :param dim, dimension of the problem (number of nodes of the network) :param objfunc, objective function object :param initfunc, initialize function object :param statsfile, the file name of a CSV tect file to save the results :param coordx, list with x-axis coordinates :param coordy, list with y-axis coordinates :param run, the number of the current execution :param gen, generations of the GA :param pop, population size of the GA :param cross, crossover rate of the GA :param mut, mutation rate of the GA :return best, a list of the diameters with the maximum fitness """ # Get the arguments run = kwargs.get('run', 0) gen = kwargs.get('gen', 100) pop = kwargs.get('pop', 80) xover = kwargs.get('xover', 0.9) mut = kwargs.get('mut', 0.02) # Genome instance genome = G1DList.G1DList(dim) genome.initializator.set(lambda x, **args: initfunc(x, dim)) # The evaluator function (objective function) genome.evaluator.set(lambda x, **args: objfunc(x, coordx, coordy, coordz)) genome.mutator.set(Mutators.G1DListMutatorSwap) genome.crossover.set(Crossovers.G1DListCrossoverTwoPoint) # Genetic Algorithm instance ga = GSimpleGA.GSimpleGA(genome) ga.setMinimax(Consts.minimaxType["minimize"]) # Use roulette wheel with linear scaling p = ga.getPopulation() p.scaleMethod.set(Scaling.LinearScaling) ga.selector.set(Selectors.GRouletteWheel) ga.setGenerations(gen) ga.setPopulationSize(pop) ga.setCrossoverRate(xover) ga.setMutationRate(mut) # Save stats to CSV file for later analysis csv_adapter = DBAdapters.DBFileCSV(identify="run" + str(run), filename=statsfile, reset=False) ga.setDBAdapter(csv_adapter) # Do the evolution, with stats dump # frequency of certain generations ga.evolve(freq_stats=100) stats = ga.getStatistics() print(stats) best = ga.bestIndividual() return best
from pyevolve import G1DList from pyevolve import GSimpleGA from pyevolve import Selectors from pyevolve import DBAdapters import pyevolve def eval_func(chromosome): score = 0.0 for value in chromosome: if value == 0: score += 1 return score pyevolve.logEnable() genome = G1DList.G1DList(50) genome.setParams(rangemin=0, rangemax=10) genome.evaluator.set(eval_func) ga = GSimpleGA.GSimpleGA(genome) ga.selector.set(Selectors.GRouletteWheel) ga.setGenerations(500) ga.setPopulationSize(10) ga.terminationCriteria.set(GSimpleGA.ConvergenceCriteria) csv_adapter = DBAdapters.DBFileCSV(identify="run1", filename="statsfyuk.csv") ga.setDBAdapter(csv_adapter) ga.evolve(freq_stats=20) print ga.bestIndividual()
def run(self): """ run the Genetic Training""" genome = G2DList.G2DList(*self.shapeParamList) genome.setParams(rangemin=0, rangemax=1, gauss_mu=0, gauss_sigma=self.mutationSigma) genome.evaluator.set(self.evaluateParam) genome.initializator.set(Initializators.G2DListInitializatorReal) genome.mutator.set(Mutators.G2DListMutatorRealGaussian) if self.crossover == "uniform": # Uniform means not regarding the location genome.crossover.set(Crossovers.G2DListCrossoverUniform) elif self.crossover == "type": # keep the same type of arguments together genome.crossover.set(Crossovers.G2DListCrossoverSingleHPoint) elif self.crossover == "node": # keep parameter of the same node together genome.crossover.set(Crossovers.G2DListCrossoverSingleVPoint) else: raise NotImplementedError ga = GSimpleGA.GSimpleGA(genome) if self.maximization: ga.setMinimax(Consts.minimaxType["maximize"]) else: ga.setMinimax(Consts.minimaxType["minimize"]) if self.selector == "tournament": ga.selector.set(Selectors.GTournamentSelector) elif self.selector == "roulette": ga.selector.set(Selectors.GRouletteWheel) else: raise NotImplementedError ga.setCrossoverRate(self.crossoverRate) ga.setMutationRate(self.mutationRate) ga.setPopulationSize(self.populationSize) ga.setGenerations(self.noGenerations) sqlite_adapter = DBAdapters.DBSQLite( dbname=self.databaseName, identify="default") # save statistics csv_adapter = DBAdapters.DBFileCSV( filename=self.csvName, identify="default") # save statistics ga.setDBAdapter(sqlite_adapter) #ga.setDBAdapter(csv_adapter) pop = ga.getPopulation() if self.scaling == "sigma": pop.scaleMethod.set(Scaling.SigmaTruncScaling) elif self.scaling == "exponential": pop.scaleMethod.set(Scaling.ExponentialScaling) elif self.scaling == "rank": pop.scaleMethod.set(GeneticTraining.rankScaling) # change Class elif self.scaling == "linear": pop.scaleMethod.set(GeneticTraining.linearScaling) # change Class else: raise NotImplementedError ga.setElitism(True) ga.setElitismReplacement(2) t_init = time.time() ga.evolve() t_tot = time.time() - t_init best = ga.bestIndividual() self.data = self.fetchData() #self.data = self.fetchCSVData() return best.genomeList, best.getRawScore(), t_tot
def eval_func(chromosome): i = np.array(chromosome.genomeList)[np.newaxis, :, :] return float(np.squeeze(n.run(i, channel, layer))) genome = G2DList.G2DList(cfg.layerTestSizes[layer], cfg.layerTestSizes[layer]) genome.setParams(rangemin=0, rangemax=1, gauss_mu=0, gauss_sigma=1) genome.evaluator.set(eval_func) genome.crossover.set(Crossovers.G2DListCrossoverUniform) genome.mutator.set(Mutators.G2DListMutatorRealGaussian) ga = GSimpleGA.GSimpleGA(genome) # ga.selector.set(Selectors.GRouletteWheel) ga.setPopulationSize(NPop) ga.setGenerations(NGen) csvfile_adapter = DBAdapters.DBFileCSV(filename="%s/%i_%i.csv" % (outDir, layer, channel), frequency=NGen / 10) ga.setDBAdapter(csvfile_adapter) # sqlite_adapter = DBAdapters.DBSQLite(identify="%i_%i" % (layer, channel),frequency=NGen/10) # ga.setDBAdapter(sqlite_adapter) ga.evolve(freq_stats=NGen / 10) bi = ga.bestIndividual() pl.imsave('%s/ga_%i.png' % (outDir, NGen), np.array(bi.genomeList), cmap=pl.cm.gray)