Skip to content

Commit

Permalink
TODO: fix CapacityOrDurationIsViolated list index out of bounds bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Ioanna2001 committed Jan 21, 2022
1 parent e918152 commit 0972b13
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
45 changes: 26 additions & 19 deletions Optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ def __init__(self):
self.positionOfSecondNode = None
self.durChangeFirstRt = None
self.durChangeSecondRt = None
self.moveDur = 10**9
self.moveDur = 0

def Initialize(self, rt1, rt2, nd1, nd2, dur1, dur2, mvd):
"""Full constructor
Expand Down Expand Up @@ -128,7 +128,7 @@ def __init__(self):
self.positionOfSecondRoute = None
self.positionOfFirstNode = None
self.positionOfSecondNode = None
self.moveDur = 10**9
self.moveDur = 0

def Initialize(self, positionOfFirstRoute, positionOfSecondRoute, positionOfFirstNode, positionOfSecondNode, moveDur):
"""Full constructor
Expand Down Expand Up @@ -296,12 +296,17 @@ def FindBestSwapMove(self) -> SwapMove:
if rt2.duration + durChangeSecondRoute > rt2.duration:
continue

moveDur = durAdded1 + durAdded2 - (durRemoved1 + durRemoved2)
moveDur = durChangeSecondRoute + durChangeFirstRoute

if moveDur < self.swapMove.moveDur:
copys = SwapMove()
copys.Initialize(firstRouteIndex, secondRouteIndex, firstNodeIndex, secondNodeIndex,
durChangeFirstRoute, durChangeSecondRoute, moveDur)
self.allSwapMoves.append(copys)
self.swapMove.Initialize(firstRouteIndex, secondRouteIndex, firstNodeIndex, secondNodeIndex,
durChangeFirstRoute, durChangeSecondRoute, moveDur)
self.allSwapMoves.append(self.swapMove)
return self.swapMove
self.terminateSearch = True


def FindBestTwoOptMove(self) -> TwoOptMove:
Expand Down Expand Up @@ -341,13 +346,14 @@ def FindBestTwoOptMove(self) -> TwoOptMove:
durAdded = self.distanceMatrix[A.id][K.id] + self.distanceMatrix[B.id][L.id]
durRemoved = self.distanceMatrix[A.id][B.id] + self.distanceMatrix[K.id][L.id]
moveDur = durAdded - durRemoved

allto = TwoOptMove()
allto.Initialize(rtInd1, rtInd2, nodeInd1, nodeInd2, moveCost)
self.allTwoOptMoves.append(allto)
if moveDur < 0:
allto = TwoOptMove()
allto.Initialize(rtInd1, rtInd2, nodeInd1, nodeInd2, moveCost)
self.allTwoOptMoves.append(allto)

if moveDur < self.twoOptMove.moveDur:
return self.twoOptMove.Initialize(rtInd1, rtInd2, nodeInd1, nodeInd2, moveDur)
self.terminateSearch = True

def ApplyRelocationMove(self):

Expand Down Expand Up @@ -390,8 +396,8 @@ def ApplySwapMove(self):
sm = self.swapMove

oldDur = CalculateTotalDuration(self.distanceMatrix, self.initialSolution) # TODO Implement inside Utils.py
rt1 = self.initialSolution.routes[sm.positionOfFirstRoute]
rt2 = self.initialSolution.routes[sm.positionOfSecondRoute]
rt1 = self.optimizedSolution.routes[sm.positionOfFirstRoute]
rt2 = self.optimizedSolution.routes[sm.positionOfSecondRoute]
b1 = rt1.sequenceOfNodes[sm.positionOfFirstNode]
b2 = rt2.sequenceOfNodes[sm.positionOfSecondNode]
rt1.sequenceOfNodes[sm.positionOfFirstNode] = b2
Expand All @@ -415,8 +421,8 @@ def ApplySwapMove(self):
def ApplyTwoOptMove(self):
top = self.twoOptMove

rt1: Route = self.initialSolution.routes[top.positionOfFirstRoute]
rt2: Route = self.initialSolution.routes[top.positionOfSecondRoute]
rt1: Route = self.optimizedSolution.routes[top.positionOfFirstRoute]
rt2: Route = self.optimizedSolution.routes[top.positionOfSecondRoute]

