Worker.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. #!/usr/bin/env python
  2. from threading import Thread, Lock
  3. import sys
  4. import time
  5. from aman.com import Weather
  6. from aman.com.Euroscope import Euroscope
  7. from aman.config.Airport import Airport
  8. from aman.config.AirportSequencing import AirportSequencing
  9. from aman.sys.aco.Colony import Colony
  10. from aman.sys.aco.Configuration import Configuration
  11. from aman.sys.aco.Node import Node
  12. from aman.sys.WeatherModel import WeatherModel
  13. from aman.sys.RecedingHorizonControl import RecedingHorizonControl
  14. from aman.types.Inbound import Inbound
  15. from aman.types.PerformanceData import PerformanceData
  16. class Worker(Thread):
  17. def __init__(self, icao : str, configuration : Airport, weather : Weather,
  18. performance : PerformanceData, euroscope : Euroscope):
  19. Thread.__init__(self)
  20. self.StopThread = None
  21. self.Icao = icao
  22. self.Configuration = configuration
  23. self.SequencingConfiguration = configuration.DefaultSequencing
  24. self.PerformanceData = performance
  25. self.UpdateLock = Lock()
  26. self.ReportQueue = {}
  27. self.WeatherModel = WeatherModel(configuration.GaforId, weather)
  28. self.RecedingHorizonControl = RecedingHorizonControl(configuration.RecedingHorizonControl)
  29. self.Euroscope = euroscope
  30. # merge the constraint information with the GNG information
  31. for runway in self.Configuration.GngData.ArrivalRoutes:
  32. for star in self.Configuration.GngData.ArrivalRoutes[runway]:
  33. for name in self.Configuration.ArrivalRouteConstraints:
  34. if name == star.Name:
  35. for constraint in self.Configuration.ArrivalRouteConstraints[name]:
  36. foundWaypoint = False
  37. for waypoint in star.Route:
  38. if constraint.Name == waypoint.Name:
  39. waypoint.Altitude = constraint.Altitude
  40. waypoint.Speed = constraint.Speed
  41. waypoint.BaseTurn = constraint.BaseTurn
  42. waypoint.FinalTurn = constraint.FinalTurn
  43. foundWaypoint = True
  44. break
  45. if False == foundWaypoint:
  46. sys.stderr.write('Unable to find ' + constraint.Name + ' in ' + name)
  47. sys.exit(-1)
  48. break
  49. self.setDaemon(True)
  50. self.start()
  51. def acquireLock(self):
  52. if None != self.UpdateLock:
  53. self.UpdateLock.acquire()
  54. def releaseLock(self):
  55. if None != self.UpdateLock:
  56. self.UpdateLock.release()
  57. def run(self):
  58. counter = 0
  59. while None == self.StopThread:
  60. time.sleep(1)
  61. counter += 1
  62. if 0 != (counter % 10):
  63. continue
  64. self.acquireLock()
  65. # perform some book-keeping
  66. self.RecedingHorizonControl.cleanupWindows()
  67. # update the aircraft information in RHC
  68. for callsign in self.ReportQueue:
  69. report = self.ReportQueue[callsign]
  70. if 0 != report.distanceToIAF and '' != report.initialApproachFix:
  71. inbound = Inbound(report, self.PerformanceData)
  72. Node(inbound, inbound.ReportTime, self.WeatherModel, self.Configuration.GngData, self.SequencingConfiguration)
  73. if None != inbound.InitialArrivalTime:
  74. self.RecedingHorizonControl.updateReport(inbound)
  75. else:
  76. print('Unable to find all data of ' + report.aircraft.callsign)
  77. self.ReportQueue.clear()
  78. # search the ACO relevant aircrafts
  79. relevantInbounds, earliestArrivalTime = self.RecedingHorizonControl.optimizationRelevantInbounds()
  80. if None != relevantInbounds:
  81. start = time.process_time()
  82. # get the last landing aircrafts per runway before the RHC stage to check for constraints
  83. # this is required to handle the overlap between windows
  84. preceedingInbounds = {}
  85. for runway in self.SequencingConfiguration.ActiveArrivalRunways:
  86. inbound = self.RecedingHorizonControl.lastFixedInboundOnRunway(runway.Runway.Name)
  87. if None != inbound:
  88. preceedingInbounds[runway.Runway.Name] = inbound
  89. # configure the ACO run
  90. acoConfig = Configuration(constraints = self.SequencingConfiguration, nav = self.Configuration.GngData,
  91. earliest = earliestArrivalTime, weather = self.WeatherModel,
  92. preceeding = None if 0 == len(preceedingInbounds) else preceedingInbounds,
  93. ants = 5 * len(relevantInbounds), generations = 5 * len(relevantInbounds))
  94. # perform the ACO run
  95. aco = Colony(relevantInbounds, acoConfig)
  96. aco.optimize()
  97. if None != aco.Result:
  98. for inbound in aco.Result:
  99. self.RecedingHorizonControl.resequenceInbound(inbound)
  100. print('Delays: FCFS=' + str(aco.FcfsDelay.total_seconds()) + ', ACO=' + str(aco.ResultDelay.total_seconds()))
  101. # measure the exuction time of the overall optimization process
  102. print('Execution time: ' + str(time.process_time() - start) + ' seconds')
  103. else:
  104. print('No relevant inbounds found for the optimization in ' + self.Icao)
  105. # send the sequence to the GUI and Euroscope
  106. sequence = self.RecedingHorizonControl.sequence()
  107. self.Euroscope.sendSequence(self.Icao, sequence, self.WeatherModel)
  108. self.releaseLock()
  109. def inboundSequence(self):
  110. self.acquireLock()
  111. sequence = self.RecedingHorizonControl.sequence()
  112. self.releaseLock()
  113. return sequence
  114. def configure(self, configuration : AirportSequencing):
  115. self.acquireLock()
  116. self.SequencingConfiguration = configuration
  117. self.releaseLock()