Example #1
0
    def log(self):
        apple = len(traf.id)
        limit = 5
        # datalogger.update()

        if apple > 0:
            # print('Fuelflows are: {} at {}'.format(traf.perf.fuelflow, str(sim.utc.strftime("%H:%M:%S"))))
            self.counter += np.ones(apple) * np.logical_and(
                traf.alt < 1, True)  # _and , traf.M<0.007

            # datalogger.save2()
            if np.size((np.nonzero(self.counter > limit))) > 0:
                delcounter = np.delete(traf.id2idx(traf.id),
                                       np.nonzero(self.counter < limit))
                if len(range(0, len(delcounter))) > 0:
                    for i in range(0, len(delcounter)):
                        delcounter[i] = int(delcounter[i])
                        self.deltime[delcounter[i]] = str(
                            sim.utc.strftime("%d-%b-%Y %H:%M:%S"))
                else:
                    delcounter[0] = int(delcounter[0])
                    self.deltime[0] = str(
                        sim.utc.strftime("%d-%b-%Y %H:%M:%S"))
                # Print some stuff
                self.fuelused = self.initmass - traf.perf.mass
                datalogger.talko(delcounter)

                # Save some stuff
                traf.resultstosave = datalogger.save(traf.resultstosave,
                                                     delcounter)
                traf.delete(delcounter)
        pass
Example #2
0
    def update(self):
        ''' Update flight efficiency metrics
            2D and 3D distance [m], and work done (force*distance) [J] '''
        if not self.active:
            return

        #self.data_collection_count += np.around(self.dt, decimals = 0) #internal timestamp in minutes
        self.data_collection_count += 5

        #resultantspd = np.sqrt(traf.gs * traf.gs + traf.vs * traf.vs)
        self.distance2D += self.dt * traf.gs / nm  #in nautical miles
        #self.distance3D += self.dt * resultantspd

        if traf.asas.inconf.any():
            in_conf = np.where(traf.asas.inconf == True)
            traf.timeinconf[in_conf] += self.dt

        if settings.performance_model == 'openap':
            self.work += (traf.perf.thrust * self.dt * traf.gs)
        else:
            self.work += (traf.perf.Thr * self.dt * traf.gs)

        # ToDo: Add autodelete for descending with swTaxi:
        if self.swtaxi:
            pass  # To be added!!!

        if len(traf.asas.confpairs) > 0 and traf.asas.priocode == "SRS1":
            self.count_relevancy += 1
            if self.count_relevancy == 5:
                #takes relevancy data each five iterations
                self.update_relevancy()
                self.count_relevancy = 0

        self.update_asasdata()
        # Find out which aircraft are currently inside the experiment area, and
        # determine which aircraft need to be deleted.
        inside = areafilter.checkInside(self.name, traf.lat, traf.lon,
                                        traf.alt)
        #gets all aircraft that have left the experiment area since the last update
        #delidx = np.intersect1d(np.where(np.array(self.inside)==True), np.where(np.array(inside)==False))
        delidx = np.where(np.array(inside) == False)
        #self.inside = inside

        # Log flight statistics when for deleted aircraft
        if delidx[0].any():
            self.logger.log(
                np.array(traf.id)[delidx], traf.cretime[delidx],
                sim.simt - traf.cretime[delidx], traf.timeinconf[delidx],
                self.distance2D[delidx], self.work[delidx], traf.alt[delidx],
                traf.tas[delidx])
            self.inconf = traf.asas.inconf
            #self.update_asasdata(deletion=True)
            # delete all aicraft in self.delidx
            for idx in delidx:
                #print("Aircraft %s was/were deleted" % (np.array(traf.id)[idx]))
                traf.delete(idx)
Example #3
0
    def update(self):
        ''' Update flight efficiency metrics
            2D and 3D distance [m], and work done (force*distance) [J] '''
        if not self.active:
            return

        resultantspd = np.sqrt(traf.gs * traf.gs + traf.vs * traf.vs)
        self.distance2D += self.dt * traf.gs
        self.distance3D += self.dt * resultantspd

        if settings.performance_model == 'nap':
            self.work += (traf.perf.thrust * self.dt * resultantspd)
        else:
            self.work += (traf.perf.Thr * self.dt * resultantspd)

        # ToDo: Add autodelete for descending with swTaxi:
        if self.swtaxi:
            pass  # To be added!!!

        # Find out which aircraft are currently inside the experiment area, and
        # determine which aircraft need to be deleted.
        inside = areafilter.checkInside(self.name, traf.lat, traf.lon,
                                        traf.alt)
        delidx = np.intersect1d(np.where(np.array(self.inside) == True),
                                np.where(np.array(inside) == False))
        self.inside = inside

        # Log flight statistics when for deleted aircraft
        if len(delidx) > 0:
            self.logger.log(
                np.array(traf.id)[delidx],
                self.create_time[delidx],
                sim.simt - self.create_time[delidx],
                self.distance2D[delidx],
                self.distance3D[delidx],
                self.work[delidx],
                traf.lat[delidx],
                traf.lon[delidx],
                traf.alt[delidx],
                traf.tas[delidx],
                traf.vs[delidx],
                traf.hdg[delidx],
                # traf.ap.origlat[delidx],
                # traf.ap.origlon[delidx],
                # traf.ap.destlat[delidx],
                # traf.ap.destlon[delidx],
                traf.asas.active[delidx],
                traf.pilot.alt[delidx],
                traf.pilot.tas[delidx],
                traf.pilot.vs[delidx],
                traf.pilot.hdg[delidx])

        # delete all aicraft in self.delidx
        for idx in delidx:
            traf.delete(idx)
Example #4
0
    def update(self):
        ''' Update flight efficiency metrics
            2D and 3D distance [m], and work done (force*distance) [J] '''
        if not self.active:
            return

        resultantspd = np.sqrt(traf.gs * traf.gs + traf.vs * traf.vs)
        self.distance2D += self.dt * traf.gs
        self.distance3D += self.dt * resultantspd

        if settings.performance_model == 'nap':
            self.work += (traf.perf.thrust * self.dt * resultantspd)
        else:
            self.work += (traf.perf.Thr * self.dt * resultantspd)

        # ToDo: Add autodelete for descending with swTaxi:
        if self.swtaxi:
            pass # To be added!!!

        # Find out which aircraft are currently inside the experiment area, and
        # determine which aircraft need to be deleted.
        inside = areafilter.checkInside(self.name, traf.lat, traf.lon, traf.alt)
        delidx = np.intersect1d(np.where(np.array(self.inside)==True), np.where(np.array(inside)==False))
        self.inside = inside

        # Log flight statistics when for deleted aircraft
        if len(delidx) > 0:
            self.logger.log(
                np.array(traf.id)[delidx],
                self.create_time[delidx],
                sim.simt - self.create_time[delidx],
                self.distance2D[delidx],
                self.distance3D[delidx],
                self.work[delidx],
                traf.lat[delidx],
                traf.lon[delidx],
                traf.alt[delidx],
                traf.tas[delidx],
                traf.vs[delidx],
                traf.hdg[delidx],
                # traf.ap.origlat[delidx],
                # traf.ap.origlon[delidx],
                # traf.ap.destlat[delidx],
                # traf.ap.destlon[delidx],
                traf.asas.active[delidx],
                traf.pilot.alt[delidx],
                traf.pilot.tas[delidx],
                traf.pilot.vs[delidx],
                traf.pilot.hdg[delidx]
            )

        # delete all aicraft in self.delidx
        for idx in delidx:
            traf.delete(idx)
Example #5
0
    def update(self):
        if self.active:
            ''' Update inside log for delarea '''

            # Determine which aircraft are currently inside the deletion area
            self.currinside = areafilter.checkInside(self.areaname, traf.lat,
                                                     traf.lon, traf.alt)

            # Check which aircraft have left the deletion area
            exits = np.logical_and(self.previnside,
                                   np.logical_not(self.currinside))

            # Delete aircraft which have left the deletion area
            if np.any(exits):
                traf.delete(np.arange(traf.ntraf)[exits])

            # Update previously stored insids
            self.previnside = np.array(self.currinside)
Example #6
0
def update():
    global first_time, done, delete_dict, done_count
    if first_time:
        done = dict.fromkeys(traf.id)
        delete_dict = dict.fromkeys(traf.id, False)
        first_time = False

    for id in traf.id:
        if id not in done:
            done[id] = False
        if id not in delete_dict:
            delete_dict[id] = False
    dest_lat_lon = [(52.6, 4.73), (52.3, 4.36), (52.33, 5.19), (51.52, 5.33),
                    (51.8, 5.06), (51.82, 5.75), (52.30, 6.0)]
    # dest_lat_lon[-dest]

    if done.keys():
        for agent_id in done.keys():
            # Initialize reward to 0.
            done[agent_id] = False
            # First check if goal area is reached
            idx = traf.id2idx(agent_id)
            dest = traf.dest_temp[idx]
            dest_lat, dest_lon = dest_lat_lon[-dest]
            dist = geo.kwikdist(traf.lat[idx], traf.lon[idx], dest_lat,
                                dest_lon)
            if dist <= 5:
                done[agent_id] = True
                done_count += 1

        for agent_id in done.keys():
            if agent_id in delete_dict:
                if done[agent_id] and not delete_dict[agent_id]:
                    traf.delete(traf.id2idx(agent_id))
                    print('Deleted ', agent_id)
                    delete_dict[agent_id] = True
    save_metrics()
    if done_count >= 125:
        sim.reset()

    return
