Ejemplo n.º 1
0
def strategyAction(strategy, state, DQN_model=None):
    """ 
    Determines the action of the given strategy on the state.

    Parameters: 
    strategy (str): strategy to use

    state (State): current State of the environment

    Returns: 
    action (str): action of the strategy given the state.

    """
    if strategy == "DQN":
        if DQN_model is None:
            print("No DQN model given in the function strategyAction")
            return ACTIONS[0]

        q_value = [predict(DQN_model, state, a) for a in ACTIONS]
        return ACTIONS[np.argmax(q_value)]

    if strategy == "Random":
        return np.random.choice(ACTIONS)

    if strategy == "Trade":
        return ACTIONS[-1]

    if strategy == "Nothing":
        return ACTIONS[-2]

    if strategy == "RandomBattery":
        return np.random.choice(ACTIONS[0:2])

    if strategy == "SmartBattery":
        if state.panelProd > state.consumption:
            return ACTIONS[0]
        else:
            return ACTIONS[1]

    if strategy == "SmartBattery2":
        if state.panelProd > state.consumption and state.battery < BATTERY_CAPACITY * 0.9999:
            return ACTIONS[0]
        elif state.panelProd > state.consumption:
            return ACTIONS[-1]
        else:
            return ACTIONS[1]
Ejemplo n.º 2
0
    parser.add_argument('--num_class', type=int, default=10)

    args = parser.parse_args()
    testfolder = args.testfolder
    print(args.testfolder)

    # read true_ids
    true_ids = np.load(os.path.join(testfolder, 'true_ids.npy'))

    # read files
    files = os.listdir(testfolder)
    files.sort()

    # predict the ids
    predict_ids = []
    time_use = []

    for filename in files:
        # ignore true_ids.npy
        if filename == 'true_ids.npy':
            continue

        data = np.load(os.path.join(testfolder, filename))

        predict_id = predict(data, true_ids, args.num_class)
        predict_ids.append(predict_id)

    # compute the test accuracy
    test_accuracy = np.mean(np.array(predict_ids) == np.array(true_ids))
    print('Test Accuracy: {:.2f}'.format(test_accuracy))
Ejemplo n.º 3
0
def test(env: Env, nb_step, DQN_model=None):
    """ 
    Displays figures to compare the result of the DQN algorithm to other predefined strategies.

    Parameters: 
    env (Env): environment on which the strategies are tested

    nb_step (int): number of steps on which to evaluate strategies

    DQN_model: the model returned by the DQN algorithm.
    If this parameter is set to None, then only the predefined strategies are tested.
    This is useful to check the environment.

    """

    env.reset(nb_step=nb_step)
    initState = copy.deepcopy(env.currentState)

    conso, prod, price = [], [], []

    actions_qvalue = {}
    for a in ACTIONS:
        actions_qvalue[a] = []

    for i in range(nb_step):
        env.step(ACTIONS[0])

        conso.append(env.currentState.consumption)
        prod.append(env.currentState.panelProd)
        price.append(env.currentState.price)

    actions, cost = {}, {}
    battery, charge, discharge, generate, trade = {}, {}, {}, {}, {}

    strategies_list = STRATEGIES[:]

    if DQN_model is None:
        strategies_list.remove("DQN")

    for strategy in strategies_list:
        actions[strategy], cost[strategy] = [], []
        (
            battery[strategy],
            charge[strategy],
            discharge[strategy],
            generate[strategy],
            trade[strategy],
        ) = ([], [], [], [], [])

        env.currentState = copy.deepcopy(initState)
        for i in range(nb_step):
            if strategy == "DQN":
                q_value = predict(DQN_model, env.currentState, ACTIONS)
                action = ACTIONS[np.argmax(q_value)]
                for q, a in zip(q_value, ACTIONS):
                    actions_qvalue[a].append(float(q))
            else:
                action = strategyAction(strategy, env.currentState)
            _, _, step_cost = env.step(action)

            cost[strategy].append(step_cost)
            actions[strategy].append(action)
            battery[strategy].append(env.currentState.battery)

            charge[strategy].append(env.currentState.charge)
            discharge[strategy].append(env.currentState.discharge)
            generate[strategy].append(env.currentState.generate)
            trade[strategy].append(env.currentState.trade)

    fig, axs = plt.subplots(len(strategies_list))
    for i, s in enumerate(strategies_list):
        axs[i].plot(trade[s])
        axs[i].plot(battery[s])
        axs[i].legend(["Trade", "Battery"])
        axs[i].title.set_text(s)
    plt.figure(1)

    fig, axs = plt.subplots(len(strategies_list))

    for i, s in enumerate(strategies_list):
        axs[i].plot(actions[s])
        axs[i].legend(["Actions"])
        axs[i].title.set_text(s)
    plt.figure(2)

    fig, axs = plt.subplots(2)
    axs[0].plot(conso)
    axs[0].plot(prod)
    axs[1].plot(price)
    axs[0].legend(["Consumption", "Production"])
    axs[1].title.set_text("Price")
    plt.figure(3)

    fig, ax = plt.subplots()
    for s in strategies_list:
        ax.plot(np.cumsum(cost[s]))

    ax.legend(strategies_list)
    ax.title.set_text("Cost")
    plt.figure(4)

    fig, ax = plt.subplots()
    for a in ACTIONS:
        ax.plot(actions_qvalue[a])
    ax.legend(ACTIONS)
    ax.title.set_text("Q-value")
    plt.figure(5)

    plt.show()
