Ejemplo n.º 1
0
class TrainOptimizer:
    def __init__(self, index, stocks):
        '''
        Args:
            index (Stock)
                Index (FTSE-100) represented as a Stock
            stocks (List[Stock])
                List of stocks containing the assets in the portfolio
        '''
        # pdb.set_trace()
        self.index = index
        self.stocks = stocks

    def train(self, train_interval, validate_interval, test_interval):
        '''
        Args:
            train_interval (list[int]) # E.G. (10, 100)
                integers - specifying the range (STAR, END) of data we want to use to train the Optimizer 
        '''

        # STEPS:
        # 1. Intialize GA parameters
        # 2. Set Training/Prediction intervals
        # 3. Initialize Evaluator
        # 4. Initialize Optimizer
        # 5. Train

        # pdb.set_trace()
        portfolioSize, minWeight, transactionCost, lmbda, predictionInterval, rebalancingPeriod = get_tracker_parameters(
        )  # we set these paramaters as per the PFGA applciaiton
        probCrossover, probReplacement, probMutation, sizeTournament = get_genetics_parameters(
        )

        self.optimizer = GeneticAlgorithm(
            50,
            100,
            self.stocks,
            self.index,
            genetic_params=(probCrossover, probReplacement, probMutation,
                            sizeTournament),
            tracker_params=(portfolioSize, minWeight, rebalancingPeriod,
                            transactionCost, lmbda))
        self.optimizer.run(train_interval, validate_interval, test_interval)

    def best_portfolio(self, datatype='test'):
        return self.optimizer.best_portfolio(datatype)

    def solution(self):
        return self.optimizer.solution()
def main(argv):
    start = time.time()
    parser = argparse.ArgumentParser()

    #Creatuer settings
    parser.add_argument('--im_size', type=int, default=9, metavar='N',\
      help='Size of creature')
    parser.add_argument('--voxel_types', type=int, default=5, metavar='N',\
      help='How many different types of voxels')
    parser.add_argument('--number_neighbors', type=int, default=7, metavar='N',\
      help='Number of neighbors')
    parser.add_argument('--initial_noise', type=int, default=1, metavar='N',\
      help='initial_noise')
    # GA setting
    parser.add_argument('--sigma', type=float, default = 0.03,\
      help='Sigma')
    parser.add_argument('--N', type=int, default = 3,\
      help='N')
    # Voxelyze settings
    parser.add_argument('--simtime',type=int,default=0.55,\
      help='Simulation time in voxelyze')
    parser.add_argument('--initime',type=int,default=0.05,\
      help='Intiation time of simulation in voxelyze')
    parser.add_argument('--fraction',type=float,default=0.9,\
      help='Fraction of the optimal integration step. The lower, the more stable (and slower) the simulation.')
    parser.add_argument('--run_directory', type=str, default='basic_data')
    parser.add_argument('--run_name', type=str, default='Basic')
    #General settings
    parser.add_argument('--popsize', type=int, default =10, \
      help='Population size.') #3000
    parser.add_argument('--generations', type=int, default = 4,\
      help='Generations.') #50000
    parser.add_argument('--threads', type=int, default=4, metavar='N',\
      help='threads')
    parser.add_argument('--optimizer', type=str, default='ga', metavar='N',\
      help='ga')
    parser.add_argument('--recurrent', type=int, default=0, metavar='N',\
      help='0 = not recurrent, 1 = recurrent')
    parser.add_argument('--data_read', type=int, default=0, metavar='N',\
      help='0 = not data read, 1 = dataread')
    parser.add_argument('--cell_sleep', type=int, default=0, metavar='N',\
      help='0 = not sleep, 0 = sleep')
    parser.add_argument('--growth_facter', type=int, default=1, metavar='N',\
      help='0 = not growt facter, 1 = growth facter exist')
    parser.add_argument('--seed', type=int, default=1, metavar='N',\
      help='seed')
    parser.add_argument('--folder', type=str, default='results', metavar='N',\
      help='folder to store results')
    parser.add_argument('--show', type=str, default='none', metavar='N',\
      help='visualize genome')
    parser.add_argument('--expression', type=int, default=0, metavar='N',\
      help='seperate gene expression')
    parser.add_argument('--fig_output_rate', type=int, default=1, metavar='N',\
      help='fig_output_rate')

    args = parser.parse_args()

    # You need to add variables that you want to refer to in evaluation.py.
    settings = {'voxel_types':args.voxel_types, 'im_size':args.im_size, 'optimizer':args.optimizer, \
    'recurrent' : args.recurrent, 'cell_sleep': args.cell_sleep, 'growth_facter':args.growth_facter, 'expression': args.expression, 'seed':args.seed, 'fraction':args.fraction,\
    'simtime':args.simtime, 'initime':args.initime, 'run_directory':args.run_directory, 'run_name':args.run_name, 'data_read':args.data_read,\
    'number_neighbors':args.number_neighbors, 'sigma':args.sigma, 'N':args.N, 'fig_output_rate':args.fig_output_rate, 'initial_noise':args.initial_noise}
    #TODO do not alwaus pass the whole target image

    input_dim = settings['number_neighbors']
    voxel_types = settings['voxel_types']

    if settings['growth_facter']:
        input_dim = 3 * 9 * 2
        voxel_types = settings['voxel_types'] + 1

    if settings['cell_sleep']:
        input_dim = settings['number_neighbors'] + 1
        voxel_types = settings['voxel_types'] + 1

    model = MorphNet(input_dim=input_dim,
                     number_state=voxel_types,
                     recurrent=settings['recurrent'])

    if args.show != 'none':
        x = torch.load(args.show)
        ca_fitness(x, True, "iterations.png")
        exit()

    print("OPTIMIZER ", args.optimizer)

    inputfile_path = args.run_directory + "/voxelyzeFiles/"
    outputfile_path = args.run_directory + "/fitnessFiles/"
    tempfile_path = args.run_directory + "/tempFiles/"
    bestocreatures_path = args.run_directory + "/bestofFiles/"
    time_path = args.run_directory + "/timeFiles/"
    model_path = args.run_directory + "/model_stateFiles/"
    dev_states_path = args.run_directory + "/dev_stateFiles/"
    try:
        if not os.path.exists(inputfile_path):
            os.makedirs(inputfile_path)
    except OSError as err:
        print(err)
    try:
        if not os.path.exists(outputfile_path):
            os.makedirs(outputfile_path)
    except OSError as err:
        print(err)
    try:
        if not os.path.exists(tempfile_path):
            os.makedirs(tempfile_path)
    except OSError as err:
        print(err)
    try:
        if not os.path.exists(bestocreatures_path):
            os.makedirs(bestocreatures_path)
    except OSError as err:
        print(err)
    try:
        if not os.path.exists(time_path):
            os.makedirs(time_path)
    except OSError as err:
        print(err)
    try:
        if not os.path.exists(model_path):
            os.makedirs(model_path)
    except OSError as err:
        print(err)
    try:
        if not os.path.exists(dev_states_path):
            os.makedirs(dev_states_path)
    except OSError as err:
        print(err)
    #0.03 worked for evolving heart
    if args.optimizer == 'ga':
        ga = GeneticAlgorithm(model.get_weights(), run_robot_simulation, population_size=args.popsize, sigma=0.1, \
          num_threads=args.threads, folder=args.folder, settings=settings) #0.05 works okay
        ga.run(args.generations, print_step=1)
    else:
        print('No optimizer specified')

    process_time = time.time() - start
    str_ = str(process_time)
    with open("{0}/timeFiles/process_time.txt".format(args.run_directory),
              'wt') as f:
        f.write(str_)
