Exemplo n.º 1
0
def test_episodes():
    """
    Test to check that length of episode,
    cumulative reward and done signal are sent correctly
    """

    data = pd.read_csv("data/train.csv", index_col=0, parse_dates=True)

    env = RyeFlexEnv(data=data)
    plotter = RyeFlexEnvEpisodePlotter()
    length = int(env._episode_length.days * 24)

    # Example with random initial state
    done = False
    cumulative_reward = env._cumulative_reward




    while not done:
        action = env.action_space.sample()
        state, reward, done, info = env.step(action)
        new_cumulative_reward = info["cumulative_reward"]

        assert round(new_cumulative_reward - cumulative_reward, 5) == round(reward, 5)

        cumulative_reward = new_cumulative_reward
        plotter.update(info)

    assert len(plotter._states) == length

    plotter.plot_episode(show=True)
    
    mydata = np.array(wind)
    # scipy.io.savemat('wind.mat', mydata)

    # Example where environment are set to partial known state
    env.reset(start_time=datetime(2020, 2, 3), battery_storage=1)

    done = False
    while not done:
        action = env.action_space.sample()
        state, reward, done, info = env.step(action)
        plotter.update(info)

    assert len(plotter._states) == length
    plotter.plot_episode(show=False)
Exemplo n.º 2
0
def main() -> None:
    root_dir = dirname(abspath(join(__file__, "../")))
    data = pd.read_csv(join(root_dir, "data/train.csv"),
                       index_col=0,
                       parse_dates=True)

    env = RyeFlexEnv(data=data)
    env.reset(start_time=datetime(2021, 2, 1, 0, 0))
    plotter = RyeFlexEnvEpisodePlotter()

    # INSERT YOUR OWN ALGORITHM HERE
    agent = RandomActionAgent(env.action_space)

    # Example with random initial state
    info = {}
    done = False
    # Initial state
    state = env._state

    while not done:

        # INSERT YOUR OWN ALGORITHM HERE
        action = agent.get_action(state)

        state, reward, done, info = env.step(action)

        plotter.update(info)

    print(f"Your test score is: {info['cumulative_reward']} NOK")

    plotter.plot_episode()
Exemplo n.º 3
0
def main() -> None:
    root_dir = dirname(abspath(join(__file__, "../")))
    data = pd.read_csv(join(root_dir, "data/train.csv"),
                       index_col=0,
                       parse_dates=True)

    env = RyeFlexEnv(data=data)
    plotter = RyeFlexEnvEpisodePlotter()
    agent = ConstantActionAgent()

    # Example with random initial state
    info = {}
    done = False
    # Initial state
    state = env._state

    while not done:

        action = agent.get_action()

        state, reward, done, info = env.step(action)

        plotter.update(info)

    print(f"Your score is: {info['cumulative_reward']} NOK")
    plotter.plot_episode()

    # Example where environment are reset
    env.reset(start_time=datetime(2020, 2, 3), battery_storage=1)

    done = False
    while not done:
        action = agent.get_action()

        state, reward, done, info = env.step(action)

        plotter.update(info)

    print(f"Your score is: {info['cumulative_reward']} NOK")
    plotter.plot_episode()
