/// <summary>
        /// Solves the instance
        /// </summary>
        protected override void Solve()
        {
            // Initialize meta-info
            VolumeOfContainers = Instance.Containers.Sum(c => c.Mesh.Volume);

            // Init ordering
            List <Container>     containers = null;
            List <VariablePiece> pieces     = null;

            int[][] orientationsPerPiece = null;
            Init(out containers, out pieces, out orientationsPerPiece);

            // Generate initial solution
            ExtremePointInsertion(Solution, containers, pieces, orientationsPerPiece);

            // Improve solution
            if (Config.Improvement)
            {
                // Log visual
                LogVisuals(Solution, true);

                // Improve
                Solution = GASP(Solution, containers, pieces, orientationsPerPiece, ExtremePointInsertion);
            }

            // Log
            if (Config.Log != null)
            {
                Config.Log("EPs available: " + Solution.ExtremePoints.Sum(kvp => kvp.Count) + "\n");
                Config.Log(Solution.VolumeContained.ToString(ExportationConstants.EXPORT_FORMAT_SHORT, ExportationConstants.FORMATTER) + " / " +
                           VolumeOfContainers.ToString(ExportationConstants.EXPORT_FORMAT_SHORT, ExportationConstants.FORMATTER) + "\n");
            }
        }
예제 #2
0
 /// <summary>
 /// Logs basic information about the progress
 /// </summary>
 /// <param name="piece">The currently added piece</param>
 /// <param name="container">The container to which the piece is added</param>
 protected void LogProgress(COSolution solution, Piece piece, Container container)
 {
     if (Config.Log != null && DateTime.Now.Ticks - LogOldMillis > 10000000)
     {
         LogOldMillis = DateTime.Now.Ticks;
         Config.Log(solution.ExploitedVolume.ToString(ExportationConstants.EXPORT_FORMAT_SHORT, ExportationConstants.FORMATTER) + " / " +
                    VolumeOfContainers.ToString(ExportationConstants.EXPORT_FORMAT_SHORT, ExportationConstants.FORMATTER) +
                    " - Piece " + piece.ID + " -> Container " + container.ID + "\n");
     }
 }
예제 #3
0
        protected override void Solve()
        {
            // --> Initialization
            // Initialize meta-info
            VolumeOfContainers = Instance.Containers.Sum(c => c.Mesh.Volume);

            // Init ordering
            List <VariablePiece> pieces = null;

            Init(out Solution.ConstructionContainerOrder, out pieces, out Solution.ConstructionOrientationOrder);

            // --> Construction part
            // Log
            Config.Log?.Invoke("Starting construction ... " + Environment.NewLine);
            // Generate initial solution
            ExtremePointInsertion(Solution, Solution.ConstructionContainerOrder, pieces, Solution.ConstructionOrientationOrder);

            // --> Improvement part
            // Log
            Config.Log?.Invoke("Starting improvement ... " + Environment.NewLine);

            // Measure performance compared to given solution
            double initialExploitedVolume = Solution.ExploitedVolume;

            // TODO move the following into the config
            int    intervalIterationCountMax = 100;
            double alpha = 0.9;

            // Init counters
            int    currentIteration         = 0;
            int    lastImprovement          = 0;
            double currentTemperature       = 1000;
            int    currentIntervalIteration = 0;

            // Init solutions
            COSolution acceptedSolution = Solution.Clone();

            // Main loop
            do
            {
                // Init solution for this turn
                COSolution currentSolution = acceptedSolution.Clone();

                // Destroy the solution
                ALNSDestroy(currentSolution);

                // Repair the solution
                ALNSRepair(currentSolution);

                // Check whether we want to accept the solution or discard it
                if (ALNSAccept(acceptedSolution.ExploitedVolume, currentSolution.ExploitedVolume, Solution.ExploitedVolume, currentTemperature))
                {
                    acceptedSolution = currentSolution;
                }

                // Lower temperature if iteration limit is reached
                if (currentIntervalIteration >= intervalIterationCountMax)
                {
                    currentIntervalIteration = 0;
                    currentTemperature      *= alpha;
                }

                // Store every new best solution
                if (acceptedSolution.ExploitedVolume > Solution.ExploitedVolume)
                {
                    Solution = acceptedSolution;
                }

                // Log visuals
                LogVisuals(Solution, false);

                // Log
                if (DateTime.Now.Ticks - LogOldMillis > 1000000)
                {
                    LogOldMillis = DateTime.Now.Ticks;
                    Config.Log?.Invoke(currentIteration + ". " +
                                       Solution.ExploitedVolume.ToString(ExportationConstants.EXPORT_FORMAT_SHORT, ExportationConstants.FORMATTER) + " / " +
                                       VolumeOfContainers.ToString(ExportationConstants.EXPORT_FORMAT_SHORT, ExportationConstants.FORMATTER) +
                                       " (" + (Solution.ExploitedVolume / VolumeOfContainers * 100).ToString(ExportationConstants.EXPORT_FORMAT_SHORT, ExportationConstants.FORMATTER) + " %) - Current: " +
                                       acceptedSolution.ExploitedVolume.ToString(ExportationConstants.EXPORT_FORMAT_SHORT, ExportationConstants.FORMATTER) + " / " +
                                       VolumeOfContainers.ToString(ExportationConstants.EXPORT_FORMAT_SHORT, ExportationConstants.FORMATTER) +
                                       " (" + ((Solution.ExploitedVolume / VolumeOfContainers - initialExploitedVolume / VolumeOfContainers) * 100).ToString(ExportationConstants.EXPORT_FORMAT_SHORT, ExportationConstants.FORMATTER) + " %)" +
                                       " Time: " + (DateTime.Now - Config.StartTimeStamp).TotalSeconds +
                                       " \n");
                    Config.LogSolutionStatus?.Invoke((DateTime.Now - Config.StartTimeStamp).TotalSeconds, Solution.ExploitedVolume);
                }

                // Update counters
                currentIteration++;
                currentIntervalIteration++;
            } while (currentIteration - lastImprovement < Config.StagnationDistance && !Cancelled && !TimeUp);
        }