コード例 #1
0
        private void RunGreedyVsNoGreedy()
        {
            var resultsFile = $"{ResultsFolder}greedy_vs_ilp_{DateTime.Now.ToString("yyyy-dd-M-HH-mm-ss")}.csv";

            using (var writer = new StreamWriter(resultsFile))
                using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                {
                    csv.WriteHeader(typeof(GreedyVsILPResult));
                    csv.NextRecord();
                }

            for (int i = 1; i <= NumberOfInstances; i++)
            {
                using (var stream = File.Open(resultsFile, FileMode.Append))
                    using (var writer = new StreamWriter(stream))
                        using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                        {
                            csv.Configuration.HasHeaderRecord = false;

                            var instanceFile = $"{InstancesFolder}/Exact{i}.txt";
                            var configFile   = $"{ConfigsFolder}tune_Exact{i}_0.prm";

                            var cinemaILP    = CinemaReader.Read(instanceFile);
                            var cinemaGreedy = CinemaReader.Read(instanceFile);;

                            var solverILP = new ILPSolver(cinemaILP);
                            var timesILP  = solverILP.Solve(false, Options.Debug, paramFile: configFile);

                            var greedySolver = new GreedySolver(cinemaGreedy);
                            var timesGreedy  = greedySolver.Solve();

                            var result = new GreedyVsILPResult
                            {
                                InstanceFile        = instanceFile,
                                ConfigFile          = configFile,
                                TotalNumberOfGroups = cinemaILP.TotalNumberOfGroups,
                                TotalNumberOfPeople = cinemaILP.TotalNumberOfPeople,

                                ILPTime    = timesILP["Total"],
                                GreedyTime = timesGreedy["Total"],

                                SeatedGreedy = cinemaGreedy.CountSeated(),
                                SeatedILP    = cinemaILP.CountSeated(),

                                ValidGreedy = cinemaGreedy.Verify(),
                                ValidILP    = cinemaILP.Verify(),

                                Capacity = cinemaGreedy.InitialCapacity
                            };

                            csv.WriteRecord(result);
                            csv.NextRecord();
                        }
            }
        }
コード例 #2
0
        private void RunTuneVsNoTune()
        {
            var resultsFile = $"{ResultsFolder}tune_vs_no_tune_{DateTime.Now.ToString("yyyy-dd-M-HH-mm-ss")}.csv";

            using (var writer = new StreamWriter(resultsFile))
                using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                {
                    csv.WriteHeader(typeof(TuneVsNoTuneResult));
                    csv.NextRecord();
                }

            for (int i = 1; i <= NumberOfInstances; i++)
            {
                using (var stream = File.Open(resultsFile, FileMode.Append))
                    using (var writer = new StreamWriter(stream))
                        using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                        {
                            csv.Configuration.HasHeaderRecord = false;

                            var instanceFile = $"{InstancesFolder}/Exact{i}.txt";
                            var configFile   = $"{ConfigsFolder}tune_Exact{i}_0.prm";

                            var cinemaTuned    = CinemaReader.Read(instanceFile);
                            var cinemaNotTuned = CinemaReader.Read(instanceFile);;

                            var solverTuned = new ILPSolver(cinemaTuned);
                            var timesTuned  = solverTuned.Solve(false, Options.Debug, paramFile: configFile);

                            var solverNotTuned = new ILPSolver(cinemaNotTuned);
                            var timesNotuned   = solverNotTuned.Solve(false, Options.Debug);

                            var result = new TuneVsNoTuneResult
                            {
                                InstanceFile        = instanceFile,
                                ConfigFileTuned     = configFile,
                                TotalNumberOfGroups = cinemaTuned.TotalNumberOfGroups,
                                TotalNumberOfPeople = cinemaTuned.TotalNumberOfPeople,

                                OptimizationTimeNotTuned = timesNotuned["Optimizing"],
                                OptimizationTimeTuned    = timesTuned["Optimizing"],

                                SeatedNotTuned = cinemaNotTuned.CountSeated(),
                                SeatedTuned    = cinemaTuned.CountSeated(),

                                ValidNotTuned = cinemaNotTuned.Verify(),
                                ValidTuned    = cinemaTuned.Verify()
                            };

                            csv.WriteRecord(result);
                            csv.NextRecord();
                        }
            }
        }
コード例 #3
0
        private void RunNormalSolver()
        {
            var resultsFile = $"{ResultsFolder}normal_solver_{DateTime.Now.ToString("yyyy-dd-M-HH-mm-ss")}.csv";

            using (var writer = new StreamWriter(resultsFile))
                using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                {
                    csv.WriteHeader(typeof(NormalILPSolverResult));
                    csv.NextRecord();
                }

            for (int i = 1; i <= NumberOfInstances; i++)
            {
                using (var stream = File.Open(resultsFile, FileMode.Append))
                    using (var writer = new StreamWriter(stream))
                        using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                        {
                            csv.Configuration.HasHeaderRecord = false;

                            var instanceFile = $"{InstancesFolder}/Exact{i}.txt";
                            var configFile   = $"{ConfigsFolder}tune_Exact{i}_0.prm";

                            var cinema = CinemaReader.Read(instanceFile);

                            var solver = new ILPSolver(cinema);
                            var times  = solver.Solve(false, Options.Debug, paramFile: configFile);

                            var result = new NormalILPSolverResult
                            {
                                InstanceFile = instanceFile,
                                ConfigFile   = configFile,

                                TotalNumberOfGroups = cinema.TotalNumberOfGroups,
                                TotalNumberOfPeople = cinema.TotalNumberOfPeople,

                                OptimizationTime = times["Optimizing"],
                                ConstraintTime   = times["Add Constraints"],

                                Seated   = cinema.CountSeated(),
                                Valid    = cinema.Verify(),
                                Capacity = cinema.InitialCapacity
                            };

                            csv.WriteRecord(result);
                            csv.NextRecord();
                        }
            }
        }
