Exemple #1
0
		private static bool Init(TspGraph input, int tabuListCap, int maxRestarts, int pityTimer)
		{
			try
			{
				InputGraph = input.GraphMatrix;
				numbOfCities = input.Dimension;
				MaxRestarts = maxRestarts;
				PityTimer = pityTimer;
				TabuListCap = tabuListCap;
				globalBestDistance = int.MaxValue;

				tabuList = new Int16[numbOfCities, numbOfCities, 2]; //miesci wszystkie miasta ale miasta 0 nie ruszy i tak

				//for (int i = 0; i < numbOfCities; i++)
				//	tabuList[i] = new Int16[2];
				resetTabuList();

				currentSolution = new List<Int16>(numbOfCities + 1);

				for (Int16 i = 0; i < currentSolution.Capacity - 1; i++)
					currentSolution.Add(i);
				currentSolution.Add(0);
			}
			catch (Exception e)
			{
				Console.WriteLine("Błąd. Opis błędu: " + e);
				return false;
			}		

			return true;
		}
Exemple #2
0
		public static TimeSpan TimeMeasured { get; private set; } //zmierzony czas wykonywania obliczen
		#endregion

		public static void SolveTsp(TspGraph input, int elitismSize, double crossoverRate, double mutationRate, int populationSize, CrossoverType cT, int limit = 300, int generations = 10000, int tournamentSize = 2)
		{
			if (input.Dimension > 2 && input.Name != null && input.GraphMatrix != null)
			{
				if (elitismSize % 2 == 1)
				{
					elitismSize--;
					Console.WriteLine("Ilość elit powinna być podzielna przez 2. Zmniejszono o 1.");
				}
				if (populationSize % 2 == 1)
				{
					populationSize--;
					Console.WriteLine("Wielkość populacji powinna być podzielna przez 2. Zmniejszono o 1.");
				}

				bool allright = Init(input, generations, populationSize, elitismSize, tournamentSize, mutationRate, crossoverRate, cT, limit);

				if (allright)
				{
					stopwatch = new Stopwatch();
					stopwatch.Start();
					Search();
					stopwatch.Stop();
					TimeMeasured = stopwatch.Elapsed;
				}
			}
			else Console.WriteLine("Przed uruchomieniem algorytmu wczytaj odpowiednio dane wejściowe.\n(Wciśnij dowolny klawisz aby wrócić)");
		}
Exemple #3
0
		//testy dla wybranych instancji z tsplib umieszczonych w folderze ./tsplib
		private static double[,] DpTestsTspLib()
		{
			double[,] timesArray = new double[4, 100];
			int i = 0;

			TspGraph graph = new TspGraph();
			graph.ReadGraphFromFile(AppDomain.CurrentDomain.BaseDirectory + @"\tsplib\gr17.tsp");

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspDynamicProgramming.SolveTsp(graph);
				timesArray[i, j] = TspDynamicProgramming.TimeMeasured.TotalMilliseconds;
				TspDynamicProgramming.ClearCollections();
			}
			i++;

			graph = new TspGraph();
			graph.ReadGraphFromFile(AppDomain.CurrentDomain.BaseDirectory + @"\tsplib\gr21.tsp");

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspDynamicProgramming.SolveTsp(graph);
				timesArray[i, j] = TspDynamicProgramming.TimeMeasured.TotalMilliseconds;
				TspDynamicProgramming.ClearCollections();
			}
			i++;

			graph = new TspGraph();
			graph.ReadGraphFromFile(AppDomain.CurrentDomain.BaseDirectory + @"\tsplib\gr24.tsp");

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspDynamicProgramming.SolveTsp(graph);
				timesArray[i, j] = TspDynamicProgramming.TimeMeasured.TotalMilliseconds;
				TspDynamicProgramming.ClearCollections();
			}
			i++;

			graph = new TspGraph();
			graph.ReadGraphFromFile(AppDomain.CurrentDomain.BaseDirectory + @"\tsplib\br17.atsp");

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspDynamicProgramming.SolveTsp(graph);
				timesArray[i, j] = TspDynamicProgramming.TimeMeasured.TotalMilliseconds;
				TspDynamicProgramming.ClearCollections();
			}

			return timesArray;
		}
Exemple #4
0
		public static TimeSpan TimeMeasured { get; private set; } //zmierzony czas wykonywania obliczen
		#endregion

		public static void SolveTsp(TspGraph input, int tabuListCap, int maxRestarts = 10, int pityTimer = 100)
		{
			if (input.Dimension > 2 && input.Name != null && input.GraphMatrix != null)
			{
				bool allright = Init(input, tabuListCap, maxRestarts, pityTimer);

				if (allright)
				{
					Stopwatch stopwatch = new Stopwatch();
					stopwatch.Start();
					Search();
					stopwatch.Stop();
					TimeMeasured = stopwatch.Elapsed;
				}
			}
			else Console.WriteLine("Przed uruchomieniem algorytmu wczytaj odpowiednio dane wejściowe.\n(Wciśnij dowolny klawisz aby wrócić)");
		}
