def incircle( lat, lon): # Check whether position is inside current circle definition if not swcircle: return True else: dist = latlondist(ctrlat, ctrlon, lat, lon) / nm return dist <= radius
def dist_goal(traf, _id): _id = traf.id2idx(_id) olat = traf.lat[_id] olon = traf.lon[_id] ilat, ilon = traf.ap.route[_id].wplat[0], traf.ap.route[_id].wplon[0] dist = geo.latlondist(olat, olon, ilat, ilon) / geo.nm return dist
def normalize(self, value, what, context=None, state=None, id_=None): if what == 'spd': if value > self.tas_max: self.tas_max = value if value < self.tas_min: self.tas_min = value return (value - self.tas_min) / (self.tas_max - self.tas_min) if what == 'rt': return value / (self.num_routes - 1) if what == 'state': d_goal = self.dist_goal[id_] / self.max_d spd = self.normalize(value[2], 'spd') rt = self.normalize(value[3], 'rt') acc = value[4] + 0.5 norm_array = np.array([d_goal, spd, rt, acc, 3 / self.max_d]) return norm_array if what == 'context': rt_own = int(state[3]) d_goal = self.dist_goal[id_] / self.max_d spd = self.normalize(context[2], 'spd') rt = self.normalize(context[3], 'rt') acc = context[4] + 0.5 rt_int = int(context[3]) if rt_own == rt_int: dist_away = abs(value[0] - d_goal) dist_own_intersection = 0 dist_int_intersection = 0 # else: dist_own_intersection = abs( self.intersection_distances[rt_own][rt_int] / self.max_d - value[0]) dist_int_intersection = abs( self.intersection_distances[rt_int][rt_own] / self.max_d - d_goal) d = geo.latlondist(state[0], state[1], context[0], context[1]) / geo.nm dist_away = d / self.max_d context_arr = np.array([ d_goal, spd, rt, acc, dist_away, dist_own_intersection, dist_int_intersection ]) return context_arr.reshape([1, 1, 7])
def dist_goal(_id): global ac_routes global positions _id = traf.id2idx(_id) olat = traf.lat[_id] olon = traf.lon[_id] ilat, ilon = traf.ap.route[_id].wplat[0], traf.ap.route[_id].wplon[0] dist = geo.latlondist(olat, olon, ilat, ilon) / geo.nm return dist
def normalize_context(self, cur_state, context, state, id_): own_route = int(state[3]) goal_distance = self.dist_goal[id_] / self.max_d alt = self.normalize_alt(state[2]) route = state[3] / (self.no_routes - 1) vs = state[4] route_int = int(context[3]) if own_route == route_int: dist_away = abs(state[0] - goal_distance) else: d = geo.latlondist(state[0],state[1],context[0],context[1])/geo.nm dist_away = d / self.max_d context_array = np.array([goal_distance, alt, route, vs, dist_away]) return context_array.reshape(1,1,5)
def onradar(self, lat, lon): """Return boolean (also numpy array) with on or off radar screen (range check)""" # Radar mode if not self.swnavdisp: # Normal case if self.lon1 > self.lon0: sw = (lat > self.lat0) * (lat < self.lat1) * \ (lon > self.lon0) * (lon < self.lon1) == 1 # Wrap around: else: sw = (lat > self.lat0) * (lat < self.lat1) * \ ((lon > self.lon0) + (lon < self.lon1)) == 1 # Else NAVDISP mode else: base = 30. * (self.lat1 - self.lat0) dist = geo.latlondist(self.ndlat, self.ndlon, lat, lon) / nm sw = dist < base return sw
def onradar(self, lat, lon): """Return boolean (also numpy array) with on or off radar screen (range check)""" # Radar mode if not self.swnavdisp: # Normal case if self.lon1 > self.lon0: sw = (lat > self.lat0) * (lat < self.lat1) * \ (lon > self.lon0) * (lon < self.lon1) == 1 # Wrap around: else: sw = (lat > self.lat0) * (lat < self.lat1) * \ ((lon > self.lon0) + (lon < self.lon1)) == 1 # Else NAVDISP mode else: base = 30. * (self.lat1 - self.lat0) dist = geo.latlondist(self.ndlat, self.ndlon, lat, lon) / nm sw = dist < base return sw
def get_dist(pos1, pos2): return latlondist(pos1[0], pos1[1], pos2[0], pos2[1]) / nm
def update(self,gain): # Time step update of source # Get time step dt = sim.simt - self.tupdate self.tupdate = sim.simt # Time for a new aircraft? if dt>0.0: # Calculate probability of a geberate occurring with flow chances = 1.0-gain*self.flow*dt/3600. #flow is in a/c per hour=360 seconds if random.random() >= chances: # Runways defined? => use runway lines (queues) if len(self.runways)>0: # We do not yet need to create an aircraft gennow = False # Find shortest line and put it in isel = random.randint(0,len(self.runways)-1) self.rwyline[isel] = self.rwyline[isel] + 1 else: # Yes we do need to generate one now gennow = True else: gennow = False # Check for generating aircraft # First check runways for a/c already in line: txt = "" for i in range(len(self.runways)): # Runway vacated and there is one waiting? if sim.simt-self.rwytotime[i]>self.dtakeoff and self.rwyline[i]>0: #if self.name == "EHAM": # print(sim.simt, self.runways[i], self.rwytotime[i]) self.rwytotime[i] = sim.simt self.rwyline[i] = self.rwyline[i]-1 # Choose and aicraft type, check for distance if len(self.dest)>0: idest = int(random.random() * len(self.dest)) else: idest = -1 if idest>=0: acid = randacname(self.name,self.dest[idest]) if self.desttype[idest]=="seg" or self.dest[idest][:4]=="SEGM": lat,lon,hdg = getseg(self.dest[idest]) else: success,posobj = txt2pos(self.dest[idest],ctrlat,ctrlon) lat,lon = posobj.lat,posobj.lon distroute = latlondist(self.lat,self.lon,lat,lon)/nm else: acid = randacname(self.name, self.name) if self.destactypes[idest] == []: actype = random.choice(self.actypes) actype = checkactype(actype, distroute, self.actypes) else: actype = random.choice(self.destactypes[idest]) stack.stack("CRE "+",".join([acid, actype, str(self.rwylat[i]),str(self.rwylon[i]),str(self.rwyhdg[i]), "0.0","0.0"])) #wplat,wplon = kwikpos(self.rwylat[i],self.rwylon[i],self.rwyhdg[i],5.0*nm) #stack.stack(acid + " ADDWPT ",wplat," ",wplon) #stack.stack(acid+"LNAV ON") if idest>=0: if self.dest[idest][:4] != "SEGM": stack.stack(acid + " DEST " + self.dest[idest]) else: stack.stack(acid + " DEST " + str(self.destlat[idest]) + " " + str(self.destlon[idest])) if self.name[:4] != "SEGM": stack.stack(acid + " ORIG " + self.name) else: stack.stack(acid + " ORIG " + str(self.lat) + " " + str(self.lon)) stack.stack(acid + " SPD 250") stack.stack(acid + " ALT FL100") stack.stack(acid + " HDG " + str(self.rwyhdg[i])) stack.stack(acid+" LNAV OFF") # Not runway, then define instantly at position with random heading or in case of segment inward heading if gennow: if not self.incircle: lat,lon = kwikpos(ctrlat,ctrlon,self.segdir,radius) hdg = self.segdir-180 elif self.type=="seg": lat,lon,brg = getseg(self.name) hdg = (brg+180)%360 elif self.type=="rwy": lat,lon = self.lat,self.lon hdg = self.hdg # Runway heading else: hdg = random.random()*360. if self.startaltmin and self.startaltmax: alt = random.randint(int(self.startaltmin), int(self.startaltmax)) else: alt = random.randint(200, 300) * 100 * ft if self.startspdmin and self.startspdmax: spd = random.randint(int(self.startspdmin), int(self.startspdmax)) else: spd = random.randint(250, 350) alttxt, spdtxt = "FL" + str(int(round(alt / (100 * ft)))), str(spd) # Add destination if len(self.dest)>0: idest = int(random.random() * len(self.dest)) acid = randacname(self.name,self.dest[idest]) else: acid = randacname(self.name,self.name) idest = -1 stack.stack("CRE " + ",".join([acid, random.choice(self.actypes), str(self.lat), str(self.lon), str(int(hdg%360)), alttxt,spdtxt])) if idest>=0: if self.dest[idest][:4] != "SEGM": stack.stack(acid + " DEST " + self.dest[idest]) else: stack.stack(acid + " DEST " + str(self.destlat[idest])+" "+str(self.destlon[idest])) if self.name[:4] != "SEGM": stack.stack(acid + " ORIG " + self.name) else: stack.stack(acid + " ORIG " + str(self.lat)+" "+str(self.lon)) if alttxt=="0" and spdtxt =="0": stack.stack(acid+" SPD 250") stack.stack(acid+" ALT FL100") else: if idest>=0: if self.desttype[idest] == "seg": lat, lon, hdg = getseg(self.dest[idest]) brg, dist = kwikqdrdist(self.lat, self.lon, lat, lon) stack.stack(acid + " HDG " + str(brg)) else: stack.stack(acid + " LNAV ON")
def dist_goal(states, trafficfic, i): o_lat, o_lon = states i_lat, i_lon = trafficfic.ap.route[i].wplat[0], trafficfic.ap.route[ i].wplon[0] dist = geo.latlondist(o_lat, o_lon, i_lat, i_lon) / geo.nm return dist
def get_dist(self, a, b): a_port = self.departure[a] b_port = self.arrival[b] return latlondist(a_port["lat"], a_port["lon"], b_port["lat"], b_port["lon"]) / nm
def incircle(lat,lon): # Check whether position is inside current circle definition if not swcircle: return True else: dist = latlondist(ctrlat,ctrlon,lat,lon)/nm return dist<=radius
def update(self,gain): # Time step update of source # Get time step dt = sim.simt - self.tupdate self.tupdate = sim.simt # Time for a new aircraft? if dt>0.0: # Calculate probability of a geberate occurring with flow chances = 1.0-gain*self.flow*dt/3600. #flow is in a/c per hour=360 seconds if random.random() >= chances: # Runways defined? => use runway lines (queues) if len(self.runways)>0: # We do not yet need to create an aircraft gennow = False # Find shortest line and put it in isel = random.randint(0,len(self.runways)-1) self.rwyline[isel] = self.rwyline[isel] + 1 else: # Yes we do need to generate one now gennow = True else: gennow = False # Check for generating aircraft # First check runways for a/c already in line: txt = "" for i in range(len(self.runways)): # Runway vacated and there is one waiting? if sim.simt-self.rwytotime[i]>self.dtakeoff and self.rwyline[i]>0: #if self.name == "EHAM": # print(sim.simt, self.runways[i], self.rwytotime[i]) self.rwytotime[i] = sim.simt self.rwyline[i] = self.rwyline[i]-1 # Choose and aicraft type, check for distance if len(self.dest)>0: idest = int(random.random() * len(self.dest)) else: idest = -1 if idest>=0: acid = randacname(self.name,self.dest[idest]) if self.desttype[idest]=="seg" or self.dest[idest][:4]=="SEGM": lat,lon,hdg = getseg(self.dest[idest]) else: success,posobj = txt2pos(self.dest[idest],ctrlat,ctrlon) lat,lon = posobj.lat,posobj.lon distroute = latlondist(self.lat,self.lon,lat,lon)/nm else: acid = randacname(self.name, self.name) if self.destactypes[idest] == []: actype = random.choice(self.actypes) actype = checkactype(actype, distroute, self.actypes) else: actype = random.choice(self.destactypes[idest]) stack.stack("CRE "+",".join([acid, actype, str(self.rwylat[i]),str(self.rwylon[i]),str(self.rwyhdg[i]), "0.0","0.0"])) stack.stack(acid + " SPD 250") stack.stack(acid + " ALT FL100") stack.stack(acid + " HDG " + str(self.rwyhdg[i])) # TBD: Add waypoint for after take-off? if idest>=0: if self.dest[idest][:4] != "SEGM": stack.stack(acid + " DEST " + self.dest[idest]) else: stack.stack(acid + " DEST " + str(self.destlat[idest]) + " " + str(self.destlon[idest])) if self.name[:4] != "SEGM": stack.stack(acid + " ORIG " + self.name) else: stack.stack(acid + " ORIG " + str(self.lat) + " " + str(self.lon)) if idest>=0 and self.desttype[idest]=="seg": lat,lon,hdg = getseg(self.dest[idest]) brg,dist = kwikqdrdist(self.lat,self.lon,lat,lon) #stack.stack(acid+" HDG "+str(brg)) else: stack.stack(acid+" LNAV OFF") #stack.stack(acid+" VNAV ON") # Not runway, then define instantly at position with random heading or in case of segment inward heading if gennow: if not self.incircle: lat,lon = kwikpos(ctrlat,ctrlon,self.segdir,radius) hdg = self.segdir-180 elif self.type=="seg": lat,lon,brg = getseg(self.name) hdg = (brg+180)%360 elif self.type=="rwy": lat,lon = self.lat,self.lon hdg = self.hdg # Runway heading else: hdg = random.random()*360. if (self.type=="apt" or self.type=="rwy") and self.incircle: alttxt,spdtxt = str(0),str(0) else: alttxt,spdtxt = "FL"+str(random.randint(200,300)), str(random.randint(250,350)) # Add destination if len(self.dest)>0: idest = int(random.random() * len(self.dest)) acid = randacname(self.name,self.dest[idest]) else: acid = randacname(self.name,self.name) idest = -1 stack.stack("CRE " + ",".join([acid, random.choice(self.actypes), str(self.lat), str(self.lon), str(int(hdg%360)), alttxt,spdtxt])) if idest>=0: if self.dest[idest][:4] != "SEGM": stack.stack(acid + " DEST " + self.dest[idest]) else: stack.stack(acid + " DEST " + str(self.destlat[idest])+" "+str(self.destlon[idest])) if self.name[:4] != "SEGM": stack.stack(acid + " ORIG " + self.name) else: stack.stack(acid + " ORIG " + str(self.lat)+" "+str(self.lon)) if alttxt=="0" and spdtxt =="0": stack.stack(acid+" SPD 250") stack.stack(acid+" ALT FL100") else: if idest>=0: if self.desttype[idest] == "seg": lat, lon, hdg = getseg(self.dest[idest]) brg, dist = kwikdist(self.lat, self.lon, lat, lon) stack.stack(acid + " HDG " + str(brg)) else: stack.stack(acid + " LNAV ON")