-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
145 lines (116 loc) · 4.86 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import initialization
import fitness
import parentSelection
import survivorSelection
import recombination
import mutation
import distance
import visual
import random
import math
from datetime import datetime
# Comp 3201 - Final Project
# Authors: Mackenzie Peyton, David MacInnis
def main():
distanceDictionary = {}
populationSize = 500
matingPoolSize = int(populationSize * 0.5)
mutRate = 0.2
xOverRate = 0.9
genLimit = 15000
fileName = "TSP_WesternSahara_29.txt"
# Asks user which file they want to load
files = ["TSP_WesternSahara_29.txt", "TSP_Uruguay_734.txt", "TSP_Canada_4663.txt"]
print("Which is the file you want to load?\n[1] "+ files[0] + "\n[2] " + files[1] + "\n[3] " + files[2] + "\n")
fileNum = input("Enter number: ")
type(fileNum)
fileName = files[int(fileNum) - 1]
print("\nLoading:" + fileName + "\n")
startTime = datetime.now()
coordinates = []
current = []
avgFitVsGenArray = [] # for ploting at the end
# Get data from file
with open(fileName, 'r') as file:
for line in file:
line = line.replace("\n","")
current = line.split(" ")
for i in range(len(current)):
current[i] = float(current[i])
coordinates.append(current)
current = []
# Pre Calculations of all possible distances
distanceDictionary = distance.preCal(coordinates)
gen = 0
# Create initial population
population = initialization.randomization(populationSize, coordinates)
# For fitness pass in array and if it is a population
fitnessArray = fitness.preCalSumOfDistance(population, distanceDictionary, True) # Pre Calculation Method
# To be used in MPS
correctedFitnessArray = fitness.fitnessCorrection(fitnessArray)
pastFit = 0
stall = 0
while gen < genLimit:
# Pick parents
parentsIndex = parentSelection.MPS(correctedFitnessArray, matingPoolSize)
# Randomly pair up parents
random.shuffle(parentsIndex)
# Reproduction
offspring = []
offspringFitness = []
# Initialize the counter(i) for the parents in the mating pool
i = 0
# Offspring are generated using selected parents in the mating pool
while len(offspring) < matingPoolSize:
# Recombination
if random.random() < xOverRate:
off1, off2 = recombination.modifiedNPointCrossover(population[parentsIndex[i]].copy(),population[parentsIndex[i+1]].copy())
else:
off1 = population[parentsIndex[i]].copy()
off2 = population[parentsIndex[i+1]].copy()
# Mutation
if random.random() < mutRate:
if stall > 10: # Switch to random swap
off1 = mutation.randomSwap(off1)
else: # Scramble Mutation
off1 = mutation.scrambleMutation(off1)
if random.random() < mutRate:
if stall > 10: # Switch to random swap
off1 = mutation.randomSwap(off1)
else: # Scramble Mutation
off2 = mutation.scrambleMutation(off2)
offspring.append(off1)
offspringFitness.append(fitness.preCalSumOfDistance(off1, distanceDictionary, False)) # Pre Calculation Method
offspring.append(off2)
offspringFitness.append(fitness.preCalSumOfDistance(off2, distanceDictionary, False)) # Pre Calculation Method
# Update counter
i += 2
if pastFit == round(sum(fitnessArray)/len(fitnessArray),2):
stall += 1
else:
pastFit = round(sum(fitnessArray)/len(fitnessArray),2)
stall = 0
# Plot for avg fit vs gen
avgFitVsGenArray.append(round(sum(fitnessArray)/len(fitnessArray),2))
# Population for the next generation
population, fitnessArray = survivorSelection.mu_plus_lambda(population, fitnessArray, offspring, offspringFitness)
# Update the generation counter
gen += 1
print("generation", gen, ": best fitness", min(fitnessArray), "average fitness", round(sum(fitnessArray)/len(fitnessArray),2))
# Evolution ends
# Stop timing of program
print('\nTime elasped: ', datetime.now() - startTime, '\n')
# The final best solution(s)
drawThis = -1
bestSolAppeared = 0
for i in range (0, populationSize):
if fitnessArray[i] == min(fitnessArray):
bestSolAppeared += 1
drawThis = i
print("best solution", min(fitnessArray),"appeared",bestSolAppeared,"time(s)\n")
# Draw solution to screen
visual.drawGraph(population[drawThis], math.floor(min(fitnessArray)), genLimit, fileName, populationSize)
# Draw avg fit vs gen to screen
visual.avgFitVsGen(avgFitVsGenArray, math.floor(min(fitnessArray)), genLimit, fileName, populationSize)
# end of main
main()