/// <summary> /// Merge partial solutions into the final one. /// </summary> /// <param name="solutions">Binary serialized DVRP solutions <see cref="DvrpSolution"/>.</param> /// <returns>Binary serialized string with a description of the best solution.</returns> public override byte[] MergeSolution(byte[][] solutions) { try { var finalSolution = new DvrpSolution(double.MaxValue, null); foreach (var t in solutions) { using (var memoryStream = new MemoryStream(t)) { var solution = (DvrpSolution)_formatter.Deserialize(memoryStream); if (solution.FinalDistance < finalSolution.FinalDistance) { finalSolution = solution; } } } using (var memoryStream = new MemoryStream()) { var result = "Final Distance: " + finalSolution.FinalDistance; foreach (var route in finalSolution.CarsRoutes) { result += "\n"; if (route.Length == 0) { break; } result = route.Aggregate(result, (current, i) => current + (" " + i)); } _formatter.Serialize(memoryStream, result); return(memoryStream.ToArray()); } } catch (Exception ex) { Exception = ex; State = TaskSolverState.Error; return(null); } }
/// <summary> /// Merge partial solutions into the final one. /// </summary> /// <param name="solutions">Binary serialized DVRP solutions <see cref="DvrpSolution"/>.</param> /// <returns>Binary serialized string with a description of the best solution.</returns> public override byte[] MergeSolution(byte[][] solutions) { try { var finalSolution = new DvrpSolution(double.MaxValue, null); foreach (var t in solutions) { using (var memoryStream = new MemoryStream(t)) { var solution = (DvrpSolution) _formatter.Deserialize(memoryStream); if (solution.FinalDistance < finalSolution.FinalDistance) { finalSolution = solution; } } } using (var memoryStream = new MemoryStream()) { var result = "Final Distance: " + finalSolution.FinalDistance; foreach (var route in finalSolution.CarsRoutes) { result += "\n"; if (route.Length == 0) break; result = route.Aggregate(result, (current, i) => current + (" " + i)); } _formatter.Serialize(memoryStream, result); return memoryStream.ToArray(); } } catch (Exception ex) { Exception = ex; State = TaskSolverState.Error; return null; } }