if rt1 == rt2:
reversedSegment = reversed(rt1.sequenceOfNodes[top.positionOfFirstNode + 1: top.positionOfSecondNode + 1])
Expand All @@ -436,7 +442,7 @@ def ApplyTwoOptMove(self):
UpdateRouteLoadDurAndProfit(self.distanceMatrix, rt1)
UpdateRouteLoadDurAndProfit(self.distanceMatrix, rt2)

self.optimizedSolution.duration += top.moveDur
self.optimizedSolution.duration = CalculateTotalDuration(self.distanceMatrix, self.optimizedSolution)

def run(self):

Expand Down Expand Up @@ -519,14 +525,15 @@ def Shake(s, k: int, distanceMatrix):
elif k == 1:
solutions = ls.allSwapMoves
indx = random.randint(0, len(solutions) - 1)
ls.swapMove = solutions[indx]
ls.ApplySwapMove()
ss = ls.optimizedSolution
lsInitial.swapMove = solutions[indx]
lsInitial.ApplySwapMove()
ss = lsInitial.optimizedSolution
elif k == 2:
solutions = ls.ApplyTwoOptMoves()
ls.twoOptMove = solutions[indx]
ls.ApplyTwoOptMove()
ss = ls.optimizedSolution
indx = random.randint(0, len(solutions) - 1)
lsInitial.twoOptMove = solutions[indx]
lsInitial.ApplyTwoOptMove()
ss = lsInitial.optimizedSolution
return ss

'''
Expand Down
3 changes: 1 addition & 2 deletions Solver.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def solve(self):
self.overallBestSol.duration = CalculateTotalDuration(self.distanceMatrix, self.overallBestSol)
print("duration before vns")
print(self.overallBestSol.duration)
self.overallBestSol = VNS(self.overallBestSol, 1, self.distanceMatrix)
self.overallBestSol = VNS(self.overallBestSol, 2, self.distanceMatrix)
print("duration after vns")
self.overallBestSol.duration = CalculateTotalDuration(self.distanceMatrix, self.overallBestSol)
print(self.overallBestSol.duration)
Expand Down Expand Up @@ -235,7 +235,6 @@ def MinimumInsertions(self, itr=30, foundSolution: Solution = None) -> Solution:
else:
termination = True


for r in solution.routes:
solution.duration += r.travelled
solution.profit += r.profit
Expand Down
4 changes: 2 additions & 2 deletions Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def CapacityOrDurationIsViolated(distanceMatrix: list[int], rt1: Route, nodeInd1
Returns:
boolean: True, if capacity restrictions or duration restrictions are violated
"""
rt1Duration = rt1.duration - distanceMatrix[rt1.sequenceOfNodes[nodeInd1]][rt1.sequenceOfNodes[nodeInd1 + 1]]
rt1Duration = rt1.duration - distanceMatrix[rt1.sequenceOfNodes[nodeInd1].id][rt1.sequenceOfNodes[nodeInd1 + 1].id]
rt1FirstSegmentDuration = 0
rt1FirstSegmentLoad = 0
for i in range(0, nodeInd1):
Expand All @@ -102,7 +102,7 @@ def CapacityOrDurationIsViolated(distanceMatrix: list[int], rt1: Route, nodeInd1
rt1SecondSegmentDuration = rt1Duration - rt1FirstSegmentDuration
rt1SecondSegmentLoad = rt1.load - rt1FirstSegmentLoad

rt2Duration = rt2.duration - distanceMatrix[rt2.sequenceOfNodes[nodeInd2]][rt1.sequenceOfNodes[nodeInd2 + 1]]
rt2Duration = rt2.duration - distanceMatrix[rt2.sequenceOfNodes[nodeInd2].id][rt1.sequenceOfNodes[nodeInd2 + 1].id]
rt2FirstSegmentDuration = 0
rt2FirstSegmentLoad = 0
for i in range(0, nodeInd2):
Expand Down

0 comments on commit 0972b13

Please sign in to comment.