Exemple #5
0
		//inicjalizacja zasobow klasy przed wykonywaniem obliczen
		private static bool Init (TspGraph input)
		{
			InputGraph = input.GraphMatrix;
			numbOfCities = input.Dimension;
			try
			{
				powah = (Int64) Math.Pow(2, numbOfCities);
				g = new Int16[numbOfCities][];
				p = new Int16[numbOfCities][];
				PathDistance = int.MaxValue;
				OutputList = new List<Int16>();

				for (int i = 0; i < numbOfCities; i++)
				{
					g[i] = new Int16[powah];
					p[i] = new Int16[powah];
				}
			}
			catch (OutOfMemoryException e)
			{
				Console.WriteLine("Brak pamięci. Opis błędu: " + e);
				return false;
			}
			catch (OverflowException e)
			{
				Console.WriteLine(e);
				return false;
			}

			for (int i = 0; i < numbOfCities; i++)
			{
				for (Int64 j = 0; j < powah; j++)
				{
					g[i][j] = Int16.MaxValue;
					p[i][j] = Int16.MaxValue;
				}
			}

			for (int i = 0; i < numbOfCities; i++)
			{
				g[i][0] = InputGraph[i][0];
			}

			return true;
		}
Exemple #6
0
		private static double[,] DpTests(bool tspOrAtsp)
		{
			double[,] timesArray = new double[6,100];
			int numbOfCities = 14;

			for (int i = 0; i < timesArray.GetLength(0); i++, numbOfCities += 2)
			{
				for (int j = 0; j < timesArray.GetLength(1); j++)
				{
					TspGraph graph = new TspGraph(numbOfCities, tspOrAtsp);
					TspDynamicProgramming.SolveTsp(graph);
					timesArray[i, j] = TspDynamicProgramming.TimeMeasured.TotalMilliseconds;
					TspDynamicProgramming.ClearCollections();
				}
			}

			return timesArray;
		}
Exemple #7
0
		public static TimeSpan TimeMeasured { get; private set; } //zmierzony czas wykonywania obliczen
		#endregion
		//rozwiazanie problemu dla podanych danych wejsciowych
		public static void SolveTsp (TspGraph input)
		{
			if (input.Dimension > 2 && input.Name != null && input.GraphMatrix != null)
			{
				//jesli udalo sie zainicjalizowac zasoby bez problemow, to mozna wykonac obliczenia
				bool allright = Init(input);
				if (allright)
				{
					Stopwatch stopwatch = new Stopwatch();
					stopwatch.Start();
					PathDistance = ComputeDistance(0, powah - 2);
					stopwatch.Stop();
					TimeMeasured = stopwatch.Elapsed;
					OutputList.Add(0);
					GetOptimalPath(0, powah - 2);
					OutputList.Add(0);
				}
				else Console.WriteLine("Nie wykonano obliczeń, ponieważ wystąpił błąd.");
			}
			else Console.WriteLine("Przed uruchomieniem algorytmu wczytaj odpowiednio dane wejściowe.\n(Wciśnij dowolny klawisz aby wrócić)");
		}
Exemple #8
0
		private static bool Init(TspGraph input, int generations, int populationSize, int elitismSize, int tournamentSize, double mutationRate, double crossoverRate, CrossoverType cT, int limit)
		{
			try
			{
				InputGraph = input.GraphMatrix;
				numbOfCities = input.Dimension;
				Generations = generations;
				PopulationSize = populationSize;
				ElitismSize = elitismSize;
				TournamentSize = tournamentSize;
				MutationRate = mutationRate;
				CrossoverRate = crossoverRate;
				globalBestFitness = 0;
				rand1 = new Random();
				rand2 = new Random();
				cType = cT;
				timeLimit = limit;

				fitnessList = new List<double>(populationSize);
				population = new List<List<Int16>>(populationSize);

				for (int i = 0; i < population.Capacity; i++)
				{
					population.Add(new List<short>(numbOfCities + 1));
					for (Int16 j = 0; j < population[i].Capacity - 1; j++)
						population[i].Add(j);
					population[i].Add(0);
					RandomizeSolution(population[i], i);
					fitnessList.Add(CalculateFitness(population[i]));
				}
			}
			catch (Exception e)
			{
				Console.WriteLine("Błąd. Opis błędu: " + e);
				return false;
			}

			return true;
		}
