Пример #1
0
        horizon = choice([5, 10, 20])
        mix = choice([0, .25, .5, .75, 1])
        none_prob = choice([0, None])
        thres_coeff = np.random.uniform(0, .5, size=2)
    else:
        entry_rate = 3
        death_rate = .01
        n_iters = 5
        horizon = 10
        mix = .5
        none_prob = 0
        thres_coeff = 0.5, .01

    max_expected_size = entry_rate / death_rate

    env = OPTNKidneyExchange(5, .1, 500, seed=12345, initial_size=0)

    opt = optimal(env)
    gre = greedy(env)

    o = get_n_matched(opt["matched"], 0, env.time_length)
    g = get_n_matched(gre["matched"], 0, env.time_length)

    rewards = np.zeros(env.time_length)

    #%%
    for t in range(env.time_length):

        while True:

            optsim = OptimalSimulationWithPriors(env,
Пример #2
0
    gamma = 0.99

    print(net)

    net.opt = torch.optim.Adam(net.parameters(), lr=0.0001)

    #%%

    for epoch in range(5):

        for seed in [0, 1, 2] * 5:

            print("Creating environment")
            env = OPTNKidneyExchange(entry_rate,
                                     death_rate,
                                     time_length,
                                     seed=seed)

            print("Solving environment")
            opt = optimal(env)
            gre = greedy(env)

            o = get_n_matched(opt["matched"], 0, env.time_length)
            g = get_n_matched(gre["matched"], 0, env.time_length)

            rewards = []
            actions = []
            t = 0
            print("Beginning")

            for t in range((2 * horizon + 1) * (epoch + 1)):
Пример #3
0
    g_acts = greedy_actions(env, t, n_repeats)
    if len(g_acts) == 0:
        return 0
    values = dict()
    for g in g_acts:
        snap = snapshot(env, t)
        snap.removed_container[t].update(g_acts)
        avg_value = np.mean(simulate_value(snap, t, horizon, 
                                       gamma, n_iters))
        values[g] = avg_value
    return values


#%%

env = OPTNKidneyExchange(5, .1, 100, seed = 12345)

opt = optimal(env)
gre = greedy(env)
o = get_n_matched(opt["matched"], 0, env.time_length)
g = get_n_matched(gre["matched"], 0, env.time_length)

#%%
time_limit = env.time_lenth
t = 0
horizon = 10 #int(argv[1]) 
n_iters = 1 #int(argv[2])
gamma = 1 #float(argv[3])
n_times = 5
rewards = []
matched = set()       
Пример #4
0
        if env_type == "abo":
            env = ABOKidneyExchange(entry_rate=np.random.randint(5, 10),
                                    death_rate=np.random.choice(
                                        [.1, .09, .08, .07, .06, .05]),
                                    time_length=1000,
                                    seed=s)
        elif env_type == "saidman":
            env = SaidmanKidneyExchange(entry_rate=np.random.randint(5, 10),
                                        death_rate=np.random.choice(
                                            [.1, .09, .08, .07, .06, .05]),
                                        time_length=1000,
                                        seed=s)
        elif env_type == "optn":
            env = OPTNKidneyExchange(entry_rate=np.random.randint(5, 10),
                                     death_rate=np.random.choice(
                                         [.1, .09, .08, .07, .06, .05]),
                                     time_length=1000,
                                     seed=s)
    else:
        env_type = "optn"
        s = 1234
        env = OPTNKidneyExchange(entry_rate=5,
                                 death_rate=.1,
                                 time_length=1000,
                                 seed=1234)

    X, G, E, Y = get_features(env)

    x_filename = "X_{}_{}".format(env_type, s)
    g_filename = "G_{}_{}".format(env_type, s)
    e_filename = "E_{}_{}".format(env_type, s)
        a_probs = evaluate_policy(net, env, t)
        cycles = list(map(tuple, optimal(env, t, t)["matched_cycles"][t]))
        if len(cycles) == 0:
            return None
        np_probs = np.zeros(len(cycles))
        for q, (i, j) in enumerate(cycles):
            np_probs[q] = np.mean((a_probs[liv_idx[i]], a_probs[liv_idx[j]]))
        selected = np.argmax(np_probs)
        if np_probs[selected] >= thres:
            return cycles[selected]
        else:
            return None
#%%

    print("Creating environment")
    env = OPTNKidneyExchange(entry_rate, death_rate, time_length, seed=12345)

    opt = optimal(env)
    gre = greedy(env)

    o = get_n_matched(opt["matched"], 0, env.time_length)
    g = get_n_matched(gre["matched"], 0, env.time_length)

    for i in range(1000):

        env.removed_container.clear()

        print("Solving environment")

        rewards = []
        actions = []
Пример #6
0
    if platform == "linux":
        entry_rate = choice([3, 5, 10])
        death_rate = choice([.01, .05, .1])
        n_iters = choice([1, 10, 20, 100])
        horizon = choice([5, 10, 20])
        mix = choice([0, .25, .5, .75, 1])
        none_prob = choice([0, None])
    else:
        entry_rate = 3
        death_rate = .01
        n_iters = 5
        horizon = 3
        mix = .5
        none_prob = None

    env = OPTNKidneyExchange(5, .1, 500, seed=12345, initial_size=0)

    opt = optimal(env)
    gre = greedy(env)

    o = get_n_matched(opt["matched"], 0, env.time_length)
    g = get_n_matched(gre["matched"], 0, env.time_length)

    rewards = np.zeros(env.time_length)
    dsdsd
    #%%
    for t in range(env.time_length):

        while True:

            optsim = OptimalSimulationWithPriors(env,
Пример #7
0
thres = 0.3
algo = joblib.load("results/traditional_ml/{}_{}_{}.pkl".format(envtype, algorithm, add))

seed = clock_seed()

args = {'entry_rate': entry_rate,
        'death_rate': death_rate,
        'time_length': 1001,
        'seed': seed}

if envtype == "abo":
    env = ABOKidneyExchange(,
elif envtype == "saidman":
    env = SaidmanKidneyExchange(,
elif envtype == "optn":
    env = OPTNKidneyExchange(**args)

opt = optimal(env)
gre = greedy(env)

g = get_n_matched(gre["matched"], 0, env.time_length)
o = get_n_matched(opt["matched"], 0, env.time_length)

#%%
rewards = np.zeros(env.time_length)
for t in trange(env.time_length):
    
    liv = np.array(env.get_living(t))
    A = env.A(t)
    has_cycle = np.diag(A @ A) > 0
    liv_and_cycle = liv[has_cycle]
else:
    frac_ndd = 0
time_length = 1000

t = time()

print("Entry:", entry_rate, "Death", death_rate, "Cycle", max_cycle, "Chain",
      max_chain)

env_params = dict(entry_rate=entry_rate,
                  death_rate=death_rate,
                  time_length=time_length,
                  fraction_ndd=frac_ndd)

envname, env = choice([
    ("OPTN", OPTNKidneyExchange(entry_rate, death_rate, time_length)),
    ("RSU", SaidmanKidneyExchange(entry_rate, death_rate, time_length)),
    ("ABO", ABOKidneyExchange(**env_params))
])

print("\tSolving optimal")
opt = optimal(env, max_cycle=max_cycle, max_chain=max_chain)

print("\tSolving greedy")
gre = greedy(env, max_cycle=max_cycle, max_chain=max_chain)

t_diff = time() - t
res = [
    envname, env.entry_rate, env.death_rate, env.time_length, frac_ndd,
    max_cycle, max_chain, opt["obj"], gre["obj"], gre["obj"] / opt["obj"],
    t_diff