/// <summary> /// Returns true if the point at the given original location index is in error. /// </summary> public static bool IsInError <T>(this IDirectedWeightMatrixAlgorithm <T> algorithm, int locationIdx) { LocationError le; RouterPointError rpe; return(algorithm.TryGetError(locationIdx, out le, out rpe)); }
/// <summary> /// Builds the result route in segments divided by routes between customers. /// </summary> /// <returns></returns> public static List <Result <Route> > TryBuildRoutes <T>(this IDirectedWeightMatrixAlgorithm <T> algorithm, Tour tour) { var routes = new List <Result <Route> >(); // TODO: check what to do here, use the cached version or not? var weightHandler = algorithm.Profile.DefaultWeightHandler(algorithm.Router); foreach (var pair in tour.Pairs()) { // TODO: extract more info at once! var pairFromDepartureId = algorithm.SourcePaths[DirectedHelper.ExtractDepartureId(pair.From)]; var pairToArrivalId = algorithm.TargetPaths[DirectedHelper.ExtractArrivalId(pair.To)]; var pairFromEdgeId = algorithm.Router.Db.Network.GetEdges(pairFromDepartureId.From.Vertex).First(x => x.To == pairFromDepartureId.Vertex).IdDirected(); var pairToEdgeId = algorithm.Router.Db.Network.GetEdges(pairToArrivalId.Vertex).First(x => x.To == pairToArrivalId.From.Vertex).IdDirected(); var pairFromId = DirectedHelper.ExtractId(pair.From); var pairToId = DirectedHelper.ExtractId(pair.To); var fromRouterPoint = algorithm.RouterPoints[pairFromId]; var toRouterPoint = algorithm.RouterPoints[pairToId]; var localRouteRaw = algorithm.Router.TryCalculateRaw(algorithm.Profile, weightHandler, pairFromEdgeId, pairToEdgeId, null).Value; localRouteRaw.StripSource(); localRouteRaw.StripTarget(); var localRoute = algorithm.Router.BuildRoute(algorithm.Profile, weightHandler, fromRouterPoint, toRouterPoint, localRouteRaw); routes.Add(localRoute); } return(routes); }
/// <summary> /// Creates a new router. /// </summary> /// <param name="directedWeightMatrixAlgorithm">The directed weight matrix algorithm.</param> /// <param name="turnPenalty">The turn penalty.</param> /// <param name="fixedTurns">Turn that cannot change, if any.</param> public DirectedSequenceRouter(IDirectedWeightMatrixAlgorithm <float> directedWeightMatrixAlgorithm, float turnPenalty, Tuple <bool?, bool?>[] fixedTurns = null) { _massResolvingAlgorithm = directedWeightMatrixAlgorithm.MassResolver; _directedWeightMatrixAlgorithm = directedWeightMatrixAlgorithm; _turnPenalty = turnPenalty; _fixedTurns = fixedTurns; }
/// <summary> /// Creates a new router. /// </summary> public SequenceDirectedRouter(IDirectedWeightMatrixAlgorithm <float> weightMatrixAlgorithm, float turnPenalty, Tour sequence, SolverBase <float, SequenceDirectedProblem, SequenceDirectedObjective, Tour, float> solver = null) { _turnPenalty = turnPenalty; _sequence = sequence; _weightMatrixAlgorithm = weightMatrixAlgorithm; _solver = solver; }
/// <summary> /// Creates a new TSP router. /// </summary> public TSPRouter(IDirectedWeightMatrixAlgorithm <float> weightMatrixAlgorithm, float turnPenalty, int first, int?last = null, SolverBase <float, TSProblem, TSPObjective, Itinero.Optimization.Tours.Tour, float> solver = null) { _turnPenalty = turnPenalty; _first = first; _last = last; _weightMatrixAlgorithm = weightMatrixAlgorithm; _solver = solver; }
/// <summary> /// Creates a new TSP router. /// </summary> public STSPRouter(IDirectedWeightMatrixAlgorithm <float> weightMatrixAlgorithm, float turnPenalty, float max, int first, int?last = null, SolverBase <float, STSProblem, STSPObjective, Tour, STSPFitness> solver = null) { _turnPenalty = turnPenalty; _max = max; _weightMatrixAlgorithm = weightMatrixAlgorithm; _first = first; _last = last; _solver = solver; }
/// <summary> /// Gets the orginal location index for the given corrected routerpoint index. /// </summary> public static int OriginalLocationIndex <T>(this IDirectedWeightMatrixAlgorithm <T> algorithm, int correctedIdx) { var resolvedIndex = algorithm.OriginalIndexOf(correctedIdx); if (resolvedIndex != -1) { return(algorithm.MassResolver.LocationIndexOf(resolvedIndex)); } return(-1); }
/// <summary> /// Gets the index in the weight matrix, given the orginal location index. /// </summary> public static int WeightIndex <T>(this IDirectedWeightMatrixAlgorithm <T> algorithm, int locationIdx) { var resolvedIndex = algorithm.MassResolver.ResolvedIndexOf(locationIdx); if (resolvedIndex != -1) { return(algorithm.CorrectedIndexOf(resolvedIndex)); } return(-1); }
/// <summary> /// Tries to get an error for the given original location index. /// </summary> public static bool TryGetError <T>(this IDirectedWeightMatrixAlgorithm <T> algorithm, int locationIdx, out LocationError locationError, out RouterPointError routerPointError) { locationError = null; routerPointError = null; if (algorithm.MassResolver.Errors.TryGetValue(locationIdx, out locationError)) { return(true); } var resolvedIndex = algorithm.MassResolver.ResolvedIndexOf(locationIdx); if (algorithm.Errors.TryGetValue(resolvedIndex, out routerPointError)) { return(true); } return(false); }
/// <summary> /// Builds the result route in segments divided by routes between customers. /// </summary> /// <returns></returns> public static List <Route> BuildRoutes <T>(this IDirectedWeightMatrixAlgorithm <T> algorithm, Tour tour) { var routes = new List <Route>(); // TODO: check what to do here, use the cached version or not? var weightHandler = algorithm.Profile.DefaultWeightHandler(algorithm.Router); foreach (var pair in tour.Pairs()) { // TODO: extract more info at once! var pairFromDepartureId = algorithm.SourcePaths[DirectedHelper.ExtractDepartureId(pair.From)]; var pairToArrivalId = algorithm.TargetPaths[DirectedHelper.ExtractArrivalId(pair.To)]; var pairFromEdgeId = algorithm.Router.Db.Network.GetEdges(pairFromDepartureId.From.Vertex).First(x => x.To == pairFromDepartureId.Vertex).IdDirected(); var pairToEdgeId = algorithm.Router.Db.Network.GetEdges(pairToArrivalId.Vertex).First(x => x.To == pairToArrivalId.From.Vertex).IdDirected(); var pairFromId = DirectedHelper.ExtractId(pair.From); var pairToId = DirectedHelper.ExtractId(pair.To); var fromRouterPoint = algorithm.RouterPoints[pairFromId]; var toRouterPoint = algorithm.RouterPoints[pairToId]; var localRouteRaw = algorithm.Router.TryCalculateRaw(algorithm.Profile, weightHandler, pairFromEdgeId, pairToEdgeId, null).Value; localRouteRaw.StripSource(); localRouteRaw.StripTarget(); var localRoute = algorithm.Router.BuildRoute(algorithm.Profile, weightHandler, fromRouterPoint, toRouterPoint, localRouteRaw); if (localRoute.IsError) { throw new Itinero.Exceptions.RouteNotFoundException( string.Format("Part of the tour was not found: {0}[{1}] -> {2}[{3}] - {4}.", pair.From, pairFromId, pair.To, pairToId, localRoute.ErrorMessage)); } routes.Add(localRoute.Value); } return(routes); }