Beispiel #1
0
        static void TuneSecond()
        {
            var dataSets = new DataSets.OptimizationDataSets().TwentyDataSets.Take(15).ToArray();

            const int numValues = 4;
            const int numRuns   = 1;
            var       table     = new int[numRuns][];
            var       allCosts  = new int[numRuns][][];
            const int timeLimit = 360 * 1000;

            {
                var calcDuration = ((numValues) * dataSets.Length * (timeLimit / 1000) * numRuns);
                Console.WriteLine($"calculated duration: {calcDuration}s , {calcDuration / 60} min, {calcDuration / 3600d} h");
            }

            for (int run = 0; run < numRuns; run++)
            {
                table[run]    = new int[numValues];
                allCosts[run] = new int[numValues][];
                for (int vrpTimeLimitFactor = 0; vrpTimeLimitFactor < numValues; vrpTimeLimitFactor++)
                {
                    Console.ForegroundColor = ConsoleColor.Cyan;
                    Console.WriteLine($"tuning param: {vrpTimeLimitFactor}");
                    Console.ForegroundColor           = ConsoleColor.White;
                    allCosts[run][vrpTimeLimitFactor] = new int[dataSets.Length];
                    var cumCost = 0;

                    var i = 0;
                    foreach (var dataSet in dataSets)
                    {
                        double factor = 0;
                        switch (vrpTimeLimitFactor)
                        {
                        case 0:
                            factor = 0;
                            break;

                        case 1:
                            factor = 0.05;
                            break;

                        case 2:
                            factor = 0.10;
                            break;

                        case 3:
                            factor = 0.15;
                            break;
                        }
                        // ReSharper disable PossibleLossOfFraction
                        var solver = new IRuettae.Core.ILP2.Solver(dataSet, factor);
                        allCosts[run][vrpTimeLimitFactor][i] = solver.Solve(timeLimit, null, null).Cost();

                        cumCost += allCosts[run][vrpTimeLimitFactor][i];
                        i++;
                    }


                    table[run][vrpTimeLimitFactor] = cumCost;
                }


                // write result in csv file
                using (var sw = new StreamWriter($"{DateTime.Now:yy-MM-dd-HH-mm-ss}{run}.csv"))
                {
                    sw.WriteLine(string.Join(";", table[run]));
                    Console.WriteLine(string.Join(";", table[run]));
                }

                // write full result in csv file
                using (var sw = new StreamWriter($"{DateTime.Now:yy-MM-dd-HH-mm-ss}full_{run}.csv"))
                {
                    var results = new string[allCosts[run].Length];
                    for (int x = 0; x < allCosts[run].Length; x++)
                    {
                        if (allCosts[run][x] != null)
                        {
                            var cellResult = new int[allCosts[run][x].Length];
                            for (int res = 0; res < allCosts[run][x].Length; res++)
                            {
                                cellResult[res] = allCosts[run][x][res];
                            }

                            results[x] = string.Join(",", cellResult);
                        }
                        else
                        {
                            results[x] = "";
                        }
                    }

                    sw.WriteLine(string.Join(";", results));
                    Console.WriteLine(string.Join(";", results));
                }
            }
        }
        private static void EvaluateAlgorithm(string[] args)
        {
            BigHr();
            Console.WriteLine("Program written to evaluate the different optimisation algorithms.");
            Console.WriteLine();

            var algorithmSelection = args.Length == 0 ? QueryAlgorithmSelection() : (Algorithms)Enum.Parse(typeof(Algorithms), args[0]);

            SmallHr();
            Console.WriteLine();

            var datasetSelection = args.Length == 0 ? QueryDatasetSelection() : GetDatasetSelection(int.Parse(args[1]));

            SmallHr();
            Console.WriteLine();

            var runs = args.Length == 0 ? QueryNumberOfRuns() : int.Parse(args[2]);

            SmallHr();
            Console.WriteLine();
            Console.WriteLine("Starting the algorithm now");
            BigHr();


            for (int i = 0; i < runs; i++)
            {
                foreach (var dataset in datasetSelection)
                {
                    try
                    {
                        var(input, coordinates, timelimit) = GetDataset(dataset);
                        string  savepath   = $"{DateTime.Now:yy-MM-dd-HH-mm-ss}_DataSet_{dataset}";
                        ISolver solver     = null;
                        var     fastFactor = 60;
                        switch (algorithmSelection)
                        {
                        case Algorithms.ILPFast:
                            timelimit /= fastFactor;
                            goto case Algorithms.ILP;

                        case Algorithms.ILP:
                            solver = new ILPSolver(input, new ILPConfig
                            {
                                ClusteringMIPGap = 0,
                                SchedulingMIPGap = 0,

                                ClusteringTimeLimitMiliseconds = (long)(0.7 * timelimit),
                                SchedulingTimeLimitMiliseconds = (long)(0.3 * timelimit),
                                TimeSliceDuration = 120
                            });
                            savepath += "_ILP";
                            break;

                        case Algorithms.LocalSolverFast:
                            timelimit /= fastFactor;
                            goto case Algorithms.LocalSolver;

                        case Algorithms.LocalSolver:
                            solver = new Solver(input, new LocalSolverConfig
                            {
                                VrpTimeLimitFactor          = 0.1,
                                VrptwTimeLimitFactor        = 0.8,
                                MaxNumberOfAdditionalSantas = 0,
                            });
                            savepath += "_LocalSolver";
                            break;

                        case Algorithms.GA:
                            solver    = new GenAlgSolver(input, new GenAlgConfig(input));
                            savepath += "_GA";
                            break;

                        case Algorithms.GAFast:
                            timelimit /= fastFactor;
                            solver     = new GenAlgSolver(input, new GenAlgConfig(input));
                            savepath  += "_GAFast";
                            break;

                        case Algorithms.ILP2Fast:
                            timelimit /= fastFactor;
                            goto case Algorithms.ILP2;

                        case Algorithms.ILP2:
                            solver    = new IRuettae.Core.ILP2.Solver(input, 0.1, dataset.ToString());
                            savepath += "_ILP2";
                            break;

                        case Algorithms.ILPIP5GurobiFast:
                            timelimit /= fastFactor;
                            goto case Algorithms.ILPIP5Gurobi;

                        case Algorithms.ILPIP5Gurobi:
                            solver = new ILPIp5GurobiSolver(input, new ILPIp5GurobiConfig
                            {
                                ClusteringMIPGap = 0,
                                SchedulingMIPGap = 0,

                                ClusteringTimeLimitMiliseconds = (long)(0.7 * timelimit),
                                SchedulingTimeLimitMiliseconds = (long)(0.3 * timelimit),
                                TimeSliceDuration = 120
                            });
                            savepath += "_ILPIp5Gurobi";
                            break;

                        case Algorithms.GoogleRoutingFast:
                            timelimit /= fastFactor;
                            solver     = new GoogleRoutingSolver(input, GoogleRoutingConfig.GetDefault(input));
                            savepath  += "_GoogleRoutingFast";
                            break;

                        case Algorithms.GoogleRouting:
                            solver    = new GoogleRoutingSolver(input, GoogleRoutingConfig.GetDefault(input));
                            savepath += "_GoogleRouting";
                            break;
                        }

                        AddUnavailableBetweenDays(input);

                        OptimizationResult result = null;

                        void WriteConsoleInfo(object sender, string s)
                        {
                            Console.WriteLine($"Info ({DateTime.Now:HH-mm-ss}): {s}");
                        }

                        void WriteConsoleProgress(object sender, ProgressReport report)
                        {
                            Console.WriteLine($"Progress: {report}");
                        }
#if DEBUG
                        using (var sw = new StreamWriter(savepath + "-log.txt", true))
                        {
                            result = solver.Solve(timelimit, WriteConsoleProgress,
                                                  (sender, s) =>
                            {
                                WriteConsoleInfo(sender, s);
                                sw.WriteLine(s);
                            });
                        }
#else
                        result = solver.Solve(timelimit, WriteConsoleProgress, WriteConsoleInfo);
#endif

                        BigHr();

                        File.WriteAllText(savepath + ".json", JsonConvert.SerializeObject(result));

                        var summary = new StringBuilder();
                        summary.AppendLine($"Solver: {AlgorithmsDictionary[algorithmSelection]}");
                        summary.AppendLine($"Dataset{dataset}: {DatasetDictionary[dataset]}");
                        summary.AppendLine($"TimeElapsed [s]: {result.TimeElapsed}");
                        try
                        {
                            if (!result.IsValid())
                            {
                                summary.AppendLine(
                                    $"IMPORTANT: This result seems to be invalid. The reason is \"{result.Validate()}\"");
                            }
                        }
                        catch
                        {
                            summary.AppendLine("error while checking invalidity");
                        }

                        summary.AppendLine($"Cost: {result.Cost()}");
                        summary.AppendLine($"NumberOfNotVisitedFamilies: {result.NumberOfNotVisitedFamilies()}");
                        summary.AppendLine($"NumberOfMissingBreaks: {result.NumberOfMissingBreaks()}");
                        summary.AppendLine($"NumberOfAdditionalSantas: {result.NumberOfAdditionalSantas()}");
                        summary.AppendLine($"AdditionalSantaWorkTime: {result.AdditionalSantaWorkTime()}");
                        summary.AppendLine($"VisitTimeInUnavailable: {result.VisitTimeInUnavailable()}");
                        summary.AppendLine($"WayTimeOutsideBusinessHours: {result.WayTimeOutsideBusinessHours()}");
                        summary.AppendLine($"VisitTimeInDesired: {result.VisitTimeInDesired()}");
                        summary.AppendLine($"SantaWorkTime: {result.SantaWorkTime()}");
                        summary.AppendLine($"LongestDay: {result.LongestDay()}");
                        summary.AppendLine($"NumberOfRoutes: {result.NumberOfRoutes()}");

                        File.WriteAllText(savepath + ".txt", summary.ToString());
                        Console.WriteLine();
                        Console.WriteLine("Done solving");
                        Console.WriteLine(summary.ToString());
                        ResultDrawer.DrawResult(savepath, result, coordinates);
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine($"An exception occured: {e.Message}");
                    }
                }
            }
        }