Colony.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env python
  2. from datetime import datetime, timedelta
  3. import numpy as np
  4. import random
  5. import sys
  6. import pytz
  7. from aman.sys.aco.Ant import Ant
  8. from aman.sys.aco.Configuration import Configuration
  9. from aman.sys.aco.RunwayManager import RunwayManager
  10. from aman.types.Inbound import Inbound
  11. # This class implements the ant colony of the following paper:
  12. # https://sci-hub.mksa.top/10.1109/cec.2019.8790135
  13. class Colony:
  14. def associateInbound(rwyManager : RunwayManager, inbound : Inbound, earliestArrivalTime : datetime, useITA : bool):
  15. rwy, eta, _ = rwyManager.selectArrivalRunway(inbound, useITA, earliestArrivalTime)
  16. eta = max(earliestArrivalTime, eta)
  17. inbound.PlannedRunway = rwy
  18. inbound.PlannedStar = inbound.ArrivalCandidates[rwy.Name].Star
  19. inbound.PlannedArrivalRoute = inbound.ArrivalCandidates[rwy.Name].ArrivalRoute
  20. inbound.PlannedArrivalTime = eta
  21. inbound.InitialArrivalTime = inbound.ArrivalCandidates[rwy.Name].InitialArrivalTime
  22. inbound.PlannedTrackmiles = inbound.ArrivalCandidates[rwy.Name].Trackmiles
  23. rwyManager.RunwayInbounds[rwy.Name] = inbound
  24. def calculateInitialCosts(rwyManager : RunwayManager, inbounds, earliestArrivalTime : datetime):
  25. overallDelay = timedelta(seconds = 0)
  26. # assume that the inbounds are sorted in FCFS order
  27. for inbound in inbounds:
  28. Colony.associateInbound(rwyManager, inbound, earliestArrivalTime, False)
  29. overallDelay += inbound.PlannedArrivalTime - inbound.InitialArrivalTime
  30. return overallDelay
  31. def __init__(self, configuration : Configuration):
  32. self.Configuration = configuration
  33. self.ResultDelay = None
  34. self.Result = None
  35. rwyManager = RunwayManager(self.Configuration)
  36. delay = Colony.calculateInitialCosts(rwyManager, self.Configuration.Inbounds, self.Configuration.EarliestArrivalTime)
  37. self.FcfsDelay = delay
  38. # run the optimization in every cycle to ensure optimal spacings based on TTG
  39. if 0.0 >= delay.total_seconds():
  40. delay = timedelta(seconds = 1.0)
  41. # initial value for the optimization
  42. self.Configuration.ThetaZero = 1.0 / (len(self.Configuration.Inbounds) * (delay.total_seconds() / 60.0))
  43. self.PheromoneMatrix = np.ones(( len(self.Configuration.Inbounds), len(self.Configuration.Inbounds) ), dtype=float) * self.Configuration.ThetaZero
  44. def optimize(self):
  45. # FCFS is the best solution
  46. if None != self.Result:
  47. return
  48. # define the tracking variables
  49. bestSequence = None
  50. # run the optimization loops
  51. for _ in range(0, self.Configuration.ExplorationRuns):
  52. # select the first inbound
  53. index = random.randint(1, len(self.Configuration.Inbounds)) - 1
  54. candidates = []
  55. for _ in range(0, self.Configuration.AntCount):
  56. # let the ant find a solution
  57. ant = Ant(self.PheromoneMatrix, self.Configuration)
  58. ant.findSolution(index)
  59. # fallback to check if findSolution was successful
  60. if None == ant.SequenceDelay or None == ant.Sequence or None == ant.SequenceScore:
  61. sys.stderr.write('Invalid ANT run detected!')
  62. sys.exit(-1)
  63. candidates.append(
  64. [
  65. ant.SequenceDelay,
  66. ant.Sequence,
  67. ant.SequenceScore,
  68. ant.SequenceDelay.total_seconds() / ant.SequenceScore
  69. ]
  70. )
  71. # find the best solution in all candidates of this generation
  72. bestCandidate = None
  73. for candidate in candidates:
  74. if None == bestCandidate or candidate[3] < bestCandidate[3]:
  75. bestCandidate = candidate
  76. dTheta = 1.0 / ((candidate[0].total_seconds() / 60.0) or 1.0)
  77. for i in range(1, len(candidate[1])):
  78. update = (1.0 - self.Configuration.Epsilon) * self.PheromoneMatrix[candidate[1][i - 1], candidate[1][i]] + dTheta
  79. self.PheromoneMatrix[candidate[1][i - 1], candidate[1][i]] = max(update, self.Configuration.ThetaZero)
  80. # check if we find a new best candidate
  81. if None != bestCandidate:
  82. if None == bestSequence or bestCandidate[0] < bestSequence[0]:
  83. bestSequence = bestCandidate
  84. # create the final sequence
  85. if None != bestSequence:
  86. # create the resulting sequence
  87. self.ResultDelay = bestSequence[0]
  88. self.Result = []
  89. # finalize the sequence
  90. rwyManager = RunwayManager(self.Configuration)
  91. for i in range(0, len(bestSequence[1])):
  92. self.Result.append(self.Configuration.Inbounds[bestSequence[1][i]])
  93. Colony.associateInbound(rwyManager, self.Result[-1], self.Configuration.EarliestArrivalTime, True)