private void handleSolutionsMessage(SolutionsMessage solutionsMessage) { bool isOnGoing = false; if (solutionsMessage.Solutions[0].Type == SolutionsSolutionType.Ongoing) { SmartConsole.PrintLine("Ongoing computations. Waiting for full solution", SmartConsole.DebugLevel.Basic); } else { for (int i = 0; i < solutionsMessage.Solutions.Count(); i++) { if (solutionsMessage.Solutions[i].Type == SolutionsSolutionType.Ongoing) { isOnGoing = true; break; } } if (isOnGoing) { SmartConsole.PrintLine("Ongoing computations. Waiting for full solution", SmartConsole.DebugLevel.Basic); } else { DateTime nowTime = DateTime.Now; int deltaSecs = nowTime.Second - Program.COMP_TIME.Second; int deltaMs = nowTime.Millisecond - Program.COMP_TIME.Millisecond; string timeStr = "Seconds = " + deltaSecs + ", Milliseconds = " + deltaMs; SmartConsole.PrintLine("Complete solution has been received", SmartConsole.DebugLevel.Advanced); finalSolutionHelper(solutionsMessage); SmartConsole.PrintLine("Computation Time: \n" + timeStr, SmartConsole.DebugLevel.Advanced); // TODO print solution keepAliveTimer.Stop(); } } }
private void handleSolutionMessage(SolutionsMessage message) { for (int i = 0; i < systemTracker.Node.ParallelThreads; i++) { if (systemTracker.Node.TaskThreads[i].StatusThread.State == StatusThreadState.Idle) { /// HOW DO I GET DVRP SOLVER HERE? DVRPSolver dvrpSolver = new DVRPSolver(message.Solutions[0].Data);// TEMPORARY systemTracker.Node.TaskThreads[i].StatusThread.State = StatusThreadState.Busy; systemTracker.Node.TaskThreads[i].CurrentTask = new Cluster.Task((int)message.Id, message.ProblemType, new byte[1]) { Status = Cluster.TaskStatus.Merging }; //saving solutions to subTasks for (int j = 0; j < message.Solutions.Count(); j++) { systemTracker.Node.TaskThreads[i].CurrentTask.subTasks.Add(new Task((int)message.Id, message.ProblemType, message.Solutions[j].Data)); } systemTracker.Node.TaskThreads[i].TaskSolver = dvrpSolver; systemTracker.Node.TaskThreads[i].Thread = new Thread(new ThreadStart(systemTracker.Node.TaskThreads[i].Start)); systemTracker.Node.TaskThreads[i].Thread.Start(); break; } } }
public void Parse_XMLString_SolutionMessage() { /*********** Actual message ***********/ string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"xml_samples\Solutions.xml"); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(path); string xmlStr = xmlDoc.OuterXml; string name = Message.GetMessageName(xmlStr); SolutionsMessage actualMessage = null; if (name == SolutionsMessage.ELEMENT_NAME) actualMessage = SolutionsMessage.Construct(xmlStr); /*********** Expected message ***********/ // construct fields for SolutionsMessage string problemType = "TSP"; ulong id = 12; byte[] commonData = { 1, 23, 25, 2, 5, 5, 5, 5, 2, 26, 87 }; // fields for Solution1 ulong taskId1 = 123; bool timeoutOccured1 = false; SolutionsSolutionType typeField1 = SolutionsSolutionType.Final; ulong computationsTime1 = 12334; byte[] data1 = { 24, 252, 6, 43, 57, 88 }; Solution solution1 = new Solution(taskId1, timeoutOccured1, typeField1, computationsTime1, data1); // fields for Solution2 ulong taskId2 = 321; bool timeoutOccured2 = true; SolutionsSolutionType typeField2 = SolutionsSolutionType.Ongoing; ulong computationsTime2 = 43321; byte[] data2 = { 24, 25, 6, 3, 7, 8 }; Solution solution2 = new Solution(taskId2, timeoutOccured2, typeField2, computationsTime2, data2); Solution[] solutions = { solution1, solution2 }; SolutionsMessage expectedMessage = new SolutionsMessage(problemType, id, commonData, solutions); Assert.AreEqual(expectedMessage, actualMessage); }
/// <summary> /// SolutionRequest is sent by Computational Client /// </summary> /// <param name="messagePackage"></param> private void handleSolutionRequestMessage(MessagePackage messagePackage) { SolutionRequestMessage message = (SolutionRequestMessage)messagePackage.Message; Task task = taskTracker.GetTask((int)message.Id); SolutionsMessage solutionMessage = new SolutionsMessage(task.Type, (ulong)task.ID, task.CommonData, task.Solutions); server.Send(messagePackage.Socket, solutionMessage); if (task.Solutions[0].Type != SolutionsSolutionType.Ongoing) { taskTracker.Tasks.Remove(task); } }
/// <summary> /// in this function solution message with solved partial solution is sent (it checks if all subtasks of task are solved. /// It returns true in case of completition of sending the message. /// </summary> /// <param name="numberOfTask"></param> /// <param name="node"></param> /// <param name="messagePackage"></param> /// <returns></returns> private bool isMergeSolutionSent(int numberOfTask, NetworkNode node, MessagePackage messagePackage) { if (taskTracker.Tasks[numberOfTask].Status != Cluster.TaskStatus.Merging && taskTracker.Tasks[numberOfTask].Status != Cluster.TaskStatus.Merged && taskTracker.Tasks[numberOfTask].subTasks.Count != 0) { for (int j = 0; j < taskTracker.Tasks[numberOfTask].subTasks.Count; j++) { if (taskTracker.Tasks[numberOfTask].subTasks[j].Status != Cluster.TaskStatus.Solved) { return false; } } } else { return false; } if (taskTracker.Tasks[numberOfTask].Type == node.SolvableProblems[0]) { taskTracker.Tasks[numberOfTask].Status = Cluster.TaskStatus.Merging; Solution[] solutions = new Solution[taskTracker.Tasks[numberOfTask].subTasks.Count]; for (int k = 0; k < solutions.Count(); k++) { solutions[k] = new Solution(SolutionsSolutionType.Final); } for (int j = 0; j < taskTracker.Tasks[numberOfTask].subTasks.Count; j++) { solutions[j].Data = taskTracker.Tasks[numberOfTask].subTasks[j].Solutions[0].Data; } SolutionsMessage solutionMessage = new SolutionsMessage(taskTracker.Tasks[numberOfTask].Type, (ulong)taskTracker.Tasks[numberOfTask].ID, taskTracker.Tasks[numberOfTask].CommonData, solutions); server.Send(messagePackage.Socket, solutionMessage); SmartConsole.PrintLine("Solution Message has been sent to Task Manager", SmartConsole.DebugLevel.Advanced); return true; } return false; }
/*******************************************************************/ /************************ PRIVATE METHODS **************************/ /*******************************************************************/ /*******************************************************************/ /************************* PUBLIC METHODS **************************/ /*******************************************************************/ public void Start() { switch (currentTask.Status) { case TaskStatus.Dividing: byte[][] dividedProblems = taskSolver.DivideProblem(4); PartialProblem[] partialProblems = new PartialProblem[dividedProblems.Count()]; for (int i = 0; i < dividedProblems.Count(); i++) { partialProblems[i] = new PartialProblem((ulong)currentTask.ID, dividedProblems[i], (ulong)NodeID); } SolvePartialProblemsMessage solvePartialProblemsMessage = new SolvePartialProblemsMessage(currentTask.Type, (ulong) currentTask.ID, currentTask.CommonData, (ulong)4, partialProblems); Console.Write(">>Sending solve partial problems message. "); messageProcessor.Communicate(solvePartialProblemsMessage); this.statusThread.State = StatusThreadState.Idle; this.currentTask = null; break; case TaskStatus.Solving: byte[] solvedPartialProblem = taskSolver.Solve(currentTask.BaseData, new TimeSpan(0, 0, 5)); Solution[] solutions = new Solution[1]; //TODO subTask id, timeout checking , computations time solutions[0] = new Solution((ulong)currentTask.ID, false, SolutionsSolutionType.Partial, 4000, solvedPartialProblem); SolutionsMessage solutionMessage = new SolutionsMessage(currentTask.Type, (ulong)currentTask.ID, currentTask.CommonData, solutions); Console.WriteLine(">>Sending solution message. "); messageProcessor.Communicate(solutionMessage); statusThread.State = StatusThreadState.Idle; this.currentTask = null; break; case TaskStatus.Merging: byte[][] partialSolutions = new byte[currentTask.subTasks.Count()][]; for(int i=0;i<currentTask.subTasks.Count();i++){ partialSolutions[i] = currentTask.subTasks[i].BaseData; } byte[] mergedSolution = taskSolver.MergeSolution(partialSolutions); Solution[] solution = new Solution[1]; //TODO subTask id, timeout checking , computations time solution[0] = new Solution((ulong)currentTask.ID, false, SolutionsSolutionType.Final, 4000, mergedSolution); SolutionsMessage finalSolutionMessage = new SolutionsMessage(currentTask.Type, (ulong)currentTask.ID, currentTask.CommonData, solution); messageProcessor.Communicate(finalSolutionMessage); this.statusThread.State = StatusThreadState.Idle; this.currentTask = null; break; } // Implement in private methods: // Depending on what has to be computed // run a task solver method. // TODO collect result. }
private void finalSolutionHelper(SolutionsMessage solutionsMessage) { //File.Create("Solution.txt"); FileStream fs1 = new FileStream("Solution.txt", FileMode.OpenOrCreate, FileAccess.Write); StreamWriter writer = new StreamWriter(fs1); byte[] data = solutionsMessage.Solutions[0].Data; BinaryFormatter formatter = new BinaryFormatter(); Result finalResults = (Result)formatter.Deserialize(new MemoryStream(data)); SmartConsole.PrintHeader("TASK ID: "+ solutionsMessage.Id +" RESULTS"); writer.WriteLine("TASK ID: " + solutionsMessage.Id + " RESULTS"); int[] finalRoute = finalResults.route; float finalDistance = finalResults.length; List<int> nextDays = finalResults.nextDay; SmartConsole.PrintLine("Distance: " + finalDistance, SmartConsole.DebugLevel.Advanced); writer.WriteLine("Distance: " + finalDistance); int vehicleIndex = 0; string msg = ""; for(int i = 0;i < finalRoute.Length; i++) { if (finalRoute[i] == -1) msg += "\n"; else msg += finalRoute[i] + ", "; } SmartConsole.PrintLine("Path: \n" + msg, SmartConsole.DebugLevel.Advanced); writer.Write("Path: \n" + msg); string nextDayStr = ""; for (int l = 0; l < nextDays.Count; l++) { nextDayStr += nextDays[l] + ", "; } if(!nextDayStr.Equals("")) { SmartConsole.PrintLine("Next Day: \n" + nextDayStr, SmartConsole.DebugLevel.Advanced); writer.Write("\nNext Day: \n" + nextDayStr); } writer.Close(); fs1.Close(); Program.doWork = false; }
public static SolutionsMessage CreateSolutionsMessage() { string problemType = "TSP"; ulong id = 12; byte[] commonData = { 1, 23, 25, 2, 5, 5, 5, 5, 2, 26, 87 }; // fields for Solution1 ulong taskId1 = 123; bool timeoutOccured1 = false; SolutionsSolutionType typeField1 = SolutionsSolutionType.Final; ulong computationsTime1 = 12334; byte[] data1 = { 24, 252, 6, 43, 57, 88 }; Solution solution1 = new Solution(taskId1, timeoutOccured1, typeField1, computationsTime1, data1); // fields for Solution2 ulong taskId2 = 321; bool timeoutOccured2 = true; SolutionsSolutionType typeField2 = SolutionsSolutionType.Ongoing; ulong computationsTime2 = 43321; byte[] data2 = { 24, 25, 6, 3, 7, 8 }; Solution solution2 = new Solution(taskId2, timeoutOccured2, typeField2, computationsTime2, data2); Solution[] solutions = { solution1, solution2 }; SolutionsMessage expectedMessage = new SolutionsMessage(problemType, id, commonData, solutions); return expectedMessage; }