/// <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> /// Tries all 3Opt Moves for the neighbourhood of v1. /// </summary> /// <returns></returns> public bool Try3OptMoves(TSProblem problem, float[][] weights, Tour tour, int v1, out float delta) { // get v_2. var v2 = tour.GetNeigbour(v1); if (v2 < 0) { delta = 0; return(false); } var betweenV2V1 = tour.Between(v2, v1); //var weightV1V2 = weights[v1][v2]; var weightV1V2 = weights.WeightWithoutTurns(v1, v2); if (DirectedHelper.ExtractId(v2) == problem.First && !problem.Last.HasValue) { // set to zero if not closed. weightV1V2 = 0; } int v3 = -1; NearestNeighbours neighbours = null; if (_nearestNeighbours) { neighbours = problem.GetNNearestNeighboursForward(10, DirectedHelper.ExtractId(v1)); } foreach (int v4 in betweenV2V1) { if (v3 >= 0 && v3 != v1) { var v4Id = DirectedHelper.ExtractId(v4); if (!_nearestNeighbours || neighbours.Contains(v4Id)) { //var weightV1V4 = weights[v1][v4]; var weightV1V4 = weights.WeightWithoutTurns(v1, v4); //var weightV3V4 = weights[v3][v4]; var weightV3V4 = weights.WeightWithoutTurns(v3, v4); if (v4Id == problem.First && !problem.Last.HasValue) { // set to zero if not closed. weightV1V4 = 0; weightV3V4 = 0; } var weightV1V2PlusV3V4 = weightV1V2 + weightV3V4; //var weightsV3 = weights[v3]; var weightsV3 = weights[DirectedHelper.ExtractDepartureId(v3)]; if (this.Try3OptMoves(problem, weights, tour, v1, v2, v3, weightsV3, v4, weightV1V2PlusV3V4, weightV1V4, out delta)) { return(true); } } } v3 = v4; } delta = 0; 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); }
/// <summary> /// Applies this operator. /// </summary> public bool Apply(SequenceDirectedProblem problem, SequenceDirectedObjective objective, Tour solution, out float delta) { var weights = problem.Weights; delta = objective.Zero; var fitnessBefore = objective.Calculate(problem, solution); var enumerator = solution.GetEnumerator(); while (enumerator.MoveNext()) { var previous = enumerator.Current; var previousDepartureId = DirectedHelper.ExtractDepartureId(previous); var current = solution.GetNeigbour(previous); if (current == Constants.NOT_SET) { // last of open. continue; } var next = solution.GetNeigbour(current); var nextArrivalId = Constants.NOT_SET; if (next != Constants.NOT_SET) { nextArrivalId = DirectedHelper.ExtractArrivalId(next); } int currentTurn, currentId, currentArrivalId, currentDepartureId; DirectedHelper.ExtractAll(current, out currentArrivalId, out currentDepartureId, out currentId, out currentTurn); var weightBefore = weights[previousDepartureId][currentArrivalId]; var weightAfter = float.MaxValue; var newDirectedId = Constants.NOT_SET; if (next == Constants.NOT_SET) { // there is no next, only one option here: // 0 or 1: arrival id offset = 0 OR // 2 or 3: arrival id offset = 1 if (currentArrivalId % 2 == 0) { // check turn = 2. weightAfter = weights[previousDepartureId][currentArrivalId + 1]; if (weightAfter < weightBefore) { // do the switch. newDirectedId = DirectedHelper.BuildDirectedId(currentId, 2); } } else { // check turn = 0 weightAfter = weights[previousDepartureId][currentArrivalId - 1]; if (weightAfter < weightBefore) { // do the switch. newDirectedId = DirectedHelper.BuildDirectedId(currentId, 0); } } } else { // three options left, excluding the current one of 4. weightBefore += weights[currentDepartureId][nextArrivalId]; weightBefore += problem.TurnPenalties[currentTurn]; currentArrivalId = currentArrivalId - (currentArrivalId % 2); currentDepartureId = currentDepartureId - (currentDepartureId % 2); for (var i = 0; i < 4; i++) { if (i == currentTurn) { continue; } int arrivalOffset, departureOffset; DirectedHelper.ExtractOffset(i, out arrivalOffset, out departureOffset); var newWeightAfter = weights[previousDepartureId][currentArrivalId + arrivalOffset] + weights[currentDepartureId + departureOffset][nextArrivalId] + problem.TurnPenalties[i]; if (newWeightAfter < weightAfter && newWeightAfter < weightBefore) { newDirectedId = DirectedHelper.BuildDirectedId(currentId, i); weightAfter = newWeightAfter; } } } // switch if a new directed if was found. if (newDirectedId != Constants.NOT_SET) { solution.Replace(current, newDirectedId); } } var fitnessAfter = objective.Calculate(problem, solution); delta = objective.Subtract(problem, fitnessBefore, fitnessAfter); return(objective.CompareTo(problem, delta, objective.Zero) > 0); }
/// <summary> /// Optimizes the given part of the tour by choosing the best improvements in either the departureOffset or arrivalOffset of the first/last customers or the turns at any of the intermediate ones. /// </summary> private float OptimizePart(TSPTWProblem problem, List <int> part) { int turn1, arrivalId1, departureId1, id1; int turn2, arrivalId2, departureId2, id2; int turn3, arrivalId3, departureId3, id3; var delta = 0f; // the positive difference. var arrivalIdChange = -1; var departureIdChange = -1; var customerIdx = -1; var customerTurn = -1; // try changing the departureId. DirectedHelper.ExtractAll(part[0], out arrivalId1, out departureId1, out id1, out turn1); DirectedHelper.ExtractAll(part[1], out arrivalId2, out departureId2, out id2, out turn2); var weight = problem.TurnPenalties[turn1]; weight += problem.Times[departureId1][arrivalId2]; var new0 = DirectedHelper.SwitchDepartureOffset(part[0]); DirectedHelper.ExtractAll(new0, out arrivalId3, out departureId3, out id3, out turn3); var newWeight = problem.TurnPenalties[turn3]; newWeight += problem.Times[departureId3][arrivalId2]; if (newWeight < weight) { // there was an improvement found in changing the departure id. departureIdChange = DirectedHelper.ExtractDepartureId(new0); delta = newWeight - weight; } // try changing the arrivalId. DirectedHelper.ExtractAll(part[part.Count - 2], out arrivalId1, out departureId1, out id1, out turn1); DirectedHelper.ExtractAll(part[part.Count - 1], out arrivalId2, out departureId2, out id2, out turn2); weight = problem.TurnPenalties[turn2]; weight += problem.Times[departureId1][arrivalId2]; var newLast = DirectedHelper.SwitchArrivalOffset(part[part.Count - 1]); DirectedHelper.ExtractAll(newLast, out arrivalId3, out departureId3, out id3, out turn3); newWeight = problem.TurnPenalties[turn3]; newWeight += problem.Times[departureId1][arrivalId3]; if (newWeight < weight && delta < (newWeight - weight)) { // there was an improvement found in changing the arrival id. arrivalIdChange = DirectedHelper.ExtractDepartureId(newLast); departureIdChange = -1; delta = newWeight - weight; } for (var c = 1; c < part.Count - 1; c++) { var perviousDepartureId = DirectedHelper.ExtractDepartureId(part[c - 1]); DirectedHelper.ExtractAll(part[c], out arrivalId1, out departureId1, out id1, out turn1); var nextArrivalid = DirectedHelper.ExtractArrivalId(part[c + 1]); weight = problem.Times[perviousDepartureId][arrivalId1]; weight += problem.TurnPenalties[turn1]; weight += problem.Times[departureId1][nextArrivalid]; for (var t = 0; t < 3; t++) { if (t == turn1) { continue; } var newDirectedId = DirectedHelper.BuildDirectedId(id1, t); DirectedHelper.ExtractAll(newDirectedId, out arrivalId2, out departureId2, out id2, out turn2); newWeight = problem.Times[perviousDepartureId][arrivalId2]; newWeight += problem.TurnPenalties[turn2]; newWeight += problem.Times[departureId2][nextArrivalid]; if (newWeight < weight && delta < (newWeight - weight)) { // there was an improvement found in changing the turn. arrivalIdChange = -1; departureIdChange = -1; customerIdx = c; customerTurn = t; delta = newWeight - weight; } } } if (delta > 0) { if (departureIdChange != -1) { part[0] = DirectedHelper.SwitchDepartureOffset(part[0]); } else if (arrivalIdChange != -1) { part[part.Count - 1] = DirectedHelper.SwitchArrivalOffset(part[part.Count - 1]); } else if (customerIdx != -1) { part[customerIdx] = DirectedHelper.BuildDirectedId( DirectedHelper.ExtractId(part[customerIdx]), customerTurn); } } return(delta); }
/// <summary> /// Inserts the given customer at the best location if possible. /// </summary> /// <param name="tour">The tour to insert into.</param> /// <param name="weights">The directed weights.</param> /// <param name="turnPenalties">The turn pentalties.</param> /// <param name="customer">The customer to insert.</param> /// <param name="metric">The metric to use, time, distance or custom.</param> /// <param name="max">The maximum allowed cost of the insertion.</param> public static Weight InsertCheapestDirected(this Tour tour, Weight[][] weights, Weight[] turnPenalties, ProfileMetric metric, int customer, Weight max) { if (tour.Count == 1) { // there is only one customer, the first one in the route. if (tour.First == tour.Last) { // there is one customer but it's both the start and the end. var firstId = DirectedHelper.ExtractId(tour.First); var bestCost = new Weight() { Distance = float.MaxValue, Time = float.MaxValue, Value = float.MaxValue }; var bestDirected1Id = int.MaxValue; var bestDirected2Id = int.MaxValue; for (var turn1 = 0; turn1 < 4; turn1++) { var firstDirectedId = DirectedHelper.BuildDirectedId(firstId, turn1); var firstArrivalId = DirectedHelper.ExtractArrivalId(firstDirectedId); var firstDepartureId = DirectedHelper.ExtractDepartureId(firstDirectedId); for (var turn2 = 0; turn2 < 4; turn2++) { var customerDirectedId = DirectedHelper.BuildDirectedId(customer, turn2); var customerArrivalId = DirectedHelper.ExtractArrivalId(customerDirectedId); var customerDepartureId = DirectedHelper.ExtractDepartureId(customerDirectedId); var weight = turnPenalties[turn1]; weight += turnPenalties[turn2]; weight += weights[firstDepartureId][customerArrivalId]; weight += weights[customerDepartureId][firstArrivalId]; if (bestCost.GetForMetric(metric) > weight.GetForMetric(metric)) { bestDirected1Id = firstDirectedId; bestDirected2Id = customerDirectedId; bestCost = weight; } } } if (bestCost.GetForMetric(metric) <= max.GetForMetric(metric)) { tour.Replace(tour.First, bestDirected1Id); tour.InsertAfter(tour.First, bestDirected2Id); return bestCost; } } else { // there is one customer, the last one is not set. var firstId = DirectedHelper.ExtractId(tour.First); int departureOffset1, arrivalOffset2; var cost = DirectedHelper.CheapestInsert(weights, turnPenalties, firstId, customer, metric, out departureOffset1, out arrivalOffset2); if (cost.GetForMetric(metric) <= max.GetForMetric(metric)) { var newFirst = DirectedHelper.UpdateDepartureOffset( DirectedHelper.BuildDirectedId(firstId, 0), departureOffset1); var customerDirectedId = DirectedHelper.UpdateArrivalOffset( DirectedHelper.BuildDirectedId(customer, 0), arrivalOffset2); tour.Replace(tour.First, newFirst); tour.InsertAfter(tour.First, customerDirectedId); return cost; } } } else { // at least 2 customers already exist, insert a new one in between. var cost = Weight.MaxValue; var departureOffsetFrom = Constants.NOT_SET; var arrivalOffsetTo = Constants.NOT_SET; var turn = Constants.NOT_SET; var location = new Pair(int.MaxValue, int.MaxValue); foreach (var pair in tour.Pairs()) { int departureOffset1, arrivalOffset3, turn2; var fromIsFirst = tour.IsFirst(pair.From); var toIsLast = tour.IsLast(pair.To); var localCost = CheapestInsertionDirectedHelper.CalculateCheapestInsert(weights, turnPenalties, metric, pair.From, customer, pair.To, !fromIsFirst, !toIsLast, out departureOffset1, out arrivalOffset3, out turn2); if (localCost.GetForMetric(metric) < cost.GetForMetric(metric)) { cost = localCost; location = pair; departureOffsetFrom = departureOffset1; arrivalOffsetTo = arrivalOffset3; turn = turn2; } } if (cost.GetForMetric(metric) <= max.GetForMetric(metric)) { var directedId = DirectedHelper.BuildDirectedId(customer, turn); tour.InsertAfter(location.From, directedId); // update departure offset at from. var newFromId = DirectedHelper.UpdateDepartureOffset(location.From, departureOffsetFrom); if (location.From != newFromId) { tour.Replace(location.From, newFromId); } var newToId = DirectedHelper.UpdateArrivalOffset(location.To, arrivalOffsetTo); if (location.To != newToId) { tour.Replace(location.To, newToId); } return cost; } } return Weight.Zero; // insert failed, probably costs are too high (above given max parameter). }
/// <summary> /// Tries all 3Opt Moves for the neighbourhood of v_1 containing v_3. /// </summary> /// <returns></returns> public bool Try3OptMoves(TSProblem problem, float[][] weights, Tour tour, int v1, int v2, int v3, float[] weightsV3, int v4, float weightV1V2PlusV3V4, float weightV1V4, out float delta) { var v2ArrivalId = DirectedHelper.ExtractArrivalId(v2); // TODO: optimize this, extract all v2 info in one go. var v2Id = DirectedHelper.ExtractId(v2); var betweenV4V1Enumerator = tour.Between(v4, v1).GetEnumerator(); if (betweenV4V1Enumerator.MoveNext()) { var v5 = betweenV4V1Enumerator.Current; if (v5 != v1) { while (betweenV4V1Enumerator.MoveNext()) { var v6 = betweenV4V1Enumerator.Current; //var weightV3V6 = weightsV3[v6]; var v6Id = DirectedHelper.ExtractId(v6); // TODO: optimize this, extract all v6 info in one go. var v6ArrivalId = DirectedHelper.ExtractArrivalId(v6); var v5DepartureId = DirectedHelper.ExtractDepartureId(v5); var weightV3V6 = weightsV3[v6ArrivalId]; //var weightV5V2 = weights[v5][v2]; var weightV5V2 = weights[v5DepartureId][v2ArrivalId]; //var weightV5V6 = weights[v5][v6]; var weightV5V6 = weights[v5DepartureId][v6ArrivalId]; if (v6Id == problem.First && !problem.Last.HasValue) { // set to zero if not closed. weightV3V6 = 0; weightV5V6 = 0; } if (v2Id == problem.First && !problem.Last.HasValue) { // set to zero if not closed. weightV5V2 = 0; } // calculate the total weight of the 'new' arcs. var weightNew = weightV1V4 + weightV3V6 + weightV5V2; // calculate the total weights. var weight = weightV1V2PlusV3V4 + weightV5V6; if (weight - weightNew > _epsilon) { // actually do replace the vertices. var countBefore = tour.Count; delta = weightNew - weight; tour.ReplaceEdgeFrom(v1, v4); tour.ReplaceEdgeFrom(v3, v6); tour.ReplaceEdgeFrom(v5, v2); //int count_after = route.Count; // set bits. this.Set(problem, v3, false); this.Set(problem, v5, false); return(true); // move succeeded. } v5 = v6; } } } delta = 0; return(false); }
/// <summary> /// Applies this operator. /// </summary> public bool Apply(TSProblem problem, TSPObjective objective, Tour solution, out float delta) { if (problem.Weights.Length <= 2) { delta = 0; return(false); } var weights = problem.Weights; delta = 0; // test switching directions in random order. if (_pool == null || solution.Count != _pool.Size) { // create a new pool. _pool = new RandomPool(solution.Count); } else { // just reset the existing one. _pool.Reset(); } while (_pool.MoveNext()) { var previous = solution.GetDirectedId(_pool.Current); var previousDepartureId = DirectedHelper.ExtractDepartureId(previous); var current = solution.GetNeigbour(previous); if (current == Constants.NOT_SET) { // last of open. continue; } var next = solution.GetNeigbour(current); var nextArrivalId = Constants.NOT_SET; if (next != Constants.NOT_SET) { nextArrivalId = DirectedHelper.ExtractArrivalId(next); } int currentTurn, currentId, currentArrivalId, currentDepartureId; DirectedHelper.ExtractAll(current, out currentArrivalId, out currentDepartureId, out currentId, out currentTurn); var weightBefore = weights[previousDepartureId][currentArrivalId]; var weightAfter = float.MaxValue; var newDirectedId = Constants.NOT_SET; if (next == Constants.NOT_SET) { // there is no next, only one option here: // 0 or 1: arrival id offset = 0 OR // 2 or 3: arrival id offset = 1 if (currentArrivalId % 2 == 0) { // check turn = 2. weightAfter = weights[previousDepartureId][currentArrivalId + 1]; if (weightAfter < weightBefore) { // do the switch. newDirectedId = DirectedHelper.BuildDirectedId(currentId, 2); } } else { // check turn = 0 weightAfter = weights[previousDepartureId][currentArrivalId - 1]; if (weightAfter < weightBefore) { // do the switch. newDirectedId = DirectedHelper.BuildDirectedId(currentId, 0); } } } else { // three options left, excluding the current one of 4. weightBefore += weights[currentDepartureId][nextArrivalId]; weightBefore += problem.TurnPenalties[currentTurn]; currentArrivalId = currentArrivalId - (currentArrivalId % 2); currentDepartureId = currentDepartureId - (currentDepartureId % 2); for (var i = 0; i < 4; i++) { if (i == currentTurn) { continue; } int arrivalOffset, departureOffset; DirectedHelper.ExtractOffset(i, out arrivalOffset, out departureOffset); var newWeightAfter = weights[previousDepartureId][currentArrivalId + arrivalOffset] + weights[currentDepartureId + departureOffset][nextArrivalId] + problem.TurnPenalties[i]; if (newWeightAfter < weightAfter && newWeightAfter < weightBefore) { newDirectedId = DirectedHelper.BuildDirectedId(currentId, i); weightAfter = newWeightAfter; } } } // switch if a new directed if was found. if (newDirectedId != Constants.NOT_SET) { delta = weightAfter - weightBefore; // negative when improvement. solution.Replace(current, newDirectedId); } } return(delta < 0); }