Ejemplo n.º 1
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()
Ejemplo n.º 2
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)
Ejemplo n.º 3
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)
Ejemplo n.º 4
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()