Exemplo n.º 4
0
def main() -> None:
    root_dir = dirname(abspath(join(__file__, "../")))
    data = pd.read_csv(join(root_dir, "data/test.csv"),
                       index_col=0,
                       parse_dates=True)
    env = RyeFlexEnv(data=data)
    env.reset(start_time=datetime(2021, 2, 1, 0, 0))
    data2 = pd.read_csv(join(root_dir, "data/train.csv"),
                        index_col=0,
                        parse_dates=True)
    data = pd.concat([data2, data])
    plotter = RyeFlexEnvEpisodePlotter()

    # INSERT YOUR OWN ALGORITHM HERE
    #agent = KalmanAgent(env.action_space)

    # Example with random initial state
    info = {}
    done = False
    # Initial state
    state = env._state.vector
    N = 28
    while not done:
        #PV = data.loc[env._time:env._time + N*env._time_resolution, "pv_production"]
        #W = data.loc[env._time:env._time + N*env._time_resolution, "wind_production"]
        #C = data.loc[env._time:env._time + N*env._time_resolution, "consumption"]
        spot = data.loc[env._time:env._time + N * env._time_resolution,
                        "spot_market_price"]
        #print("State t: ", state[0] - state[1] - state[2] + action[0] + action[1])

        C = data.loc[env._time - 47 * env._time_resolution:env._time,
                     "consumption"]
        PV = data.loc[env._time - 47 * env._time_resolution:env._time,
                      "pv_production"]
        Wind = data.loc[env._time:env._time + N * env._time_resolution,
                        "wind_speed_50m:ms"]
        Wind_prod_last = data.loc[env._time, "wind_production"]
        C_estim = [np.array(C[-1])]
        PV_estim = [np.array(PV[-1])]
        for i in range(N):
            c = get_predicted_consumption(C[-48:])
            C_estim.append(c)
            C = np.concatenate([C, c])
            pv = get_predicted_solar_power(PV[-48:])
            PV_estim.append(pv)
            PV = np.concatenate([PV, pv])
        # W = []
        # for x in Wind:
        #     W.append(get_predicted_wind_power(x))
        # W = np.array(W)
        W = get_predicted_wind_power_stupid(Wind, Wind_prod_last, N)
        C = np.hstack(C_estim)
        action = MPC_step(N, state[3:6], PV[1:], W[1:], C[1:], spot[1:])
        state, reward, done, info = env.step(action)
        print(env._time)
        plotter.update(info)

    print(f"Your test score is: {info['cumulative_reward']} NOK")
    plotter.plot_episode()
def main() -> None:
    root_dir = dirname(abspath(join(__file__, "../")))
    data = pd.read_csv(join(root_dir, "data/train.csv"),
                       index_col=0,
                       parse_dates=True)

    env = RyeFlexEnv(data)

    print(env.observation_space, env.action_space)

    base_actions = np.array([[1, 1], [1, 0], [0, 1], [1, 0.1], [0.1, 1]])
    actions = base_actions.copy()
    for i in [-2, -1.5, -1.2, -1, -0.1, -0.01, 0.01, 0.1]:
        actions = np.append(actions, base_actions * i, 0)
    #actions = np.array([[0.1,0], [-0.1,0], [0, 0.1], [0,-0.1]])

    agent = Policy(env.observation_space.shape[0] + 1, actions)
    print(list(agent.parameters()))

    lossFunc = nn.MSELoss()
    optimizer = optim.Adam(agent.parameters(), lr=learning_rate)
    scheduler = optim.lr_scheduler.StepLR(optimizer, 30, 0.1)
    for i in range(30):
        if i == 29:
            plotter = RyeFlexEnvEpisodePlotter()
        env.reset()
        info = None
        done = False
        # Initial state
        state = env._state.vector
        state = np.append([state], [0])
        j = 0
        while not done:
            j += 1
            action, Q1 = agent(state)
            print(action)
            state, reward, done, info = env.step(action)
            state = np.append([state], [j])
            if not done:
                _, Q2 = agent(state)
                loss = lossFunc(Q1, reward + agent.gamma * Q2)
            else:
                reward = torch.FloatTensor(np.array(reward))
                reward.requires_grad = True
                loss = lossFunc(Q1, reward)
            optimizer.zero_grad()
            loss.backward()
            optimizer.step()
            if i == 29:
                plotter.update(info)
        scheduler.step()
        if i % 1 == 0:
            print(f"Your score is: {info['cumulative_reward']} NOK")
    plotter.plot_episode()
    plt.plot(np.arange(len(agent.loss_history)), agent.loss_history)
    plt.show()
    print(list(agent.parameters()))
