def __init__(self, backtest_filters, lcbt, args): """ :param backtest_filters: :param lcbt: :param args: """ self.iteration = 0 self.iteration_time = 0 self.backtest_filters = backtest_filters self.lcbt = lcbt self.args = args for _ in range(self.args.population_size): filters = [] mate_filters = [] for backtest_filter in backtest_filters.values(): lc_filter = backtest_filter(args) lc_filter.set_current(random.randint(0, lc_filter.get_count() - 1)) filters.append(lc_filter) mate_filter = backtest_filter(args) mate_filters.append(mate_filter) self.population.append(dict(filters=filters)) self.mate_population.append(dict(filters=mate_filters)) csv_field_names = [] for lc_filter in self.population[0]["filters"]: csv_field_names.append(lc_filter.get_name()) csv_field_names.extend([ 'expected_apy', 'num_loans', 'num_defaulted', 'pct_defaulted', 'avg_default_loss', 'net_apy' ]) self.csvwriter = csv.DictWriter(utilities.Unbuffered(open(self.args.csvresults, 'w')), fieldnames=csv_field_names, extrasaction='ignore') self.csvwriter.writeheader()
response_queue = MultiProcessingQueue() for worker_idx in range(args.workers): mp_worker(worker_idx, args, work_queue, response_queue) ga_test = ParallelGATest(BackTestFilters, lcbt, args, work_queue, response_queue) ga_test.run() else: lcbt = LCBT(ConversionFilters, args, worker_idx=-1) lcbt.initialize() ga_test = GATest(BackTestFilters, lcbt, args) ga_test.run() return 0 if __name__ == "__main__": sys.stdout = utilities.Unbuffered(sys.stdout) if DEBUG: sys.argv.append("-v") if TESTRUN: import doctest doctest.testmod() if PROFILE: import cProfile import pstats profile_filename = 'lcbt_profile.prof' cProfile.run('main()', profile_filename) with open("profile_stats.txt", "wb") as statsfile: p = pstats.Stats(profile_filename, stream=statsfile) stats = p.strip_dirs().sort_stats('cumulative')