Skip to content
Snippets Groups Projects
Commit 32dda218 authored by awenjb's avatar awenjb
Browse files

Clean code

- replace all [ . ] by .at( . )  when looking into a vector
parent dd723af1
Branches
No related tags found
No related merge requests found
......@@ -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);
}
......
......@@ -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;
}
......
......@@ -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);
}
}
......
......@@ -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;
}
}
......
......@@ -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);
......
......@@ -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;
......
......@@ -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);
}
......
......@@ -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
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment