RecedingHorizonControl.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 insertInWindow(self, inbound : Inbound, usePTA : bool):
  17. if False == usePTA:
  18. referenceTime = inbound.InitialArrivalTime
  19. else:
  20. referenceTime = inbound.PlannedArrivalTime
  21. inserted = False
  22. for i in range(0, len(self.Windows)):
  23. window = self.Windows[i]
  24. # find the correct window
  25. if window.StartTime <= referenceTime and window.EndTime > referenceTime:
  26. self.AssignedWindow[inbound.Callsign] = [ i, 0 ]
  27. inbound.FixedSequence = i < self.FreezedIndex
  28. if True == inbound.FixedSequence and None == inbound.PlannedArrivalTime:
  29. inbound.PlannedArrivalTime = inbound.InitialArrivalTime
  30. window.insert(inbound)
  31. inserted = True
  32. break
  33. # create the new window
  34. if False == inserted:
  35. if 0 != len(self.Windows):
  36. lastWindowTime = self.Windows[-1].EndTime
  37. else:
  38. lastWindowTime = dt.utcfromtimestamp(int(time.time())).replace(tzinfo = pytz.UTC)
  39. timestep = timedelta(seconds = self.Configuration.WindowSize)
  40. while True:
  41. self.Windows.append(RecedingHorizonWindow(lastWindowTime, lastWindowTime + timestep))
  42. if self.Windows[-1].EndTime > referenceTime:
  43. window = self.Windows[-1]
  44. window.insert(inbound)
  45. self.AssignedWindow[inbound.Callsign] = [ len(self.Windows) - 1, 0 ]
  46. inbound.FixedSequence = len(self.Windows) < self.FreezedIndex
  47. if True == inbound.FixedSequence and None == inbound.PlannedArrivalTime:
  48. inbound.PlannedArrivalTime = inbound.InitialArrivalTime
  49. break
  50. lastWindowTime = self.Windows[-1].EndTime
  51. window.Inbounds.sort(key = lambda x: x.PlannedArrivalTime if None != x.PlannedArrivalTime else x.InitialArrivalTime)
  52. def updateReport(self, inbound : Inbound):
  53. # check if we need to update
  54. if inbound.Callsign in self.AssignedWindow:
  55. index = self.AssignedWindow[inbound.Callsign][0]
  56. self.AssignedWindow[inbound.Callsign][1] = 0
  57. plannedInbound = self.Windows[index].inbound(inbound.Callsign)
  58. plannedInbound.Report = inbound.Report
  59. plannedInbound.ReportTime = inbound.ReportTime
  60. plannedInbound.CurrentPosition = inbound.CurrentPosition
  61. # ingore fixed updates
  62. if True == plannedInbound.FixedSequence or index <= self.FreezedIndex:
  63. plannedInbound.FixedSequence = True
  64. return
  65. plannedInbound.WTC = inbound.WTC
  66. # check if we need to update the inbound
  67. if None == plannedInbound.PlannedStar:
  68. reference = inbound.InitialArrivalTime
  69. if plannedInbound.InitialArrivalTime > reference:
  70. reference = plannedInbound.InitialArrivalTime
  71. if reference < self.Windows[index].StartTime or reference >= self.Windows[index].EndTime:
  72. self.Windows[index].remove(inbound.Callsign)
  73. self.AssignedWindow.pop(inbound.Callsign)
  74. inbound.InitialArrivalTime = reference
  75. self.updateReport(inbound)
  76. else:
  77. plannedInbound.InitialArrivalTime = reference
  78. self.Windows[index].Inbounds.sort(key = lambda x: x.PlannedArrivalTime if None != x.PlannedArrivalTime else x.InitialArrivalTime)
  79. else:
  80. self.insertInWindow(inbound, False)
  81. def resequenceInbound(self, inbound : Inbound):
  82. index = self.AssignedWindow[inbound.Callsign][0]
  83. if inbound.PlannedArrivalTime < self.Windows[index].StartTime or inbound.PlannedArrivalTime >= self.Windows[index].EndTime:
  84. self.Windows[index].remove(inbound.Callsign)
  85. self.AssignedWindow.pop(inbound.Callsign)
  86. self.insertInWindow(inbound, True)
  87. else:
  88. inbound.FixedSequence = index < self.FreezedIndex
  89. self.Windows[index].Inbounds.sort(key = lambda x: x.PlannedArrivalTime if None != x.PlannedArrivalTime else x.InitialArrivalTime)
  90. def lastFixedInboundOnRunway(self, runway : str):
  91. # no inbounds available
  92. if 0 == len(self.Windows):
  93. return None
  94. # search from the back to the front to find the last inbound
  95. for i in range(min(self.FreezedIndex, len(self.Windows)), -1, -1):
  96. for inbound in self.Windows[i].Inbounds:
  97. if runway == inbound.PlannedRunway.Name:
  98. return inbound
  99. # no inbound found
  100. return None
  101. def optimizationRelevantInbounds(self):
  102. # no new inbounds
  103. if len(self.Windows) <= self.FreezedIndex:
  104. return None, None
  105. inbounds = []
  106. earliestArrivalTime = self.Windows[self.FreezedIndex + 1].StartTime
  107. # check the overlapping windows
  108. for i in range(self.FreezedIndex + 1, len(self.Windows)):
  109. for inbound in self.Windows[i].Inbounds:
  110. inbounds.append(inbound)
  111. if 20 <= len(inbounds):
  112. break
  113. # check if we found relevant inbounds
  114. if 0 != len(inbounds):
  115. inbounds.sort(key = lambda x: x.PlannedArrivalTime if None != x.PlannedArrivalTime else x.InitialArrivalTime)
  116. return inbounds, earliestArrivalTime
  117. else:
  118. return None, None
  119. def sequence(self):
  120. inbounds = []
  121. for i in range(0, len(self.Windows)):
  122. for inbound in self.Windows[i].Inbounds:
  123. inbounds.append(inbound)
  124. return inbounds
  125. def cleanupWindows(self):
  126. currentUtc = dt.utcfromtimestamp(int(time.time())).replace(tzinfo = pytz.UTC)
  127. offsetCorrection = 0
  128. # delete the non-required windows
  129. while 0 != len(self.Windows) and currentUtc > self.Windows[0].EndTime:
  130. # cleanup the association table
  131. for inbound in self.Windows[0].Inbounds:
  132. self.AssignedWindow.pop(inbound.Callsign)
  133. offsetCorrection += 1
  134. self.Windows.pop(0)
  135. # correct the association table
  136. if 0 != offsetCorrection:
  137. for callsign in self.AssignedWindow:
  138. self.AssignedWindow[callsign][0] -= offsetCorrection
  139. if self.AssignedWindow[callsign][0] <= self.FreezedIndex:
  140. self.Windows[self.AssignedWindow[callsign][0]].inbound(callsign).FixedSequence = True
  141. # delete the non-updated aircrafts and increase the missed-counter for later runs
  142. callsigns = []
  143. for callsign in self.AssignedWindow:
  144. if 2 < self.AssignedWindow[callsign][1]:
  145. self.Windows[self.AssignedWindow[callsign][0]].remove(callsign)
  146. callsigns.append(callsign)
  147. self.AssignedWindow[callsign][1] += 1
  148. for callsign in callsigns:
  149. self.AssignedWindow.pop(callsign)