Ejemplo n.º 1
0
        private void ProcessDivideProblem()
        {
            DivideProblemMessage processedMessage = null;

            lock (divideProblemMessageQueue)
            {
                if (divideProblemMessageQueue.Count > 0)
                {
                    processedMessage = divideProblemMessageQueue.Dequeue();
                }
            }
            if (processedMessage == null)
            {
                return;
            }

            _logger.Debug("Processing divideMessage. " + processedMessage.Id);

            TaskSolverDVRP taskSolver = CreateTaskSolver(processedMessage.ProblemType, processedMessage.Data);

            var dividedProblem = taskSolver.DivideProblem((int)processedMessage.ComputationalNodes);

            _logger.Debug("Finished dividing problem");


            var solutionsMessage = new PartialProblemsMessage()
            {
                CommonData              = processedMessage.Data,
                Id                      = processedMessage.Id,
                ProblemType             = processedMessage.ProblemType,
                SolvingTimeout          = (ulong)Timeout.TotalMilliseconds,
                SolvingTimeoutSpecified = true,
                PartialProblems         = new SolvePartialProblemsPartialProblem[(int)processedMessage.ComputationalNodes]
            };

            solutionsMessage.PartialProblems = new SolvePartialProblemsPartialProblem[dividedProblem.Length];

            for (int i = 0; i < dividedProblem.Length; i++)
            {
                solutionsMessage.PartialProblems[i] = new SolvePartialProblemsPartialProblem()
                {
                    Data   = dividedProblem[i],
                    TaskId = (ulong)i
                };
            }

            var socket = communicationModule.SetupClient();

            communicationModule.Connect(socket);
            var message = SerializeMessage(solutionsMessage);

            communicationModule.SendData(message, socket);
            communicationModule.CloseSocket(socket);
        }