Example #7
0
def update():
    global reward, idx_mc, reset_bool, connected, obs, prev_obs, done_count, final_obs, done, delete_dict
    if connected:
        # Bluesky first timestep starts with update step, so use the reset_bool to determine wheter a reset has occured or not. Then create environment agents.
        if reset_bool:
            #Randomize starting location depending on boundaries in settings.
            aclat = np.random.rand(settings.n_ac) * (settings.max_lat - settings.min_lat) + settings.min_lat
            aclon = np.random.rand(settings.n_ac) * (settings.max_lon - settings.min_lon) + settings.min_lon
            achdg = np.random.randint(1, 360, settings.n_ac)
            acalt = np.ones(settings.n_ac) * 7620
            # acspd = np.ones(settings.n_ac) * 300
            print('Created ', str(settings.n_ac), ' random aircraft, resetted!')
            traf.create(n=settings.n_ac, aclat=aclat, aclon=aclon, achdg=achdg, #360 * np.random.rand(1)
            acspd=150, acalt=acalt, actype='B777')#settings.acspd)
            reset_bool = False
            return
        # print('After action HDG: ' + str(traf.hdg[0]))
        obs = calc_state()
        reward, done = calc_reward()

        for agent_id in done.keys():
            if done[agent_id] and not delete_dict[agent_id]:
                traf.delete(traf.id2idx(agent_id))
                print('Deleted ', agent_id)
                done_count += 1
                final_obs[agent_id] = obs[agent_id]
                delete_dict[agent_id] = True

        idx_mc += 1
        client_mc.log_returns(eid, reward, done, info=[])
        if idx_mc == 500 or done_count == settings.n_ac:
            for agent_id in done.keys():
                if not done[agent_id]:
                    final_obs[agent_id] = obs[agent_id]

            print('total reward', reward)
            print('Done with Episode: ', eid)
            client_mc.end_episode(eid, final_obs)
            sim.reset()
Example #8
0
    def log(self):
        apple = len(traf.id)
        limit = 30
        # datalogger.update()

        if apple > 0:
            # print(traf.id)
            # print(self.counter)
            # print('apple is', apple)
            # print(traf.alt)
            # print(traf.M)
            # print(traf.perf.mass)
            # print(self.initmass)
            # print(np.logical_and(traf.alt<1, traf.M<0.002))
            self.counter += np.ones(apple) * np.logical_and(
                traf.alt < 1, traf.M < 0.007)

            if np.size((np.nonzero(self.counter > limit))) > 0:
                delcounter = np.delete(traf.id2idx(traf.id),
                                       np.nonzero(self.counter < limit))
                #print(delcounter)
                # print(self.deltime)

                if len(range(0, len(delcounter))) > 0:
                    for i in range(0, len(delcounter)):
                        delcounter[i] = int(delcounter[i])
                        self.deltime[delcounter[i]] = str(
                            sim.utc.strftime("%d-%b-%Y %H:%M:%S"))
                else:
                    delcounter[0] = int(delcounter[0])
                    self.deltime[0] = str(
                        sim.utc.strftime("%d-%b-%Y %H:%M:%S"))
                # print(delcounter)

                # print(traf.id)
                # print(traf.counter)
                # print(self.initmass)
                # print(self.inittime)
                # print(self.deltime)
                # print(self.fuelused)

                # Print some stuff
                self.fuelused = self.initmass - traf.perf.mass

                # print('after assignment')
                # print(self.deltime)
                datalogger.talko(delcounter)

                # Save some stuff
                try:
                    results2 = traf.resultstosave
                    # print('delcounter according to save fnc!')
                    # print(delcounter)
                    traf.resultstosave = datalogger.save(results2, delcounter)
                except:
                    test = pd.DataFrame(columns=[
                        'AC ID', 'Actual Departure Time', 'Arrival Time',
                        'Fuel Consumed'
                    ])
                    traf.resultstosave = datalogger.save(test, delcounter)
                    del test

                # Clear the to-be-deleted AC
                #self.counter = np.delete(self.counter, delcounter)
                # self.fuelused = np.delete(self.fuelused, delcounter)
                # self.initmass = np.delete(self.initmass, delcounter)
                # for i in delcounter:
                #     self.deltime.pop(delcounter[i])
                #     self.inittime.pop(delcounter[i])
                #     = np.delete(self.deltime, delcounter)
                # self.inittime = np.delete(self.inittime, delcounter)

                #print(traf.resultstosave)
                #print('deleting aircrafts now :)')
                traf.delete(delcounter)
                # self.counter = np.delete(self.counter, delcounter)
                del delcounter
        pass
