Exemplo n.º 1
0
        private void CreateAndSendPartialSolution(SolvePartialProblems message,
                        SolvePartialProblemsPartialProblem problem)
        {
            var taskSolver = CreateSolverOrSendError(message.ProblemType, message.CommonData);
            if (taskSolver == null) return;

            var resultData = taskSolver.Solve(problem.Data, new TimeSpan());

            var solution = new Solutions
            {
                Solutions1 = new[] {new SolutionsSolution
                {
                    TaskId = problem.TaskId,
                    TaskIdSpecified = true,
                    Type = SolutionsSolutionType.Partial,
                    TimeoutOccured = false,
                    ComputationsTime = 1,
                    Data = resultData
                }},
                Id = message.Id,
                ProblemType = "DVRP",
                CommonData = problem.Data
            };

            SendMessageNoResponse(solution);
        }
        public Message DivideProblem(DivideProblem divideProblem)
        {
            log.DebugFormat("Division of problem has started. ({0})", divideProblem.Id);

            if (!SolvableProblems.Contains(divideProblem.ProblemType))
            {
                log.Debug("Not supported problem type.");
                return(new Error()
                {
                    ErrorMessage = "not supported problem type",
                    ErrorType = ErrorErrorType.InvalidOperation
                });
            }

            var commonData = divideProblem.Data;
            var taskSolver = _resolver.GetInstanceByBaseTypeName(divideProblem.ProblemType, commonData);
            var bytes      = taskSolver.DivideProblem(0);

            log.DebugFormat("Length of divide problem message: {0}", bytes?.Sum(x => x.Length));
            //adding info about partial problems, their task ids, and partialProblem
            //some things can be temporary (partialProblems?)
            storage.AddIssue(divideProblem.Id, new ProblemInfo()
            {
                ProblemsCount  = bytes?.GetLength(0) ?? 0,
                ProblemType    = divideProblem.ProblemType,
                SolutionsCount = 0,
                CommonData     = commonData
            });

            var problemsList = new List <SolvePartialProblemsPartialProblem>();

            //iterate through all partial problems and create proper messages
            for (var i = 0; i < (bytes?.GetLength(0) ?? 0); i++)
            {
                var partialProblem = new SolvePartialProblemsPartialProblem()
                {
                    TaskId = (ulong)i,
                    Data   = bytes[i],
                    NodeID = componentId
                };

                problemsList.Add(partialProblem);
                //adding info about subtask to task manager memory
                storage.AddTaskToIssue(divideProblem.Id, partialProblem);
            }

            log.DebugFormat("Division finished. ({0})", divideProblem.Id);
            //creating msg
            var partialProblems = new SolvePartialProblems()
            {
                ProblemType     = divideProblem.ProblemType,
                Id              = divideProblem.Id,
                CommonData      = divideProblem.Data,
                PartialProblems = problemsList.ToArray()
            };

            return(partialProblems);
        }
        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;
            }
        }
Exemplo n.º 4
0
        public void PartialProblemsMessageSerialization()
        {
            string testData   = System.IO.File.ReadAllText(@"XMLTestData\PartialProblemsMessage.xml");
            var    serializer = new ComputationSerializer <PartialProblemsMessage>();

            SolvePartialProblemsPartialProblem pp = new SolvePartialProblemsPartialProblem()
            {
                TaskId = 20,
                Data   = new byte[] { 0, 0, 10 }
            };

            var partialProblemsMessage = new PartialProblemsMessage()
            {
                ProblemType             = "TSP",
                Id                      = 1,
                CommonData              = new byte[] { 0, 0, 25 },
                SolvingTimeout          = 50,
                SolvingTimeoutSpecified = true,
                PartialProblems         = new SolvePartialProblemsPartialProblem[] { pp }
            };
            var result = serializer.Serialize(partialProblemsMessage);

            Assert.AreEqual(result, testData);
        }
        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();
                }
            }
        }
 internal void AddTaskToIssue(ulong id, SolvePartialProblemsPartialProblem partialProblem)
 {
     _currentProblems[id].PartialSolutions.Add(partialProblem.TaskId, null);
 }
Exemplo n.º 7
0
        private void SendPartialProblems(ulong id)
        {
            try
            {
                taskSolver = new DVRPTaskSolver(problem.Data);
                var dividedProblems = taskSolver.DivideProblem((int) problem.ComputationalNodes);
                var solvePartialProblemsPartialProblem = new SolvePartialProblemsPartialProblem[dividedProblems.Length];
                for (int i = 0; i < dividedProblems.Length; i++)
                {
                    solvePartialProblemsPartialProblem[i] = new SolvePartialProblemsPartialProblem {Data = dividedProblems[i], TaskId = ++taskId};
                }

                var partialProblems = new SolvePartialProblems
                {
                    CommonData = problem.Data,
                    Id = id,
                    ProblemType = "DVRP",
                    PartialProblems = solvePartialProblemsPartialProblem,
                    SolvingTimeout = 100000,
                    SolvingTimeoutSpecified = true
                };
                networkAdapter.Send(partialProblems, true);
                Console.WriteLine("SendSolvePartialProblems");
            }
            catch (Exception e)
            {
                Console.WriteLine("Cannot send partial problems to server: " + e.Message);
            }
        }