Ejemplo n.º 3
0
population_size = 100
genome_length = 5

ga = GeneticAlgorithm(fitness_function,
                      pop_size=population_size,
                      genome_length=genome_length,
                      lb=[1, 1, 0, 0, 0],
                      ub=[20, 20, 2, 2, 2])
ga.generate_binary_population()

# No. of pairs of individuals to be picked to mate
ga.number_of_pairs = 4

# Selective pressure from interval [1.0, 2.0]
# the lower value, the less will the fitness play role

ga.selective_pressure = 1.4
ga.mutation_rate = 0.1

ga.run(100)

best_genome, best_fitness = ga.get_best_genome()
print("Best values for x: ", (1 * best_genome))
print("Best value for f(x):", best_fitness)

# population = ga.population
# print(population, " population")

# fitness_vector = ga.get_fitness_vector()
# print(fitness_vector, " fit vec")
Ejemplo n.º 4
0
    def GA_portfolio_optimize(self,
                              selected_strategy=[
                                  "tesmom", "2nearest", "term_structure",
                                  "hedging_pressure"
                              ],
                              target="Mean_Variance",
                              loss_weight=[1, 1, 1]):
        # print(selected_strategy)
        ret = deepcopy(self.strategy_return_forward)
        for test_name_i, backtest_result_i in deepcopy(ret).items():
            for method_name_i, return_i in backtest_result_i.items():
                if method_name_i not in selected_strategy:
                    del (ret[test_name_i][method_name_i])

        for test_name_i in deepcopy(ret).keys():
            if len(ret[test_name_i]) == 0:
                del (ret[test_name_i])

        self.selected_strategy_return_forward = ret

        flag = 0
        for test_name_i, backtest_result_i in ret.items():
            for method_name_i, return_i in backtest_result_i.items():
                return_i = return_i.resample("M").sum()
                if flag == 0:
                    total_ret = pd.DataFrame(
                        return_i.values,
                        index=return_i.index,
                        columns=['{}_{}'.format(test_name_i, method_name_i)])
                    flag = 1
                elif flag == 1:
                    return_i = pd.DataFrame(
                        return_i.values,
                        index=return_i.index,
                        columns=['{}_{}'.format(test_name_i, method_name_i)])
                    total_ret = total_ret.join(return_i, how="outer")
        # print(total_ret)
        total_ret.to_csv('total_ret.csv')

        #上面的所有步骤都是为了将数据转化为合适的形式传入到优化函数中
        flag = 0
        start = time.clock()
        counter = 0
        total_len = len(total_ret) * 12 - 72
        for i in range(1, len(total_ret) + 1):
            s = {}
            counter += min(i, 12)
            ret = total_ret.iloc[max(0, i - 12):i, :]
            x = GeneticAlgorithm(ret)
            if target == 'test_':
                x.run_softmax(target=target, loss_weight=loss_weight)
            else:
                x.run(target)
            _, cleaned_weights = x.best_portfolio("None")
            s[total_ret.index[i - 1]] = cleaned_weights
            # print(s)
            df = pd.DataFrame(s, index=ret.columns)
            print(df)
            if flag == 0:
                final_weights = df
                flag = 1
            else:
                final_weights = final_weights.join(df)

            elapsed = (time.clock() - start)
            total_time = (elapsed / (counter) * (total_len))
            print('Time processed remained : {:.2f}/{:.2f}'.format(
                elapsed, total_time))

        #得到权重配比的dataframe
        final_weights = final_weights.T
        self.weights = final_weights
        return self.weights
Ejemplo n.º 5
0
from GA import GeneticAlgorithm

ga = GeneticAlgorithm()
ga.run()

# pass