Example #9
0
def update():
    global recording, prevconfpairs, confframe, delframe, resos, reso_index, ntrafframe, first_ntrafframe, first_confframe, locframe, first_locframe, first_delframe, first_logger, headings, densities, heading_index, density_index, reso, superfirst, i
    stack.stack('FF')
    if superfirst:
        superfirst = False
        stack.stack('PLUGIN MVPSPD')
        stack.stack('PLUGIN MVPSPDFTR')
        stack.stack('PLUGIN MVPVREL')
        stack.stack('PLUGIN MVPVRELFTR')
        stack.stack('ASAS ON')
        stack.stack('CDMETHOD CStateBased')
        stack.stack('RESO '+resos[reso_index])
        stack.stack('PCALL '+'Wouter_MTM/'+str(headings[heading_index])+'_'+str(densities[density_index])+'_'+str(i)+'.SCN')
        stack.stack('FF')
    if traf.ntraf > 0 and not recording:                                                                          # Voor low density case.
        recording = True
    else:
        pass

    tlist_id = [x for x in traf.id if x[0] == 'T']
    tlist_idx = traf.id2idx(tlist_id)

    if first_ntrafframe:
        ntrafframe = np.array([sim.simt, traf.ntraf])
        first_ntrafframe = False
    else:
        ntrafframe = np.vstack((ntrafframe, np.array([sim.simt, traf.ntraf])))

    if first_locframe:
        locframe = np.array([sim.simt, traf.lat, traf.lon])
        first_locframe = False
    else:
        if sim.simt % 5*60 < 1:
            locframe = np.vstack((locframe, np.array([sim.simt, traf.lat, traf.lon])))
        else:
            pass

    if recording:                                                                                               # Omgekeerd Statement, de not moet eigenlijk weg.
        confpairs, lospairs, inconf, tcpamax, qdr, dist, dcpa, tcpa, tLOS = \
        StateBased.detect(StateBased, traf, traf, bs.traf.cd.rpz, bs.traf.cd.hpz, bs.traf.cd.dtlookahead)
        #newgscapped = ConflictResolution.resolve(traf.asas,traf)
        if len([x for x in confpairs if x not in prevconfpairs]) > 0:
            newcomers = [confpairs.index(i) for i in confpairs if i not in prevconfpairs]
            for n in newcomers:
                confname = confpairs[n]
                ac1 = confname[0]
                ac2 = confname[1]
                ac1idx = traf.id2idx(ac1)
                ac2idx = traf.id2idx(ac2)
                bearing = qdr[n]
                distance = dist[n]
                distancecpa = dcpa[n]
                timecpa = tcpa[n]
                timeLOS = tLOS[n]
                initialtas = traf.tas[ac1idx]
                initialhdg = traf.hdg[ac1idx]
                initialtasi = traf.tas[ac2idx]
                initialhdgi = traf.hdg[ac2idx]
                latitude = traf.lat[ac1idx]
                longitude = traf.lon[ac1idx]
                latitudei = traf.lat[ac2idx]
                longitudei = traf.lon[ac2idx]

                if first_confframe:
                    confframe = np.array([sim.simt, confname, ac1idx, ac1, bearing, distance, distancecpa, timecpa, timeLOS, initialtas, initialhdg, initialtasi, initialhdgi, latitude, longitude, latitudei, longitudei])
                    first_confframe = False
                else:
                    confframe = np.vstack((confframe, np.array([sim.simt, confname, ac1idx, ac1, bearing, distance, distancecpa, timecpa, timeLOS, initialtas, initialhdg, initialtasi, initialhdgi, latitude, longitude, latitudei, longitudei])))
        prevconfpairs = confpairs
        if sim.simt > 3.5*3600 and not first_confframe and not first_delframe: # Moet 4 ofzo zijn
            if not os.path.exists('/BSData2/Dens'+str(densities[density_index])+'/Head'+str(headings[heading_index])):
                os.makedirs('/BSData2/Dens'+str(densities[density_index])+'/Head'+str(headings[heading_index]), exist_ok=True)

            #pd.DataFrame(confframe).to_csv(str(headings[heading_index])+'_'+str(densities[density_index])+'_'+str(i)+'_'+reso+'_ResultsDF.csv', sep=';')
            pd.DataFrame(confframe).to_csv('/BSData2/Dens'+str(densities[density_index])+'/Head'+str(headings[heading_index])+'/'+str(i)+'-'+resos[reso_index]+'-confframe.csv' ,sep=';')

            #pd.DataFrame(delframe).to_csv(str(headings[heading_index])+'_'+str(densities[density_index])+'_'+str(i)+'_'+reso+'_Results-DelsDF.csv', sep=';')
            pd.DataFrame(delframe).to_csv('/BSData2/Dens'+str(densities[density_index])+'/Head'+str(headings[heading_index])+'/'+str(i)+'-'+resos[reso_index]+'-delframe.csv' ,sep=';')

            #pd.DataFrame(ntrafframe).to_csv(str(headings[heading_index])+'_'+str(densities[density_index])+'_'+str(i)+'_'+reso+'_Results-ntrafDF.csv', sep=';')
            pd.DataFrame(ntrafframe).to_csv('/BSData2/Dens'+str(densities[density_index])+'/Head'+str(headings[heading_index])+'/'+str(i)+'-'+resos[reso_index]+'-ntrafframe.csv' ,sep=';')

            #pd.DataFrame(locframe).to_csv(str(headings[heading_index])+'_'+str(densities[density_index])+'_'+str(i)+'_'+reso+'_Results-locsDF.csv', sep=';')
            pd.DataFrame(locframe).to_csv('/BSData2/Dens'+str(densities[density_index])+'/Head'+str(headings[heading_index])+'/'+str(i)+'-'+resos[reso_index]+'-locframe.csv' ,sep=';')

            #stack.stack('HOLD')
            stack.stack('RESET')

            reso_index += 1

            c = False
            if reso_index > (len(resos) - 1):
                reso_index = 0
                i += 1
                if i > 10: #10
                    i = 1
                    heading_index += 1
                    if heading_index > 9: #9
                        heading_index = 0
                        density_index += 1
                        if density_index > 0: #9
                            stack.stack('HOLD')
                            print('complete')
                            c = True

            if not c:
                stack.stack('CDMETHOD CStateBased')
                stack.stack('RESO '+resos[reso_index])
                stack.stack('PCALL '+'Wouter_MTM/'+str(headings[heading_index])+'_'+str(densities[density_index])+'_'+str(i)+'.SCN')
                stack.stack('FF')
            else:
                stack.stack('HOLD')
            first_confframe = True
            first_delframe = True
            first_ntrafframe = True
            recording = False
        
    for aircraft in traf.id:
        if traf.actwp.lon[traf.id2idx(aircraft)] > 10:
            if first_delframe:
                delframe = np.array([sim.simt, aircraft, traf.lat[traf.id2idx(aircraft)], traf.lon[traf.id2idx(aircraft)], traf.distflown[traf.id2idx(aircraft)]])
                first_delframe = False       
            else:
                delframe = np.vstack((delframe, np.array([sim.simt, aircraft, traf.lat[traf.id2idx(aircraft)], traf.lon[traf.id2idx(aircraft)], traf.distflown[traf.id2idx(aircraft)]])))                                                      
            
            traf.delete(traf.id2idx(aircraft))
        else:
            pass
