×
Stochastic GRASP & VND Local Search Algorithm
FUNCTION MetaHeuristic_CVRP(Nodes, DistanceMatrix, Demands, Capacity, Depth, MaxIterations):
GlobalBestDistance = INFINITY
GlobalBestRoutes = NULL
FOR iter = 1 TO MaxIterations DO:
// --- PHASE 1: SAVINGS CALCULATION ---
SavingsList = []
FOR EACH city i = 1 TO N:
FOR EACH city j = i+1 TO N:
S_ij = Distance(i, Depot) + Distance(Depot, j) - Distance(i, j)
SavingsList.append({i, j, saving: S_ij})
Sort SavingsList in DESCENDING order of saving
// --- PHASE 2: INITIALIZATION ---
CurrentRoutes = []
FOR EACH city i = 1 TO N:
Route = [Depot, i, Depot]
Route.Load = Demands[i]
CurrentRoutes.append(Route)
// --- PHASE 3: STOCHASTIC ROUTE MERGING (GRASP CORE) ---
WHILE True:
ValidCandidates = []
FOR EACH pair (i, j) in SavingsList:
Route_i = findRoute(i)
Route_j = findRoute(j)
IF Route_i != Route_j AND (i and j are adjacent to Depot):
IF Route_i.Load + Route_j.Load <= Capacity:
ValidCandidates.append({Route_i, Route_j, i, j})
IF length(ValidCandidates) == Depth: BREAK FOR
IF length(ValidCandidates) == 0: BREAK WHILE
RandomIndex = randomInt(0, length(ValidCandidates) - 1)
SelectedMerge = ValidCandidates[RandomIndex]
MergedRoute = Merge(SelectedMerge.Route_i, SelectedMerge.Route_j)
CurrentRoutes.remove(SelectedMerge.Route_i)
CurrentRoutes.remove(SelectedMerge.Route_j)
CurrentRoutes.append(MergedRoute)
Remove SelectedMerge's pair from SavingsList
END WHILE
// --- PHASE 4: VARIABLE NEIGHBORHOOD DESCENT (LOCAL SEARCH) ---
IF InterSwapEnabled: CurrentRoutes = ApplyInterRouteSwap(CurrentRoutes, Capacity)
IF Inter2OptEnabled: CurrentRoutes = ApplyInterRouteCrossExchange(CurrentRoutes, Capacity)
IF IntraSwapEnabled: CurrentRoutes = ApplyIntraRouteSwap(CurrentRoutes)
IF Intra2OptEnabled: CurrentRoutes = ApplyIntraRoute2Opt(CurrentRoutes)
// --- PHASE 5: EVALUATION ---
CurrentTotalDist = CalculateTotalDistance(CurrentRoutes)
IF CurrentTotalDist < GlobalBestDistance:
GlobalBestDistance = CurrentTotalDist
GlobalBestRoutes = DeepCopy(CurrentRoutes)
END FOR
RETURN GlobalBestRoutes, GlobalBestDistance
END FUNCTION