public void SolutionsEmptyListTest()
        {
            //Arrange
            Solutions s = new Solutions("name", 123, new byte[]{}, new Solution[] {});

            //Act
            byte[] data = s.GetXmlData();

            //Debug.WriteLine(System.Text.Encoding.UTF8.GetString(data));
            Console.WriteLine(System.Text.Encoding.UTF8.GetString(data));
            //Assert
            Assert.IsNotNull(data);
        }
        public void SolutionsTest()
        {
            //Arrange
            Solutions s = new Solutions("name", 123, new byte[] { 1, 3, 5 }, new Solution[] {
            new Solution(null, false, SolutionType.Ongoing, 100, new byte[]{ 1, 2}),
            new Solution(123, true, SolutionType.Partial, 400, new byte[]{ 3, 5, 7, 8})
            });

            //Act
            byte[] data = s.GetXmlData();

            //Debug.WriteLine(System.Text.Encoding.UTF8.GetString(data));
            Console.WriteLine(System.Text.Encoding.UTF8.GetString(data));
            //Assert
            Assert.IsNotNull(data);
        }
Exemple #3
0
        public void MergeThreadFunc(Solutions msg, ComputationalThread thread)
        {
            var asm = Assembly.Load(AssemblyName.GetAssemblyName(Path.GetFullPath(msg.ProblemType + ".dll")));//Assembly.LoadFile(Path.GetFullPath("DVRP.dll"));
            //Type t = asm.GetType("DVRP.DVRP");
            Type t = asm.GetTypes().Where(x => x.IsSubclassOf(typeof(UCCTaskSolver.TaskSolver))).FirstOrDefault();
            if (t == null)
                throw new ArgumentException("Brak odpowiedniej klasy w module.");

            var methodInfo = t.GetMethod("MergeSolution");

            object[] constructor_param = new object[1];
            constructor_param[0] = msg.CommonData;

            var o = Activator.CreateInstance(t, constructor_param);

            Solutions solutions_msg;
            object[] param = new object[1];
            param[0] = PartialSolutions[msg.Id].ToArray();
            try
            {
                methodInfo.Invoke(o, param);
            }
            catch (Exception e)
            {
                MessageBox.Show("Moduł '" + msg.ProblemType + ".dll' zakończył działanie z błędem:\n\n" + e.InnerException.Message, "Błąd modułu", MessageBoxButtons.OK, MessageBoxIcon.Error);
                Application.Exit();
            }
            var meth = t.GetMethod("get_Solution");

            byte[] ans = (byte[])meth.Invoke(o, null);

            // TimeSpan ts = DateTime.Now - start_time;

            Solution final_solution = new Solution(msg.Id, false, SolutionType.Final, thread.HowLong, ans);
            List<Solution> solution_to_send = new List<Solution>();
            solution_to_send.Add(final_solution);

            solutions_msg = new Solutions(msg.ProblemType, msg.Id, msg.CommonData, solution_to_send);

            client.Work(solutions_msg.GetXmlData());
            SetComputationalThreadIdle((ulong)thread.ProblemInstanceId, (ulong)thread.TaskId);
        }
        public void SolutionsConstructorTest()
        {
            //Arrange
            byte[] commonData = new byte[0];
            Solution[] solutionsArray = new Solution[]{new Solution(0, true, SolutionType.Ongoing, 120, new byte[0])};

            //Act
            Solutions solutions = new Solutions("name", 0, commonData, solutionsArray);

            //Assert
            Assert.IsNotNull(solutions);
        }
Exemple #5
0
        public void SolutionsParseTest()
        {
            //Arrange
            Solutions s = new Solutions("name", 123, new byte[] { 1, 3, 5 }, new Solution[] {
            new Solution(null, false, SolutionType.Ongoing, 100, new byte[]{ 1, 2}),
            new Solution(123, true, SolutionType.Partial, 400, new byte[]{ 3, 5, 7, 8})
            });

            byte[] data = s.GetXmlData();

            //Act
            XMLParser parser = new XMLParser(data);

            //Assert
            Assert.IsNotNull(parser);
            Assert.AreEqual(MessageTypes.Solutions, parser.MessageType);
            Solutions rs = (Solutions)parser.Message;
            Assert.AreEqual(s.CommonData.Length, rs.CommonData.Length);
            Assert.AreEqual(s.Id, rs.Id);
            Assert.AreEqual(s.ProblemType, rs.ProblemType);
            Assert.AreEqual(s.SolutionsList.Count, rs.SolutionsList.Count);
            Solution sol = s.SolutionsList[0];
            Solution rsol = rs.SolutionsList[0];
            Assert.AreEqual(sol.ComputationsTime, rsol.ComputationsTime);
            Assert.AreEqual(sol.Data.Length, rsol.Data.Length);
            Assert.AreEqual(sol.TaskId, rsol.TaskId);
            Assert.AreEqual(sol.TimeoutOccured, rsol.TimeoutOccured);
            Assert.AreEqual(sol.Type, rsol.Type);
        }
