-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
executable file
·67 lines (52 loc) · 2.71 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from pytspsolver.entities import TSProblem
from pytspsolver.experiments import Experiment
from pytspsolver.solvers import *
from pytspsolver.utilities import create_random_problem, get_tsp_lib_problem, Visualizer
import matplotlib
matplotlib.use("TkAgg")
import matplotlib.pyplot as plt
# Create the problems
#problem1 = create_random_problem("TestProb",3)
#problem2 = create_random_problem(4)
#problem3 = create_random_problem("Prob_Sym_100",100)
tsp_prob = get_tsp_lib_problem("gr17.tsp", "notebooks/datasets")
#problem3.visualize(plt)
#symeteric_problems = []
#symeteric_problems.extend([create_random_problem("Problem_Sym_2_"+str(i),i) for i in range(3,40)])
#symeteric_problems.extend([create_random_problem("Problem_Sym_3_"+str(i),i) for i in range(3,130)])
#symeteric_problems.extend([create_random_problem("Problem_Sym_4_"+str(i),i) for i in range(3,130)])
#symeteric_problems.extend([create_random_problem("Problem_Sym_5_"+str(i),i) for i in range(3,130)])
#asymeteric_problems = [create_random_problem("Problem_Asym_"+str(i),i, asymeteric=True) for i in range(3,10)]
# Initialize Solvers
exhaustive_search = ExhaustiveSearch(time_limit=50)
greedy_search = GreedySearch()
genetic_algorithm1 = GeneticAlgorithm(generations=1000, mutation_rate=0.05, population_size=200, elite_size=20, selection_operator='tournament')
genetic_algorithm2 = GeneticAlgorithm(name="GA2", generations=1000, mutation_rate=0.001, population_size=100, elite_size=10, selection_operator='tournament')
genetic_algorithm3 = GeneticAlgorithm(name="GA Roulette", generations=1000, mutation_rate=0.01, population_size=200, elite_size=20, selection_operator='roulette')
# Create Experiment
experiment = Experiment()
experiment.add_problem(tsp_prob)
#experiment.add_problems(symeteric_problems)
#experiment.add_problems(asymeteric_problems)
# Add solvers to use in experiment
#experiment.add_solver(exhaustive_search)
experiment.add_solver(greedy_search)
# experiment.add_solver(genetic_algorithm1)
experiment.add_solver(genetic_algorithm2)
# experiment.add_solver(genetic_algorithm3)
# Run the experiment desired number of times
experiment.run(epoch=1)
if experiment.has_any_invalid_solutions():
print(experiment.get_invalid_solutions())
results = experiment.get_results()
# Set up Visualizer with experiment results
visualizer = Visualizer(results)
# Show visualizations
#visualizer.plot_n_vs_time(plt, greedy_search._name)
#visualizer.plot_n_vs_time(plt, exhaustive_search._name)
# visualizer.n_vs_time_all_table()
visualizer.plot_n_vs_time_all(plt)
visualizer.plot_time_vs_cost_all(plt, "gr17.tsp")
# visualizer.plot_time_vs_cost(plt, genetic_algorithm2._name, 'Prob_Sym_40')
# visualizer.plot_solver_vs_cost(plt, 'Prob_Sym_40')
visualizer.plot_problem_vs_cost_all(plt)