def plot_probability_to_all(runTime, individuals, stations, timeTotal, startTimeOneTour, startTimeTwoTour, startTimeOneRetour, startTimeTwoRetour, frequencyOne, frequencyTwo, trainTime, numTrainTour, numTrainRetour): probabilities = [0.001, 0.005, 0.01, 0.05] maxRank = [1.05, 1.1, 1.3, 1.7] replaceNumber = [2, 5, 8, 10] type = "proportional" for i in replaceNumber: plt.figure() for j in probabilities: meetingTrains, passByTour = ge.initialPopulation( stations, individuals, numTrainTour, numTrainRetour) x, y = ge.convergence(runTime, individuals, stations, timeTotal, startTimeOneTour, startTimeTwoTour, startTimeOneRetour, startTimeTwoRetour, frequencyOne, frequencyTwo, trainTime, meetingTrains, passByTour, j, numTrainTour, numTrainRetour, maxRank[0], i, type) plt.plot(x, y, label="probability=" + str(j)) plt.legend() plt.title("Proportional, replaceNumber=" + str(i)) plt.xlabel("Computation time") plt.ylabel("Fitness") plt.savefig("Proportional.replaceNumber" + str(i) + ".pdf") # plt.show() type = "rank" for i in maxRank: for j in replaceNumber: plt.figure() for k in probabilities: meetingTrains, passByTour = ge.initialPopulation( stations, individuals, numTrainTour, numTrainRetour) x, y = ge.convergence(runTime, individuals, stations, timeTotal, startTimeOneTour, startTimeTwoTour, startTimeOneRetour, startTimeTwoRetour, frequencyOne, frequencyTwo, trainTime, meetingTrains, passByTour, k, numTrainTour, numTrainRetour, i, j, type) plt.plot(x, y, label="probability=" + str(k)) plt.legend() plt.title("Rank, max=" + str(i) + ", replaceNumber=" + str(j)) plt.xlabel("Computation time") plt.ylabel("Fitness") plt.savefig("Rank.max" + str(i) + "replaceNumber" + str(j) + ".pdf")
def plot_individuals_analysis(runTime, individuals, stations, timeTotal, startTimeOneTour, startTimeTwoTour, startTimeOneRetour, startTimeTwoRetour, frequencyOne, frequencyTwo, trainTime, numTrainTour, numTrainRetour): probabilities = 0.01 maxRank = 1.1 replaceNumber = 2 individuals = [5, 10, 20, 50, 100] type = "rank" plt.figure() for i in individuals: xi = 0 for j in range(5): meetingTrains, passByTour = ge.initialPopulation( stations, i, numTrainTour, numTrainRetour) x, y = ge.convergence(runTime, i, stations, timeTotal, startTimeOneTour, startTimeTwoTour, startTimeOneRetour, startTimeTwoRetour, frequencyOne, frequencyTwo, trainTime, meetingTrains, passByTour, probabilities, numTrainTour, numTrainRetour, maxRank, int(i * 0.2), type) xi += y xi /= 5 plt.plot(x, y, label="n=" + str(i)) plt.legend() plt.title("Rank, P=0.01, M=1.1, R=0.2") plt.xlabel("Seconds") plt.ylabel("Fitness") plt.savefig("Individuals.pdf")
def plot_max_analysis(runTime, individuals, stations, timeTotal, startTimeOneTour, startTimeTwoTour, startTimeOneRetour, startTimeTwoRetour, frequencyOne, frequencyTwo, trainTime, numTrainTour, numTrainRetour): probabilities = 0.01 maxRank = [1.05, 1.1, 1.2, 1.4, 1.6, 1.8] replaceNumber = 2 type = "rank" plt.figure() for i in maxRank: xi = 0 for j in range(5): meetingTrains, passByTour = ge.initialPopulation( stations, individuals, numTrainTour, numTrainRetour) x, y = ge.convergence(runTime, individuals, stations, timeTotal, startTimeOneTour, startTimeTwoTour, startTimeOneRetour, startTimeTwoRetour, frequencyOne, frequencyTwo, trainTime, meetingTrains, passByTour, probabilities, numTrainTour, numTrainRetour, i, replaceNumber, type) xi += y xi /= 5 plt.plot(x, y, label="M=" + str(i)) plt.legend() plt.title("Rank, P=0.01, R=2, n=10") plt.xlabel("Seconds") plt.ylabel("Fitness") plt.savefig("Max.pdf")
def plot_Prob_analysis(runTime, individuals, stations, timeTotal, startTimeOneTour, startTimeTwoTour, startTimeOneRetour, startTimeTwoRetour, frequencyOne, frequencyTwo, trainTime, numTrainTour, numTrainRetour): probabilities = [0.001, 0.004, 0.007, 0.01, 0.03, 0.05] maxRank = 1.1 replaceNumber = 2 type = "rank" plt.figure() for i in probabilities: xi = 0 for j in range(5): meetingTrains, passByTour = ge.initialPopulation( stations, individuals, numTrainTour, numTrainRetour) x, y = ge.convergence(runTime, individuals, stations, timeTotal, startTimeOneTour, startTimeTwoTour, startTimeOneRetour, startTimeTwoRetour, frequencyOne, frequencyTwo, trainTime, meetingTrains, passByTour, i, numTrainTour, numTrainRetour, maxRank, replaceNumber, type) xi += y xi /= 5 plt.plot(x, y, label="P=" + str(i)) plt.legend() plt.title("Rank, M=1.1, R=2, n=10") plt.xlabel("Seconds") plt.ylabel("Fitness") plt.savefig("Probability.pdf")
def runNormal(stations, startTimeOneTour, startTimeOneRetour, startTimeTwoTour, startTimeTwoRetour, frequencyOne, frequencyTwo, trainTime, numTrainTour, numTrainRetour, trainTypesTour, trainTypesRetour, runTime, individuals, probabilities, maxRank, replace, T, k, timeTotal): meetingTrains, passByTour = ge.initialPopulation(stations, individuals, numTrainTour, numTrainRetour) x1, y1, meetingTrains1, passByTour1 = ge.convergence2( runTime, individuals, stations, timeTotal, startTimeOneTour, startTimeTwoTour, startTimeOneRetour, startTimeTwoRetour, frequencyOne, frequencyTwo, trainTime, meetingTrains, passByTour, probabilities, numTrainTour, numTrainRetour, maxRank, replace, "rank") x2, y2, meetingTrains2, passByTour2 = en.run2( runTime, stations, timeTotal, startTimeOneTour, startTimeTwoTour, startTimeOneRetour, startTimeTwoRetour, frequencyOne, frequencyTwo, trainTime, T, numTrainTour, numTrainRetour, k, 1) plt.figure() plt.plot(x1, y1, label="Genetic algorithm") plt.plot(x2, y2, label="Simulated annealing") plt.xlabel("Computation time, seconds") plt.ylabel("Fitness") plt.title("Convergence plots for normal problem, train run time " + str(timeTotal)) plt.legend() plt.show() tr.runTrains2(stations, timeTotal, startTimeOneTour, startTimeTwoTour, startTimeOneRetour, startTimeTwoRetour, frequencyOne, frequencyTwo, trainTime, passByTour1, numTrainTour, numTrainRetour, trainTypesTour, meetingTrains1, trainTypesRetour) tr.runTrains2(stations, timeTotal, startTimeOneTour, startTimeTwoTour, startTimeOneRetour, startTimeTwoRetour, frequencyOne, frequencyTwo, trainTime, passByTour2, numTrainTour, numTrainRetour, trainTypesTour, meetingTrains2, trainTypesRetour)
def plot_prob2(runTime, individuals, stations, timeTotal, startTimeOneTour, startTimeTwoTour, startTimeOneRetour, startTimeTwoRetour, frequencyOne, frequencyTwo, trainTime, numTrainTour, numTrainRetour): probabilities = 0.01 maxRank = 1.1 replaceNumber = 2 type = "rank" plt.figure() meetingTrains, passByTour = ge.initialPopulation(stations, individuals, numTrainTour, numTrainRetour) x, y = ge.convergence(runTime, individuals, stations, timeTotal, startTimeOneTour, startTimeTwoTour, startTimeOneRetour, startTimeTwoRetour, frequencyOne, frequencyTwo, trainTime, meetingTrains, passByTour, probabilities, numTrainTour, numTrainRetour, maxRank, replaceNumber, type) plt.plot(x, y, label="M=1.1, P=0.01, R=2", c='r') for i in range(4): meetingTrains, passByTour = ge.initialPopulation( stations, individuals, numTrainTour, numTrainRetour) x, y = ge.convergence(runTime, individuals, stations, timeTotal, startTimeOneTour, startTimeTwoTour, startTimeOneRetour, startTimeTwoRetour, frequencyOne, frequencyTwo, trainTime, meetingTrains, passByTour, probabilities, numTrainTour, numTrainRetour, maxRank, replaceNumber, type) plt.plot(x, y, c='r') maxRank = 1.7 type = "rank" meetingTrains, passByTour = ge.initialPopulation(stations, individuals, numTrainTour, numTrainRetour) x, y = ge.convergence(runTime, individuals, stations, timeTotal, startTimeOneTour, startTimeTwoTour, startTimeOneRetour, startTimeTwoRetour, frequencyOne, frequencyTwo, trainTime, meetingTrains, passByTour, probabilities, numTrainTour, numTrainRetour, maxRank, replaceNumber, type) plt.plot(x, y, label="M=1.7, P=0.01, R=2", c='b') for i in range(4): meetingTrains, passByTour = ge.initialPopulation( stations, individuals, numTrainTour, numTrainRetour) x, y = ge.convergence(runTime, individuals, stations, timeTotal, startTimeOneTour, startTimeTwoTour, startTimeOneRetour, startTimeTwoRetour, frequencyOne, frequencyTwo, trainTime, meetingTrains, passByTour, probabilities, numTrainTour, numTrainRetour, maxRank, replaceNumber, type) plt.plot(x, y, c='b') probabilities = 0.05 maxRank = 1.3 replaceNumber = 5 type = "rank" meetingTrains, passByTour = ge.initialPopulation(stations, individuals, numTrainTour, numTrainRetour) x, y = ge.convergence(runTime, individuals, stations, timeTotal, startTimeOneTour, startTimeTwoTour, startTimeOneRetour, startTimeTwoRetour, frequencyOne, frequencyTwo, trainTime, meetingTrains, passByTour, probabilities, numTrainTour, numTrainRetour, maxRank, replaceNumber, type) plt.plot(x, y, label="M=1.3, P=0.05, R=5", c='g') for i in range(4): meetingTrains, passByTour = ge.initialPopulation( stations, individuals, numTrainTour, numTrainRetour) x, y = ge.convergence(runTime, individuals, stations, timeTotal, startTimeOneTour, startTimeTwoTour, startTimeOneRetour, startTimeTwoRetour, frequencyOne, frequencyTwo, trainTime, meetingTrains, passByTour, probabilities, numTrainTour, numTrainRetour, maxRank, replaceNumber, type) plt.plot(x, y, c='g') plt.legend() #plt.title("Rank, probability=0.1, replaceNumber=" + str(i)) plt.xlabel("Seconds") plt.ylabel("Fitness") plt.savefig("Different1")
def runToPerfectOne(runTime, individuals, probabilities, maxRank, replace, T, k, timeTotal): stations = 5 startTimeOneTour = 0 startTimeOneRetour = 1 startTimeTwoTour = 400 startTimeTwoRetour = 17 frequencyOne = 26 frequencyTwo = 26 trainTime = [[3, 8, 8, 3], [4, 7, 15, 4]] numTrainTour, trainTypesTour = tr.calcNumTrain(startTimeOneTour, startTimeTwoTour, frequencyOne, frequencyTwo, timeTotal) numTrainRetour, trainTypesRetour = tr.calcNumTrain(startTimeOneRetour, startTimeTwoRetour, frequencyOne, frequencyTwo, timeTotal) #Perform genetic convergence plot meetingTrains, passByTour = ge.initialPopulation(stations, individuals, numTrainTour, numTrainRetour) x1, y1, meetingTrains1, passByTour1 = ge.convergence2( runTime, individuals, stations, timeTotal, startTimeOneTour, startTimeTwoTour, startTimeOneRetour, startTimeTwoRetour, frequencyOne, frequencyTwo, trainTime, meetingTrains, passByTour, probabilities, numTrainTour, numTrainRetour, maxRank, replace, "rank") #Perform simulated annealing convergence plot x2, y2, meetingTrains2, passByTour2 = en.run2( runTime, stations, timeTotal, startTimeOneTour, startTimeTwoTour, startTimeOneRetour, startTimeTwoRetour, frequencyOne, frequencyTwo, trainTime, T, numTrainTour, numTrainRetour, k, 1) #Assumed best solution passByTour = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] meetingTrains = [[1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] #Fitness of best solution bestFitness = tr.fitness(stations, timeTotal, startTimeOneTour, startTimeTwoTour, startTimeOneRetour, startTimeTwoRetour, frequencyOne, frequencyTwo, trainTime, passByTour, meetingTrains) #Plotting plt.figure() plt.plot(x1, y1, label="Genetic algorithm") plt.plot(x2, y2, label="Simulated annealing") bestList = np.ones(len(x1)) * bestFitness plt.plot(x1, bestList, label="Probably best solution") plt.xlabel("Computation time, seconds") plt.ylabel("Fitness") plt.title("Convergence plots for constructed problem, train run time " + str(timeTotal)) plt.legend() plt.show() #More plotting tr.runTrains2(stations, timeTotal, startTimeOneTour, startTimeTwoTour, startTimeOneRetour, startTimeTwoRetour, frequencyOne, frequencyTwo, trainTime, passByTour1, numTrainTour, numTrainRetour, trainTypesTour, meetingTrains1, trainTypesRetour) tr.runTrains2(stations, timeTotal, startTimeOneTour, startTimeTwoTour, startTimeOneRetour, startTimeTwoRetour, frequencyOne, frequencyTwo, trainTime, passByTour2, numTrainTour, numTrainRetour, trainTypesTour, meetingTrains2, trainTypesRetour) tr.runTrains2(stations, timeTotal, startTimeOneTour, startTimeTwoTour, startTimeOneRetour, startTimeTwoRetour, frequencyOne, frequencyTwo, trainTime, passByTour, numTrainTour, numTrainRetour, trainTypesTour, meetingTrains, trainTypesRetour)