Example #10
0
    def update(self, dt):
        ''' Update flight efficiency metrics
            2D and 3D distance [m], and work done (force*distance) [J] '''
        if self.active:
            resultantspd = np.sqrt(traf.gs * traf.gs + traf.vs * traf.vs)
            self.distance2D += dt * traf.gs
            self.distance3D += dt * resultantspd

            # Find out which aircraft are currently inside the experiment area, and
            # determine which aircraft need to be deleted.
            insdel = areafilter.checkInside(self.delarea, traf.lat, traf.lon,
                                            traf.alt)
            insexp = insdel if not self.exparea else \
                areafilter.checkInside(self.exparea, traf.lat, traf.lon, traf.alt)
            # Find all aircraft that were inside in the previous timestep, but no
            # longer are in the current timestep
            delidx = np.where(
                np.array(self.insdel) * (np.array(insdel) == False))[0]
            self.insdel = insdel

            # Count new conflicts where at least one of the aircraft is inside
            # the experiment area
            # Store statistics for all new conflict pairs
            # Conflict pairs detected in the current timestep that were not yet present in the previous timestep
            confpairs_new = list(set(traf.cd.confpairs) - self.prevconfpairs)
            if confpairs_new:
                # If necessary: select conflict geometry parameters for new conflicts
                # idxdict = dict((v, i) for i, v in enumerate(traf.cd.confpairs))
                # idxnew = [idxdict.get(i) for i in confpairs_new]
                # dcpa_new = np.asarray(traf.cd.dcpa)[idxnew]
                # tcpa_new = np.asarray(traf.cd.tcpa)[idxnew]
                # tLOS_new = np.asarray(traf.cd.tLOS)[idxnew]
                # qdr_new = np.asarray(traf.cd.qdr)[idxnew]
                # dist_new = np.asarray(traf.cd.dist)[idxnew]

                newconf_unique = {frozenset(pair) for pair in confpairs_new}
                ac1, ac2 = zip(*newconf_unique)
                idx1 = traf.id2idx(ac1)
                idx2 = traf.id2idx(ac2)
                newconf_inside = np.logical_or(insexp[idx1], insexp[idx2])

                nnewconf_exp = np.count_nonzero(newconf_inside)
                if nnewconf_exp:
                    self.confinside_all += nnewconf_exp
                    self.conflog.log(self.confinside_all)
            self.prevconfpairs = set(traf.cd.confpairs)

            # Register distance values upon entry of experiment area
            newentries = np.logical_not(self.insexp) * insexp
            self.dstart2D[newentries] = self.distance2D[newentries]
            self.dstart3D[newentries] = self.distance3D[newentries]
            self.workstart[newentries] = traf.work[newentries]
            self.entrytime[newentries] = sim.simt

            # Log flight statistics when exiting experiment area
            exits = np.logical_and(self.insexp, np.logical_not(insexp))
            # Update insexp
            self.insexp = insexp

            if np.any(exits):
                self.flst.log(
                    np.array(traf.id)[exits], self.create_time[exits],
                    sim.simt - self.entrytime[exits],
                    (self.distance2D[exits] - self.dstart2D[exits]) / nm,
                    (self.distance3D[exits] - self.dstart3D[exits]) / nm,
                    (traf.work[exits] - self.workstart[exits]) * 1e-6,
                    traf.lat[exits], traf.lon[exits], traf.alt[exits] / ft,
                    traf.tas[exits] / kts, traf.vs[exits] / fpm,
                    traf.hdg[exits], traf.cr.active[exits],
                    traf.aporasas.alt[exits] / ft,
                    traf.aporasas.tas[exits] / kts,
                    traf.aporasas.vs[exits] / fpm, traf.aporasas.hdg[exits])

            # delete all aicraft in self.delidx
            if len(delidx) > 0:
                traf.delete(delidx)

        # Autodelete for descending with swTaxi:
        if not self.swtaxi:
            delidxalt = np.where((self.oldalt >= self.swtaxialt) *
                                 (traf.alt < self.swtaxialt))[0]
            self.oldalt = traf.alt
            if len(delidxalt) > 0:
                traf.delete(list(delidxalt))
