def _make_alleles(self): """ Returns a GAllelle.Galleles instance with alleles corresponding to the parameters specified by the user""" alleles = GAllele.GAlleles() count = 0 for param in self.get_parameters().values(): allele = None count += 1 val = param.evaluate() #now grab the value low = param.low high = param.high metadata = param.get_metadata()[0][1] #then it's a float or an int, or a member of an array if ('low' in metadata or 'high' in metadata) or array_test.search(param.targets[0]): if isinstance(val, real_types): #some kind of float allele = GAllele.GAlleleRange(begin=low, end=high, real=True) #some kind of int if isinstance(val, int_types): allele = GAllele.GAlleleRange(begin=low, end=high, real=False) elif "values" in metadata and isinstance(metadata['values'], iterable_types): allele = GAllele.GAlleleList(metadata['values']) if allele: alleles.add(allele) else: self.raise_exception("%s is not a float, int, or enumerated \ datatype. Only these 3 types are allowed"%(param.targets[0]),ValueError) self.count = count return alleles
def test_createAlleleRange(self): _allelerange = GAllele.GAlleleRange(10, 20) self.assertEqual(_allelerange.beginEnd, [(10, 20)]) self.assertEqual(_allelerange.minimum, 10) self.assertEqual(_allelerange.maximum, 20) _allelerange = GAllele.GAlleleRange(1.0, 2.0, real=True) self.assertEqual(_allelerange.real, True)
def run_ga(fast): """Initialize and run the genetic algorithms.""" alleles = GAllele.GAlleles() # create list of all alleles alleles.add(GAllele.GAlleleRange(1, 1000)) # range for the number of days alleles.add(GAllele.GAlleleRange(1, 20)) # range for hidden nodes alleles.add(GAllele.GAlleleRange(1, 10)) # number of epochs alleles.add(GAllele.GAlleleList([True, False])) # bias alleles.add( GAllele.GAlleleList([float(x * Decimal("0.1")) for x in range(0, 10)])) # momentum alleles.add(GAllele.GAlleleList([TanhLayer, SigmoidLayer])) # tanh/sigmoid genome = G1DList.G1DList(len(alleles)) genome.setParams(allele=alleles, fast=fast) genome.evaluator.set(eval_func) genome.mutator.set(Mutators.G1DListMutatorAllele) genome.initializator.set(Initializators.G1DListInitializatorAllele) gen_alg = GSimpleGA.GSimpleGA(genome) gen_alg.setMultiProcessing(not fast) gen_alg.selector.set(Selectors.GRouletteWheel) gen_alg.setGenerations(20) gen_alg.evolve(freq_stats=0) return gen_alg.bestIndividual()
def test_AlleleRange_slicing(self): _allelerange = GAllele.GAlleleRange(10, 20) _allelerange.add(30, 40) self.assertEqual(_allelerange[0], (10, 20)) _allelerange[1] = (50, 60) self.assertEqual(_allelerange[1], (50, 60)) with self.assertRaises(ValueError): _allelerange[1] = (60, 50)
def test_AlleleRange_add(self): _allelerange = GAllele.GAlleleRange(10, 20) _allelerange.add(30, 40) self.assertEqual(_allelerange.beginEnd, [(10, 20), (30, 40)]) self.assertEqual(_allelerange.minimum, 10) self.assertEqual(_allelerange.maximum, 40) with self.assertRaises(ValueError): _allelerange.add(40, 30)
def run_simulation(pset, max_generations, initial_list=None): # Genome instance setOfAlleles = GAllele.GAlleles() setOfAlleles.add(GAllele.GAlleleRange(0, 51)) setOfAlleles.add(GAllele.GAlleleRange(0, 6)) setOfAlleles.add(GAllele.GAlleleRange(0, 51)) # Genome instance, 1D List of 50 elements genome = G1DList.G1DList(3) genome.setParams(allele=setOfAlleles) #Fitness function fe = FitnessEvaluator(pset.fitness_fnc, pset.fitness_temp) Consts.CDefScaleLinearMultiplier = pset.fitness_temp application = "OS" if "oxide" in pset.fitness_fnc.__name__ else "LS" st = StatTrack(fe, pset.mutation_rate, pset.tournament_rate, include_ridiculous=pset.include_ridiculous, application=application) genome.crossover.set(pset.crossover_fnc) genome.mutator.set(pset.mutation_fnc) genome.evaluator.set(fe.array_to_score) genome.initializator.set(pset.initialization_fnc) ga = GSimpleGA.GSimpleGA(genome) ga.selector.set(pset.selection_fnc) ga.terminationCriteria.set(AllFoundCriteria) ga.stepCallback.set(st.evolve_callback) ga.setPopulationSize(pset.popsize) ga.setGenerations(max_generations) if pset.elitism_num > 0: ga.setElitism(True) ga.setElitismReplacement((int)(math.ceil(pset.elitism_num * pset.popsize))) else: ga.setElitism(False) ga.setMutationRate(pset.mutation_rate) # TODO: figure out niching stats_freq = 0 ga.evolve(freq_stats=stats_freq) return st
def test_AlleleRange_getRandomAllele(self): _allelerange = GAllele.GAlleleRange(10, 20) random_allele = _allelerange.getRandomAllele() self.assertTrue( random_allele, any([x[0] <= random_allele <= x[1] for x in _allelerange.beginEnd])) _allelerange.add(30, 40) random_allele = _allelerange.getRandomAllele() self.assertTrue( random_allele, any([x[0] <= random_allele <= x[1] for x in _allelerange.beginEnd])) _allelerange = GAllele.GAlleleRange(1.0, 2.0, real=True) random_allele = _allelerange.getRandomAllele() self.assertTrue( random_allele, any([x[0] <= random_allele <= x[1] for x in _allelerange.beginEnd]))
def run_main(): # Genome instance setOfAlleles = GAllele.GAlleles() # From 0 to 10 we can have only some # defined ranges of integers for i in xrange(11): a = GAllele.GAlleleRange(0, i) setOfAlleles.add(a) # From 11 to 19 we can have a set # of elements for i in xrange(11, 20): # You can even add objects instead of strings or # primitive values a = GAllele.GAlleleList(['a', 'b', 'xxx', 666, 0]) setOfAlleles.add(a) print setOfAlleles genome = G1DList.G1DList(20) genome.setParams(allele=setOfAlleles) # The evaluator function (objective function) genome.evaluator.set(eval_func) # This mutator and initializator will take care of # initializing valid individuals based on the allele set # that we have defined before genome.mutator.set(Mutators.G1DListMutatorAllele) genome.initializator.set(Initializators.G1DListInitializatorAllele) # Genetic Algorithm Instance ga = GSimpleGA.GSimpleGA(genome) ga.setGenerations(40) # Do the evolution, with stats dump # frequency of 10 generations ga.evolve(freq_stats=5) # Best individual print ga.bestIndividual()
def run(self): # Allele define valid chromosome value alleles = GAllele.GAlleles() # Define gene with 2 chromosomes # MA type alleles.add(GAllele.GAlleleList([0, 1, 2, 3, 4])) # MA range alleles.add(GAllele.GAlleleRange(1, 99)) # Genome instance, 1D List genome = G1DList.G1DList(len(alleles)) # Sets the range max and min of the 1D List genome.setParams(allele=alleles) # The evaluator function (evaluation function) genome.evaluator.set(self.fitness) # This mutator and initializator will take care of # initializing valid individuals based on the allele set # that we have defined before genome.mutator.set(Mutators.G1DListMutatorAllele) genome.initializator.set(Initializators.G1DListInitializatorAllele) # Genetic Algorithm Instance ga = GSimpleGA.GSimpleGA(genome) # Set the Roulette Wheel selector method, the number of generations and # the termination criteria ga.selector.set(Selectors.GRouletteWheel) ga.setGenerations(self.gen) ga.setPopulationSize(self.pop) ga.terminationCriteria.set(GSimpleGA.ConvergenceCriteria) pop = ga.getPopulation() pop.scaleMethod.set(Scaling.SigmaTruncScaling) ga.evolve(freq_stats=10) # Best individual self.best = ga.bestIndividual()
eta = chromo[3] rho = chromo[4] diffs = np.zeros(len(strikes) * len(terms)) n = 0 for i, k in enumerate(strikes): for j, t in enumerate(terms): diffs[n] = vanilla[i, j] - _heston.ucall(futs[j], k, t, v, vbar, lambd, eta, rho) n += 1 score = norm(diffs) return score # Genome instance setOfAlleles = GAllele.GAlleles() setOfAlleles.add(GAllele.GAlleleRange(0.0, 5.0, True)) setOfAlleles.add(GAllele.GAlleleRange(0.0, 5.0, True)) setOfAlleles.add(GAllele.GAlleleRange(5.0, 15.0, True)) setOfAlleles.add(GAllele.GAlleleRange(0.0, 5.0, True)) setOfAlleles.add(GAllele.GAlleleRange(-1, 1, True)) genome = G1DList.G1DList(5) genome.setParams(allele=setOfAlleles) genome.evaluator.set(heston_evaluate) genome.mutator.set(Mutators.G1DListMutatorAllele) genome.initializator.set(Initializators.G1DListInitializatorAllele) # Genetic Algorithm Instance ga = GSimpleGA.GSimpleGA(genome) # ga.setGenerations(100) # ga.setPopulationSize(100)
def Grid_Constructor(numline, numParm, data): alleles = GAllele.GAlleles() for i in range(0, numline * numParm): alleles.add(GAllele.GAlleleRange(data[i][0], data[i][1])) return alleles
def test_AlleleRange_repr(self): _allelerange = GAllele.GAlleleRange(10, 20) self.assertIsInstance(repr(_allelerange), str)
def run_ga(): global ga #___________________Genome instance # setOfAlleles = GAllele.GAlleleList() pars_min = velocity_min + depth_min + vpvs_min pars_max = velocity_max + depth_max + vpvs_max num_pars = len(pars_min) for (vmin, vmax) in zip(pars_min, pars_max): tmp = GAllele.GAlleleRange(vmin, vmax, real=True) setOfAlleles.add(tmp) genome = G1DList.G1DList(num_pars) genome.setParams(allele=setOfAlleles) genome.initializator.set(Initializators.G1DListInitializatorAllele) genome.mutator.set(Mutators.G1DListMutatorAllele) genome.crossover.set(Crossovers.G1DListCrossoverUniform) #___________________The evaluator function (objective function) # genome.evaluator.set(eval_func) #___________________Genetic Algorithm Instance # ga = GSimpleGA.GSimpleGA(genome, seed=int(seed)) if num_cpu: ga.setMultiProcessing(True, True, int(num_cpu)) if ga_selector == 'T': ga.selector.set(Selectors.GTournamentSelector) if ga_selector == 'R': ga.selector.set(Selectors.GRouletteWheel) if ga_selector == 'N': ga.selector.set(Selectors.GRankSelector) if ga_selector == 'M': ga.selector.setRandomApply(True) ga.selector.set(Selectors.GTournamentSelector, 0.75) ga.selector.add(Selectors.GRouletteWheel, 0.20) ga.selector.add(Selectors.GRankSelector) ga.setMinimax(Consts.minimaxType["minimize"]) ga.setGenerations(int(generationSize)) ga.setPopulationSize(int(populationSize)) ga.setCrossoverRate(pCrossover) ga.setMutationRate(pMutation) ga.setElitism(True) ga.setElitismReplacement(int(num_etlsm)) #___________________Sets the DB Adapter # sqlite_adapter = DBAdapters.DBSQLite(identify=dbase_name, resetDB=eval(resetDB)) ga.setDBAdapter(sqlite_adapter) #___________________Do the evolution # ga.evolve(freq_stats=5) #___________________Print Best individual # best = ga.bestIndividual() best_rs = best.getRawScore() best_v = best.genomeList[:len(velocity_min)] best_d = best.genomeList[len(velocity_min):2 * len(velocity_min)] best_r = best.genomeList[2 * len(velocity_min):] print '' print '+++ Best Raw Score =', best_rs print '+++ FinalModel :' print ' +++ Velocity :', rnd(best_v, 2) print ' +++ Depth :', rnd(best_d, 2) print ' +++ VpVs :', rnd(best_r, 2) return best, best_rs, best_v, best_d, best_r
def test_AlleleRange_clear(self): _allelerange = GAllele.GAlleleRange(10, 20) _allelerange.clear() self.assertEqual(_allelerange.beginEnd, [])
def test_AlleleRange_getMinimum(self): _allelerange = GAllele.GAlleleRange(10, 20) self.assertEqual(_allelerange.getMaximum(), 20)
def test_AlleleRange_iter(self): _allelerange = GAllele.GAlleleRange(10, 20) self.assertIsInstance(iter(_allelerange), Iterable)
def test_AlleleRange_real(self): _allelerange = GAllele.GAlleleRange(10, 20) self.assertEqual(_allelerange.getReal(), False) _allelerange.setReal(flag=True) self.assertEqual(_allelerange.getReal(), True)
def test_AlleleRange_len(self): _allelerange = GAllele.GAlleleRange(10, 20) _allelerange.add(30, 40) self.assertEqual(len(_allelerange), 2)
keys = init_cutpoints.keys() i = 0 for key in keys: dict_[key] = chromosome[i:i+3] i += 3 return dict_ chromosome_ = convert_to_list(init_cutpoints) setOfAlleles = GAllele.GAlleles() keys = init_cutpoints.keys() for key in keys: for _ in range(0, 3): if key != 'cip': a = GAllele.GAlleleRange(int(df[key].min()), int(df[key].max())) setOfAlleles.add(a) else: a = GAllele.GAlleleRange(0, 10) setOfAlleles.add(a) df_actual = pd.read_csv("data/Cust_Actual.csv", index_col=['index']) genome = G1DList.G1DList(len(chromosome_)) genome.setParams(allele=setOfAlleles) def check_sorted(chromosome): i = 0 for _ in range(5): original = chromosome[i:i+3] sorted_ = sorted(original)
def run_main(): # Global runner instance that will also be used by eval_func global runner global log # "Settings" working_dir = '../work' log_dir = '../log' compare_values = '../ext/Detalierte Mechanismus.csv' log = Logger() log.setLogLevel('debug') # load parameter format defines from parameters.py import parameters pformat = parameters.parameter_format # load KivaRunner l = Logger() l.setLogLevel('info') runner = KivaRunner(working_dir, log_dir, compare_values, pformat, l) if runner.error: return # Genome instance setOfAlleles = GAllele.GAlleles() # loop trough parameter format to create correct alleles for p in pformat: minimum = p[2] maximum = p[3] # log.debug("maximum: %s, minimum: %s" % (maximum, minimum)) a = GAllele.GAlleleRange(minimum, maximum, not isinstance(minimum, int)) setOfAlleles.add(a) genome = G1DList.G1DList(len(pformat)) genome.setParams(allele=setOfAlleles) # The evaluator function (objective function) genome.evaluator.set(eval_func) # This mutator and initializator will take care of # initializing valid individuals based on the allele set # that we have defined before genome.mutator.set(Mutators.G1DListMutatorAllele) # genome.initializator.set(G1DListInitializatorAlleleKiva) genome.initializator.set(Initializators.G1DListInitializatorAllele) # Genetic Algorithm Instance ga = GSimpleGA.GSimpleGA(genome) ga.setGenerations(10) ga.setPopulationSize(20) ga.setMinimax(Consts.minimaxType["minimize"]) # ga.setCrossoverRate(1.0) ga.setMutationRate(0.1) ga.selector.set(Selectors.GRouletteWheel) sqlite_adapter = DBAdapters.DBSQLite(identify="ex1", resetDB=True) ga.setDBAdapter(sqlite_adapter) # Do the evolution, with stats dump # frequency of 10 generations ga.evolve(freq_stats=1) # Best individual best = ga.bestIndividual() print "\nBest individual score: %.2f" % (best.getRawScore(), ) print best