コード例 #4
0
        private void RunTuning()
        {
            var resultsFile = $"{ResultsFolder}greedy_vs_ilp_{DateTime.Now.ToString("yyyy-dd-M-HH-mm-ss")}.csv";

            using (var writer = new StreamWriter(resultsFile))
                using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                {
                    csv.WriteHeader(typeof(GreedyVsILPResult));
                    csv.NextRecord();
                }

            for (int i = 1; i <= NumberOfInstances; i++)
            {
                var instanceFile = $"{InstancesFolder}/Exact{i}.txt";
                var configFile   = $"{ConfigsFolder}tune_Exact{i}_0.prm";
                var tuneFile     = $"{TuneFolder}tune_Exact{i}_0.prm";

                var cinemaILP = CinemaReader.Read(instanceFile);

                var solverILP = new ILPSolver(cinemaILP);
                solverILP.Solve(true, Options.Debug, tuneFile, configFile);
            }
        }
コード例 #5
0
        private void RunTryGreedyFirst()
        {
            var resultsFile = $"{ResultsFolder}greedy_first_{DateTime.Now.ToString("yyyy-dd-M-HH-mm-ss")}.csv";

            using (var writer = new StreamWriter(resultsFile))
                using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                {
                    csv.WriteHeader(typeof(TryGreedyFirstResult));
                    csv.NextRecord();
                }

            for (int i = 1; i <= NumberOfInstances; i++)
            {
                using (var stream = File.Open(resultsFile, FileMode.Append))
                    using (var writer = new StreamWriter(stream))
                        using (var csv = new CsvWriter(writer, CultureInfo.InvariantCulture))
                        {
                            csv.Configuration.HasHeaderRecord = false;

                            var instanceFile = $"{InstancesFolder}/Exact{i}.txt";
                            var configFile   = $"{ConfigsFolder}tune_Exact{i}_0.prm";

                            var cinema = CinemaReader.Read(instanceFile);

                            var greedyCinema = CinemaReader.Read(instanceFile);
                            var greedySolver = new GreedySolver(greedyCinema);

                            var totalTime = Utils.TimeAction(() =>
                            {
                                var times = greedySolver.Solve();

                                if (cinema.TotalNumberOfPeople != greedyCinema.CountSeated())
                                {
                                    var ilpCinema = CinemaReader.Read(instanceFile);
                                    var ilpSolver = new ILPSolver(ilpCinema);

                                    times = ilpSolver.Solve(Options.Tune, Options.Debug, Options.TuneOutputFile, configFile);

                                    cinema = ilpCinema;
                                }
                                else
                                {
                                    cinema = greedyCinema;
                                }
                            }, "Time");

                            var result = new TryGreedyFirstResult
                            {
                                InstanceFile = instanceFile,
                                ConfigFile   = configFile,

                                TotalNumberOfGroups = cinema.TotalNumberOfGroups,
                                TotalNumberOfPeople = cinema.TotalNumberOfPeople,

                                TotalTime = totalTime,

                                Seated   = cinema.CountSeated(),
                                Valid    = cinema.Verify(),
                                Capacity = cinema.InitialCapacity
                            };

                            csv.WriteRecord(result);
                            csv.NextRecord();
                        }
            }
        }
コード例 #6
0
        public void Run()
        {
            var totalTime = Utils.TimeAction(() =>
            {
                var cinema = CinemaReader.Read(Options.InstanceConfig.InstanceFile);
                Dictionary <string, string> times = new Dictionary <string, string>();

                Console.WriteLine("Solving: " + Options.InstanceConfig.InstanceFile);
                Console.WriteLine(cinema);

                if (Options.GreedyOnly)
                {
                    var solver = new GreedySolver(cinema);

                    if (Options.Debug)
                    {
                        Console.WriteLine("Solving via Greedy only");
                    }

                    times = solver.Solve();
                }
                else if (Options.ILPOnly)
                {
                    var solver = new ILPSolver(cinema);

                    if (Options.Debug)
                    {
                        Console.WriteLine("Solving via ILP only");
                    }

                    times = solver.Solve(Options.Tune, Options.Debug, Options.TuneOutputFile, Options.InstanceConfig.ConfigFile);
                }
                else
                {
                    if (Options.Debug)
                    {
                        Console.WriteLine("Solving via both");
                    }

                    var greedyCinema = CinemaReader.Read(Options.InstanceConfig.InstanceFile);
                    var greedySolver = new GreedySolver(greedyCinema);

                    times = greedySolver.Solve();

                    if (cinema.TotalNumberOfPeople != greedyCinema.CountSeated())
                    {
                        var ilpCinema = CinemaReader.Read(Options.InstanceConfig.InstanceFile);
                        var ilpSolver = new ILPSolver(ilpCinema);

                        times = ilpSolver.Solve(Options.Tune, Options.Debug, Options.TuneOutputFile, Options.InstanceConfig.ConfigFile);

                        cinema = ilpCinema;
                    }
                    else
                    {
                        cinema = greedyCinema;
                    }
                }

                Console.WriteLine(cinema);
                Console.WriteLine("People seated:" + cinema.CountSeated() + " out of " + cinema.TotalNumberOfPeople);
                Console.WriteLine($"Valid cinema:{cinema.Verify()}");
            }, "Time");

            Console.WriteLine($"Total Solving Time: {totalTime}");
        }
コード例 #7
0
        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}");
                    }
                }
            }
        }