Example #11
0
    def update(self):
        if not self.connected:
            return

        # t1 = time.time()
        # Get states from OpenSky. If all states fails try getting own states only.
        states = self.get_states()
        if states is None:
            if self.authenticated:
                states = self.get_states(ownonly=True)
            if states is None:
                return

        # Current time
        curtime = time.time()

        # States contents:
        icao24, acid, orig, time_pos, last_contact, lon, lat, geo_alt, on_gnd, \
            spd, hdg, vspd, sensors, baro_alt, squawk, spi, pos_src = states[:17]

        # Relevant params as numpy arrays
        lat = np.array(lat, dtype=np.float64)
        lon = np.array(lon, dtype=np.float64)
        alt = np.array(baro_alt, dtype=np.float64)
        hdg = np.array(hdg, dtype=np.float64)
        vspd = np.array(vspd, dtype=np.float64)
        spd = np.array(spd, dtype=np.float64)
        acid = np.array([i.strip() for i in acid], dtype=np.str_)
        icao24 = np.array(icao24, dtype=np.str_)

        idx = np.array(traf.id2idx(acid))

        # Split between already existing and new aircraft
        newac = idx == -1
        other = np.logical_not(newac)

        # Filter out invalid entries
        valid = np.logical_not(np.logical_or.reduce(
            [np.isnan(x) for x in [lat, lon, alt, hdg, vspd, spd]]))
        newac = np.logical_and(newac, valid)
        other = np.logical_and(other, valid)
        n_new = np.count_nonzero(newac)
        n_oth = np.count_nonzero(other)

        # t2 = time.time()

        # Create new aircraft
        if n_new:
            actype = [actypes.get(str(i), 'B744') for i in icao24[newac]]
            traf.create(n_new, actype, alt[newac], spd[newac], None,
                        lat[newac], lon[newac], hdg[newac], acid[newac])
            self.my_ac[-n_new:] = True

        # t3 = time.time()

        # Update the rest
        if n_oth:
            traf.move(idx[other], lat[other], lon[other], alt[other], hdg[other], \
                      spd[other], vspd[other])
            self.upd_time[idx[other]] = curtime

        # t4 = time.time()

        # remove aircraft with no message for less than 1 minute
        # opensky already filters
        delidx = np.where(np.logical_and(self.my_ac, curtime - self.upd_time > 10))[0]
        if len(delidx) > 0:
            traf.delete(delidx)
Example #12
0
    def update(self):
        if not self.connected:
            return

        # t1 = time.time()
        if settings.opensky_ownonly:
            states = self.get_states(ownonly=True)
            if states is None:
                return
        else:
        # Get states from OpenSky. If all states fails try getting own states only.
            states = self.get_states()
            if states is None:
                if self.authenticated:
                    states = self.get_states(ownonly=True)
                if states is None:
                    return

        # Current time
        curtime = time.time()

        # States contents:
        icao24, acid, orig, time_pos, last_contact, lon, lat, geo_alt, on_gnd, \
            spd, hdg, vspd, sensors, baro_alt, squawk, spi, pos_src = states[:17]

        # Relevant params as numpy arrays
        lat = np.array(lat, dtype=np.float64)
        lon = np.array(lon, dtype=np.float64)
        alt = np.array(baro_alt, dtype=np.float64)
        hdg = np.array(hdg, dtype=np.float64)
        vspd = np.array(vspd, dtype=np.float64)
        spd = np.array(spd, dtype=np.float64)
        acid = np.array([i.strip() for i in acid], dtype=np.str_)
        icao24 = np.array(icao24, dtype=np.str_)

        idx = np.array(traf.id2idx(acid))

        # Split between already existing and new aircraft
        newac = idx == -1
        other = np.logical_not(newac)

        # Filter out invalid entries
        valid = np.logical_not(np.logical_or.reduce(
            [np.isnan(x) for x in [lat, lon, alt, hdg, vspd, spd]]))
        newac = np.logical_and(newac, valid)
        other = np.logical_and(other, valid)
        n_new = np.count_nonzero(newac)
        n_oth = np.count_nonzero(other)

        # t2 = time.time()

        # Create new aircraft
        if n_new:
            actype = [actypes.get(str(i), 'B744') for i in icao24[newac]]
            traf.create(n_new, actype, alt[newac], spd[newac], None,
                        lat[newac], lon[newac], hdg[newac], acid[newac])
            self.my_ac[-n_new:] = True

        # t3 = time.time()

        # Update the rest
        if n_oth:
            traf.move(idx[other], lat[other], lon[other], alt[other], hdg[other], \
                      spd[other], vspd[other])
            self.upd_time[idx[other]] = curtime

        # t4 = time.time()

        # remove aircraft with no message for less than 1 minute
        # opensky already filters
        delidx = np.where(np.logical_and(self.my_ac, curtime - self.upd_time > 10))[0]
        if len(delidx) > 0:
            traf.delete(delidx)
