-
Notifications
You must be signed in to change notification settings - Fork 2
/
Main.py
84 lines (65 loc) · 2.59 KB
/
Main.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import csv
# Other Modules
import Others.City_Manager as cm
from Others.City import City
from Others.Graph_Plotter import plotProblem, plotSolution
# Algorithms
from Algorithms.Greedy_Algorithm.main import greedyAlgorithm
from Algorithms.Opt2_Algorithm.main import opt2Algorithm
from Algorithms.Simulated_Annealing.main import simulatedAnnealingAlgorithm
from Algorithms.Genetic_Algorithm.main import geneticAlgorithm
from Algorithms.Ant_Colony_Optimization.main import antColonyOptimization
from Algorithms.Black_Hole_Optimization.main import blackHoleOptimization
from Algorithms.JustALook_Algorithm.main import justALookAlgorithm
def loadDataset(datasetName):
cm.clear()
# Creating the Random Problem
if (datasetName[0:6] == 'Random'):
for _ in range(int(datasetName[7:])):
cm.addCity(City())
# Loading the given Dataset
else:
with open('Data/' + datasetName + '.txt') as csv_file:
csv_reader = csv.reader(csv_file, delimiter=' ')
for row in csv_reader:
cm.addCity(City(float(row[0]), float(row[1])))
# Pre-calculating Distances
cm.calculateDistances()
if __name__ == '__main__':
# Put the dataset name here
# Put 'Random-X' for random generation of X-city problem
datasetName = 'Random-50'
loadDataset(datasetName)
# Plotting the Problem
plotProblem()
# Applying Algorithms
# Each algorithm will return 3-Tuple (Solution, Distance, Time Taken)
# Note: For changing algorithm parameters, check their main.py
# Greedy Algorithm
greedy = greedyAlgorithm()
plotSolution(greedy[0])
print(f'Distance: {greedy[1]}, Time: {greedy[2]}')
# 2Opt Algorithm
opt2 = opt2Algorithm()
plotSolution(opt2[0])
print(f'Distance: {opt2[1]}, Time: {opt2[2]}')
# Simulated_Annealing
simulatedAnnealing = simulatedAnnealingAlgorithm()
plotSolution(simulatedAnnealing[0])
print(f'Distance: {simulatedAnnealing[1]}, Time: {simulatedAnnealing[2]}')
# Genetic Algorithm
genetic = geneticAlgorithm()
plotSolution(genetic[0])
print(f'Distance: {genetic[1]}, Time: {genetic[2]}')
# Ant Colony Optimization
antColony = antColonyOptimization()
plotSolution(antColony[0])
print(f'Distance: {antColony[1]}, Time: {antColony[2]}')
# Black Hole Optimization
blackHole = blackHoleOptimization()
plotSolution(blackHole[0])
print(f'Distance: {blackHole[1]}, Time: {blackHole[2]}')
# Just-a-Look Algorithm
justALook = justALookAlgorithm()
plotSolution(justALook[0])
print(f'Distance: {justALook[1]}, Time: {justALook[2]}')