Exemplo n.º 6
0
def main() -> None:
    root_dir = dirname(abspath(join(__file__, "../")))
    data = pd.read_csv(join(root_dir, "data/train.csv"),
                       index_col=0,
                       parse_dates=True)
    env = RyeFlexEnv(data=data)
    env.reset(start_time=datetime(2020, 2, 1, 0, 0))
    plotter = RyeFlexEnvEpisodePlotter()

    # INSERT YOUR OWN ALGORITHM HERE
    #agent = KalmanAgent(env.action_space)

    # Example with random initial state
    info = {}
    done = False
    # Initial state
    state = env._state.vector
    agent = RandomActionAgent(env.action_space)
    #agent = KalmanAgent(env.action_space, state)
    i = 0
    while not done:

        # INSERT YOUR OWN ALGORITHM HERE
        #print(state[0], data.at[env._time + env._time_resolution, "consumption"])
        state[0] = data.at[env._time + env._time_resolution, "consumption"]
        state[1] = data.at[env._time + env._time_resolution, "pv_production"]
        state[2] = data.at[env._time + env._time_resolution, "wind_production"]
        action = agent.get_action(state)

        #action = np.array([0,0])
        #print("State t: ", state[0] - state[1] - state[2] + action[0] + action[1])

        state, reward, done, info = env.step(action)

        #print("State t+1: ", state[0] - state[1] - state[2] + action[0] + action[1])

        plotter.update(info)
        i += 1
        # if i == 24*10:
        #     break

    print(f"Your test score is: {info['cumulative_reward']} NOK")
    plotter.plot_episode(True)
def main() -> None:
    root_dir = dirname(abspath(join(__file__, "../")))
    data = pd.read_csv(join(root_dir, "data/train.csv"), index_col=0, parse_dates=True)

    env = RyeFlexEnv(data)

    agent = RandomActionAgent(action_space=env.action_space)
    plotter = RyeFlexEnvEpisodePlotter()
    info = None
    done = False
    # Initial state
    state = env._state

    while not done:
        action = agent.get_action()
        state, reward, done, info = env.step(action)
        plotter.update(info)

    print(f"Your score is: {info['cumulative_reward']} NOK")
    plotter.plot_episode()
Exemplo n.º 8
0
def main() -> None:
    root_dir = dirname(abspath(join(__file__, "../")))
    data = pd.read_csv(join(root_dir, "data/test.csv"),
                       index_col=0,
                       parse_dates=True)

    env = RyeFlexEnv(data=data)
    plotter = RyeFlexEnvEpisodePlotter()
    data2 = pd.read_csv(join(root_dir, "data/train.csv"),
                        index_col=0,
                        parse_dates=True)
    data3 = pd.concat([data2, data])

    # Reset episode to feb 2021, and get initial state
    state = env.reset(start_time=datetime(2021, 2, 1, 0, 0))

    # INSERT YOUR OWN ALGORITHM HERE
    #agent = SimpleStateBasedAgent()

    info = {}
    done = False
    start = 15
    end = 19
    Val = 25
    ratio = (end - start) / (24 - start + end)
    while not done:
        if env._time.hour < end and env._time.hour > start:
            action = np.array([-Val, 0])
        else:
            action = np.array([ratio * Val, 0])
        state, reward, done, info = env.step(action)

        plotter.update(info)

    print(f"Your test score is: {info['cumulative_reward']} NOK")

    plotter.plot_episode()
Exemplo n.º 9
0
def main() -> None:
    root_dir = dirname(abspath(join(__file__, "../")))
    data = pd.read_csv(join(root_dir, "data/train.csv"),
                       index_col=0,
                       parse_dates=True)

    env = RyeFlexEnv(data=data)
    plotter = RyeFlexEnvEpisodePlotter()
    agent = SimpleStateBasedAgent()

    # Get initial state
    state = env.get_state_vector()
    info = {}
    done = False

    while not done:
        action = agent.get_action(state)

        state, reward, done, info = env.step(action)

        plotter.update(info)

    print(f"Your score is: {info['cumulative_reward']} NOK")
    plotter.plot_episode()