Exemple #6
0
        public void NodeThreadFunc(/*object o, MethodInfo methodInfo,*/ SolvePartialProblems msg, PartialProblem pp, ComputationalThread ct)
        {
            DateTime start_time = DateTime.Now;

            var asm = Assembly.Load(AssemblyName.GetAssemblyName(Path.GetFullPath(msg.ProblemType + ".dll")));
            //Type t = asm.GetType("DVRP.DVRP");
            Type t = asm.GetTypes().Where(x => x.IsSubclassOf(typeof(UCCTaskSolver.TaskSolver))).FirstOrDefault();

            var methodInfo = t.GetMethod("Solve");
            object[] constructor_params = new object[1];
            constructor_params[0] = msg.CommonData;
            var o = Activator.CreateInstance(t, constructor_params);
            /*********event handler*/

            var eventInfo = t.GetEvent("SolutionsMergingFinished");
            Type tDelegate = eventInfo.EventHandlerType;

            MethodInfo addHandler = eventInfo.GetAddMethod();

            Type returnType = GetDelegateReturnType(tDelegate);
            Console.WriteLine(returnType.ToString());

            DynamicMethod handler = new DynamicMethod("", null,
                                  GetDelegateParameterTypes(tDelegate), t);

            ILGenerator ilgen = handler.GetILGenerator();

            Type[] showParameters = { typeof(String) };
            MethodInfo simpleShow = typeof(CNNode).GetMethod("SetComputationalThreadIdle");
            Console.WriteLine(simpleShow.ToString());

            ilgen.Emit(OpCodes.Ldstr, "string");//ct.ProblemInstanceId.Value);//Ldstr,"This event handler was constructed at run time.");
            ilgen.Emit(OpCodes.Call, simpleShow);
            //   ilgen.Emit(OpCodes.Pop);
            ilgen.Emit(OpCodes.Ret);

            // Complete the dynamic method by calling its CreateDelegate
            // method. Use the "add" accessor to add the delegate to
            // the invocation list for the event.
            //
            Delegate dEmitted = handler.CreateDelegate(tDelegate);
            addHandler.Invoke(o, new Object[] { dEmitted });

            if (methodInfo != null)
            {
                object[] param = new object[2];

                param[0] = pp.Data;
                if (msg.SolvingTimeout == null)
                    param[1] = null;
                else param[1] = new TimeSpan((long)msg.SolvingTimeout * 10000000);

                byte[] result = null;

                try
                {
                    result = (byte[])methodInfo.Invoke(o, param);
                }
                catch (Exception e)
                {
                    MessageBox.Show("Moduł '" + msg.ProblemType + ".dll' zakończył działanie z błędem:\n\n" + e.InnerException.Message, "Błąd modułu", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    Application.Exit();
                }

                TimeSpan ts = DateTime.Now - start_time;
                Solution s = new Solution(pp.TaskId, false, SolutionType.Partial, (ulong)ts.TotalSeconds, result);

                solution.Add(s);
                Console.WriteLine("sending partial solutions");
                Solutions solutions = new Solutions(msg.ProblemType, msg.Id, msg.CommonData, solution);

                client.Work(solutions.GetXmlData());

                SetComputationalThreadIdle(msg.Id, pp.TaskId);
            }
            else Console.WriteLine("Method equal to null");
        }
Exemple #7
0
        /// <summary>
        /// Aktualizuje informację o TM i CN. Sprawdza czy jest dla nich nowe zadanie i jeśli znajdzie, odsyła.
        /// </summary>
        /// <param name="obj">Status TM/CN</param>
        /// <returns></returns>
        private MessageObject UpdateAndGiveData(MessageObject obj)
        {
            MessageObject response = null;
            Status status = obj as Status;

            lock (lockObj)
            {
                Node node = taskManagers.Find(item => item.Id == status.Id);
                if (node == null)
                    node = computationalNodes.Find(item => item.Id == status.Id);

                if (node != null)
                {
                    node.Update(status.Threads);
                    UpdateTemporaryProblems(node.TemporaryProblems);

                    if (node.GetAvailableThreads() > 0)
                    {
                        if (node.GetType() == typeof(TaskManager))
                        {
                            Problem partialP = problems.Find(t => t.Status == ProblemStatus.PartiallySolved
                                && node.SolvableProblems.Contains(t.ProblemType));

                            if (partialP != null)
                            {
                                partialP.Status = ProblemStatus.WaitingForSolutions;

                                List<Solution> solutions = new List<Solution>();
                                node.TemporaryProblems.Add(new TempProblem(partialP.Id, ProblemStatus.WaitingForSolutions, null));

                                foreach (var pp in partialP.PartialProblems)
                                {
                                    solutions.Add(new Solution(pp.TaskId, pp.TimeoutOccured, SolutionType.Partial, pp.ComputationsTime, pp.Data));
                                }

                                response = new Solutions(partialP.ProblemType, partialP.Id, partialP.CommonData, solutions);
                                return response;
                            }

                            Problem newP = problems.Find(t => t.Status == ProblemStatus.New
                                && node.SolvableProblems.Contains(t.ProblemType));

                            if (newP != null)
                            {
                                newP.Status = ProblemStatus.WaitingForDivision;
                                node.TemporaryProblems.Add(new TempProblem(newP.Id, ProblemStatus.WaitingForDivision, null));
                                ulong computationalNodesCount = (ulong)computationalNodes.Sum(t => t.GetAvailableThreads());
                                response = new DivideProblem(newP.ProblemType, newP.Id, newP.Data, computationalNodesCount);
                                return response;
                            }
                        }
                        else    // ComputationalNode
                        {
                            Problem dividedP = problems.Find(t => t.Status == ProblemStatus.Divided
                                && node.SolvableProblems.Contains(t.ProblemType));

                            if (dividedP != null)
                            {
                                List<CommunicationXML.PartialProblem> pp =
                                    dividedP.GetPartialProblemListToSolve(node.GetAvailableThreads());

                                response =
                                    new SolvePartialProblems(dividedP.ProblemType, dividedP.Id,
                                        dividedP.CommonData, dividedP.SolvingTimeout, pp);

                                List<TempPartial> ppnums = new List<TempPartial>();

                                foreach(var x in pp)
                                {
                                    ppnums.Add(new TempPartial(x.TaskId, PartialProblemStatus.Sended));
                                }

                                node.TemporaryProblems.Add(new TempProblem(dividedP.Id, dividedP.Status, ppnums));

                                return response;
                            }
                        }
                    }
                }
            }

            return response;
        }
Exemple #8
0
        /// <summary>
        /// Wysyłanie rozwiązania do CC.
        /// </summary>
        /// <param name="obj">SolutionRequest</param>
        private Solutions SendSolution(MessageObject obj)
        {
            Solutions response = null;
            SolutionRequest request = obj as SolutionRequest;

            lock (lockObj)
            {
                Problem p = problems.Find(x => x.Id == request.Id);

                if (p != null)
                {
                    List<Solution> solutions = new List<Solution>();

                    if (p.Status == ProblemStatus.Solved)
                    {
                        solutions.Add(new Solution(p.TimeoutOccured, SolutionType.Final, p.ComputationsTime, p.Data));

                        // Usuwanie wysłanego problemu z listy.
                        problems.Remove(p);
                    }
                    else
                    {
                        foreach (var s in p.PartialProblems)
                        {
                            solutions.Add(
                                new Solution(s.TaskId, s.TimeoutOccured,
                                    s.PartialProblemStatus == PartialProblemStatus.Solved ?
                                    SolutionType.Partial : SolutionType.Ongoing, s.ComputationsTime, s.Data));
                        }

                        if (solutions.Count == 0)
                            solutions.Add(Solution.GetEmptySolution());
                    }

                    response = new Solutions(p.ProblemType, p.Id, p.CommonData, solutions);
                }
            }

            return response;
        }