Example #13
0
    def update(self, dt):
        ''' Update flight efficiency metrics
            2D and 3D distance [m], and work done (force*distance) [J] '''
        if self.active:
            resultantspd = np.sqrt(traf.gs * traf.gs + traf.vs * traf.vs)
            self.distance2D += dt * traf.gs
            self.distance3D += dt * resultantspd
            self.work += (traf.perf.thrust * dt * resultantspd)

            # Find out which aircraft are currently inside the experiment area, and
            # determine which aircraft need to be deleted.
            insdel = areafilter.checkInside(self.delarea, traf.lat, traf.lon,
                                            traf.alt)
            insexp = insdel if not self.exparea else \
                areafilter.checkInside(self.exparea, traf.lat, traf.lon, traf.alt)
            # Find all aircraft that were inside in the previous timestep, but no
            # longer are in the current timestep
            delidx = np.where(
                np.array(self.insdel) * (np.array(insdel) == False))[0]
            self.insdel = insdel

            # Count new conflicts where at least one of the aircraft is inside
            # the experiment area
            if traf.asas.confpairs_new:
                newconf_unique = {
                    frozenset(pair)
                    for pair in traf.asas.confpairs_new
                }
                ac1, ac2 = zip(*newconf_unique)
                idx1 = traf.id2idx(ac1)
                idx2 = traf.id2idx(ac2)
                newconf_inside = np.logical_or(insexp[idx1], insexp[idx2])

                nnewconf_exp = np.count_nonzero(newconf_inside)
                if nnewconf_exp:
                    self.confinside_all += nnewconf_exp
                    self.conflog.log(self.confinside_all)

            # Register distance values upon entry of experiment area
            newentries = np.logical_not(self.insexp) * insexp
            self.dstart2D[newentries] = self.distance2D[newentries]
            self.dstart3D[newentries] = self.distance3D[newentries]
            self.workstart[newentries] = self.work[newentries]
            self.entrytime[newentries] = sim.simt

            # Log flight statistics when exiting experiment area
            exits = self.insexp * np.logical_not(insexp)
            if np.any(exits):
                self.flst.log(
                    np.array(traf.id)[exits], self.create_time[exits],
                    sim.simt - self.entrytime[exits],
                    self.dstart2D[exits] - self.distance2D[exits],
                    self.dstart3D[exits] - self.distance3D[exits],
                    self.workstart[exits] - self.work[exits], traf.lat[exits],
                    traf.lon[exits], traf.alt[exits], traf.tas[exits],
                    traf.vs[exits], traf.hdg[exits], traf.asas.active[exits],
                    traf.pilot.alt[exits], traf.pilot.tas[exits],
                    traf.pilot.vs[exits], traf.pilot.hdg[exits])

            # delete all aicraft in self.delidx
            if len(delidx) > 0:
                traf.delete(delidx)

        # Autodelete for descending with swTaxi:
        if not self.swtaxi:
            delidxalt = np.where((self.oldalt >= self.swtaxialt) *
                                 (traf.alt < self.swtaxialt))[0]
            self.oldalt = traf.alt
            if len(delidxalt) > 0:
                traf.delete(list(delidxalt))
Example #14
0
def update():
    global connected, reset_bool, done, done_count, delete_dict, idx_mc, obs
    # global reward, idx_mc, reset_bool, connected, obs, prev_obs, done_count, final_obs, done, delete_dict
    if connected:
        # Bluesky first timestep starts with update step, so use the reset_bool to determine wheter a reset has
        # occured or not. Then create environment agents.
        if reset_bool:
            # Randomize starting location depending on boundaries in settings.
            aclat, aclon = latlon_randomizer(settings)
            achdg = np.random.randint(1, 360, settings.n_ac)
            acalt = np.ones(settings.n_ac) * 5000 * ft

            # Create equal distribution of destinations
            dest_idx = destination_creator(settings)

            # print('Heading destination', dest_hdg)
            # print('Destination idx', dest_idx)

            # dest_idx = np.random.randint(3, size=settings.n_ac) + 1
            if not settings.evaluate:
                print('Created ', str(settings.n_ac),
                      ' random aircraft, resetted!')
                traf.create(
                    n=settings.n_ac,
                    aclat=aclat,
                    aclon=aclon,
                    achdg=achdg,  # 360 * np.random.rand(1)
                    acspd=250 * kts,
                    acalt=acalt,
                    actype='B737-800',
                    dest=dest_idx)  # settings.acspd)

            ilsgate('garbage')
            reset_bool = False
            # n_ac_neighbours = 1
            return

        # Calculate observations, current amount of n_ac_neighbours and info dict. Info dict is used in the centralized
        # variant to correctly insert other agent actions.
        obs, n_ac_neighbours, info = calc_state()

        # Calculate reward depending on current obs, and check if episode/agents are done.
        reward, done = calc_reward(n_ac_neighbours)

        # Delete agents that are set to done.
        for agent_id in done.keys():
            if agent_id in delete_dict:
                if done[agent_id] and not delete_dict[agent_id]:
                    traf.delete(traf.id2idx(agent_id))
                    print('Deleted ', agent_id)
                    done_count += 1
                    delete_dict[agent_id] = True

        # Track nr of timesteps
        idx_mc += 1
        save_metrics()
        # Return observations and rewards
        client_mc.log_returns(eid, reward, info, done)

        # Check if episode is done either by horizon or all a/c landed, but more likely crashed into each other.
        if not settings.evaluate:
            if idx_mc >= settings.max_timesteps or done_count >= (
                    settings.n_ac - settings.n_neighbours - 1):
                print('Done with Episode: ', eid)
                client_mc.end_episode(eid, obs)
                time.sleep(0.5)
                sim.reset()
