public static void run() { ArrayList scores = new ArrayList(); for (int file = 0; file < 3; file++) { scores = new ArrayList(); Specimen fileBest = new Specimen(); fileBest.Cost = int.MaxValue; MatrixReader mr = new MatrixReader(); mr.load(Variables.Paths[file]); Console.WriteLine(mr.name); for (int crossParam = 1; crossParam <= 2; crossParam++) { for (int mutatuinParam = 1; mutatuinParam <= 2; mutatuinParam++) { for (int PopulationSizeIndex = 0; PopulationSizeIndex < 3; PopulationSizeIndex++) { for (int ttl = 40; ttl <= 120; ttl += 40) { int cost = 0; for (int i = 0; i < 3; i++) { TSPGenetic tsp = new TSPGenetic(mr.Matrix, ttl, Variables.PopulationSize[PopulationSizeIndex], 0.01f, 0.8f, mutatuinParam, crossParam); tsp.run(); cost += tsp.GetBestSpecimen().Cost; if (tsp.GetBestSpecimen().Cost < fileBest.Cost) { fileBest = tsp.GetBestSpecimen(); } } scores.Add(new Score(mutatuinParam, crossParam, ttl, cost / 3, 0.01f, 0.8f, Variables.PopulationSize[PopulationSizeIndex])); Console.WriteLine(((Score)scores[scores.Count - 1]).CrossingType + " " + ((Score)scores[scores.Count - 1]).MutationType + " " + ((Score)scores[scores.Count - 1]).PopulationSize); } } } } using (StreamWriter streamWriter = File.CreateText(@"D:\tsp3\wyniki\pkt2" + mr.name + ".txt")) { var writer = new CsvWriter(streamWriter); writer.Configuration.RegisterClassMap <ScoreMap>(); writer.WriteRecords(scores); } //wybieranie najlepszego trymu mutacji i krzyżowania Score best = new Score(1, 1, 1, int.MaxValue, 0f, 0f, 0); foreach (Score score in scores) { if (score.Cost < best.Cost) { best = (Score)score; } } int bestmutator; int bestcrosser; if (best.CrossingType.Equals("PMX")) { bestcrosser = 1; } else { bestcrosser = 2; } if (best.MutationType.Equals("Invert")) { bestmutator = 1; } else { bestmutator = 2; } scores = new ArrayList(); float[] mutators = { 0.01f, 0.05f, 0.1f }; float[] crossators = { 0.5f, 0.7f, 0.9f }; for (int mutator = 0; mutator < 3; mutator++) { int cost = 0; for (int i = 0; i < 3; i++) { TSPGenetic tsp = new TSPGenetic(mr.Matrix, 60, best.PopulationSize, mutators[mutator], 0.8f, bestmutator, bestcrosser); tsp.run(); cost += tsp.GetBestSpecimen().Cost; if (tsp.GetBestSpecimen().Cost < fileBest.Cost) { fileBest = tsp.GetBestSpecimen(); } } scores.Add(new Score(bestmutator, bestcrosser, 60, cost / 3, mutators[mutator], 0.8f, best.PopulationSize)); Console.WriteLine("Mutation " + mutator); } using (StreamWriter streamWriter = File.CreateText(@"D:\tsp3\wyniki\pkt3" + mr.name + ".txt")) { var writer = new CsvWriter(streamWriter); writer.Configuration.RegisterClassMap <ScoreMap>(); writer.WriteRecords(scores); } scores = new ArrayList(); for (int crossator = 0; crossator < 3; crossator++) { int cost = 0; for (int i = 0; i < 3; i++) { TSPGenetic tsp = new TSPGenetic(mr.Matrix, 60, best.PopulationSize, 0.01f, crossators[crossator], bestmutator, bestcrosser); tsp.run(); cost += tsp.GetBestSpecimen().Cost; if (tsp.GetBestSpecimen().Cost < fileBest.Cost) { fileBest = tsp.GetBestSpecimen(); } } scores.Add(new Score(bestmutator, bestcrosser, 60, cost / 3, 0.01f, crossators[crossator], best.PopulationSize)); Console.WriteLine("Crossing " + crossator); } using (StreamWriter streamWriter = File.CreateText(@"D:\tsp3\wyniki\pkt4" + mr.name + ".txt")) { var writer = new CsvWriter(streamWriter); writer.Configuration.RegisterClassMap <ScoreMap>(); writer.WriteRecords(scores); } using (StreamWriter streamWriter = File.CreateText(@"D:\tsp3\wyniki\pkt5" + mr.name + ".txt")) { streamWriter.WriteLine("Cost: " + fileBest.Cost + "\n"); foreach (var i in fileBest.Path) { streamWriter.Write(i + "=>"); } streamWriter.Write("\b"); streamWriter.Write("\b"); } } }
public static void menu() { bool exist = true; int chose, chose1; int populationSize = 300; int temp;//zmienna tymczasowa do wczytywania intów float tempFloat; float mutationFactor = 0.1f; float crossingFactor = 0.8f; int crossingMethod = 1; int mutationMethod = 1; MatrixReader matrixReader = new MatrixReader(); int ttl = 30; //domyślne kryterium stopu TSPGenetic tsp; bool loaded = false; while (exist) { System.Console.WriteLine( "1. Wczytaj dane\n2. Kryterium stopu\n3. Rozmiar populacji początkowej\n4. Współczynnik mutacji\n5. Współczynnik krzyżowania\n6. Wybór metody krzyżowania\n7. Wybór metody mutacji\n8. Uruchom algorytm\n9. Testy\n0 Zakończ program"); chose = int.Parse(System.Console.ReadLine()); switch (chose) { case 1: string path = Variables.Paths[0]; System.Console.WriteLine("1. ftv47\n2. ftv170\n3. rbg403\n4.Inny plik"); if (!int.TryParse(System.Console.ReadLine(), out chose1)) { System.Console.WriteLine("To nie jest liczba"); } else { bool exist1 = true; while (exist1) { switch (chose1) { case 1: path = Variables.Paths[0]; exist1 = false; break; case 2: path = Variables.Paths[1]; exist1 = false; break; case 3: path = Variables.Paths[2]; exist1 = false; break; case 4: Console.WriteLine("Podaj scieżkę: "); path = Console.ReadLine(); if (!System.IO.File.Exists(path)) { exist1 = false; } else { Console.WriteLine("Plik nie istnieje"); } break; } } matrixReader.load(path); matrixReader.print(); loaded = true; } break; case 2: System.Console.WriteLine("Podaj wartość stopu w sekundach: "); if (!int.TryParse(System.Console.ReadLine(), out temp)) { System.Console.WriteLine("To nie jest liczba"); } else { if (temp > 0 && temp < 10000) { ttl = temp; } else { System.Console.WriteLine("Liczba spoza zakresu"); } } break; case 3: System.Console.WriteLine("Podaj wartość stopu w sekundach: "); if (!int.TryParse(System.Console.ReadLine(), out temp)) { System.Console.WriteLine("To nie jest liczba"); } else { populationSize = temp; } break; case 4: System.Console.WriteLine("Podaj wartość współczynnika mutacji: "); if (!float.TryParse(System.Console.ReadLine(), out tempFloat)) { System.Console.WriteLine("To nie jest liczba"); } else { mutationFactor = tempFloat; } break; case 5: System.Console.WriteLine("Podaj wartość współczynnika krzyżowania: "); if (!float.TryParse(System.Console.ReadLine(), out tempFloat)) { System.Console.WriteLine("To nie jest liczba"); } else { crossingFactor = tempFloat; } break; case 6: System.Console.WriteLine("1. PMX\n2. OX "); if (!int.TryParse(System.Console.ReadLine(), out temp)) { System.Console.WriteLine("To nie jest liczba"); } else { if (temp > 0 && temp < 3) { crossingMethod = temp; } else { System.Console.WriteLine("Liczba spoza zakresu"); } } break; case 7: System.Console.WriteLine("1. Invert 1\n2. Scrumble"); if (!int.TryParse(System.Console.ReadLine(), out temp)) { System.Console.WriteLine("To nie jest liczba"); } else { if (temp > 0 && temp < 3) { mutationMethod = temp; } else { System.Console.WriteLine("Liczba spoza zakresu"); } } break; case 8: if (loaded) { tsp = new TSPGenetic(matrixReader.Matrix, ttl, populationSize, mutationFactor, crossingFactor, mutationMethod, crossingMethod); tsp.run(); Console.WriteLine("Najleszy koszt to: " + tsp.GetBestSpecimen().Cost + ", który został uzyskany przy ścieżce:"); tsp.printPath(tsp.GetBestSpecimen().Path); } else { Console.WriteLine("Najpierw wczytaj dane"); } break; case 9: Test.run(); break; case 0: exist = false; break; default: Console.WriteLine("Poza zakresem"); break; } } }