Beispiel #1
0
    "Crossover-Approach": npoint_crossover,
    "Mutation-Aproach": multi_point_mutation,
    "Replacement-Approach": elitism_replacement,
}

log_name = "TSP_Algo_opt"
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))
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')
Beispiel #3
0
    "Tournament-Size": 5,
    "Crossover-Approach": singlepoint_crossover,
    "Mutation-Aproach": single_point_mutation,
    "Replacement-Approach": elitism_replacement
}

log_name = "mp0-8"

number_of_runs = 30

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

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

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

# save the config

# consolidate the runs information
from os import listdir, path, mkdir
from os.path import isfile, join
Beispiel #4
0
# Problem Instance
tsp_problem_instance = TravelSalesmanProblem(decision_variables=dv,
                                             constraints="")

# print(params)

startCum = timer()

results = []

number_of_runs = 1

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

    ga_observer = GeneticAlgorithmObserver(ga)
    ga.register_observer(ga_observer)
    ga.search()
    results.append(ga._fittest.fitness)

print(
    f"{datasets[dataset]},{number_of_runs},{min(results)},{max(results)},{(timer()-startCum)/number_of_runs}"
)
Beispiel #5
0
ts_init_params = {
    "Maximum-Iterations": 20,
    "Stop-Conditions": "Alternative-01",
    "Neighborhood-Size": -1,
    "Memory-Size": -1,
    "Neighborhood-Function": tsp_bitflip_get_neighbors
}
"""
solution = GeneticAlgorithm(
    problem_instance = tsp
)
"""

solution = GeneticAlgorithm(problem_instance=tsp,
                            params=params,
                            init_params=sa_init_params)

import matplotlib.pyplot as plt

fit = []
for i in range(1, 20):
    print(solution.search())
    fit.append(solution.search().fitness)

avg = sum(fit) / len(fit)

plt.plot(range(1, 20), fit)
plt.axhline(y=avg, color='r', linestyle='-', label=str(avg))
plt.legend()
plt.show()
Beispiel #6
0
    "Population-Size": 20,
    "Number-of-Generations": 1000,
    "Crossover-Probability": 0.1,
    "Mutation-Probability": 0.8,
    # operators / approaches
    "Initialization-Approach": initialize_using_hc,
    "Selection-Approach": parent_selection.select,
    "Tournament-Size": 10,
    "Crossover-Approach": cycle_crossover,
    "Mutation-Aproach": inversion_mutation,
    "Replacement-Approach": elitism_replacement,
}


number_of_runs = 5000

startCum = timer()

# Run the same configuration many times
# --------------------------------------------------------------------------------------------------
for run in range(1, number_of_runs + 1):
    start = timer()
    # Genetic Algorithm
    ga = GeneticAlgorithm(problem_instance=tsp_problem_instance, params=params, run=run)
    ga.search()

    if ga._fittest.fitness < 770:
        print(
            f"solution fitness: {ga._fittest.fitness}, representation: {ga._fittest.representation}\n"
        )