public DiscreteSA4TSP(TSPInstance instance, int initialSolutions, int levelLength, double tempReduction) : base(initialSolutions, levelLength, tempReduction) { Instance = instance; generatedSolutions = 0; }
public DiscreteSS4TSP(TSPInstance instance, int poolSize, int refSetSize, double explorationFactor) : base(poolSize, refSetSize, explorationFactor) { Instance = instance; generatedSolutions = 0; }
// Implementation of the 2-opt (first improvement) local search algorithm. public static void LocalSearch2OptFirst(TSPInstance instance, int[] path) { int tmp; double currentFitness, bestFitness; bestFitness = Fitness(instance, path); for (int j = 1; j < path.Length; j++) { for (int i = 0; i < j; i++) { // Swap the items. tmp = path[j]; path[j] = path[i]; path[i] = tmp; // Evaluate the fitness of this new solution. currentFitness = Fitness(instance, path); if (currentFitness < bestFitness) { return; } // Undo the swap. tmp = path[j]; path[j] = path[i]; path[i] = tmp; } } }
public static int[] GreedySolution(TSPInstance instance) { int[] solution = new int[instance.NumberCities]; bool[] visited = new bool[instance.NumberCities]; for (int i = 0; i < instance.NumberCities; i++) { if (i == 0) { solution[i] = 0; } else { int currentCity = solution[i - 1]; int nextCity; double bestCost = double.MaxValue; for (nextCity = 1; nextCity < instance.NumberCities; nextCity++) { if (!visited[nextCity] && instance.Costs[currentCity, nextCity] < bestCost) { solution[i] = nextCity; bestCost = instance.Costs[currentCity, nextCity]; } } } visited[solution[i]] = true; } return(solution); }
public static int[] GetNeighbor(TSPInstance instance, int[] solution) { int[] neighbor = new int[instance.NumberCities]; int a = Statistics.RandomDiscreteUniform(0, solution.Length - 1); int b = a; while (b == a) { b = Statistics.RandomDiscreteUniform(0, solution.Length - 1); } for (int i = 0; i < solution.Length; i++) { if (i == a) { neighbor[i] = solution[b]; } else if (i == b) { neighbor[i] = solution[a]; } else { neighbor[i] = solution[i]; } } return(neighbor); }
public DiscreteTS4TSP(TSPInstance instance, double rclTreshold, int tabuListLength, int neighborChecks) : base(tabuListLength, neighborChecks) { Instance = instance; RclTreshold = rclTreshold; }
public DiscretePSO4TSP(TSPInstance instance, int partsCount, double prevConf, double neighConf, int[] lowerBounds, int[] upperBounds) : base(partsCount, prevConf, neighConf, lowerBounds, upperBounds) { Instance = instance; generatedSolutions = 0; }
public void Start(string fileInput, string fileOutput, int timeLimit) { TSPInstance instance = new TSPInstance(fileInput); int[] assignment = TSPUtils.GreedySolution(instance); TSPUtils.LocalSearch2OptFirst(instance, assignment); TSPSolution solution = new TSPSolution(instance, assignment); solution.Write(fileOutput); }
public void Start(string inputFile, string outputFile, int timeLimit) { TSPInstance instance = new TSPInstance(inputFile); DiscreteSS ss = new DiscreteSS2OptBest4TSP(instance, poolSize, refSetSize, explorationFactor); ss.Run(timeLimit - timePenalty); TSPSolution solution = new TSPSolution(instance, ss.BestSolution); solution.Write(outputFile); }
public DiscretePSO2OptBest4TSP(TSPInstance instance, int partsCount, double prevConf, double neighConf, int[] lowerBounds, int[] upperBounds) : base(partsCount, prevConf, neighConf, lowerBounds, upperBounds) { Instance = instance; LocalSearchEnabled = true; generatedSolutions = 0; }
public DiscretePSO2OptFirst4TSP(TSPInstance instance, int partsCount, double prevConf, double neighConf, int[] lowerBounds, int[] upperBounds) : base(partsCount, prevConf, neighConf, lowerBounds, upperBounds) { Instance = instance; LocalSearchEnabled = true; generatedSolutions = 0; }
public DiscreteILS2OptFirst4TSP(TSPInstance instance, int restartIterations, int[] lowerBounds, int[] upperBounds) : base(restartIterations, lowerBounds, upperBounds) { Instance = instance; RepairEnabled = true; generatedSolutions = 0; }
public DiscreteMA4TSP(TSPInstance instance, int popSize, double mutationProbability, int[] lowerBounds, int[] upperBounds) : base(popSize, mutationProbability, lowerBounds, upperBounds) { Instance = instance; RepairEnabled = true; generatedSolutions = 0; }
public DiscreteUMDA4TSP(TSPInstance instance, int popSize, double truncFactor, int[] lowerBounds, int[] upperBounds) : base(popSize, truncFactor, lowerBounds, upperBounds) { Instance = instance; RepairEnabled = true; }
public DiscreteHMSAwGRASP2OptFirst4TSP(TSPInstance instance, double rclThreshold, int graspIterations, int initialSolutions, int levelLength, double tempReduction) : base(initialSolutions, levelLength, tempReduction) { GRASP = new DiscreteGRASP2OptFirst4TSP(instance, rclThreshold); Instance = instance; GRASPIterations = graspIterations; }
public void Start(string inputFile, string outputFile, int timeLimit) { TSPInstance instance = new TSPInstance(inputFile); int levelLength = (int) Math.Ceiling(levelLengthFactor * (instance.NumberCities * (instance.NumberCities - 1))); DiscreteHMSAwGRASP2OptBest4TSP hm = new DiscreteHMSAwGRASP2OptBest4TSP(instance, rclTreshold, graspIterations, initialSolutions, levelLength, tempReduction); hm.Run(timeLimit - timePenalty); TSPSolution solution = new TSPSolution(instance, hm.BestSolution); solution.Write(outputFile); }
public DiscreteHMTSwGRASP2OptBest4TSP(TSPInstance instance, double rclThreshold, int graspIterations, int tabuListLength, int neighborChecks) : base(tabuListLength, neighborChecks) { GRASP = new DiscreteGRASP2OptBest4TSP(instance, rclThreshold); Instance = instance; GRASPIterations = graspIterations; }
public void Start(string inputFile, string outputFile, int timeLimit) { TSPInstance instance = new TSPInstance(inputFile); MaxMinAntSystem aco = new MaxMinAntSystem2OptBest4TSP(instance, numberAnts, rho, alpha, beta, maxReinit, candidateLength, candidateWeight); // Solving the problem and writing the best solution found. aco.Run(timeLimit - timePenalty); TSPSolution solution = new TSPSolution(instance, aco.BestSolution); solution.Write(outputFile); }
public void Start(string fileInput, string fileOutput, int timeLimit) { TSPInstance instance = new TSPInstance(fileInput); int levelLength = (int) Math.Ceiling(levelLengthFactor * (instance.NumberCities * (instance.NumberCities - 1))); DiscreteSA sa = new DiscreteSA4TSP(instance, initialSolutions, levelLength, tempReduction); sa.Run(timeLimit - timePenalty); TSPSolution solution = new TSPSolution(instance, sa.BestSolution); solution.Write(fileOutput); }
public DiscreteHMSAwGRASP2OptBest4TSP(TSPInstance instance, double rclThreshold, int graspIterations, int initialSolutions, int levelLength, double tempReduction) : base(initialSolutions, levelLength, tempReduction) { GRASP = new DiscreteGRASP2OptBest4TSP(instance, rclThreshold); Instance = instance; GRASPIterations = graspIterations; }
public void Start(string inputFile, string outputFile, int timeLimit) { TSPInstance instance = new TSPInstance(inputFile); DiscreteSS ss = new DiscreteSS4TSP(instance, poolSize, refSetSize, explorationFactor); ss.Run(timeLimit - timePenalty); TSPSolution solution = new TSPSolution(instance, ss.BestSolution); solution.Write(outputFile); }
public void Start(string inputFile, string outputFile, int timeLimit) { TSPInstance instance = new TSPInstance(inputFile); int neighborChecks = (int) Math.Ceiling(neighborChecksFactor * (instance.NumberCities * (instance.NumberCities - 1))); int tabuListLength = (int) Math.Ceiling(tabuListFactor * instance.NumberCities); DiscreteTS ts = new DiscreteTS4TSP(instance, rclTreshold, tabuListLength, neighborChecks); ts.Run(timeLimit - timePenalty); TSPSolution solution = new TSPSolution(instance, ts.BestSolution); solution.Write(outputFile); }
// Implementation of the GRC solution's construction algorithm. public static int[] GRCSolution(TSPInstance instance, double rclThreshold) { int numCities = instance.NumberCities; int[] path = new int[instance.NumberCities]; int totalCities = numCities; int index = 0; double best = 0; double cost = 0; int city = 0; // Restricted Candidate List. SortedList<double, int> rcl = new SortedList<double, int>(); // Available cities. bool[] visited = new bool[numCities]; path[0] = Statistics.RandomDiscreteUniform(0, numCities-1); visited[path[0]] = true; numCities --; while (numCities > 0) { rcl = new SortedList<double, int>(); for (int i = 0; i < totalCities; i++) { if (!visited[i]) { cost = instance.Costs[path[index], i]; if(rcl.Count == 0) { best = cost; rcl.Add(cost, i); } else if( cost < best) { // The new city is the new best; best = cost; for (int j = rcl.Count-1; j > 0; j--) { if (rcl.Keys[j] > rclThreshold * best) { rcl.RemoveAt(j); } else { break; } } rcl.Add(cost, i); } else if (cost < rclThreshold * best) { // The new city is a mostly good candidate. rcl.Add(cost, i); } } } city = rcl.Values[Statistics.RandomDiscreteUniform(0, rcl.Count-1)]; index++; visited[city] = true; path[index] = city; numCities--; } return path; }
public void Start(string inputFile, string outputFile, int timeLimit) { TSPInstance instance = new TSPInstance(inputFile); int levelLength = (int)Math.Ceiling(levelLengthFactor * (instance.NumberCities * (instance.NumberCities - 1))); DiscreteHMSAwGRASP2OptFirst4TSP hm = new DiscreteHMSAwGRASP2OptFirst4TSP(instance, rclTreshold, graspIterations, initialSolutions, levelLength, tempReduction); hm.Run(timeLimit - timePenalty); TSPSolution solution = new TSPSolution(instance, hm.BestSolution); solution.Write(outputFile); }
public void Start(string fileInput, string fileOutput, int timeLimit) { TSPInstance instance = new TSPInstance(fileInput); int levelLength = (int)Math.Ceiling(levelLengthFactor * (instance.NumberCities * (instance.NumberCities - 1))); DiscreteSA sa = new DiscreteSA4TSP(instance, initialSolutions, levelLength, tempReduction); sa.Run(timeLimit - timePenalty); TSPSolution solution = new TSPSolution(instance, sa.BestSolution); solution.Write(fileOutput); }
public static double Fitness(TSPInstance instance, int[] path) { double cost = 0; for (int i = 1; i < path.Length; i++) { cost += instance.Costs[path[i-1],path[i]]; } cost += instance.Costs[path[path.Length-1],path[0]]; return cost; }
public void Start(string inputFile, string outputFile, int timeLimit) { TSPInstance instance = new TSPInstance(inputFile); int neighborChecks = (int)Math.Ceiling(neighborChecksFactor * (instance.NumberCities * (instance.NumberCities - 1))); int tabuListLength = (int)Math.Ceiling(tabuListFactor * instance.NumberCities); DiscreteTS ts = new DiscreteTS4TSP(instance, rclTreshold, tabuListLength, neighborChecks); ts.Run(timeLimit - timePenalty); TSPSolution solution = new TSPSolution(instance, ts.BestSolution); solution.Write(outputFile); }
public static double Distance(TSPInstance instance, int[] a, int[] b) { double distance = 0; for (int i = 0; i < a.Length - 1; i++) { if (a[i] != b[i] || a[i+1] != b[i+1]) { distance += 1; } } return distance; }
public static double Fitness(TSPInstance instance, int[] path) { double cost = 0; for (int i = 1; i < path.Length; i++) { cost += instance.Costs[path[i - 1], path[i]]; } cost += instance.Costs[path[path.Length - 1], path[0]]; return(cost); }
public void Start(string fileInput, string fileOutput, int timeLimit) { TSPInstance instance = new TSPInstance(fileInput); // Setting the parameters of the GRASP for this instance of the problem. DiscreteGRASP grasp = new DiscreteGRASP2OptBest4TSP(instance, rclThreshold); // Solving the problem and writing the best solution found. grasp.Run(timeLimit - (int)timePenalty, RunType.TimeLimit); TSPSolution solution = new TSPSolution(instance, grasp.BestSolution); solution.Write(fileOutput); }
public void Start(string fileInput, string fileOutput, int timeLimit) { TSPInstance instance = new TSPInstance(fileInput); // Setting the parameters of the GRASP for this instance of the problem. DiscreteGRASP grasp = new DiscreteGRASP2OptFirst4TSP(instance, rclThreshold); // Solving the problem and writing the best solution found. grasp.Run(timeLimit - (int)timePenalty, RunType.TimeLimit); TSPSolution solution = new TSPSolution(instance, grasp.BestSolution); solution.Write(fileOutput); }
public static double Distance(TSPInstance instance, int[] a, int[] b) { double distance = 0; for (int i = 0; i < a.Length - 1; i++) { if (a[i] != b[i] || a[i + 1] != b[i + 1]) { distance += 1; } } return(distance); }
public void Start(string inputFile, string outputFile, int timeLimit) { TSPInstance instance = new TSPInstance(inputFile); int[] lowerBounds = new int[instance.NumberCities]; int[] upperBounds = new int[instance.NumberCities]; for (int i = 0; i < instance.NumberCities; i++) { lowerBounds[i] = 0; upperBounds[i] = instance.NumberCities - 1; } DiscreteILS ils = new DiscreteILS2OptFirst4TSP(instance, restartIterations, lowerBounds, upperBounds); ils.Run(timeLimit - timePenalty); TSPSolution solution = new TSPSolution(instance, ils.BestSolution); solution.Write(outputFile); }
public static void Repair(TSPInstance instance, int[] individual) { int visitedCitiesCount = 0; bool[] visitedCities = new bool[instance.NumberCities]; bool[] repeatedPositions = new bool[instance.NumberCities]; // Get information to decide if the individual is valid. for (int i = 0; i < instance.NumberCities; i++) { if (!visitedCities[individual[i]]) { visitedCitiesCount += 1; visitedCities[individual[i]] = true; } else { repeatedPositions[i] = true; } } // If the individual is invalid, make it valid. if (visitedCitiesCount != instance.NumberCities) { for (int i = 0; i < repeatedPositions.Length; i++) { if (repeatedPositions[i]) { int count = Statistics.RandomDiscreteUniform(1, instance.NumberCities - visitedCitiesCount); for (int c = 0; c < visitedCities.Length; c++) { if (!visitedCities[c]) { count -= 1; if (count == 0) { individual[i] = c; repeatedPositions[i] = false; visitedCities[c] = true; visitedCitiesCount += 1; break; } } } } } } }
public void Start(string inputFile, string outputFile, int timeLimit) { TSPInstance instance = new TSPInstance(inputFile); int[] lowerBounds = new int[instance.NumberCities]; int[] upperBounds = new int[instance.NumberCities]; for (int i = 0; i < instance.NumberCities; i++) { lowerBounds[i] = 0; upperBounds[i] = instance.NumberCities - 1; } DiscreteILS ils = new DiscreteILS2OptBest4TSP(instance, restartIterations, lowerBounds, upperBounds); ils.Run(timeLimit - timePenalty); TSPSolution solution = new TSPSolution(instance, ils.BestSolution); solution.Write(outputFile); }
public void Start(string fileInput, string fileOutput, int timeLimit) { TSPInstance instance = new TSPInstance(fileInput); // Setting the parameters of the PSO for this instance of the problem. int[] lowerBounds = new int[instance.NumberCities]; int[] upperBounds = new int[instance.NumberCities]; for (int i = 0; i < instance.NumberCities; i++) { lowerBounds[i] = 0; upperBounds[i] = instance.NumberCities - 1; } DiscretePSO pso = new DiscretePSO4TSP(instance, (int)particlesCount, prevConf, neighConf, lowerBounds, upperBounds); // Solving the problem and writing the best solution found. pso.Run(timeLimit - (int)timePenalty); TSPSolution solution = new TSPSolution(instance, pso.BestPosition); solution.Write(fileOutput); }
public void Start(string fileInput, string fileOutput, int timeLimit) { TSPInstance instance = new TSPInstance(fileInput); // Setting the parameters of the MA for this instance of the problem. int[] lowerBounds = new int[instance.NumberCities]; int[] upperBounds = new int[instance.NumberCities]; for (int i = 0; i < instance.NumberCities; i++) { lowerBounds[i] = 0; upperBounds[i] = instance.NumberCities - 1; } DiscreteMA memetic = new DiscreteMA4TSP(instance, (int)popSize, mutProbability, lowerBounds, upperBounds); // Solving the problem and writing the best solution found. memetic.Run(timeLimit - (int)timePenalty); TSPSolution solution = new TSPSolution(instance, memetic.BestIndividual); solution.Write(fileOutput); }
public void Start(string fileInput, string fileOutput, int timeLimit) { TSPInstance instance = new TSPInstance(fileInput); // Setting the parameters of the UMDA for this instance of the problem. int popSize = (int) Math.Ceiling(popFactor * instance.NumberCities); int[] lowerBounds = new int[instance.NumberCities]; int[] upperBounds = new int[instance.NumberCities]; for (int i = 0; i < instance.NumberCities; i++) { lowerBounds[i] = 0; upperBounds[i] = instance.NumberCities - 1; } DiscreteUMDA umda = new DiscreteUMDA2OptFirst4TSP(instance, popSize, truncFactor, lowerBounds, upperBounds); // Solving the problem and writing the best solution found. umda.Run(timeLimit - (int)timePenalty); TSPSolution solution = new TSPSolution(instance, umda.BestIndividual); solution.Write(fileOutput); }
public static int[] RandomSolution(TSPInstance instance) { int[] solution = new int[instance.NumberCities]; List <int> cities = new List <int>(); for (int city = 0; city < instance.NumberCities; city++) { cities.Add(city); } for (int i = 0; i < instance.NumberCities; i++) { int cityIndex = Statistics.RandomDiscreteUniform(0, cities.Count - 1); int city = cities[cityIndex]; cities.RemoveAt(cityIndex); solution[i] = city; } return(solution); }
public void Start(string fileInput, string fileOutput, int timeLimit) { TSPInstance instance = new TSPInstance(fileInput); // Setting the parameters of the GA for this instance of the problem. int[] lowerBounds = new int[instance.NumberCities]; int[] upperBounds = new int[instance.NumberCities]; for (int i = 0; i < instance.NumberCities; i++) { lowerBounds[i] = 0; upperBounds[i] = instance.NumberCities - 1; } DiscreteGA genetic = new DiscreteGA2OptFirst4TSP(instance, (int)popSize, mutProbability, lowerBounds, upperBounds); // Solving the problem and writing the best solution found. genetic.Run(timeLimit - (int)timePenalty); TSPSolution solution = new TSPSolution(instance, genetic.BestIndividual); solution.Write(fileOutput); }
public void Start(string fileInput, string fileOutput, int timeLimit) { TSPInstance instance = new TSPInstance(fileInput); // Setting the parameters of the PSO for this instance of the problem. int[] lowerBounds = new int[instance.NumberCities]; int[] upperBounds = new int[instance.NumberCities]; for (int i = 0; i < instance.NumberCities; i++) { lowerBounds[i] = 0; upperBounds[i] = instance.NumberCities - 1; } DiscretePSO pso = new DiscretePSO2OptFirst4TSP(instance, (int)particlesCount, prevConf, neighConf, lowerBounds, upperBounds); // Solving the problem and writing the best solution found. pso.Run(timeLimit - (int)timePenalty); TSPSolution solution = new TSPSolution(instance, pso.BestPosition); solution.Write(fileOutput); }
public MaxMinAntSystem2OptBest4TSP(TSPInstance instance, int numberAnts, double rho, double alpha, double beta, int maxReinit, int candidateLength, double candidateWeight) : base(instance.NumberCities, TSPUtils.Fitness(instance, TSPUtils.RandomSolution(instance)), numberAnts, rho, alpha, beta, maxReinit) { Instance = instance; this.candidateWeight = candidateWeight; // Build the candidate list. this.candidateLists = new List<Tuple<double,int>>[Instance.NumberCities]; for (int i = 0; i < Instance.NumberCities; i++) { this.candidateLists[i] = new List<Tuple<double,int>>(); for (int j = 0; j < Instance.NumberCities; j++) { if (i != j) { this.candidateLists[i].Add(new Tuple<double,int>(Instance.Costs[i,j], j)); } } this.candidateLists[i].Sort(); this.candidateLists[i].RemoveRange(candidateLength, this.candidateLists[i].Count - candidateLength); } }
// Implementation of the 2-opt (best improvement) local search algorithm. public static void LocalSearch2OptBest(TSPInstance instance, int[] path) { int tmp; int firstSwapItem = 0, secondSwapItem = 0; double currentFitness, bestFitness; bestFitness = Fitness(instance, path); for (int j = 1; j < path.Length; j++) { for (int i = 0; i < j; i++) { // Swap the items. tmp = path[j]; path[j] = path[i]; path[i] = tmp; // Evaluate the fitness of this new solution. currentFitness = Fitness(instance, path); if (currentFitness < bestFitness) { firstSwapItem = j; secondSwapItem = i; bestFitness = currentFitness; } // Undo the swap. tmp = path[j]; path[j] = path[i]; path[i] = tmp; } } // Use the best assignment. if (firstSwapItem != secondSwapItem) { tmp = path[firstSwapItem]; path[firstSwapItem] = path[secondSwapItem]; path[secondSwapItem] = tmp; } }
public static int[] GetNeighbor(TSPInstance instance, int[] solution) { int[] neighbor = new int[instance.NumberCities]; int a = Statistics.RandomDiscreteUniform(0, solution.Length - 1); int b = a; while (b == a) { b = Statistics.RandomDiscreteUniform(0, solution.Length - 1); } for (int i = 0; i < solution.Length; i++) { if (i == a) { neighbor[i] = solution[b]; } else if (i == b) { neighbor[i] = solution[a]; } else { neighbor[i] = solution[i]; } } return neighbor; }
public void Start(string fileInput, string fileOutput, int timeLimit) { TSPInstance instance = new TSPInstance(fileInput); // Setting the parameters of the UMDA for this instance of the problem. int popSize = (int)Math.Ceiling(popFactor * instance.NumberCities); int[] lowerBounds = new int[instance.NumberCities]; int[] upperBounds = new int[instance.NumberCities]; for (int i = 0; i < instance.NumberCities; i++) { lowerBounds[i] = 0; upperBounds[i] = instance.NumberCities - 1; } DiscreteUMDA umda = new DiscreteUMDA2OptBest4TSP(instance, popSize, truncFactor, lowerBounds, upperBounds); // Solving the problem and writing the best solution found. umda.Run(timeLimit - timePenalty); TSPSolution solution = new TSPSolution(instance, umda.BestIndividual); solution.Write(fileOutput); }
public MaxMinAntSystem2OptFirst4TSP(TSPInstance instance, int numberAnts, double rho, double alpha, double beta, int maxReinit, int candidateLength, double candidateWeight) : base(instance.NumberCities, TSPUtils.Fitness(instance, TSPUtils.RandomSolution(instance)), numberAnts, rho, alpha, beta, maxReinit) { Instance = instance; this.candidateWeight = candidateWeight; // Build the candidate list. this.candidateLists = new List <Tuple <double, int> > [Instance.NumberCities]; for (int i = 0; i < Instance.NumberCities; i++) { this.candidateLists[i] = new List <Tuple <double, int> >(); for (int j = 0; j < Instance.NumberCities; j++) { if (i != j) { this.candidateLists[i].Add(new Tuple <double, int>(Instance.Costs[i, j], j)); } } this.candidateLists[i].Sort(); this.candidateLists[i].RemoveRange(candidateLength, this.candidateLists[i].Count - candidateLength); } }
public static int[] RandomSolution(TSPInstance instance) { int[] solution = new int[instance.NumberCities]; List<int> cities = new List<int>(); for (int city = 0; city < instance.NumberCities; city++) { cities.Add(city); } for (int i = 0; i < instance.NumberCities; i++) { int cityIndex = Statistics.RandomDiscreteUniform(0, cities.Count - 1); int city = cities[cityIndex]; cities.RemoveAt(cityIndex); solution[i] = city; } return solution; }
public TSPSolution(TSPInstance instance, int[] path) { Instance = instance; Path = path; }
public DiscreteGRASP2OptBest4TSP(TSPInstance instance, double rclThreshold) : base(rclThreshold) { Instance = instance; }