RecedingHorizonControl.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/usr/bin/env python
  2. import time
  3. from datetime import datetime as dt
  4. from datetime import timedelta
  5. import pytz
  6. from aman.config.RHC import RHC
  7. from aman.sys.RecedingHorizonWindow import RecedingHorizonWindow
  8. from aman.types.Inbound import Inbound
  9. class RecedingHorizonControl:
  10. def __init__(self, config : RHC):
  11. self.Windows = []
  12. # contains the current index and the missed update counter
  13. self.AssignedWindow = {}
  14. self.Configuration = config
  15. self.FreezedIndex = int(self.Configuration.FixedBeforeArrival.seconds / self.Configuration.WindowSize)
  16. def update(self, inbound : Inbound):
  17. # check if we need to update
  18. if inbound.Report.aircraft.callsign in self.AssignedWindow:
  19. index = self.AssignedWindow[inbound.Report.aircraft.callsign][0]
  20. self.AssignedWindow[inbound.Report.aircraft.callsign][1] = 0
  21. # check if we assume the scheduling as fixed
  22. if index < self.FreezedIndex:
  23. return
  24. plannedInbound = self.Windows[index].inbound(inbound.Report.aircraft.callsign)
  25. plannedInbound.CurrentPosition = inbound.CurrentPosition
  26. plannedInbound.ArrivalCandidates = inbound.ArrivalCandidates
  27. # check if we need to update the inbound
  28. if plannedInbound.PlannedArrivalTime < inbound.EarliestArrivalTime:
  29. if inbound.EarliestArrivalTime < self.Windows[index].StartTime or inbound.EarliestArrivalTime >= self.Windows[index].EndTime:
  30. self.Windows[index].remove(inbound.Report.aircraft.callsign)
  31. self.AssignedWindow.pop(inbound.Report.aircraft.callsign)
  32. self.update(inbound)
  33. else:
  34. plannedInbound.PlannedStar = inbound.PlannedStar
  35. plannedInbound.PlannedRunway = inbound.PlannedRunway
  36. plannedInbound.InitialArrivalTime = inbound.InitialArrivalTime
  37. plannedInbound.EarliestArrivalTime = inbound.EarliestArrivalTime
  38. plannedInbound.PlannedArrivalTime = inbound.EarliestArrivalTime
  39. else:
  40. inserted = False
  41. for i in range(0, len(self.Windows)):
  42. window = self.Windows[i]
  43. # find the correct window
  44. if window.StartTime <= inbound.EarliestArrivalTime and window.EndTime > inbound.EarliestArrivalTime:
  45. if i > self.FreezedIndex:
  46. self.AssignedWindow[inbound.Report.aircraft.callsign] = [ i, 0 ]
  47. inbound.PlannedArrivalTime = inbound.EarliestArrivalTime
  48. window.insert(inbound)
  49. inserted = True
  50. break
  51. # create the new window
  52. if False == inserted:
  53. if 0 != len(self.Windows):
  54. lastWindowTime = self.Windows[-1].EndTime
  55. else:
  56. lastWindowTime = dt.utcfromtimestamp(int(time.time())).replace(tzinfo = pytz.UTC)
  57. timestep = timedelta(seconds = self.Configuration.WindowSize)
  58. while True:
  59. self.Windows.append(RecedingHorizonWindow(lastWindowTime, lastWindowTime + timestep))
  60. if self.Windows[-1].EndTime > inbound.EarliestArrivalTime:
  61. self.AssignedWindow[inbound.Report.aircraft.callsign] = [ len(self.Windows) - 1, 0 ]
  62. self.Windows[-1].insert(inbound)
  63. inbound.PlannedArrivalTime = max(self.Windows[-1].StartTime, inbound.EarliestArrivalTime)
  64. self.Windows[-1].Inbounds.sort(key = lambda x: x.PlannedArrivalTime)
  65. break
  66. lastWindowTime = self.Windows[-1].EndTime
  67. def lastFixedInboundOnRunway(self, runway : str):
  68. # no inbounds available
  69. if 0 == len(self.Windows):
  70. return None
  71. # search from the back to the front to find the last inbound
  72. for i in range(min(self.FreezedIndex, len(self.Windows)), -1, -1):
  73. for inbound in self.Windows[i].Inbounds:
  74. if runway == inbound.PlannedRunway.Runway.name:
  75. return inbound
  76. # no inbound found
  77. return None
  78. def optimizationRelevantInbounds(self):
  79. # no new inbounds
  80. if len(self.Windows) <= self.FreezedIndex:
  81. return None, None
  82. inbounds = []
  83. earliestArrivalTime = None
  84. # check the overlapping windows
  85. #for i in range(self.FreezedIndex + 1, min(len(self.Windows), self.FreezedIndex + 1 + self.Configuration.WindowOverlap)):
  86. for i in range(0, len(self.Windows)):
  87. if None == earliestArrivalTime:
  88. earliestArrivalTime = self.Windows[i].StartTime
  89. for inbound in self.Windows[i].Inbounds:
  90. inbounds.append(inbound)
  91. # check if we found relevant inbounds
  92. if 0 != len(inbounds):
  93. inbounds.sort(key = lambda x: x.InitialArrivalTime)
  94. return inbounds, earliestArrivalTime
  95. else:
  96. return None, None
  97. def cleanupWindows(self):
  98. currentUtc = dt.utcfromtimestamp(int(time.time())).replace(tzinfo = pytz.UTC)
  99. offsetCorrection = 0
  100. # delete the non-required windows
  101. while 0 != len(self.Windows) and currentUtc > self.Windows[0].EndTime:
  102. # cleanup the association table
  103. for inbound in self.Windows[0].Inbounds:
  104. self.AssignedWindow.pop(inbound.Report.aircraft.callsign)
  105. offsetCorrection += 1
  106. self.Windows.pop(0)
  107. # correct the association table
  108. if 0 != offsetCorrection:
  109. for callsign in self.AssignedWindow:
  110. self.AssignedWindow[callsign][0] -= offsetCorrection
  111. # delete the non-updated aircrafts and increase the missed-counter for later runs
  112. callsigns = []
  113. for callsign in self.AssignedWindow:
  114. if 2 < self.AssignedWindow[callsign][1]:
  115. self.Windows[self.AssignedWindow[callsign][0]].remove(callsign)
  116. callsigns.append(callsign)
  117. self.AssignedWindow[callsign][1] += 1
  118. for callsign in callsigns:
  119. self.AssignedWindow.pop(callsign)