Пример #1
0
def evaluate_policy(net, env, t):

    if "GCN" in str(type(net)):
        X = env.X(t)[np.newaxis, :]
        A = env.A(t)[np.newaxis, :]
        logits, counts = net.forward(A, X)

    elif "MLP" in str(type(net)):
        X = env.X(t)
        G, N = get_additional_regressors(env, t)
        Z = np.hstack([X, G, N])[np.newaxis, :, :]
        logits, counts = net.forward(Z)

    elif "RNN" in str(type(net)):
        X = env.X(t, graph_attributes=True, dtype="numpy")
        G, N = get_additional_regressors(env, t)
        Z = np.hstack([X, G, N])[np.newaxis, :, :].transpose((1, 0, 2))
        logits, counts = net.forward(Z)
    else:
        raise ValueError("Unknown algorithm")

    probs = F.softmax(logits.squeeze(), dim=1)[:, 1].data.numpy()
    #import pdb; pdb.set_trace()
    counts = np.round(counts.data.numpy()).astype(int).flatten()[0]
    #if counts % 2 == 1: counts += 1
    return probs, counts
Пример #2
0
def evaluate_policy(net, env, t):

    if "AGCN" in str(type(net)):
        X = env.X(t)
        G, N = get_additional_regressors(env, t)
        A = env.A(t)
        yhat = net.forward(A, X, G, N)

    elif "GCN" in str(type(net)):
        X = env.X(t)[np.newaxis, :]
        A = env.A(t)[np.newaxis, :]
        yhat = net.forward(A, X)

    elif "MLP" in str(type(net)):
        X = env.X(t)
        G, N = get_additional_regressors(env, t)
        Z = np.hstack([X, G, N])
        etc1 = np.full_like(Z[:, :1], fill_value=env.entry_rate)
        etc2 = np.full_like(Z[:, :1], fill_value=env.death_rate)
        ZZ = np.c_[Z, etc1, etc2]
        ZZ = np.hstack([ZZ, ZZ**2])
        yhat = net.forward(ZZ)

    else:
        raise ValueError("Unknown algorithm")

    return yhat
Пример #3
0
def get_regressors(file, times):
    As = []
    Xs = []
    GNs = []
    Ys = []
    file["env"].removed_container.clear()
    for t in trange(file["env"].time_length):
        if t in times:
            liv = file["env"].get_living(t)
            if len(liv) == 0:
                continue
            A = file["env"].A(t, "sparse")
            X = file["env"].X(t, graph_attributes=True)
            G, N = utils.get_additional_regressors(file["env"], t)

            Y = np.zeros_like(liv).reshape(-1, 1)
            Y[np.isin(liv, list(file["opt"]["matched"][t]))] = 1

            As.append(A)
            Xs.append(X)
            GNs.append(np.hstack([G, N]))
            Ys.append(Y)

        file["env"].removed_container[t].update(file["opt"]["matched"][t])

    file["env"].removed_container.clear()
    return As, Xs, GNs, Ys
Пример #4
0
def evaluate_policy(net, env, t):
    
    if "AGCN" in str(type(net)):
        X = env.X(t)
        G,N = get_additional_regressors(env, t)
        A = env.A(t)
        yhat = net.forward(A, X, G, N)
        
    elif "GCN" in str(type(net)):
        X = env.X(t)[np.newaxis,:]
        A = env.A(t)[np.newaxis,:]
        yhat = net.forward(A, X)
        
    elif "MLP" in str(type(net)):  
        X = env.X(t)
        G, N = get_additional_regressors(env, t)
        Z = np.hstack([X, G, N])
        yhat = net.forward(Z)
             
    elif "RNN" in str(type(net)): 
        n = len(env.get_living(t))
        lens = [n]
        #lens = lens.tolist()#torch.LongTensor(lens)
        
        X = env.X(t)
        G, N = get_additional_regressors(env, t)
        Z = np.hstack([X, G, N])
        er = np.full((Z.shape[0], 1), fill_value = env.entry_rate)
        dr = np.full((Z.shape[0], 1), fill_value = env.death_rate)
        ZZ = np.hstack([Z, er, dr])[np.newaxis,:,:].transpose((1,0,2))
        ZZv = Variable(torch.FloatTensor(ZZ))
        
        ZZv = pack_padded_sequence(ZZv, lens)
        outputs,_ = net.forward(ZZv)  
        p = net.out_layer(outputs[:n,0])
        yhat = F.softmax(p, dim= 1)[:,1]   
        
    else:
        raise ValueError("Unknown algorithm")
        
    return yhat
