def runHillclimber(railroad, trainlining, runs, rerun, algorithm, image): """ Algorithm that makes an adjustment to the initial trainlining and compares\ the quality of the original trainlining to the adjusted version. Then the best\ option is the new trainlining. This continues for the amount of runs. Args: railroad (Class): lays out the connections of the Netherlands or Holland. trainlining (Class): generated solution of an algorithm of a trainlining\ through Holland or the Netherlands. runs (int): amount of iterations chosen for the algorithm to run. rerun (string): defines whether the user wants to rerun the algorithm 100 times. algorithm (string): chosen algorithm (can be all or hillclimber). image (string): defines what image is generated after the algorithm. Returns: Options: score (float): the quality of the trainlining in a value between 0 and 10000. Returns (only when algorithm == "all"): list (list): list of the countList and scoreList Returns (only when rerun == "y"): score (float): final solution of the algorithm. """ countList = [] scoreList = [] sim = False trainlining.addTrajectories(railroad) for i in range(int(runs)): countList.append(i) hillclimber(railroad, trainlining, sim) score = trainlining.calculateScore() scoreList.append(score) if ((i - 1) % 100) == 0 and rerun == "n": print(f"counter: {(i-1)} score: {score}") if rerun == "y" and algorithm == "hillclimber": return score if algorithm == "all": list = [countList, scoreList] return list elif image == "visual": visual.makeCard(railroad, trainlining) elif image == "o": visual.oldVisual(railroad, trainlining) else: visual.makeGraph(countList, scoreList) return score
def runAdvancedHillclimber(railroad, trainlining, runs, rerun, algorithm, image): """ Runs the advancedHillclimber algorithm for (runs) times. At the end it\ generates a visual representation of the results. Args: railroad (Class): lays out the connections of the Netherlands or Holland. trainlining (Class): generated solution of an algorithm of a trainlining\ through Holland or the Netherlands. runs (int): amount of iterations chosen for the algorithm to run. rerun (string): defines whether the user wants to rerun the algorithm 100 times. algorithm (string): chosen algorithm (can be all or hillclimber). image (string): defines what image is generated after the algorithm. Returns (only when algorithm == "all"): list (list): list of the countList and scoreList Returns (only when rerun == "y"): score (float): final solution of the algorithm """ trainlining.addTrajectories(railroad) countList = [] scoreList = [] for i in range(int(runs)): trainlining = advancedHillclimber(railroad, trainlining, False) score = trainlining.calculateScore() if (i % 10) == 0 and rerun == "n": print(f"counter: {i} score: {score}") countList.append(i) scoreList.append(score) if rerun == "n": for trajectory in trainlining.trajectories: print(trajectory.visitedStations) elif rerun == "y": return score if algorithm == "all": list = [countList, scoreList] return list elif image == "graph": visual.makeGraph(countList, scoreList) elif image == "old": visual.oldVisual(railroad, trainlining) else: visual.makeCard(railroad, trainlining)
def simAnnealing(railroad, trainlining, runs, rerun, algorithm, hill, image): """ In this algorithm all changes are given a probability of acceptance based on a softmax of their respective scores. As the algorithm runs the probability that lower scores are accepted decreases, and the probabilities that higher scores are accepted increases. Args: railroad (Class): lays out the connections of the Netherlands or Holland. trainlining (Class): generated solution of an algorithm of a trainlining\ through Holland or the Netherlands. runs (int): amount of iterations chosen for the algorithm to run. rerun (string): defines whether the user wants to rerun the algorithm 100 times. algorithm (string): chosen algorithm (can be all or hillclimber). image (string): defines what image is generated after the algorithm. Returns(only when algorithm == "all"): list (list): list of the countList and scoreList Returns (only when rerun == "y"): highestScore (float): final solution of the algorithm. """ if hill == "a": basic = False else: basic = True T = 1 highestScore = 0 countList = [] scoreList = [] if basic: scoreNames = [ "newScore", "oldScore", "intermediateScore", "extraScore" ] else: scoreNames = [ "startScore", "oldScore", "intermediateScore", "newScore", "extraScore" ] trainlining.addTrajectories(railroad) for i in range(int(runs)): countList.append(i) info = getScores(railroad, trainlining, basic) probabilityScores = calculateSoftmax(info[0], T) winner = chooseTrajectoryChange(probabilityScores, scoreNames) trainlining = changeTrainLining(winner, info[1], basic) T = calculateT(T, runs) score = trainlining.calculateScore() if score > highestScore: highestScore = score bestTrainLining = trainlining scoreList.append(highestScore) if ((i - 1) % 1000) == 0 and rerun == "n": print(f"counter: {(i-1)} score: {score} T = {T}") print( f"highest score: {highestScore} length {trainlining.trackLength}" ) print(len(trainlining.trajectories)) if rerun == "n": for trajectory in bestTrainLining.trajectories: print(trajectory.visitedStations) elif rerun == "y": return highestScore if algorithm == "all": list = [countList, scoreList] return list elif image == "graph": visual.makeGraph(countList, scoreList) elif image == "old": visual.oldVisual(railroad, trainlining) else: visual.makeCard(railroad, trainlining)
def genetic(trainlining, railroad, runs, rerun, algorithm, populationSize, recombinationCoefficient, mutationRate, image): """ Generates a random population of x raillinings of which the scores are calculated. Based on these scores, probabilities for each raillining are generated. The higher the probability the higher the chance that a raillining is chosen. For x times, children are generated out of two raillinings. Out of these parents and children, two are are randomly chosen to 'battle' against each other. The one with the highest score survives. Args: railroad (Class): lays out the connections of the Netherlands or Holland. trainlining (Class): generated solution of an algorithm of a trainlining\ through Holland or the Netherlands. runs (int): amount of iterations chosen for the algorithm to run. rerun (string): defines whether the user wants to rerun the algorithm 100 times. algorithm (string): chosen algorithm (can be all or hillclimber). populationSize (int): the total population size recombinationCoefficient (int): the coefficient of recombination that takes place mutationRate (int): the rate in which mutation takes place image (string): defines what image is generated after the algorithm. Returns (only when algorithm == "all"): list (list): list of the countList and scoreList Returns (only when rerun == "y"): highestScore (float): final solution of the algorithm. """ generations = int(runs) / int(populationSize) population = makePopulation(trainlining, int(populationSize), railroad) highestScore = 0 bestTrainlining = [] countList = [] scoresList = [] for i in range(int(generations)): scores = scorePopulation(trainlining, population) standardizedScores = standardize(scores) probabilityScores = calculateProbabilities(standardizedScores, population) mutatedChildren = [] mutatedChildrenScore = 0 countList.append(i) for j in range(int(populationSize)): number = 2 parents = chooseParents(population, probabilityScores, number) crossoverChild = crossover(parents, recombinationCoefficient) mutatedChild = mutate(crossoverChild, railroad, trainlining, mutationRate) trainlining.trajectories = mutatedChild mutatedChildScore = trainlining.calculateScore() mutatedChildrenScore += mutatedChildScore if mutatedChildScore > highestScore: highestScore = mutatedChildScore bestTrainlining = mutatedChild mutatedChildren.append(mutatedChild) newPopulation = tournament(trainlining, population, mutatedChildren) if (i % 10) == 0 and rerun == "n": print(f"counter: {i} score: {highestScore}") trainlining.trajectories = bestTrainlining trainlining.calculateScore() print(len(trainlining.visitedCriticalConnections)) sum = 0 for individual in newPopulation: trainlining.trajectories = individual score = trainlining.calculateScore() sum += score print(sum/len(newPopulation)) population = newPopulation scoresList.append(highestScore) trainlining.trajectories = bestTrainlining if rerun == "n": for trajectory in trainlining.trajectories: print(trajectory.visitedStations) elif rerun == "y": return highestScore if algorithm == "all": list = [countList, scoresList] return list elif image == "graph": visual.makeGraph(countList, scoresList) elif image == "old": visual.oldVisual(railroad, trainlining) else: visual.makeCard(railroad, trainlining)
def runRandom(railroad, trainlining, runs, rerun, algorithm, image): """ Executes the makeRandomRoute function for the amount of runs and keeps track\ of the solutions and other values. Args: railroad (Class): lays out the connections of the Netherlands or Holland. trainlining (Class): generated solution of an algorithm of a trainlining\ through Holland or the Netherlands. runs (int): amount of iterations chosen for the algorithm to run. rerun (string): defines whether the user wants to rerun the algorithm 100 times. algorithm (string): chosen algorithm (can be all or hillclimber). image (string): defines what image is generated after the algorithm. Returns (only when algorithm == "all"): list (list): list of the countList and scoreList Returns (only when rerun == "y"): highestScore (float): final solution of algorithm """ highestScore = 0 countList = [] scoreList = [] averageList = [] for i in range(int(runs)): trainlining.trajectories = [] countList.append(i) trainlining.addTrajectories(railroad) score = trainlining.calculateScore() averageList.append(score) if score > highestScore: bestTrainLining = trainlining highestScore = score scoreList.append(highestScore) if ((i - 1) % 100) == 0 and rerun == "n": print(f"counter: {(i-1)} score: {highestScore}") sum = 0 for score in averageList: sum += score average = sum / int(runs) if rerun == "n": print(average) if rerun == "y": return highestScore elif rerun == "n": for trajectory in bestTrainLining.trajectories: print(trajectory.visitedStations) print("\n") if algorithm == "all": list = [countList, scoreList] return list elif image == "graph": visual.makeGraph(countList, scoreList) elif image == "old": visual.oldVisual(railroad, bestTrainLining) else: visual.makeCard(railroad, bestTrainLining)
def runGreedy(railroad, trainlining, runs, rerun, algorithm, image): """ Runs the greedyTrajectory function on a randomly chosen trajectory of \ a randomly generated trainlining. Then checks which of the versions of the\ trainlining is best, and continues with the winning solution. Args: railroad (Class): lays out the connections of the Netherlands or Holland. trainlining (Class): generated solution of an algorithm of a trainlining\ through Holland or the Netherlands. runs (int): amount of iterations chosen for the algorithm to run. rerun (string): defines whether the user wants to rerun the algorithm 100 times. algorithm (string): chosen algorithm (can be all or hillclimber). image (string): defines what image is generated after the algorithm. Returns (only when algorithm == "all"): list (list): list of the countList and scoreList Returns (only when rerun == "y"): highestScore (float): final solution """ highestScore = 0 countList = [] scoreList = [] averageList = [] for i in range(int(runs)): trainlining.trajectories = [] for trajectory in range(trainlining.maxTrajectories): trajectory = greedyTrajectory(railroad, trainlining) trainlining.trajectories.append(trajectory) score = trainlining.calculateScore() countList.append(i) scoreList.append(highestScore) averageList.append(score) if score > highestScore: bestTrainLining = trainlining highestScore = score if ((i - 1) % 100) == 0 and rerun == "n": print(f"counter: {(i-1)} score: {highestScore}") if rerun == "n": for trajectory in bestTrainLining.trajectories: print(trajectory.visitedStations) sum = 0 for score in averageList: sum += score average = sum / int(runs) if rerun == "n": print(average) elif rerun == "y": return highestScore if algorithm == "all": list = [countList, scoreList] return list elif image == "visual": visual.makeCard(railroad, bestTrainLining) elif image == "graph": visual.makeGraph(countList, scoreList) elif image == "old": visual.oldVisual(railroad, bestTrainLining)