entry_rate = 5 death_rate = .1 horizon = 10 newseed = str(np.random.randint(1e8)) train = True disc = 0.1 net = torch.load("results/RNN_50-1-abo_4386504") #%% for k in [2]: print("Creating environment") env = ABOKidneyExchange(entry_rate, death_rate, time_length, seed=k) 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 = -1 print("Beginning") #%% for t in range(env.time_length):
from sys import platform, argv from random import choice import numpy as np from tqdm import trange from matching.utils.env_utils import two_cycles from matching.solver.kidney_solver2 import optimal, greedy from matching.utils.data_utils import clock_seed, get_n_matched from matching.environment.abo_environment import ABOKidneyExchange from matching.bandits.combinatorial import CombinatorialBandit env = ABOKidneyExchange(4, .1, 101) rewards = np.zeros(env.time_length) 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 t in trange(env.time_length): cycles = two_cycles(env, t) print("len(cycles): ", len(cycles)) if len(cycles) > 0: algo = CombinatorialBandit(env, t, iters_per_arm=10, max_match=4) algo.simulate() best = algo.choose() env.removed_container[t].update(best) rewards[t] = len(best)
entry_rate = 3 death_rate = .1 max_time = 200 n_sims = 100 #choice([1, 5, 10, 20, 50, 100, 500, 1000, 2000]) n_prior = 50 #choice([1, 5, 10, 20, 50]) seed = 123456 #clock_seed() print("Opening file", file) try: net = torch.load("results/" + file) except Exception as e: print(str(e)) continue env_type = "abo" env = ABOKidneyExchange(entry_rate, death_rate, max_time, seed=seed) 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) #%% np.random.seed(clock_seed()) for t in range(env.time_length): probs, count = evaluate_policy(net, env, t, dtype="numpy") for i in range(count):
m.update() m.setObjective(gb.quicksum(grb_vars), gb.GRB.MAXIMIZE) m.optimize() return m if __name__ == "__main__": import numpy as np import networkx as nx import re from matching.trimble_solver.interface import solve as trimble_solve from matching.environment.abo_environment import ABOKidneyExchange env = ABOKidneyExchange(5, .1, 100, fraction_ndd=0.1, seed=1234) m = solve_with_time_constraints(env, max_cycle=0, max_chain=3, max_chain_per_period=3) xs = [eval(v.varName) for v in m.getVars() if v.x > 0] received = dict() donated = dict() for v, w, k, t in xs: received[w] = t donated[v] = t for n in env.nodes(): if not env.node[n]["ndd"] and donated.get(n, []): assert received[n] <= donated[n]
for node in opt_t["new_heads"]: # Head of chain becomes ndd nx.set_node_attributes(env, {node: 1}, name="ndd") if hasattr(env, "data"): env.data.loc[node, "ndd"] = 1 # Delete incoming edges in_edges = list(env.in_edges(node)) env.remove_edges_from(in_edges) #env.removed_container = container return {"obj": obj, "matched": matched, "timing": timing, "opt": opts} if __name__ == "__main__": from matching.environment.abo_environment import ABOKidneyExchange env = ABOKidneyExchange(entry_rate=5, death_rate=0.1, time_length=1000, fraction_ndd=0.1) # Uncapped edge formulation #g_uef = greedy(env, 0, None, formulation="uef") #o_uef = optimal(env, 0, None, formulation="uef") # #g_uef = greedy(env, 2, 2, formulation="hpief_prime_full_red") o_hpief = optimal(env, 4, 0, formulation="hpief_prime_full_red")
import pickle from matching.utils.data_utils import clock_seed, get_features from matching.environment.abo_environment import ABOKidneyExchange from matching.environment.saidman_environment import SaidmanKidneyExchange from matching.environment.optn_environment import OPTNKidneyExchange while True: if platform == "linux": s = clock_seed() env_type = choice(["optn"]) 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:
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 ]
Y[np.isin(liv, list(m))] = 1 labels.append(Y) if len(liv) > 0: X = env.X(t)[has_cycle] subg = env.subgraph(liv) E = run_node2vec(subg) features.append(np.hstack([X, E])) env.removed_container[t].update() return np.vstack(features), np.hstack(labels) env = ABOKidneyExchange(entry_rate=5, death_rate=.1, time_length=10, seed=clock_seed()) X, Y = get_features(env) np.save("X.npy", X) np.save("Y.npy", Y) #%% #%% # # #%% #