Ejemplo n.º 2
0
        private void ProcessPartialSolutions()
        {
            SolutionsMessage solution = null;

            lock (partialSolutionsMessageQueue)
            {
                if (partialSolutionsMessageQueue.Count > 0)
                {
                    solution = partialSolutionsMessageQueue.Dequeue();
                }
            }

            if (solution == null)
            {
                return;
            }

            _logger.Debug("Merging solution");

            TaskSolverDVRP taskSolver = CreateTaskSolver(solution.ProblemType, solution.CommonData);

            taskSolver.MergeSolution(solution.Solutions.Select(x => x.Data).ToArray());

            _logger.Debug("Finished merging solution: " + solution.Id);
            _logger.Debug("Finished merging solution: " + solution.Id);
            _logger.Debug("Finished merging solution: " + solution.Id);

            SolutionsMessage solutionsMessage = new SolutionsMessage()
            {
                Id          = solution.Id,
                CommonData  = null,
                ProblemType = "DVRP",
                Solutions   = new Solution[1]
            };

            solutionsMessage.Solutions[0] = new Solution()
            {
                Type = SolutionType.Final,
                Data = taskSolver.Solution
            };

            var socket = communicationModule.SetupClient();

            communicationModule.Connect(socket);
            var message = SerializeMessage(solutionsMessage);

            communicationModule.SendData(message, socket);

            communicationModule.CloseSocket(socket);
        }
        public void SolveProblemTest17D()
        {
            string testData = System.IO.File.ReadAllText(@"DVRPTestData\okul17D.vrp");

            byte[]         problemData = CommunicationModule.ConvertStringToData(testData);
            TaskSolverDVRP taskSolver  = new TaskSolverDVRP(problemData);

            byte[][] division  = taskSolver.DivideProblem(6);
            byte[][] solutions = new byte[6][];
            for (int i = 0; i <= 6; i++)
            {
                int[][] partialData = DVRPHelper.ParsePartialProblemData(division[i]);
                solutions[i] = taskSolver.Solve(division[i], new TimeSpan());
            }
            taskSolver.MergeSolution(solutions);
            DVRPPartialSolution finalSol = DVRPPartialSolution.Parse2FinalSol(CommunicationModule.ConvertDataToString(taskSolver.Solution, taskSolver.Solution.Length), taskSolver.Dvrp);

            Assert.IsTrue(Math.Abs(finalSol.pathLen - 1089) < 1);
        }
        public void GetIndexTest()
        {
            var subsets = TaskSolverDVRP.CreateSubsets(new int[10] {
                0, 1, 2, 3, 4, 5, 6, 7, 8, 9
            });                                                                                      //.ToArray();

            subsets.Sort(
                delegate(int[] tab1, int[] tab2)
            {
                if (tab1.Length < tab2.Length)
                {
                    return(-1);
                }
                else if (tab2.Length < tab1.Length)
                {
                    return(1);
                }
                else
                {
                    for (int i = 0; i < tab1.Length; i++)
                    {
                        if (tab1[i] < tab2[i])
                        {
                            return(-1);
                        }
                        else if (tab2[i] < tab1[i])
                        {
                            return(1);
                        }
                    }
                    return(0);
                }
            }
                );
            var subsetsArray = subsets.ToArray();

            long[,] comb = DVRPHelper.GetAllCombination(11);

            int wyn = DVRPHelper.GetIndex(subsetsArray[15], comb, 10);

            Assert.IsTrue(wyn == 15);
        }
        private void Solve(SolvePartialProblemsPartialProblem partialProblem, int threadNumber, TaskSolverDVRP taskSolverDvrp, int solvingTimeout, ulong id)
        {
            //TODO Timer dla wątku do StatusThread.HowLong

            Solution solution = new Solution()
            {
                ComputationsTime = 0,
                Data             = null,
                TaskId           = partialProblem.TaskId,
                TaskIdSpecified  = (int)partialProblem.TaskId < 0 ? false : true,
                TimeoutOccured   = false,
                Type             = SolutionType.Ongoing
            };

            Trace.WriteLine("\n\nSolving subproblem id " + partialProblem.TaskId + " on thread " + threadNumber + "\n\n");
            //solution.ComputationsTime =
            solution.Data = taskSolverDvrp.Solve(partialProblem.Data, new TimeSpan(0, 0, solvingTimeout));
            //solution.TimeoutOccured =
            solution.Type = SolutionType.Partial;
            Trace.WriteLine("\n\nSolved subproblem id: " + partialProblem.TaskId + "\n\n");

            lock (this.solutionsMessagesList)
            {
                for (int i = 0; i < this.solutionsMessagesList.Count; ++i)
                {
                    if (this.solutionsMessagesList[i].Id == id)
                    {
                        for (int j = 0; j < this.solutionsMessagesList[i].Solutions.Length; j++)
                        {
                            if (this.solutionsMessagesList[i].Solutions[j] == null)
                            {
                                this.solutionsMessagesList[i].Solutions[j] = solution;
                            }
                        }
                        Trace.WriteLine("\n\n Subproblem id: " + partialProblem.TaskId + " waits for complete solution\n\n");
                        break;
                    }
                }
            }
            _logger.Debug("Solved problem: " + solution.TaskId);

            lock (this.statusOfComputingThreads)
            {
                this.statusOfComputingThreads[threadNumber].State = StatusThreadState.Idle;
            }
        }
        private void SolvePartialProblemsMessage(PartialProblemsMessage partialProblemsMessage)
        {
            Trace.WriteLine("\n\nStarting solving problem id: " + partialProblemsMessage.Id);

            //All PartialProblems which were sent in single PartialProblemsMessage
            Queue <SolvePartialProblemsPartialProblem> q = new Queue <SolvePartialProblemsPartialProblem>(partialProblemsMessage.PartialProblems);

            Trace.WriteLine("\n\nSOLVING " + q.Count + " subproblems of PartialProblem\n\n");

            TaskSolverDVRP taskSolverDvrp = new TaskSolverDVRP(partialProblemsMessage.CommonData);

            SolutionsMessage solutionsMessage = new SolutionsMessage()
            {
                CommonData  = partialProblemsMessage.CommonData,
                Id          = partialProblemsMessage.Id,
                ProblemType = partialProblemsMessage.ProblemType,
                Solutions   = new Solution[q.Count],
            };

            lock ((this.solutionsMessagesList))
            {
                this.solutionsMessagesList.Add(solutionsMessage);
            }

            int idleThreadIndex;

            while (q.Count > 0)
            {
                lock (this.statusOfComputingThreads)
                {
                    //Get index number of the idle thread, which can compute
                    idleThreadIndex = this.GetIdleThreadIndex();
                }

                //If there is no idle thread, wait and try again
                if (idleThreadIndex == -1)
                {
                    Thread.Sleep(4000);
                }

                else
                {
                    SolvePartialProblemsPartialProblem problem = q.Dequeue();

                    this.statusOfComputingThreads[idleThreadIndex].ProblemInstanceId =
                        partialProblemsMessage.Id;

                    this.statusOfComputingThreads[idleThreadIndex].ProblemType =
                        partialProblemsMessage.ProblemType;

                    this.statusOfComputingThreads[idleThreadIndex].TaskId =
                        problem.TaskId;

                    this.computingThreads[idleThreadIndex] = new Thread(() =>
                                                                        this.Solve(problem, idleThreadIndex, taskSolverDvrp,
                                                                                   (int)partialProblemsMessage.SolvingTimeout, partialProblemsMessage.Id));

                    this.computingThreads[idleThreadIndex].Start();
                }
            }
        }
        public void DivideProblemTest()
        {
            string testData = System.IO.File.ReadAllText(@"DVRPTestData\okul12D.vrp");

            byte[]         problemData = CommunicationModule.ConvertStringToData(testData);
            TaskSolverDVRP taskSolver  = new TaskSolverDVRP(problemData);

            byte[][] result       = taskSolver.DivideProblem(10);
            string   fistNodeText = System.IO.File.ReadAllText(@"DVRPTestData\AllTxtFiles.txt");

            int[][][] firstNodeTab  = DVRP.ParseData(CommunicationModule.ConvertStringToData(fistNodeText));
            int[][][] firstNodeTest = DVRP.ParseData(result[0]);

            bool   ok  = true;
            string msg = "";

            if (firstNodeTab.Length == firstNodeTest.Length)
            {
                for (int i = 0; i < firstNodeTab.Length; i++)
                {
                    if (firstNodeTab[i].Length == firstNodeTest[i].Length)
                    {
                        for (int j = 0; j < firstNodeTab[i].Length; j++)
                        {
                            if (firstNodeTab[i][j].Length == firstNodeTest[i][j].Length)
                            {
                                for (int k = 0; k < firstNodeTab[i][j].Length; k++)
                                {
                                    if (firstNodeTab[i][j][k] != firstNodeTest[i][j][k])
                                    {
                                        msg = "firstNodeTab[i][j][k] != firstNodeTest[i][j][k]";
                                        ok  = false;
                                        break;
                                    }
                                }
                                if (!ok)
                                {
                                    break;
                                }
                            }
                            else
                            {
                                msg = "firstNodeTab[i][j].Length != firstNodeTest[i][j].Length";
                                ok  = false;
                                break;
                            }
                        }
                        if (!ok)
                        {
                            break;
                        }
                    }
                    else
                    {
                        msg = "firstNodeTab[i].Length == firstNodeTest.Length";
                        ok  = false;
                        break;
                    }
                }
            }
            else
            {
                msg = "firstNodeTab.Length != firstNodeTest.Length";
                ok  = false;
            }

            Assert.IsTrue(ok, msg);
        }