Exemple #9
0
		private static Tuple<double[,], int[,]> GaTestsTspLib()
		{
			double[,] timesArray = new double[10, 10];
			int[,] distancesArray = new int[10, 10];
			int i = 0;

			TspGraph graph = new TspGraph();
			graph.ReadGraphFromFile(AppDomain.CurrentDomain.BaseDirectory + @"\tsplib\gr24.tsp");

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspGenetic.SolveTsp(graph, 14, 0.5, 0.1, 72, CrossoverType.OX);
				timesArray[i, j] = TspGenetic.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspGenetic.PathDistance;
			}
			i++;

			graph = new TspGraph();
			graph.ReadGraphFromFile(AppDomain.CurrentDomain.BaseDirectory + @"\tsplib\ftv70.atsp");

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspGenetic.SolveTsp(graph, 42, 0.5, 0.1, 212, CrossoverType.OX);
				timesArray[i, j] = TspGenetic.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspGenetic.PathDistance;
			}
			i++;

			graph = new TspGraph();
			graph.ReadGraphFromFile(AppDomain.CurrentDomain.BaseDirectory + @"\tsplib\kro124p.atsp");

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspGenetic.SolveTsp(graph, 60, 0.5, 0.1, 300, CrossoverType.OX);
				timesArray[i, j] = TspGenetic.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspGenetic.PathDistance;
			}
			i++;

			graph = new TspGraph();
			graph.ReadGraphFromFile(AppDomain.CurrentDomain.BaseDirectory + @"\tsplib\ftv170.atsp");

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspGenetic.SolveTsp(graph, 102, 0.5, 0.1, 512, CrossoverType.OX);
				timesArray[i, j] = TspGenetic.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspGenetic.PathDistance;
			}
			i++;

			graph = new TspGraph();
			graph.ReadGraphFromFile(AppDomain.CurrentDomain.BaseDirectory + @"\tsplib\rbg323.atsp");

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspGenetic.SolveTsp(graph, 194, 0.5, 0.1, 968, CrossoverType.OX, 600);
				timesArray[i, j] = TspGenetic.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspGenetic.PathDistance;
			}
			i++;

			graph = new TspGraph();
			graph.ReadGraphFromFile(AppDomain.CurrentDomain.BaseDirectory + @"\tsplib\gr24.tsp");

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspGenetic.SolveTsp(graph, 14, 1.0, 0.5, 72, CrossoverType.OX);
				timesArray[i, j] = TspGenetic.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspGenetic.PathDistance;
			}
			i++;

			graph = new TspGraph();
			graph.ReadGraphFromFile(AppDomain.CurrentDomain.BaseDirectory + @"\tsplib\ftv70.atsp");

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspGenetic.SolveTsp(graph, 42, 1.0, 0.5, 212, CrossoverType.OX);
				timesArray[i, j] = TspGenetic.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspGenetic.PathDistance;
			}
			i++;

			graph = new TspGraph();
			graph.ReadGraphFromFile(AppDomain.CurrentDomain.BaseDirectory + @"\tsplib\kro124p.atsp");

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspGenetic.SolveTsp(graph, 60, 1.0, 0.5, 300, CrossoverType.OX);
				timesArray[i, j] = TspGenetic.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspGenetic.PathDistance;
			}
			i++;

			graph = new TspGraph();
			graph.ReadGraphFromFile(AppDomain.CurrentDomain.BaseDirectory + @"\tsplib\ftv170.atsp");

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspGenetic.SolveTsp(graph, 102, 1.0, 0.5, 512, CrossoverType.OX);
				timesArray[i, j] = TspGenetic.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspGenetic.PathDistance;
			}
			i++;

			graph = new TspGraph();
			graph.ReadGraphFromFile(AppDomain.CurrentDomain.BaseDirectory + @"\tsplib\rbg323.atsp");

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspGenetic.SolveTsp(graph, 194, 1.0, 0.5, 968, CrossoverType.OX, 600);
				timesArray[i, j] = TspGenetic.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspGenetic.PathDistance;
			}

			Tuple<double[,], int[,]> returnTuple = new Tuple<double[,], int[,]>(timesArray, distancesArray);

			return returnTuple;
		}
Exemple #10
0
		private static void GaParametersTests()
		{
			TspGraph graph = new TspGraph();
			graph.ReadGraphFromFile(AppDomain.CurrentDomain.BaseDirectory + @"\tsplib\gr48.tsp");

			double[] mutationRates = { 0.01, 0.05, 0.1, 0.25, 0.5, 0.75 };
			double[] crossoverRates = { 0.25, 0.5, 0.75, 0.9, 1.0 };
			List<double> allAvgDistances = new List<double>(); 

			for (int i = 0; i < crossoverRates.Length; i++)
			{
				for (int j = 0; j < mutationRates.Length; j++)
				{
					double[,] times = new double[1, 11];
					int[,] distances = new int[1, 11];

					for (int k = 0; k < 10; k++)
					{
						TspGenetic.SolveTsp(graph, 28, crossoverRates[i], mutationRates[j], 144, CrossoverType.OX, 30);
						times[0, k] = TspGenetic.TimeMeasured.TotalMilliseconds;
						distances[0, k] = TspGenetic.PathDistance;
					}

					var avgTime = GetAverageTimes(times)[0] * 11.0 / 10.0;
					var avgDistance = GetAverageDistances(distances)[0] * 11.0 / 10.0;
					times[0, times.Length - 1] = avgTime;
					distances[0, distances.Length - 1] = (int)avgDistance;
					string filename = "gr48-CR" + crossoverRates[i].ToString("F2") + "-MR" + mutationRates[j].ToString("F2");
					double[] times1d = new double[11];
					double[] distances1d = new double[11];
					allAvgDistances.Add(avgDistance);

					for (int x = 0; x < times1d.Length; x++)
					{
						times1d[x] = times[0, x];
						distances1d[x] = distances[0, x];
					}

					TimesToFile(filename + "-TimeOX.txt", times1d);
					DistancesToFile(filename + "-DistsOX.txt", distances1d);
				}
			}

			for (int i = 0; i < crossoverRates.Length; i++)
			{
				for (int j = 0; j < mutationRates.Length; j++)
				{
					double[,] times = new double[1, 11];
					int[,] distances = new int[1, 11];

					for (int k = 0; k < 10; k++)
					{
						TspGenetic.SolveTsp(graph, 28, crossoverRates[i], mutationRates[j], 144, CrossoverType.Other, 30);
						times[0, k] = TspGenetic.TimeMeasured.TotalMilliseconds;
						distances[0, k] = TspGenetic.PathDistance;
					}

					var avgTime = GetAverageTimes(times)[0] * 11.0 / 10.0;
					var avgDistance = GetAverageDistances(distances)[0] * 11.0 / 10.0;
					times[0, times.Length - 1] = avgTime;
					distances[0, distances.Length - 1] = (int)avgDistance;
					string filename = "gr48-CR" + crossoverRates[i].ToString("F2") + "-MR" + mutationRates[j].ToString("F2");
					double[] times1d = new double[11];
					double[] distances1d = new double[11];
					allAvgDistances.Add(avgDistance);

					for (int x = 0; x < times1d.Length; x++)
					{
						times1d[x] = times[0, x];
						distances1d[x] = distances[0, x];
					}

					TimesToFile(filename + "-TimeOT.txt", times1d);
					DistancesToFile(filename + "-DistsOT.txt", distances1d);
				}
			}

			DistancesToFile("gr48-AllAverageDists.txt", allAvgDistances.ToArray());
		}
