def findSol(self, timewindow): pyevolve.logEnable() genome = G1DList.G1DList(4) # range is constrained by the solutions found by MR genome.setParams(rangemin=-12.0, rangemax=2.0) # Change the initializator to Real Values genome.initializator.set(Initializators.G1DListInitializatorReal) # Change the mutator to Gaussian genome.mutator.set(Mutators.G1DListMutatorRealGaussian) # The evaluator function (objective function) genome.evaluator.set(self.eval_func(self.features)) genome.crossover.set(Crossovers.G1DListCrossoverTwoPoint) # 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) # set default parameters for the engine ga.setGenerations(100) #ga.setPopulationSize(80) #ga.setMutationRate(0.2) #ga.setCrossoverRate(0.8) ga.setMinimax(Consts.minimaxType["minimize"]) ga.setElitism(True) ga.terminationCriteria.set(GSimpleGA.ConvergenceCriteria) # Sets the DB Adapter, the resetDB flag will make the Adapter recreate # the database and erase all data every run, you should use this flag # just in the first time, after the pyevolve.db was created, you can # omit it. #sqlite_adapter = DBAdapters.DBSQLite(identify="ex1", resetDB=True) dbPath = createPath('pyevolve.db') sqlite_adapter = DBAdapters.DBSQLite(dbname=dbPath, identify="timewindow" + str(timewindow), resetIdentify=True, resetDB=False) ga.setDBAdapter(sqlite_adapter) # Do the evolution, with stats dump frequency of 20 generations ga.evolve(freq_stats=20) # Best individual best = ga.bestIndividual() stats = { 'constant': best[0], 'EMA': best[1], 'RSI': best[2], 'MACD': best[3] } return stats
def findSol(self, timewindow): pyevolve.logEnable() genome = G1DList.G1DList(4) # range is constrained by the solutions found by MR genome.setParams(rangemin=-12.0, rangemax=2.0) # Change the initializator to Real Values genome.initializator.set(Initializators.G1DListInitializatorReal) # Change the mutator to Gaussian genome.mutator.set(Mutators.G1DListMutatorRealGaussian) # The evaluator function (objective function) genome.evaluator.set(self.eval_func(self.features)) genome.crossover.set(Crossovers.G1DListCrossoverTwoPoint) # 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) # set default parameters for the engine ga.setGenerations(100) #ga.setPopulationSize(80) #ga.setMutationRate(0.2) #ga.setCrossoverRate(0.8) ga.setMinimax(Consts.minimaxType["minimize"]) ga.setElitism(True) ga.terminationCriteria.set(GSimpleGA.ConvergenceCriteria) # Sets the DB Adapter, the resetDB flag will make the Adapter recreate # the database and erase all data every run, you should use this flag # just in the first time, after the pyevolve.db was created, you can # omit it. #sqlite_adapter = DBAdapters.DBSQLite(identify="ex1", resetDB=True) dbPath = createPath('pyevolve.db') sqlite_adapter = DBAdapters.DBSQLite(dbname=dbPath, identify="timewindow" + str(timewindow), resetIdentify=True, resetDB=False) ga.setDBAdapter(sqlite_adapter) # Do the evolution, with stats dump frequency of 20 generations ga.evolve(freq_stats=20) # Best individual best = ga.bestIndividual() stats = {'constant': best[0], 'EMA': best[1], 'RSI': best[2], 'MACD': best[3] } return stats
from pyevolve import DBAdapters import pyevolve from math import sin # This function is the evaluation function, we want # to give high score to more zero'ed chromosomes def eval_func(chromosome): for xval in chromosome: score = xval * sin(31.4*xval) + 1 return score # Enable the pyevolve logging system pyevolve.logEnable() # Genome instance, 1D List of 50 elements genome = G1DList.G1DList(50) # Sets the range max and min of the 1D List genome.setParams(rangemin=-1, rangemax=2) # The evaluator function (evaluation function) genome.evaluator.set(eval_func) # Genetic Algorithm Instance ga = GSimpleGA.GSimpleGA(genome) #Change scaling method pop = ga.getPopulation() pop.scaleMethod.set(Scaling.SaturatedScaling)
] # Fitness evaluation function. def eval_func(chromosome): player = Player(chromosome) game = Game([player] + player_list, verbose=False, quiet=True) # Start the game in quiet mode. ranking = game.play_game() score = ranking.index('GA Player') return score * score # Enable the pyevolve logging system pyevolve.logEnable() # Genome instance, 1D List of 7 elements genome = G1DList.G1DList(7) # Sets the range max and min of the 1D List bestScore = len(player_list) * len(player_list) + 1 # run forever genome.setParams(rangemin=0, rangemax=100) # The evaluator function (evaluation function) genome.evaluator.set(eval_func) # Genetic Algorithm Instance ga = GSimpleGA.GSimpleGA(genome) # Set the Roulette Wheel selector method, the number of generations and
def handle(self, *args, **options): # This function is the evaluation function, we want # to give high score to more zero'ed chromosomes r = redis.Redis() evaluator_set = getattr(settings, 'HAIKU_EVALUATORS', HAIKU_EVALUATORS) def eval_func(chromosome): closeness = [] EVALUATORS = [] i = 0 for eval_cls in evaluator_set: print chromosome EVALUATORS.append((eval_cls, float(chromosome[i])/100)) i += 1 try: rated_comments = BaseHaiku.get_concrete_child().objects.rated() except AttributeError: raise CommandError("BaseHaiku child-classes' manager must implement rated/unrated (see django_haikus.models.HaikuManager") result_queue = "haiku:results_" + ''.join([random.choice(string.ascii_uppercase) for i in range(5)]) for c in rated_comments: data = (c.pk, EVALUATORS, result_queue) r.lpush(settings.REDIS_CALCULATE_QUEUE, cPickle.dumps(data)) results = [] while True: results.append(float(r.blpop(result_queue)[1])) if(len(results) == len(rated_comments)): break # `results` contains the *distance* from the target score # i.e. a lower average score is better. # results = [100, 91, 80, 100, ...,] print results average_fitness = sum(results) / len(results) print "Average fitness: %s, scaled: %s" % (average_fitness, 100 - average_fitness) return 100 - average_fitness pyevolve.logEnable() genome = G1DList.G1DList(len(evaluator_set)) # Sets the range max and min of the 1D List genome.setParams(rangemin=0, rangemax=100) # The evaluator function (evaluation function) genome.evaluator.set(eval_func) # 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(5) # ga.terminationCriteria.set(GSimpleGA.ConvergenceCriteria) # Sets the DB Adapter, the resetDB flag will make the Adapter recreate # the database and erase all data every run, you should use this flag # just in the first time, after the pyevolve.db was created, you can # omit it. sqlite_adapter = DBAdapters.DBSQLite(identify="ex1", resetDB=True) ga.setDBAdapter(sqlite_adapter) # Do the evolution, with stats dump # frequency of 20 generations ga.evolve(freq_stats=1) # Best individual print ga.bestIndividual()