Beispiel #1
0
def main(method, transform, temporal, layout, hybrid, threshold, output):
    variables = getattr(layouts, layout)

    K = list(variables.keys())

    df = utils.prep_dataframe(keep=K)

    if temporal:
        df_shifted = utils.create_shifted_features(df)
        df = df.join(df_shifted, how="outer")
        df = df.dropna()

    if transform:
        print("* Tranform data")
        X = tr.to_normal(df.values)
        df = pd.DataFrame(X, index=df.index.values, columns=df.columns.values)

    X = df.values

    if hybrid:
        model = HRF(k=5, k_star=10, variables_names=df.columns.values)
    else:
        model = GMRF(method=method[0])

    model.fit(X)

    if not hybrid:
        print("* Selected alpha = {}".format(model.alpha_))
    else:
        print("* Selected k = {}, k star = {}".format(model.k, model.k_star))

    if threshold:
        Q = model.precision_.copy()
        ts = np.arange(0., 1., 0.001)

        bics = np.empty(len(ts))
        connectivity = np.empty(len(ts))

        n = Q.shape[0]
        gmrf_test = GMRF()
        gmrf_test.mean_ = np.mean(X, axis=0)
        for i, t in enumerate(ts):
            Q[Q < t] = 0
            gmrf_test.precision_ = Q
            bics[i], _ = gmrf_test.bic(X)
            connectivity[i] = 1 - np.size(np.where(Q == 0)[0]) / (n * n)

        fig, (ax, ax1) = plt.subplots(2, 1)
        ax.plot(connectivity)
        ax1.plot(bics)

    if output:
        results_name = os.path.join(os.path.dirname(__file__), "../results/")
        if hybrid:
            BNs = np.empty(len(model.variables_names), dtype=object)
            for i in range(len(BNs)):
                BNs[i] = (model.bns[i].variables_names, model.bns[i].nodes, model.bns[i].edges)
            np.save(results_name + output + "_bns", BNs)
        else:
            np.save(results_name + output + "_prec", model.precision_)
            np.save(results_name + output + "_mean", model.mean_)
            np.save(results_name + output + "_bic_scores", model.bic_scores)

    if not hybrid:
        plt.figure()
        plt.plot(model.bic_scores)

        fig, ax = plt.subplots(1, 1)
        pl.bin_precision_matrix(model.precision_, df.columns.values, ax)

        plt.show()
def main(layout, model, transform, output):
    variables = getattr(layouts, layout)

    K = list(variables.keys())

    df = utils.prep_dataframe(keep=K)

    df_shifted = utils.create_shifted_features(df)
    df = df.join(df_shifted, how="outer")
    df = df.dropna()

    # print(list(enumerate(df.columns.values)))
    # assert False

    if transform:
        print("* Tranform data")
        X = tr.to_normal(df.values)
        df = pd.DataFrame(X, index=df.index.values, columns=df.columns.values)

    X = df.values

    if model[0] == 'gmrf':
        model = GMRF(variables_names=df.columns.values, alpha=0.1)
    elif model[0] == 'hybrid':
        model = HRF(k=5, k_star=10, variables_names=df.columns.values)

    lim = int(X.shape[0] * 0.75)
    X_train = X[:lim]
    X_test = X[lim:]

    model.fit(X_train)
    print("* Model Fitted")

    # controls_vars = ['ahu_1_outlet', 'ahu_2_outlet', 'ahu_3_outlet', 'ahu_4_outlet']
    controls_vars = ['ahu_3_outlet']
    controller = Controller(6, 15, 30)
    mdp = MDP(model, 1000, reward, 0.8, feature_creator, controller,
              controls_vars, n_jobs=3)
    mdp.learn()

    # plt.figure()
    # plt.hist(test[:, 38:42].ravel(), bins=5, range=(5, 30))

    # plt.figure()
    # plt.plot(test[:, 38:42])

    actions, states = run_simulation(X_test, controls_vars, mdp, model, controller)

    print(actions)
    actions_values_one = [None] * len(controls_vars)
    actions_values_two = [None] * len(controls_vars)
    for i in range(len(controls_vars)):
        actions_values_one[i] = [(j, a[i][1]) for j, a in enumerate(actions)
                                if a[i][0] == 0]
        actions_values_two[i] = [(j, a[i][1]) for j, a in enumerate(actions)
                                if a[i][0] == 1]

        actions_values_one[i] = list(zip(*actions_values_one[i]))
        actions_values_two[i] = list(zip(*actions_values_two[i]))

    for i in range(len(controls_vars)):
        plt.figure()
        if len(actions_values_one[i]) != 0:
            plt.plot(list(actions_values_one[i][0]), list(actions_values_one[i][1]), 'b')
        if len(actions_values_two[i]) != 0:
            plt.plot(list(actions_values_two[i][0]), list(actions_values_two[i][1]), 'g')
        plt.title(controls_vars[i])

    max_states = np.amax(states, axis=1)
    mean_states = np.mean(states, axis=1)
    min_states = np.amin(states, axis=1)

    plt.figure()
    plt.plot(max_states, 'r')
    plt.plot(mean_states, 'g')
    plt.plot(min_states, 'b')

    # plt.figure()
    # plt.plot(actions[:, 0], label="1")
    # plt.plot(actions[:, 1], label="2")
    # plt.plot(actions[:, 2], label="3")
    # plt.plot(actions[:, 3], label="4")
    # plt.legend(loc=0)

    # print(np.mean(actions, axis=0))

    plt.show()