Example #15
0
    def update(self, dt):
        """ Log all desired data if AREA is active. """
        if self.active:
            total_spd = np.sqrt(traf.gs * traf.gs + traf.vs * traf.vs)
            self.distance2D += dt * traf.gs
            self.distance3D += dt * total_spd

            # Check whether all aircraft are currently inside the experiment area.
            inside_exp = areafilter.checkInside(self.exp_area, traf.lat,
                                                traf.lon, traf.alt)
            if not np.all(inside_exp):
                raise RuntimeError('An aircraft escaped the experiment area!')

            # Check if mean speed in scenario below threshold.
            if self.stable and traf.ntraf >= 5 and np.mean(traf.gs) < 2.:
                # Turn reso OFF and unset stable flag, to let this scenario be able to finish.
                print(
                    f'WARNING! Simulation stuck.\n'
                    f'Mean speed of simulation reached {np.mean(traf.gs):.2f}m/s\n'
                    f'Setting stable flag to False and Turning RESO OFF for the rest of this scenario'
                )
                stack.stack('RESO OFF')
                self.stable = False

            # Check for arrived flights.
            # Upon reaching destination, autopilot switches off the LNAV.
            arrived = ~traf.swlnav

            # Count new conflicts and losses of separation.
            # Each conflict pair is only counted once in the entire simulation.
            # Store statistics for all new conflict pairs.
            confpairs_new = traf.cd.confpairs_unique - self.prevconfpairs
            lospairs_new = traf.cd.lospairs_unique - self.prevlospairs

            # Ignore conflicts and losses for descending or arrived aircraft.
            ignore_confpairs = set()
            ignore_lospairs = set()
            for pair in traf.cd.confpairs_unique:
                for ac in pair:
                    try:
                        idx = traf.id.index(ac)
                        if traf.vs[idx] < -1E-4 or arrived[idx]:
                            ignore_confpairs.add(pair)
                    except ValueError:
                        # Aircraft is already deleted.
                        ignore_confpairs.add(pair)
            for pair in traf.cd.lospairs_unique:
                # Do not count LoS'ses as conflicts.
                ignore_confpairs.add(pair)
                for ac in pair:
                    try:
                        idx = traf.id.index(ac)
                        if traf.vs[idx] < -1E-4 or arrived[idx]:
                            ignore_lospairs.add(pair)
                    except ValueError:
                        # Aircraft is already deleted.
                        ignore_lospairs.add(pair)
            confpairs_new -= ignore_confpairs
            lospairs_new -= ignore_lospairs

            # if lospairs_new:
            #     # Use this for debugging reso algo.
            #     print('LoS detected:', lospairs_new)
            #     print()
            #
            # if confpairs_new:
            #     # Use this for debugging reso algo.
            #     print('Conflict detected:', confpairs_new)
            #     print()

            self.ntotal_conf += len(confpairs_new)
            self.ntotal_los += len(lospairs_new)

            self.conf_log.log(
                traf.ntraf,
                len(traf.cd.confpairs_unique) - len(ignore_confpairs),
                len(traf.cd.lospairs_unique) - len(ignore_lospairs),
                self.ntotal_ac, self.ntotal_conf, self.ntotal_los,
                bool(traf.cr.do_cr), self.stable)

            # Log distance values upon entry of experiment area (includes spawning aircraft).
            newentries = np.logical_not(self.inside_exp) * inside_exp
            self.dstart2D[newentries] = self.distance2D[newentries]
            self.dstart3D[newentries] = self.distance3D[newentries]
            self.workstart[newentries] = traf.work[newentries]
            self.entrytime[newentries] = sim.simt

            # Update values for next loop.
            self.inside_exp = inside_exp
            self.prevconfpairs.update(confpairs_new)
            self.prevlospairs.update(lospairs_new)

            # Log flight statistics when reaching destination and delete aircraft.
            del_idx = np.flatnonzero(arrived)
            self.flst_log.log(
                np.array(traf.id)[del_idx], self.create_time[del_idx],
                sim.simt - self.entrytime[del_idx],
                (self.distance2D[del_idx] - self.dstart2D[del_idx]),
                (self.distance3D[del_idx] - self.dstart3D[del_idx]),
                (traf.work[del_idx] - self.workstart[del_idx]) * 1e-6,
                traf.lat[del_idx], traf.lon[del_idx], traf.alt[del_idx] / ft,
                traf.tas[del_idx], traf.vs[del_idx] / fpm, traf.hdg[del_idx],
                traf.cr.active[del_idx], traf.aporasas.alt[del_idx] / ft,
                traf.aporasas.tas[del_idx], traf.aporasas.hdg[del_idx],
                traf.aporasas.vs[del_idx] / fpm, traf.cr.do_cr, self.stable)
            traf.delete(del_idx)