Exemple #11
0
		private static Tuple<double[,], int[,]> TsTestsTspLib()
		{
			double[,] timesArray = new double[25, 10];
			int[,] distancesArray = new int[25, 10];
			int i = 0;

			TspGraph graph = new TspGraph();
			graph.ReadGraphFromFile(AppDomain.CurrentDomain.BaseDirectory + @"\tsplib\gr24.tsp");

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspTabuSearch.SolveTsp(graph, graph.Dimension / 8);
				timesArray[i, j] = TspTabuSearch.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspTabuSearch.PathDistance;
			}
			i++;

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspTabuSearch.SolveTsp(graph, graph.Dimension / 6);
				timesArray[i, j] = TspTabuSearch.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspTabuSearch.PathDistance;
			}
			i++;

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspTabuSearch.SolveTsp(graph, graph.Dimension / 4);
				timesArray[i, j] = TspTabuSearch.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspTabuSearch.PathDistance;
			}
			i++;

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspTabuSearch.SolveTsp(graph, graph.Dimension / 2);
				timesArray[i, j] = TspTabuSearch.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspTabuSearch.PathDistance;
			}
			i++;

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspTabuSearch.SolveTsp(graph, graph.Dimension * 3);
				timesArray[i, j] = TspTabuSearch.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspTabuSearch.PathDistance;
			}
			i++;

			graph = new TspGraph();
			graph.ReadGraphFromFile(AppDomain.CurrentDomain.BaseDirectory + @"\tsplib\ftv70.atsp");

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspTabuSearch.SolveTsp(graph, graph.Dimension / 8);
				timesArray[i, j] = TspTabuSearch.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspTabuSearch.PathDistance;
			}
			i++;

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspTabuSearch.SolveTsp(graph, graph.Dimension / 6);
				timesArray[i, j] = TspTabuSearch.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspTabuSearch.PathDistance;
			}
			i++;

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspTabuSearch.SolveTsp(graph, graph.Dimension / 4);
				timesArray[i, j] = TspTabuSearch.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspTabuSearch.PathDistance;
			}
			i++;

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspTabuSearch.SolveTsp(graph, graph.Dimension / 2);
				timesArray[i, j] = TspTabuSearch.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspTabuSearch.PathDistance;
			}
			i++;

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspTabuSearch.SolveTsp(graph, graph.Dimension * 3);
				timesArray[i, j] = TspTabuSearch.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspTabuSearch.PathDistance;
			}
			i++;
			
			graph = new TspGraph();
			graph.ReadGraphFromFile(AppDomain.CurrentDomain.BaseDirectory + @"\tsplib\kro124p.atsp");

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspTabuSearch.SolveTsp(graph, graph.Dimension / 8);
				timesArray[i, j] = TspTabuSearch.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspTabuSearch.PathDistance;
			}
			i++;

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspTabuSearch.SolveTsp(graph, graph.Dimension / 6);
				timesArray[i, j] = TspTabuSearch.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspTabuSearch.PathDistance;
			}
			i++;

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspTabuSearch.SolveTsp(graph, graph.Dimension / 4);
				timesArray[i, j] = TspTabuSearch.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspTabuSearch.PathDistance;
			}
			i++;

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspTabuSearch.SolveTsp(graph, graph.Dimension / 2);
				timesArray[i, j] = TspTabuSearch.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspTabuSearch.PathDistance;
			}
			i++;

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspTabuSearch.SolveTsp(graph, graph.Dimension * 3);
				timesArray[i, j] = TspTabuSearch.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspTabuSearch.PathDistance;
			}
			i++;

			graph = new TspGraph();
			graph.ReadGraphFromFile(AppDomain.CurrentDomain.BaseDirectory + @"\tsplib\ftv170.atsp");

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspTabuSearch.SolveTsp(graph, graph.Dimension / 8);
				timesArray[i, j] = TspTabuSearch.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspTabuSearch.PathDistance;
			}
			i++;

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspTabuSearch.SolveTsp(graph, graph.Dimension / 6);
				timesArray[i, j] = TspTabuSearch.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspTabuSearch.PathDistance;
			}
			i++;

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspTabuSearch.SolveTsp(graph, graph.Dimension / 4);
				timesArray[i, j] = TspTabuSearch.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspTabuSearch.PathDistance;
			}
			i++;

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspTabuSearch.SolveTsp(graph, graph.Dimension / 2);
				timesArray[i, j] = TspTabuSearch.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspTabuSearch.PathDistance;
			}
			i++;

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspTabuSearch.SolveTsp(graph, graph.Dimension * 3);
				timesArray[i, j] = TspTabuSearch.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspTabuSearch.PathDistance;
			}
			i++;

			graph = new TspGraph();
			graph.ReadGraphFromFile(AppDomain.CurrentDomain.BaseDirectory + @"\tsplib\rbg323.atsp");

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspTabuSearch.SolveTsp(graph, graph.Dimension / 8);
				timesArray[i, j] = TspTabuSearch.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspTabuSearch.PathDistance;
			}
			i++;

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspTabuSearch.SolveTsp(graph, graph.Dimension / 6);
				timesArray[i, j] = TspTabuSearch.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspTabuSearch.PathDistance;
			}
			i++;

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspTabuSearch.SolveTsp(graph, graph.Dimension / 4);
				timesArray[i, j] = TspTabuSearch.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspTabuSearch.PathDistance;
			}
			i++;

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspTabuSearch.SolveTsp(graph, graph.Dimension / 2);
				timesArray[i, j] = TspTabuSearch.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspTabuSearch.PathDistance;
			}
			i++;

			for (int j = 0; j < timesArray.GetLength(1); j++)
			{
				TspTabuSearch.SolveTsp(graph, graph.Dimension * 3);
				timesArray[i, j] = TspTabuSearch.TimeMeasured.TotalMilliseconds;
				distancesArray[i, j] = TspTabuSearch.PathDistance;
			}

			Tuple<double[,], int[,]> returnTuple = new Tuple<double[,], int[,]>(timesArray, distancesArray);

			return returnTuple;
		}