Ejemplo n.º 4
0
    # assuming that shuffle=True in keras.fit is shuffling correctly
    history = model.fit(trainX,
                        trainY,
                        shuffle=True,
                        validation_data=(testX, testY),
                        batch_size=batchSize,
                        epochs=nrOfEpochs)
    model.summary()
    model.save('models/model_' + str(int(round(time.time()))) + '.h5')
    # saving acc and loss graph as png
    plotAndSaveHistory(history)
    # predicting classes for 1% of the dataset mX
    # predict(model, mX, mY, 0.01)
elif mode is Mode.PREDICT:
    X, Y = loadData()
    (mX, mY) = preProcess(X, Y, numberOfChannels, numberOfClasses,
                          cropWindowSize, cropWindowShift, fs)
    # visuals some spectrograms
    visualizeSpectrogram(mX,
                         spectroWindowSize,
                         spectroWindowShift,
                         fs,
                         nrOfSpectrogram=1)
    model = loadModel(modelPath)

    print("keras.evaluate() result: ", model.evaluate(mX, mY))
    # predicting classes for 100% of the dataset mX
    predict(model, mX, mY, amount=1, verbose=0)
elif mode is Mode.LIVE:
    raise NotImplementedError("Live mode not implemented yet")
Ejemplo n.º 5
0
    print("kfold value: " + str(kfold))
    x_train = train_set.loc[train_set['kfold'] != kfold]
    y_train = target_df.loc[target_df['kfold'] != kfold]
    x_train.drop('kfold', inplace=True, axis=1)
    y_train.drop('kfold', inplace=True, axis=1)

    x_val = train_set.loc[train_set['kfold'] == kfold]
    y_val = target_df.loc[target_df['kfold'] == kfold]
    x_val.drop('kfold', inplace=True, axis=1)
    y_val.drop('kfold', inplace=True, axis=1)

    model = train(x_train, y_train)
    print(str(datetime.now() - beginning))

    log_loss_score, precision_score, recall_score = validate(
        model, x_val, y_val)

    if log_loss_score < log_loss and (precision_score +
                                      recall_score) / 2 > precall:

        print("log_loss: " + str(log_loss_score))
        print("precision: " + str(precision_score))
        print("recall: " + str(recall_score))

        predict(model, test_set, test_df['sig_id'])

        log_loss = log_loss_score
        best_model = model
        best_kfold = kfold
        precall = (precision_score + recall_score) / 2