def print_best_genome(ga_engine): best = ga_engine.bestIndividual() gen = ga_engine.getCurrentGeneration() raw_fitn = best.getRawScore() print "Best genome at generation %d had fitness %f (raw %f), error %f." % \ (gen, best.getFitnessScore(), raw_fitn, _fitness_to_error(raw_fitn)) print "Best genome at generation %d:" % gen, pu.raw_genes(best, True) sys.stdout.flush()
def _set_indiv(self, indiv): self.indiv = indiv self.model = indiv.getParam('model') self.day = self.model.day self.loci = self.model.loci self.genome = pu.raw_genes(indiv, True) train_iter = self.model.dataset.train_data_iterator() train_data = [t for t in train_iter()] (self.data_in, self.data_out) = random.choice(train_data)
def _fitness(indiv): """Generic fitness function, calls preprocessors, transformer and postprocessors for each model, calculates error from error function.""" global _model model = _model num_trials = indiv.getParam('num_trials') genome = pu.raw_genes(indiv, True) loci = model.loci def test_one(data_in, data_out): key = _make_cache_key(genome, data_in, data_out) _cache_mutex.acquire() try: error = _cache[key] except KeyError: _cache_mutex.release() model_out = process_data(data_in, genome, model, loci) assert (np.all(model_out.index == data_out.index)) error = model.error_func(model_out.values, data_out.values) _cache_mutex.acquire() _cache[key] = error _cache_mutex.release() return error def test_loop(): error = 0 #start = time.time() train_iter = model.dataset.train_data_iterator() for (data_in, data_out) in train_iter(): error += test_one(data_in, data_out) #print "Testing completed in ", time.time() - start #sys.stdout.flush() return error global _catch_exceptions_during_fitness_evaluation if _catch_exceptions_during_fitness_evaluation: try: error = test_loop() except Exception, e: print >> sys.stderr, "Caught exception during fitness evaluation!" print >> sys.stderr, " Offending genome was:", genome[:] tb = " " + traceback.format_exc(limit=50)[:-1] print >> sys.stderr, tb.replace("\n", "\n ") print >> sys.stderr, " Setting fitness to 0 to avoid selection." return 0
def _fitness(indiv): """Generic fitness function, calls preprocessors, transformer and postprocessors for each model, calculates error from error function.""" global _model model = _model num_trials = indiv.getParam('num_trials') genome = pu.raw_genes(indiv, True) loci = model.loci def test_one(data_in, data_out): key = _make_cache_key(genome, data_in, data_out) _cache_mutex.acquire() try: error = _cache[key] except KeyError: _cache_mutex.release() model_out = process_data(data_in, genome, model, loci) assert(np.all(model_out.index == data_out.index)) error = model.error_func(model_out.values, data_out.values) _cache_mutex.acquire() _cache[key] = error _cache_mutex.release() return error def test_loop(): error = 0 #start = time.time() train_iter = model.dataset.train_data_iterator() for (data_in, data_out) in train_iter(): error += test_one(data_in, data_out) #print "Testing completed in ", time.time() - start #sys.stdout.flush() return error global _catch_exceptions_during_fitness_evaluation if _catch_exceptions_during_fitness_evaluation: try: error = test_loop() except Exception, e: print >>sys.stderr, "Caught exception during fitness evaluation!" print >>sys.stderr, " Offending genome was:", genome[:] tb = " " + traceback.format_exc(limit=50)[:-1] print >>sys.stderr, tb.replace("\n", "\n ") print >>sys.stderr, " Setting fitness to 0 to avoid selection." return 0
def _run_models(models, dataset): data_desc = dataset.desc for model in models: if not is_mpi_slave(options): print "Optimizing parameters for model", model.name, "." run_GA(model, options) if is_mpi_slave(options): continue raw_genes = pu.raw_genes(model.genome, strip=True) print "Best genes found during evolution: ", raw_genes target, predictions = test_genome(raw_genes, model) _save_test_prediction(target, predictions, data_desc) error = concat_and_calc_error(predictions, target, model.error_func) print "Error on test phase for best genome found, " \ "%s, %i days: %5.4f" % (data_desc, len(predictions), error) if not options.no_plot: plot_test_prediction(target, predictions, model.name, data_desc, model.error_func)
def print_population(ga_engine): for indiv in ga_engine.getPopulation(): print "%9e, %9e" % (indiv.score, indiv.fitness), pu.raw_genes( indiv, True)
def print_population(ga_engine): for indiv in ga_engine.getPopulation(): print "%9e, %9e" % (indiv.score, indiv.fitness), pu.raw_genes(indiv, True)