In [1]:
%logstart
%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tspsolver.tsp_generator import TSPGenerator
from tspsolver.ga.simulator import Simulator
from tspsolver.tuning import GeneticAlgorithmParameterEstimation
OUTPUT_TABLE_DIR = "../report/tables/"
OUTPUT_FIG_DIR = "../report/figures/"
In [2]:
NUM_DATASETS = 5 # number of datasets to take the mean fitness over
NUM_POINTS = 50 # number of points to use in each dataset
tuner = GeneticAlgorithmParameterEstimation(NUM_DATASETS, NUM_POINTS)
In [14]:
params = {
"num_epochs": [1000],
"num_elites": [0, 1, 2],
"generator": ["SimplePopulationGenerator"],
"generator_population_size": [40],
"selector": ["TournamentSelection"],
"selector_tournament_size": [10],
"crossover": ["OrderCrossover"],
"crossover_pcross": [0.9],
"mutator": ["InversionMutation"],
"mutator_pmutate": [0.2]
}
In [15]:
elite_results = tuner.perform_grid_search(params)
In [16]:
elite_results
Out[16]:
In [17]:
elite_results[['num_elites', 'fitness']].to_latex(OUTPUT_TABLE_DIR + "elite_fitness.tex")
In [7]:
gen = TSPGenerator(NUM_POINTS)
data = gen.generate()
all_fitness = []
for i, row in elite_results.iterrows():
params = row.to_dict()
sim = Simulator(**params)
sim.evolve(data)
all_fitness.append(sim.get_min_fitness()[::10])
df = pd.DataFrame(np.array(all_fitness))
In [11]:
ax = df.T.plot()
ax.set_xlabel("Epoch")
ax.set_ylabel("Fitness")
plt.savefig(OUTPUT_FIG_DIR + 'elite_convergence.png', bbox_inches='tight')
In [55]:
params = {
"num_epochs": [1000],
"num_elites": [1],
"generator": ["SimplePopulationGenerator", "KNNPopulationGenerator"],
"generator_random_proportion": [0.3, 0.5, 0.6],
"generator_population_size": [40],
"selector": ["TournamentSelection"],
"selector_tournament_size": [10],
"crossover": ["OrderCrossover"],
"crossover_pcross": [0.9],
"mutator": ["InversionMutation"],
"mutator_pmutate": [0.2]
}
In [56]:
NUM_DATASETS = 5 # number of datasets to take the mean fitness over
NUM_POINTS = 100 # number of points to use in each dataset
tuner = GeneticAlgorithmParameterEstimation(NUM_DATASETS, NUM_POINTS)
knn_results = tuner.perform_grid_search(params)
In [57]:
t = knn_results.iloc[[0,3,4,5]]
t['generator_random_proportion'][0] = np.NaN
t.index = np.arange(4)
t[['generator', 'generator_random_proportion', 'fitness']]
Out[57]:
In [58]:
t[['generator', 'generator_random_proportion', 'fitness']].to_latex(OUTPUT_TABLE_DIR + "knn_fitness.tex")
In [59]:
gen = TSPGenerator(NUM_POINTS)
data = gen.generate()
p = knn_results.iloc[[0,3,4,5]]
all_fitness = []
for i, row in p.iterrows():
params = row.to_dict()
sim = Simulator(**params)
sim.evolve(data)
all_fitness.append(sim.get_min_fitness()[::10])
df = pd.DataFrame(np.array(all_fitness))
In [72]:
t['generator-name'] = t[['generator', 'generator_random_proportion']].apply(lambda x: '-'.join([x[0], str(x[1])]), axis=1)
t['generator-name'][0] = t['generator'][0]
t
Out[72]:
In [73]:
df.index = t['generator-name']
ax = df.T.plot()
ax.set_xlabel("Epoch")
ax.set_ylabel("Fitness")
plt.savefig(OUTPUT_FIG_DIR + 'knn_convergence.png', bbox_inches='tight')