Beispiel #1
0
start = timer()

number_of_runs = 1
# Run the same configuration many times
# --------------------------------------------------------------------------------------------------
for run in range(1, number_of_runs + 1):
    # Genetic Algorithm
    ga = GeneticAlgorithm(
        problem_instance=algo_opt_problem_instance,
        params=paramsOpt,
        run=run,
        log_name=log_name,
    )

    ga_observer = GeneticAlgorithmObserver(ga)
    ga.register_observer(ga_observer)
    fit = ga.search()
    ga.save_log()
    best_solutions = ga._population.solutions
    populations = ga.get_populations()

# df = consolidate(log_name)
# plot_performance_chart(df)
print("time:", timer() - start)

print("Fittest: " + str(fit.representation))
for sol in best_solutions:
    print("Last generation individual: " + str(sol.representation))
    print("Last generation fitness: " + str(sol.fitness))

for i, pop in enumerate(populations):
def one_combination():
    """
    Actually runs the algorithm with one set of parameters.
    Names the resume file from parameters of search
    Creates the resume file from all the runs for a set of parameters
    """
    log_base_dir = "./log/"  # Base dir for log of initial runs
    if not path.exists(log_base_dir):
        mkdir(log_base_dir)
    all_dir = "./log_all/"  # Base dir for resume the resume files
    if not path.exists(all_dir):
        mkdir(all_dir)

    log_name = (
        "I-" + str(valid_Init.get(params.get("Initialization-Approach"))) +
        "_S-" + str(valid_Select.get(params.get("Selection-Approach"))) +
        "_C-" + str(valid_Xover.get(params.get("Crossover-Approach"))) +
        "_M-" + str(valid_Mutation.get(params.get("Mutation-Approach"))) +
        "_R-" +
        str(valid_Replacement.get(params.get("Replacement-Approach"))) +
        "_CP-" + str((params.get("Crossover-Probability"))) + "_MP-" + str(
            (params.get("Mutation-Probability"))) + "_PS-" + str(
                (params.get("Population-Size"))) + "_TS-" + str(
                    (params.get("Tournament-Size"))) + "_G-" + str(
                        (params.get("Number-of-Generations"))))
    resume_name = f"{all_dir}{log_name}.xlsx"

    # checks if the same run as already been performed, if so, skips it.
    # -----------------
    log_dir = str(log_base_dir) + str(log_name)
    if not path.exists(log_dir):
        mkdir(log_dir)

    runs_made = [f for f in listdir(all_dir) if isfile(join(all_dir, f))]
    if (str(log_name) + ".xlsx") in runs_made:
        print(f"Run {log_name}.xlsx already made, skipping")
        return

    # Run the same configuration many times (get distribution)
    #--------------------------------------------------------------------------------------------------
    overall_best_solution = None
    number_of_runs = 30
    for run in range(1, number_of_runs + 1):
        # Genetic Algorithm
        ga = GeneticAlgorithm(problem_instance=pip_problem_instance,
                              params=params,
                              run=run,
                              log_name=log_name,
                              log_dir=log_base_dir)

        ga_observer = LocalSearchObserver(ga)
        ga.register_observer(ga_observer)
        ga.search()
        ga.save_log()

        # find the best solution over the runs
        if run == 1:
            overall_best_solution = deepcopy(ga.best_solution)
        else:
            if ga.best_solution.fitness > overall_best_solution.fitness:
                overall_best_solution = deepcopy(ga.best_solution)

        print('overall_best_solution: ', overall_best_solution.representation)
        print('overall_best_solution fitness, sharpe ratio: ',
              overall_best_solution.fitness)
        print(
            'overall_best_solution expected return: ',
            overall_best_solution.exp_return, ', ',
            overall_best_solution.exp_return - overall_best_solution.risk_free,
            'above risk free return')
        print('overall_best_solution risk: ', overall_best_solution.risk)

    # Consolidate the runs
    #--------------------------------------------------------------------------------------------------

    log_files = [f for f in listdir(log_dir) if isfile(join(log_dir, f))]
    print(log_files)

    fitness_runs = []
    columns_name = []
    counter = 0
    generations = []

    for log_name in log_files:
        if log_name.startswith('run_'):
            df = pd.read_excel(log_dir + "/" + log_name)
            fitness_runs.append(list(df.Fitness))
            columns_name.append(log_name.strip(".xslx"))
            counter += 1

            if not generations:
                generations = list(df["Generation"])

    #fitness_sum = [sum(x) for x in zip(*fitness_runs)]

    df = pd.DataFrame(list(zip(*fitness_runs)), columns=columns_name)

    fitness_sd = list(df.std(axis=1))
    fitness_mean = list(df.mean(axis=1))

    #df["Fitness_Sum"] = fitness_sum
    df["Generation"] = generations
    df["Fitness_SD"] = fitness_sd
    df["Fitness_Mean"] = fitness_mean
    df["Fitness_Lower"] = df["Fitness_Mean"] - 1.96 * df["Fitness_SD"] / (
        number_of_runs**0.5)
    df["Fitness_Upper"] = df["Fitness_Mean"] + 1.96 * df["Fitness_SD"] / (
        number_of_runs**0.5)

    #df.to_excel(log_dir + "/all.xlsx", index=False, encoding='utf-8')
    log_name = (
        "I-" + str(valid_Init.get(params.get("Initialization-Approach"))) +
        "_S-" + str(valid_Select.get(params.get("Selection-Approach")))
        +  # this one return None because of .select method
        "_C-" + str(valid_Xover.get(params.get("Crossover-Approach"))) +
        "_M-" + str(valid_Mutation.get(params.get("Mutation-Approach"))) +
        "_R-" +
        str(valid_Replacement.get(params.get("Replacement-Approach"))) +
        "_CP-" + str((params.get("Crossover-Probability"))) + "_MP-" + str(
            (params.get("Mutation-Probability"))) + "_PS-" + str(
                (params.get("Population-Size"))) + "_TS-" + str(
                    (params.get("Tournament-Size"))) + "_G-" + str(
                        (params.get("Number-of-Generations"))))

    # Exporting summary of configuration with best solution
    with pd.ExcelWriter(all_dir + f"{log_name}.xlsx") as writer:
        df.to_excel(writer,
                    sheet_name='Fitness',
                    index=False,
                    encoding='utf-8')
        pd.DataFrame([[list(overall_best_solution.representation), overall_best_solution.fitness,
                       overall_best_solution.exp_return, overall_best_solution.risk,
                       (overall_best_solution.exp_return - overall_best_solution.risk_free)]],
                     columns=["Representation", "Fitness, Sharpe Ratio", "Expected Return", "Risk", "Above Risk Free"]).\
            to_excel(writer, sheet_name='Overall_Best_Solution')