Exemple #1
0
    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)
Exemple #2
0
def main():   
    tsp = TSP(5, 50)
    ga = GeneticAlgorithm(tsp.population)
    for i in range(100000):
        ga.generation(0.01)

    
    print ga.best.solution()
Exemple #3
0
def FLNN_GA(X_train,Y_train):
    problem_size = 10
    pop_size = 10*problem_size
    max_gens = 20
    search_space = [[-0.5,0.5] for _ in range(problem_size)]
    crossf = 0.9
    mutationf = 0.005
    
    GA = GeneticAlgorithm(problem_size,pop_size,max_gens,search_space,mutationf,crossf,X_train,Y_train)
    best = GA.search()
    return best[0]
Exemple #4
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()
Exemple #5
0
 def crossover_population(self):
     crossover_result = []
     assert self.population, "Population is empty."
     while len(crossover_result) < self.population_size:
         route_1 = self.get_route_via_tournament()
         route_2 = self.get_route_via_tournament()
         crossed_1, crossed_2 = GeneticAlgorithm.crossover(route_1, route_2)
         if crossed_1.uniques_only():
             crossover_result.append(crossed_1)
         if crossed_2.uniques_only():
             crossover_result.append(crossed_2)
     self.population = crossover_result[:self.population_size]
def main():
    parameters = {"lr": [10**-i*j for i, j in product(range(3, 6), [1, 5])],
                  "dropout": np.arange(0.0, 0.55, .05),
                  "weight_decay": [10**-i*j
                                   for i, j in product(range(4, 6), [1, 5])],
                  "optimizer": [func1, func2],
                  "nb_layers": list(range(2, 21)),
                  "nb_nodes": [2**i for i in range(1, 11)],
                  "batch_size": [2**i for i in range(4, 10)]}
    # 601920
    word = "qwertzu"

#    parameters = [list(letters) for _ in range(len(word))]

    genetic_algo = GeneticAlgorithm(parameters, 40)
    population = genetic_algo.init_population()
    print(population)
    epochs = 100
    final_counter = 0

    for _ in range(epochs):
        counter = 0
        run = True
        while run:
            accuracies = np.zeros((genetic_algo.population_size, ))

            for i, chromosome in enumerate(population):
                accuracies[i] = accuracy(word, chromosome)

                if accuracies[i] == len(word):
                    res = "".join(chromosome)
                    print(f'{res} in {counter} generations')
                    final_counter += counter
                    run = False

            counter += 1
            population = genetic_algo.next_population(accuracies)

    print(f'Mean generation: {final_counter/epochs}')
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_)
Exemple #8
0
        best_iteration_output += ", "

        print(pop_size, "elements")

        # repeat multiple time (passage)
        time_history = []
        iteration_history = []
        best_found = 0
        best_iteration_history = []
        best_time_history = []
        for _ in range(passage):

            if (alg_type == "ga"):
                optimizer_algorithm = GeneticAlgorithm(pop_size,
                                                       child_percentage,
                                                       tournament_size,
                                                       mutation_factor,
                                                       plateau_lengh)
            if (alg_type == "pso"):
                optimizer_algorithm = ParticleSwarmOptimization(
                    pop_size, intertia_weight, cognitive_weight, social_weight,
                    plateau_lengh)

            start_benchmark = time.time()

            best_sample, iteration = optimizer_algorithm.optimize(
                lower_bound=lower_bound_list[bench_pos],
                upper_bound=upper_bound_list[bench_pos],
                function_to_optimize=benchmark_list[bench_pos])

            end_benchmark = time.time()
while running and same < 1000:
    plotter.ion()
    if population is None:
        population = FirstPopulation(points)
    else:
        previous_population = population.get_population()
        population = ChildsPopulation(previous_population)
    population.crossover_population()
    population.mutate_population()
    fittest_route = population.get_fittest_route()
    if best is None:
        best = fittest_route
    elif fittest_route.calculate_distance() < best.calculate_distance():
        best = fittest_route
        plotter.clear()
        plotter.draw(points, best.get_full_route())
        same = 0
        print(Population.number_of_populations, best.calculate_distance())
    else:
        same += 1
        if same > 20:
            GeneticAlgorithm.remove_instersection(best)

    plotter.show()
    plotter.pause()

plotter.draw(points, best.get_full_route())
print(best.get_full_route())
print(best.calculate_distance())
plotter.show()
Exemple #10
0
        x4 = x6 = 0
    if x6 > x4 and x6 > x5:
        x4 = x5 = 0

    if x3 == 0:
        return 0
    else:
        return x1 + (10 - (x2 / x3)) + (x4 * 9) + (x5 * 5) + (x6 * 1)


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)
import time

import os
import zipfile

POPULATION = 500
GENERATIONS = 10

now = str(datetime.datetime.now())
now = now.replace(' ', '-').replace(':', '').split('.')[0]
os.mkdir(os.getcwd() + os.sep + 'results' + os.sep + now)

t1 = time.time()

grid = Grid((10,10), 10)
opt = GeneticAlgorithm(grid, POPULATION)

print 'Generation %s - Population: %s\n' % (opt.generation, len(opt.population))

t2 = time.time()

print 'Initial population calculation time: %0.3f min' % (float(t2 - t1) / 60.0)

for i in range(1, GENERATIONS + 1):
    print '========================================'
    print '\nOn generation %s - Population: %s\n' % (opt.generation, len(opt.population))
    
    opt.evolve()
    fitness_avg = opt.grade()
    avg_repeaters = (sum([len(x[1]) for x in opt.population]) / float(len(opt.population)))
    print 'Score of this population: %s' % fitness_avg
    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
Exemple #13
0
from population import Population
import DQN
import TD3

if RANDOM_SEED:
    np.random.seed(RANDOM_SEED)
    tf.random.set_random_seed(RANDOM_SEED)

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'

if 'CartPole' in ENVIRONMENT.name:
    if 'dqn' in ALGORITHM:
        DQN.train()
    elif 'ga' in ALGORITHM:
        ga = GeneticAlgorithm()
        agents = Population(optimizer=ga)
        agents.evolve(save=True)
    elif 'es' in ALGORITHM:
        es = EvolutionStrategies()
        agents = Population(optimizer=es)
        agents.evolve(save=True)
    else:
        print("Please, check that configurations are set correctly.")

elif 'BipedalWalker' in ENVIRONMENT.name:
    if 'td3' in ALGORITHM:
        agent = TD3.Agent()
        agent.train()
    elif 'ga' in ALGORITHM:
        ga = GeneticAlgorithm()
Exemple #14
0
from GA import GeneticAlgorithm

ga = GeneticAlgorithm()
ga.run()

# pass
Exemple #15
0
    ##
    ##    city17 = City(200, 40)
    ##    tourManager.add_city(city17)
    ##
    ##    city18 = City(20, 20)
    ##    tourManager.add_city(city18)
    ##
    ##    city19 = City(60, 20)
    ##    tourManager.add_city(city19)
    ##
    ##    city20 = City(160, 20)
    ##    tourManager.add_city(city20)

    pop = Population(100)
    pop.initia_pop(tourManager)

    GA = GeneticAlgorithm(pop, 0.9999, 0.015, 5, 100, True)

    #for i in range(100):
    #    GA.evolution()

    #GA.roulette_wheel_selection()

    GA.run_test()

    #GA.run_test()

    #GA.get_best_case()

    #GA.check_pop();
Exemple #16
0
 def mutate_population(self):
     for route in self.population:
         GeneticAlgorithm.mutation(route)