Exemple #12
0
		static void Main (string[] args)
		{
			TspGraph graph = new TspGraph();
			char primaryMenuKey, secondaryMenuKey;
			string input;
			CancellationTokenSource cts = new CancellationTokenSource();
			CancellationToken ct = cts.Token;

			do
			{
				Console.WriteLine ();
				Console.WriteLine ("PEA - problem komiwojażera");
				Console.WriteLine ("-Menu główne");
				Console.WriteLine ("--1. Wczytaj graf z pliku.");
				Console.WriteLine ("--2. Przejrzyj właściwości grafu.");
				Console.WriteLine ("--3. Rozwiąż problem za pomocą wybranego algorytmu.");
				Console.WriteLine ("--4. Stwórz zestaw o określonej ilości miast.");
				Console.WriteLine ("--5. Przeprowadź testy czasowe dla losowych instancji.");
				Console.WriteLine ("--6. Przeprowadź testy czasowe dla danych z tsplib (Programowanie dynamiczne).");
				Console.WriteLine ("--7. Przeprowadź testy dla danych z tsplib (Tabu Search).");
				Console.WriteLine ("--8. Przeprowadź testy jakościowe parametrów dla gr48.tsp (Algorytm genetyczny).");
				Console.WriteLine ("--9. Przeprowadź testy dla danych z tsplib (Algorytm genetyczny).");

				primaryMenuKey = Console.ReadKey().KeyChar;

				switch (primaryMenuKey)
				{
					case '1':
						Console.Clear ();
						Console.WriteLine ("Wpisz ścieżkę pliku.");
						string filename = Console.ReadLine();
						filename = filename?.Replace(@"\", @"\\");
						graph.ReadGraphFromFile(filename);
						break;
					case '2':
						Console.Clear ();
						try
						{
							Console.WriteLine("---Graf");
							Console.WriteLine("----Nazwa: " + graph.Name);
							Console.WriteLine("----Typ: " + graph.Type);
							Console.WriteLine("----Wymiary: " + graph.Dimension);
							graph.PrintGraph();
						}
						catch (NullReferenceException)
						{
							Console.WriteLine("Graf albo jego właściwości są null.");
						}
						Console.ReadKey();
						break;
					case '3':
						do
						{
							Console.Clear ();
							Console.WriteLine("---Algorytmy");
							Console.WriteLine("----1. Rozwiąż algorytmem programowania dynamicznego.");
							Console.WriteLine("----2. Rozwiąż algorytmem przeszukiwania z zakazami.");
							Console.WriteLine("----3. Rozwiąż algorytmem genetycznym.");

							secondaryMenuKey = Console.ReadKey().KeyChar;

							switch (secondaryMenuKey)
							{
								case '1':
									cts = new CancellationTokenSource();
									Console.Clear ();
									Task.Run(() => {
										while (!cts.IsCancellationRequested)
										{
											Console.Write(".");
											Thread.Sleep(1000);
										}
									}, ct);
									TspDynamicProgramming.SolveTsp(graph);
									cts.Cancel();
									cts.Dispose();
									Console.WriteLine();
									TspDynamicProgramming.ShowResults();			
									TspDynamicProgramming.ClearCollections();
									Console.ReadKey();
									break;
								case '2':
									Console.Clear();
									int mr, pt, tlc;
									Console.WriteLine("---Podaj ilość restartów:");
									input = Console.ReadLine();

									while (!Int32.TryParse(input, out mr))
									{
										input = Console.ReadLine();
									}

									Console.WriteLine("---Podaj ilość prób bez polepszenia rezultatu:");
									input = Console.ReadLine();

									while (!Int32.TryParse(input, out pt))
									{
										input = Console.ReadLine();
									}

									Console.WriteLine("---Podaj długość listy tabu:");
									input = Console.ReadLine();

									while (!Int32.TryParse(input, out tlc))
									{
										input = Console.ReadLine();
									}
									cts = new CancellationTokenSource();
									Console.Clear();
									Task.Run(() => {
										while (!cts.IsCancellationRequested)
										{
											Console.Write(".");
											Thread.Sleep(1000);
										}
									}, ct);
									TspTabuSearch.SolveTsp(graph, tlc, mr, pt);
									cts.Cancel();
									cts.Dispose();
									Console.WriteLine();
									TspTabuSearch.ShowResults();
									Console.ReadKey();
									break;
								case '3':
									Console.Clear();
									int pop, elites;
									double mutrate, crossrate;
									Console.WriteLine("---Podaj prawdopodobieństwo krzyżowania:");
									input = Console.ReadLine();

									while (!double.TryParse(input, out crossrate))
									{
										input = Console.ReadLine();
									}

									Console.WriteLine("---Podaj prawdopodobieństwo mutacji:");
									input = Console.ReadLine();

									while (!double.TryParse(input, out mutrate))
									{
										input = Console.ReadLine();
									}

									Console.WriteLine("---Podaj wielkość populacji:");
									input = Console.ReadLine();

									while (!Int32.TryParse(input, out pop))
									{
										input = Console.ReadLine();
									}

									Console.WriteLine("---Podaj liczbę elit:");
									input = Console.ReadLine();

									while (!Int32.TryParse(input, out elites))
									{
										input = Console.ReadLine();
									}

									cts = new CancellationTokenSource();
									Console.Clear();
									Task.Run(() => {
										while (!cts.IsCancellationRequested)
										{
											Console.Write(".");
											Thread.Sleep(1000);
										}
									}, ct);
									TspGenetic.SolveTsp(graph, elites, crossrate, mutrate, pop, CrossoverType.OX, 300, 1000);
									cts.Cancel();
									cts.Dispose();
									Console.WriteLine();
									TspGenetic.ShowResults();
									Console.ReadKey();
									break;
								default:
									break;
							}
						} while (secondaryMenuKey != 27); 
						break;
					case '4':
						Console.Clear();
						int dim, maxDist;
						Console.WriteLine("---Podaj ilość miast:");
						input = Console.ReadLine();

						while (!Int32.TryParse(input, out dim))
						{
							input = Console.ReadLine();
						}

						Console.WriteLine("---Równe odległości A-B i B-A? (t - tak, n - nie)");
						
						do
						{
							input = Console.ReadLine() ?? "";
						} while (!(input.Equals("t") || input.Equals("n")));

						bool tspOrAtsp = false;

						if (input.Equals("t")) tspOrAtsp = false;
						else if (input.Equals("n")) tspOrAtsp = true;

						Console.WriteLine("---Podaj maksymalną odległość między miastami:");
						input = Console.ReadLine();

						while (!Int32.TryParse(input, out maxDist))
						{
							input = Console.ReadLine();
						}

						graph = new TspGraph(dim, tspOrAtsp, maxDist);
						Console.WriteLine("---Utworzono graf o następujących właściwościach:");
						Console.WriteLine("----Nazwa: " + graph.Name);
						Console.WriteLine("----Typ: " + graph.Type);
						Console.WriteLine("----Wymiary: " + graph.Dimension);
						Console.ReadKey();
						break;
					case '5':
						Console.Clear();
						Console.WriteLine("Zostaną przprowadzone testy. Wciśnij ESC aby anulować albo dowolny klawisz aby kontynuować.");
						secondaryMenuKey = Console.ReadKey().KeyChar;
						if (secondaryMenuKey != 27)
						{
							Console.WriteLine("Wykonywane są testy dla tsp. Może to zająć dużo czasu.");
							double[,] results = DpTests(false);
							TimesToFile("DpTsp.txt", GetAverageTimes(results));
							Console.WriteLine("Wykonywane są testy dla atsp. Może to zająć dużo czasu.");
							results = DpTests(true);
							TimesToFile("DpAtsp.txt", GetAverageTimes(results));
							Console.WriteLine("Koniec testów. Wciśnij dowolny klawisz aby wrócić do menu.");
							Console.ReadKey();
						}
						break;
					case '6':
						Console.Clear();
						Console.WriteLine("Zostaną przprowadzone testy dla danych z tsplib. Wciśnij ESC aby anulować albo dowolny klawisz aby kontynuować.");
						secondaryMenuKey = Console.ReadKey().KeyChar;
						if (secondaryMenuKey != 27)
						{
							Console.WriteLine("Wykonywane są testy. Może to zająć dużo czasu.");
							double[,] results = DpTestsTspLib();
							TimesToFile("DpTspLib.txt", GetAverageTimes(results));
							Console.WriteLine("Koniec testów. Wciśnij dowolny klawisz aby wrócić do menu.");
							Console.ReadKey();
						}
						break;
					case '7':
						Console.Clear();
						Console.WriteLine("Zostaną przprowadzone testy dla danych z tsplib. Wciśnij ESC aby anulować albo dowolny klawisz aby kontynuować.");
						secondaryMenuKey = Console.ReadKey().KeyChar;
						if (secondaryMenuKey != 27)
						{
							Console.WriteLine("Wykonywane są testy. Może to zająć dużo czasu.");
							Tuple<double[,], int[,]> results = TsTestsTspLib();
							TimesToFile("TsTspLibTests.txt", GetAverageTimes(results.Item1));
							DistancesToFile("TsTspLibDistances.txt", GetAverageDistances(results.Item2));
							Console.WriteLine("Koniec testów. Wciśnij dowolny klawisz aby wrócić do menu.");
							Console.ReadKey();
						}
						break;
					case '8':
						Console.Clear();
						Console.WriteLine("Zostaną przprowadzone testy dla danych z tsplib. Wciśnij ESC aby anulować albo dowolny klawisz aby kontynuować.");
						secondaryMenuKey = Console.ReadKey().KeyChar;
						if (secondaryMenuKey != 27)
						{
							Console.WriteLine("Wykonywane są testy. Może to zająć dużo czasu.");
							GaParametersTests();
							Console.WriteLine("Koniec testów. Wciśnij dowolny klawisz aby wrócić do menu.");
							Console.ReadKey();
						}
						break;
					case '9':
						Console.Clear();
						Console.WriteLine("Zostaną przprowadzone testy dla danych z tsplib. Wciśnij ESC aby anulować albo dowolny klawisz aby kontynuować.");
						secondaryMenuKey = Console.ReadKey().KeyChar;
						if (secondaryMenuKey != 27)
						{
							Console.WriteLine("Wykonywane są testy. Może to zająć dużo czasu.");
							Tuple<double[,], int[,]> results = GaTestsTspLib();
							TimesToFile("GaTspLibTests.txt", GetAverageTimes(results.Item1));
							DistancesToFile("GaTspLibDistances.txt", GetAverageDistances(results.Item2));
							Console.WriteLine("Koniec testów. Wciśnij dowolny klawisz aby wrócić do menu.");
							Console.ReadKey();
						}
						break;
					default:
						break;
				}
			} while (primaryMenuKey != 27);
		}
Exemple #13
0
		static void Main (string[] args)
		{
			TspGraph graph = new TspGraph();
			char primaryMenuKey, secondaryMenuKey;
			string input;
			CancellationTokenSource cts = new CancellationTokenSource();
			CancellationToken ct = cts.Token;

			do
			{
				Console.WriteLine ();
				Console.WriteLine ("ZSIK - problem komiwojażera");
				Console.WriteLine ("-Menu główne");
				Console.WriteLine ("--1. Wczytaj graf z pliku.");
				Console.WriteLine ("--2. Przejrzyj właściwości grafu.");
				Console.WriteLine ("--3. Rozwiąż problem algorytmem genetycznym.");
                Console.WriteLine ("--4. Rozwiąż problem algorytmem genetycznym wielowątkowo.");
                Console.WriteLine ("--5. Stwórz zestaw o określonej ilości miast.");
                Console.WriteLine("--6. Rozwiaz problem algorytmem dynamicznym.");

                primaryMenuKey = Console.ReadKey().KeyChar;

				switch (primaryMenuKey)
				{
					case '1':
						Console.Clear ();
						Console.WriteLine ("Wpisz ścieżkę pliku.");
						string filename = Console.ReadLine();
						filename = filename?.Replace(@"\", @"\\");
						graph.ReadGraphFromFile(filename);
						break;
					case '2':
						Console.Clear ();
						try
						{
							Console.WriteLine("---Graf");
							Console.WriteLine("----Nazwa: " + graph.Name);
							Console.WriteLine("----Typ: " + graph.Type);
							Console.WriteLine("----Wymiary: " + graph.Dimension);
							graph.PrintGraph();
						}
						catch (NullReferenceException)
						{
							Console.WriteLine("Graf albo jego właściwości są null.");
						}
						Console.ReadKey();
						break;
					case '3':
                        Console.Clear();
                        int pop, elites;
                        short startingIndex, endingIndex;
                        double mutrate, crossrate;

                        Console.WriteLine("---Podaj prawdopodobieństwo krzyżowania:");
                        input = Console.ReadLine();

                        while (!double.TryParse(input, out crossrate))
                        {
                            input = Console.ReadLine();
                        }

                        Console.WriteLine("---Podaj prawdopodobieństwo mutacji:");
                        input = Console.ReadLine();

                        while (!double.TryParse(input, out mutrate))
                        {
                            input = Console.ReadLine();
                        }

                        Console.WriteLine("---Podaj wielkość populacji:");
                        input = Console.ReadLine();

                        while (!Int32.TryParse(input, out pop))
                        {
                            input = Console.ReadLine();
                        }

                        Console.WriteLine("---Podaj liczbę elit:");
                        input = Console.ReadLine();

                        while (!Int32.TryParse(input, out elites))
                        {
                            input = Console.ReadLine();
                        }

                        Console.WriteLine("---Podaj wierzcholek startowy:");
                        input = Console.ReadLine();

                        while (!short.TryParse(input, out startingIndex) || startingIndex < 0 || startingIndex > graph.Dimension-1)
                        {
                            input = Console.ReadLine();
                        }

                        Console.WriteLine("---Podaj wierzcholek koncowy:");
                        input = Console.ReadLine();

                        while (!short.TryParse(input, out endingIndex) || startingIndex < 0 || startingIndex > graph.Dimension - 1)
                        {
                            input = Console.ReadLine();
                        }

                        cts = new CancellationTokenSource();
                        Console.Clear();
                        Task.Run(() => {
                            while (!cts.IsCancellationRequested)
                            {
                                Console.Write(".");
                                Thread.Sleep(1000);
                            }
                        }, ct);
                        TspGenetic.SolveTsp(graph, elites, crossrate, mutrate, pop, startingIndex, endingIndex, CrossoverType.OX, 300, 1000);
                        cts.Cancel();
                        cts.Dispose();
                        Console.WriteLine();
                        TspGenetic.ShowResults();
                        Console.ReadKey();
                        break;
                    case '4':
                        Console.Clear();
                        int tasksAmount = 0;

                        Console.WriteLine("---Podaj ilosc watkow:");
                        input = Console.ReadLine();

                        while (!int.TryParse(input, out tasksAmount))
                        {
                            input = Console.ReadLine();
                        }

                        Console.WriteLine("---Podaj prawdopodobieństwo krzyżowania:");
                        input = Console.ReadLine();

                        while (!double.TryParse(input, out crossrate))
                        {
                            input = Console.ReadLine();
                        }

                        Console.WriteLine("---Podaj prawdopodobieństwo mutacji:");
                        input = Console.ReadLine();

                        while (!double.TryParse(input, out mutrate))
                        {
                            input = Console.ReadLine();
                        }

                        Console.WriteLine("---Podaj wielkość populacji:");
                        input = Console.ReadLine();

                        while (!Int32.TryParse(input, out pop))
                        {
                            input = Console.ReadLine();
                        }

                        Console.WriteLine("---Podaj liczbę elit:");
                        input = Console.ReadLine();

                        while (!Int32.TryParse(input, out elites))
                        {
                            input = Console.ReadLine();
                        }

                        Console.WriteLine("---Podaj wierzcholek startowy:");
                        input = Console.ReadLine();

                        while (!short.TryParse(input, out startingIndex) || startingIndex < 0 || startingIndex > graph.Dimension - 1)
                        {
                            input = Console.ReadLine();
                        }

                        Console.WriteLine("---Podaj wierzcholek koncowy:");
                        input = Console.ReadLine();

                        while (!short.TryParse(input, out endingIndex) || startingIndex < 0 || startingIndex > graph.Dimension - 1)
                        {
                            input = Console.ReadLine();
                        }

                        cts = new CancellationTokenSource();
                        Console.Clear();
                        Task.Run(() => {
                            while (!cts.IsCancellationRequested)
                            {
                                Console.Write(".");
                                Thread.Sleep(1000);
                            }
                        }, ct);
                        TspGenetic.SolveTsp(graph, elites, crossrate, mutrate, pop, startingIndex, endingIndex, CrossoverType.OX, 300, 1000, tasksAmount);
                        cts.Cancel();
                        cts.Dispose();
                        Console.WriteLine();
                        TspGenetic.ShowResults();
                        Console.ReadKey();
                        break;
                    case '5':
						Console.Clear();
						int dim, maxDist;
						Console.WriteLine("---Podaj ilość miast:");
						input = Console.ReadLine();

						while (!Int32.TryParse(input, out dim))
						{
							input = Console.ReadLine();
						}

						Console.WriteLine("---Równe odległości A-B i B-A? (t - tak, n - nie)");
						
						do
						{
							input = Console.ReadLine() ?? "";
						} while (!(input.Equals("t") || input.Equals("n")));

						bool tspOrAtsp = false;

						if (input.Equals("t")) tspOrAtsp = false;
						else if (input.Equals("n")) tspOrAtsp = true;

						Console.WriteLine("---Podaj maksymalną odległość między miastami:");
						input = Console.ReadLine();

						while (!Int32.TryParse(input, out maxDist))
						{
							input = Console.ReadLine();
						}

						graph = new TspGraph(dim, tspOrAtsp, maxDist);
						Console.WriteLine("---Utworzono graf o następujących właściwościach:");
						Console.WriteLine("----Nazwa: " + graph.Name);
						Console.WriteLine("----Typ: " + graph.Type);
						Console.WriteLine("----Wymiary: " + graph.Dimension);
						Console.ReadKey();
						break;
                    case '6':
                        cts = new CancellationTokenSource();
                        Console.Clear();
                        Task.Run(() => {
                            while (!cts.IsCancellationRequested)
                            {
                                Console.Write(".");
                                Thread.Sleep(1000);
                            }
                        }, ct);
                        TspDynamicProgramming.SolveTsp(graph);
                        cts.Cancel();
                        cts.Dispose();
                        Console.WriteLine();
                        TspDynamicProgramming.ShowResults();
                        TspDynamicProgramming.ClearCollections();
                        Console.ReadKey();
                        break;
                    default:
						break;
				}
			} while (primaryMenuKey != 27);
		}
Exemple #14
0
		private static bool Init(TspGraph input, int generations, int populationSize, int elitismSize, int tournamentSize, double mutationRate, double crossoverRate, CrossoverType cT, int limit, short sIndex, short eIndex, int tasksAmount = 1)
		{
			try
			{
				InputGraph = input.GraphMatrix;
				numbOfCities = input.Dimension;
				Generations = generations;
				PopulationSize = populationSize;
				ElitismSize = elitismSize;
				TournamentSize = tournamentSize;
				MutationRate = mutationRate;
				CrossoverRate = crossoverRate;
				globalBestFitness = 0;
				rand1 = new Random();
				rand2 = new Random();
				cType = cT;
				timeLimit = limit;
                startingIndex = sIndex;
                endingIndex = eIndex;
                solutionFound = false;
                threadsAmount = tasksAmount;
                taskId = 0;

                threadsFitnessList = new List<List<double>>(threadsAmount);
                for(int i = 0; i < threadsAmount; i++)
                {
                    threadsFitnessList.Add(new List<double>(populationSize / threadsAmount));
                }

                threadsPopulations = new List<List<List<Int16>>>(threadsAmount);

                for (int i = 0; i < threadsAmount; i++)
                {
                    threadsPopulations.Add(new List<List<Int16>>(populationSize / threadsAmount));
                    for (int j = 0; j < threadsPopulations[i].Capacity; j++) {
                        threadsPopulations[i].Add(new List<short>(numbOfCities));
                        threadsPopulations[i][j].Add(startingIndex);
                        for (Int16 k = 0; k < threadsPopulations[i][j].Capacity; k++)
                        {
                            if (k == startingIndex || k == endingIndex) continue;
                            threadsPopulations[i][j].Add(k);
                        }
                        threadsPopulations[i][j].Add(endingIndex);
                        RandomizeSolution(threadsPopulations[i][j], j);
                        threadsFitnessList[i].Add(CalculateFitness(threadsPopulations[i][j]));
                    }
                }

				fitnessList = new List<double>(populationSize);
				population = new List<List<Int16>>(populationSize);

				for (int i = 0; i < population.Capacity; i++)
				{
					population.Add(new List<short>(numbOfCities));
                    population[i].Add(startingIndex);
                    for (Int16 j = 0; j < population[i].Capacity; j++)
                    {
                        if (j == startingIndex || j == endingIndex) continue;
                        population[i].Add(j);
                    }
					population[i].Add(endingIndex);
					RandomizeSolution(population[i], i);
					fitnessList.Add(CalculateFitness(population[i]));
				}
			}
			catch (Exception e)
			{
				Console.WriteLine("Błąd. Opis błędu: " + e);
				return false;
			}

			return true;
		}