From 32dda218470d9e4ef95d21976bf62c2b32a967fe Mon Sep 17 00:00:00 2001 From: awenjb <126257927+awenjb@users.noreply.github.com> Date: Fri, 7 Mar 2025 09:22:26 +0100 Subject: [PATCH] Clean code - replace all [ . ] by .at( . ) when looking into a vector --- src/input/data.cpp | 20 ++++++++-------- src/input/pdptw_data.cpp | 12 +++++----- .../capacity/capacity_constraint.cpp | 18 +++++++------- .../time_window/time_window_constraint.cpp | 24 +++++++++---------- src/lns/modification/pair/insert_pair.cpp | 8 +++---- src/lns/modification/pair/remove_pair.cpp | 10 ++++---- src/lns/solution/route.cpp | 2 +- src/lns/solution/solution.cpp | 4 ++-- 8 files changed, 49 insertions(+), 49 deletions(-) diff --git a/src/input/data.cpp b/src/input/data.cpp index 223816c..10d3f96 100644 --- a/src/input/data.cpp +++ b/src/input/data.cpp @@ -6,7 +6,7 @@ double data::addedCostForInsertion(PDPTWData const &data, int before, int toInse { const Matrix & matrix = data.getMatrix(); double cost = 0; - cost = matrix[before][toInsert] + matrix[toInsert][after] - matrix[before][after]; + cost = matrix.at(before).at(toInsert) + matrix.at(toInsert).at(after) - matrix.at(before).at(after); return cost; } @@ -15,7 +15,7 @@ double data::removedCostForSuppression(PDPTWData const &data, int before, int to const Matrix & matrix = data.getMatrix(); double cost = 0; - cost = matrix[before][after] - matrix[before][toRemove] - matrix[toRemove][after]; + cost = matrix.at(before).at(after) - matrix.at(before).at(toRemove) - matrix.at(toRemove).at(after); return cost; } @@ -31,28 +31,28 @@ double data::routeCost(PDPTWData const & data, Route const & route) return 0; } // cost from and to the depot - cost += matrix[0][routeIDs.at(0)]; - //std::cout << "\n route cost : " << matrix[0][routeIDs[0]] << " "; - cost += matrix[routeIDs.back()][0]; + cost += matrix.at(0).at(routeIDs.at(0)); + //std::cout << "\n route cost : " << matrix.at(0).at(routeIDs.at(0) << " "; + cost += matrix.at(routeIDs.back()).at(0); // cost in the route for (size_t i = 0; i < routeIDs.size() - 1; ++i) { - cost += matrix[routeIDs[i]][routeIDs[i+1]]; - //std::cout << matrix[routeIDs[i]][routeIDs[i+1]] << " "; + cost += matrix.at(routeIDs.at(i)).at(routeIDs.at(i+1)); + //std::cout << matrix.at(routeIDs.at(i).at(routeIDs.at(i+1) << " "; } - //std::cout << matrix[routeIDs.back()][0] << " : " << cost << "\n"; + //std::cout << matrix.at(routeIDs.back()).at(0) << " : " << cost << "\n"; return cost; } double data::TravelCost(PDPTWData const &data, int from, int to) { - return data.getMatrix()[from][to]; + return data.getMatrix().at(from).at(to); } double data::TravelTime(PDPTWData const &data, int from, int to) { - return data.getMatrix()[from][to]; + return data.getMatrix().at(from).at(to); } diff --git a/src/input/pdptw_data.cpp b/src/input/pdptw_data.cpp index ad31f15..5682449 100644 --- a/src/input/pdptw_data.cpp +++ b/src/input/pdptw_data.cpp @@ -55,7 +55,7 @@ PDPTWData::PDPTWData(int size, int capacity, Location depot, std::vector<Locatio if( loc.getLocType() == LocType::PICKUP ) { // vector indexed from 0 / Location indexed from 1 - pairs.emplace_back(loc, this->locations[loc.getPair()-1], loc.getId()); + pairs.emplace_back(loc, this->locations.at(loc.getPair()-1), loc.getId()); } } } @@ -135,7 +135,7 @@ bool PDPTWData::checkMatrix() const { for (int k = 0; k < size; k++) { - if (getMatrix()[i][j] > getMatrix()[i][k] + getMatrix()[k][j]) + if (getMatrix().at(i).at(j) > getMatrix().at(i).at(k) + getMatrix().at(k).at(j)) { return true; } @@ -152,7 +152,7 @@ bool PDPTWData::checkLocation() const // check if location id equals the position in the location vector for (size_t i = 0; i < size-1; ++i) { - if (locations[i].getId() != static_cast<int>(i)+1) { + if (locations.at(i).getId() != static_cast<int>(i)+1) { return true; } } @@ -162,9 +162,9 @@ bool PDPTWData::checkLocation() const { if (loc.getLocType() == LocType::PICKUP) { - if ( (getLocations()[loc.getPair()-1].getLocType() != LocType::DELIVERY) - || (loc.getDemand() != - getLocations()[loc.getPair()-1].getDemand()) - || (loc.getId() != getLocations()[loc.getPair()-1].getPair()) ) + if ( (getLocations().at(loc.getPair()-1).getLocType() != LocType::DELIVERY) + || (loc.getDemand() != - getLocations().at(loc.getPair()-1).getDemand()) + || (loc.getId() != getLocations().at(loc.getPair()-1).getPair()) ) { return true; } diff --git a/src/lns/constraints/capacity/capacity_constraint.cpp b/src/lns/constraints/capacity/capacity_constraint.cpp index 157c37e..ccc8d97 100644 --- a/src/lns/constraints/capacity/capacity_constraint.cpp +++ b/src/lns/constraints/capacity/capacity_constraint.cpp @@ -36,7 +36,7 @@ void CapacityConstraint::initCapacities() std::vector<int> const & CapacityConstraint::getRouteCapacities(int routeIndex) const { - return routeCapacities[routeIndex]; + return routeCapacities.at(routeIndex); } // check for every location between the pickupPosition and the deliveryPosition @@ -69,33 +69,33 @@ void CapacityConstraint::applyModif(Pair const &pair, int routeIndex, int Pickup if (addPair) { // Insert new values - routeCapacities[routeIndex].insert(routeCapacities[routeIndex].begin()+DeliveryPosition, 0); + routeCapacities.at(routeIndex).insert(routeCapacities.at(routeIndex).begin()+DeliveryPosition, 0); if (DeliveryPosition != 0) { - routeCapacities[routeIndex][DeliveryPosition] += routeCapacities[routeIndex][DeliveryPosition-1]; + routeCapacities.at(routeIndex).at(DeliveryPosition) += routeCapacities.at(routeIndex).at(DeliveryPosition-1); } - routeCapacities[routeIndex].insert(routeCapacities[routeIndex].begin()+PickupPosition, pair.getPickup().getDemand()); + routeCapacities.at(routeIndex).insert(routeCapacities.at(routeIndex).begin()+PickupPosition, pair.getPickup().getDemand()); if (PickupPosition != 0) { - routeCapacities[routeIndex][PickupPosition] += routeCapacities[routeIndex][PickupPosition-1]; + routeCapacities.at(routeIndex).at(PickupPosition) += routeCapacities.at(routeIndex).at(PickupPosition-1); } // Update value for (int i = PickupPosition + 1; i < DeliveryPosition + 1; ++i) { - routeCapacities[routeIndex][i] += pair.getPickup().getDemand(); + routeCapacities.at(routeIndex).at(i) += pair.getPickup().getDemand(); } } else { for (int i = PickupPosition + 1; i < DeliveryPosition; ++i) { - routeCapacities[routeIndex][i] += pair.getDelivery().getDemand(); + routeCapacities.at(routeIndex).at(i) += pair.getDelivery().getDemand(); } // remove pair - routeCapacities[routeIndex].erase(routeCapacities[routeIndex].begin() + DeliveryPosition); - routeCapacities[routeIndex].erase(routeCapacities[routeIndex].begin() + PickupPosition); + routeCapacities.at(routeIndex).erase(routeCapacities.at(routeIndex).begin() + DeliveryPosition); + routeCapacities.at(routeIndex).erase(routeCapacities.at(routeIndex).begin() + PickupPosition); } } diff --git a/src/lns/constraints/time_window/time_window_constraint.cpp b/src/lns/constraints/time_window/time_window_constraint.cpp index 8f5cc14..5ad8670 100644 --- a/src/lns/constraints/time_window/time_window_constraint.cpp +++ b/src/lns/constraints/time_window/time_window_constraint.cpp @@ -31,15 +31,15 @@ void TimeWindowConstraint::computeReachTimes(const PDPTWData& data, const std::v // Adjust the size of reachTimes vector reachTimes.resize(routeIDs.size(), 0); // Time to the first location - reachTimes[0] = data.getDepot().getTimeWindow().getStart() + data::TravelTime(data, 0, routeIDs.at(0)); + reachTimes.at(0) = data.getDepot().getTimeWindow().getStart() + data::TravelTime(data, 0, routeIDs.at(0)); // Compute other reachTimes (max between arrival and start of the time window + previous service time + travel time) for (int i = 1; i < routeIDs.size(); ++i) { - TimeInteger travelTime = data::TravelTime(data, routeIDs[i - 1], routeIDs[i]); + TimeInteger travelTime = data::TravelTime(data, routeIDs.at(i - 1), routeIDs.at(i)); // locations are indexed from 0 to n-1, - TimeInteger serviceTime = data.getLocation(routeIDs[i - 1]).getServiceDuration(); - TimeInteger startTW = data.getLocation(routeIDs[i - 1]).getTimeWindow().getStart(); + TimeInteger serviceTime = data.getLocation(routeIDs.at(i - 1)).getServiceDuration(); + TimeInteger startTW = data.getLocation(routeIDs.at(i - 1)).getTimeWindow().getStart(); - reachTimes[i] = std::max(reachTimes[i - 1], startTW) + serviceTime + travelTime; + reachTimes.at(i) = std::max(reachTimes.at(i - 1), startTW) + serviceTime + travelTime; } } @@ -96,7 +96,7 @@ bool TimeWindowConstraint::checkInsertion(const PDPTWData& data, const Pair & pa // Check Time Windows for (int i = 0; i < newReachTimes.size(); ++i) { - if (! data.getLocation(route[i]).getTimeWindow().isValid(newReachTimes.at(i)) ) + if (! data.getLocation(route.at(i)).getTimeWindow().isValid(newReachTimes.at(i)) ) { return false; } @@ -124,15 +124,15 @@ void TimeWindowConstraint::ApplyModif(const PDPTWData& data, const Pair & pair, } // Adjust the size of reachTimes vector - allRouteReachTimes[routeIndex].resize(routeIDs.size(), 0); + allRouteReachTimes.at(routeIndex).resize(routeIDs.size(), 0); // Time to the first location - allRouteReachTimes[routeIndex][0] = data.getDepot().getTimeWindow().getStart() + data::TravelTime(data, 0, routeIDs.at(0)); + allRouteReachTimes.at(routeIndex).at(0) = data.getDepot().getTimeWindow().getStart() + data::TravelTime(data, 0, routeIDs.at(0)); // Compute other reachTimes (max between arrival and start of the time window) for (int i = 1; i < routeIDs.size(); ++i) { - TimeInteger travelTime = data::TravelTime(data, routeIDs[i - 1], routeIDs[i]); - TimeInteger serviceTime = data.getLocation(routeIDs[i - 1]).getServiceDuration(); - TimeInteger startTW = data.getLocation(routeIDs[i - 1]).getTimeWindow().getStart(); - allRouteReachTimes[routeIndex][i] = std::max(allRouteReachTimes[routeIndex][i - 1], startTW) + serviceTime + travelTime; + TimeInteger travelTime = data::TravelTime(data, routeIDs.at(i - 1), routeIDs.at(i)); + TimeInteger serviceTime = data.getLocation(routeIDs.at(i - 1)).getServiceDuration(); + TimeInteger startTW = data.getLocation(routeIDs.at(i - 1)).getTimeWindow().getStart(); + allRouteReachTimes.at(routeIndex).at(i) = std::max(allRouteReachTimes.at(routeIndex).at(i - 1), startTW) + serviceTime + travelTime; } } diff --git a/src/lns/modification/pair/insert_pair.cpp b/src/lns/modification/pair/insert_pair.cpp index 82212c5..5a7f401 100644 --- a/src/lns/modification/pair/insert_pair.cpp +++ b/src/lns/modification/pair/insert_pair.cpp @@ -34,8 +34,8 @@ double InsertPair::evaluate(Solution const &solution) const { const std::vector<int> & routeIDs = route.getRoute(); const PDPTWData &data = solution.getData(); - int prevPickup = (pickupInsertion == 0) ? 0 : routeIDs[pickupInsertion - 1]; - int nextPickup = (pickupInsertion >= routeIDs.size()) ? 0 : routeIDs[pickupInsertion]; + int prevPickup = (pickupInsertion == 0) ? 0 : routeIDs.at(pickupInsertion - 1); + int nextPickup = (pickupInsertion >= routeIDs.size()) ? 0 : routeIDs.at(pickupInsertion); double pickupCost = data::addedCostForInsertion(data, prevPickup, pickupLocation.getId(), nextPickup); @@ -43,12 +43,12 @@ double InsertPair::evaluate(Solution const &solution) const { // the insertion of the delivery is done just after the pickup without intermediate location // otherwise, the pickup and the delivery insertion are independant and the pickup insertion does not affect the delivery insertion cost - int prevDelivery = (deliveryInsertion == 0) ? 0 : routeIDs[deliveryInsertion - 1]; + int prevDelivery = (deliveryInsertion == 0) ? 0 : routeIDs.at(deliveryInsertion - 1); if (pickupInsertion == deliveryInsertion) { prevDelivery = pickupLocation.getId(); } - int nextDelivery = (deliveryInsertion >= routeIDs.size()) ? 0 : routeIDs[deliveryInsertion]; + int nextDelivery = (deliveryInsertion >= routeIDs.size()) ? 0 : routeIDs.at(deliveryInsertion); //std::cout << "insert " << prevDelivery << " + " << deliveryLocation.getId() << " + " << nextDelivery << "\n"; double deliveryCost = data::addedCostForInsertion(data, prevDelivery, deliveryLocation.getId(), nextDelivery); diff --git a/src/lns/modification/pair/remove_pair.cpp b/src/lns/modification/pair/remove_pair.cpp index 6cf567c..11472a8 100644 --- a/src/lns/modification/pair/remove_pair.cpp +++ b/src/lns/modification/pair/remove_pair.cpp @@ -18,7 +18,7 @@ void RemovePair::modifySolution(Solution &solution) // update removedPairID - removedPairID.push_back(route.getRoute()[pickupDeletion]); + removedPairID.push_back(route.getRoute().at(pickupDeletion)); // remove the delivery before (to not have to update the index) route.deleteAt(deliveryDeletion); @@ -31,14 +31,14 @@ double RemovePair::evaluate(Solution const &solution) const std::vector<int> const &routeIDs = route.getRoute(); PDPTWData const &data = solution.getData(); - int prevPickup = (pickupDeletion == 0) ? 0 : routeIDs[pickupDeletion - 1]; + int prevPickup = (pickupDeletion == 0) ? 0 : routeIDs.at(pickupDeletion - 1); // pickup location should not be at the end of a route anyway - int nextPickup = (pickupDeletion >= routeIDs.size()) ? 0 : routeIDs[pickupDeletion + 1]; + int nextPickup = (pickupDeletion >= routeIDs.size()) ? 0 : routeIDs.at(pickupDeletion + 1); double pickupCost = data::removedCostForSuppression(data, prevPickup, pickupLocation.getId(), nextPickup); - int prevDelivery = (deliveryDeletion == 0) ? 0 : routeIDs[deliveryDeletion - 1]; - int nextDelivery = (deliveryDeletion >= routeIDs.size()) ? 0 : routeIDs[deliveryDeletion + 1]; + int prevDelivery = (deliveryDeletion == 0) ? 0 : routeIDs.at(deliveryDeletion - 1); + int nextDelivery = (deliveryDeletion >= routeIDs.size()) ? 0 : routeIDs.at(deliveryDeletion + 1); if (deliveryDeletion == pickupDeletion + 1) { prevDelivery = prevPickup; diff --git a/src/lns/solution/route.cpp b/src/lns/solution/route.cpp index eb06aad..9ac3c66 100644 --- a/src/lns/solution/route.cpp +++ b/src/lns/solution/route.cpp @@ -52,7 +52,7 @@ int Route::getLocation(int index) const spdlog::error("Invalid index when reading route: {}", index); throw std::out_of_range("Invalid index when reading route"); } - return route[index]; + return route.at(index); } diff --git a/src/lns/solution/solution.cpp b/src/lns/solution/solution.cpp index 9677f3a..e9e7b54 100644 --- a/src/lns/solution/solution.cpp +++ b/src/lns/solution/solution.cpp @@ -112,7 +112,7 @@ Route &Solution::getRoute(int routeIndex) spdlog::error("Invalid route index: {}", routeIndex); throw std::out_of_range("Invalid route index."); } - return routes[routeIndex]; + return routes.at(routeIndex); } Route const &Solution::getRoute(int routeIndex) const @@ -122,7 +122,7 @@ Route const &Solution::getRoute(int routeIndex) const spdlog::error("Invalid route index: {}", routeIndex); throw std::out_of_range("Invalid route index."); } - return routes[routeIndex]; + return routes.at(routeIndex); } double Solution::getCost() const -- GitLab