private void AssertRouteOrginsMatch(List <XYZ> expectedPath, IRoute actualRoute) { var route = actualRoute.ToList(); Assert.AreEqual(expectedPath.Count, route.Count()); for (var i = 0; i < expectedPath.Count; i++) { Assert.AreEqual(expectedPath[i], route[i].First()); } }
/// <summary> /// Executes the solver. /// </summary> /// <param name="problem"></param> /// <returns></returns> public IRoute Solve(IProblem problem) { IProblem converted_problem = problem; bool first = problem.First.HasValue; bool last = problem.Last.HasValue; // convert the problem if needed. if (!first && !last) { // add a virtual customer with distance zero to all existing customers. double[][] new_weights = new double[problem.WeightMatrix.Length + 1][]; new_weights[0] = new double[problem.WeightMatrix.Length + 1]; for (int idx = 0; idx < problem.WeightMatrix.Length + 1; idx++) { new_weights[0][idx] = 0; } for (int idx = 0; idx < problem.WeightMatrix.Length; idx++) { new_weights[idx + 1] = new double[problem.WeightMatrix.Length + 1]; new_weights[idx + 1][0] = 0; problem.WeightMatrix[idx].CopyTo(new_weights[idx + 1], 1); } converted_problem = MatrixProblem.CreateATSP(new_weights, 0); } else if (!last) { // set all weights to the first customer to zero. for (int idx = 0; idx < problem.WeightMatrix.Length; idx++) { problem.WeightMatrix[idx][problem.First.Value] = 0; } converted_problem = MatrixProblem.CreateATSP(problem.WeightMatrix, problem.First.Value); } // execute the solver on the converted problem. IRoute converted_route = this.DoSolve(converted_problem); // convert the route back. if (!first && !last) { // when a virtual customer was added the route needs converting. List <int> customers_list = converted_route.ToList <int>(); customers_list.RemoveAt(0); for (int idx = 0; idx < customers_list.Count; idx++) { customers_list[idx] = customers_list[idx] - 1; } converted_route = DynamicAsymmetricRoute.CreateFrom(customers_list, false); } else if (!last) { // the returned route will return to customer zero; convert the route. converted_route = DynamicAsymmetricRoute.CreateFrom(converted_route, false); } return(converted_route); }
public void EnsureMatches() { if (_route1.Count() != _route2.Count()) { throw new NonMatchingException(string.Format("Route lengths were: {0}, {1}", _route1.Count(), _route2.Count())); } var segments1 = _route1.ToList(); var segments2 = _route2.ToList(); for (int i = 0; i < segments1.Count(); i++) { EnsurePathsMatch(segments1[i], segments2[i], i); } }