Пример #5
0
 def evaluate_rnn(self, net, env, t):
     n = len(env.get_living(t))
     lens = [n]
     X = env.X(t)
     G, N = get_additional_regressors(env, t)
     Z = np.hstack([X, G, N])
     er = np.full((Z.shape[0], 1), fill_value=env.entry_rate)
     dr = np.full((Z.shape[0], 1), fill_value=env.death_rate)
     ZZ = np.hstack([Z, er, dr])[np.newaxis, :, :].transpose((1, 0, 2))
     ZZv = Variable(torch.FloatTensor(ZZ))
     ZZv = pack_padded_sequence(ZZv, lens)
     outputs, _ = net.forward(ZZv)
     p = net.out_layer(outputs[:n, 0])
     yhat = F.softmax(p, dim=1)[:, 1]
     return yhat.data.numpy()
Пример #6
0
def evaluate_policy(net, env, t):
    if net is None:
        yhat = np.ones_like(env.get_living(t))

    elif "GCN" in str(type(net)):
        X = env.X(t)[np.newaxis, :]
        A = env.A(t)[np.newaxis, :]
        yhat = net.forward(A, X).data.numpy().flatten()

    elif "MLP" in str(type(net)):
        X = env.X(t)
        G, N = get_additional_regressors(env, t)
        Z = np.hstack([X, G, N])
        yhat = net.forward(Z).data.numpy().flatten()

    return pd.Series(index=env.get_living(t), data=yhat)
Пример #7
0
def evaluate_policy(net, env, t):
    try:
        if "GCN" in str(type(net)):
            X = env.X(t)[np.newaxis, :]
            A = env.A(t)[np.newaxis, :]
            yhat = net.forward(A, X)

        elif "MLP" in str(type(net)):
            X = env.X(t)
            G, N = get_additional_regressors(env, t)
            Z = np.hstack([X, G, N])
            yhat = net.forward(Z)

    except Exception as e:
        import pdb
        pdb.set_trace()

    return pd.Series(index = env.get_living(t),
                     data = yhat\
                                .data\
                                .numpy()\
                                .flatten())
Пример #8
0
    
    liv = np.array(env.get_living(t))
    A = env.A(t)
    has_cycle = np.diag(A @ A) > 0
    liv_and_cycle = liv[has_cycle]
    yhat_full = np.zeros(len(liv), dtype=bool)
    
    if len(liv_and_cycle) == 0:
        continue
    
    X = env.X(t)[has_cycle]
    subg = env.subgraph(liv_and_cycle)
    if add == "none":
        XX = X
    elif add == "networkx":
        G = get_additional_regressors(env, t, dtype="numpy")[has_cycle] 
        XX = np.hstack([X, G])
    elif add == "node2vec":
        E = run_node2vec(A[has_cycle,:][:,has_cycle])
        XX = np.hstack([X, E])
    elif add == "both":
        E = run_node2vec(A[has_cycle, :][:, has_cycle])
        G = get_additional_regressors(env, t, dtype="numpy")[has_cycle]
        XX = np.hstack([X, G, E])

    yhat = algo.predict_proba(XX)[:,1] >  thres
    yhat_full[has_cycle] = yhat
    potential = liv[yhat_full]
    
    removed = optimal(env, t, t, subset=potential)["matched"][t]
    env.removed_container[t].update(removed)