Ejemplo n.º 1
0
def update_param_dict(for_reals=False):
    if for_reals:
        env = ActiveEnv()
        model_dir = 'C:\\Users\\vegar\Dropbox\Master\\thesis.git\RLpower\models'
        for model in os.listdir(model_dir):
            if 'params' in model:
                with open(os.path.join(model_dir,model),'rb') as f:
                    olds_params = pickle.load(f)
                    missing_params = [p for p in env.params if p not in olds_params]
                    params_values = {'reactive_power':False,
                                     'solar_std': 0,
                                     'total_imbalance':True,
                                     'demand_std': 0}
                    for param in missing_params:
                        olds_params[param] = params_values[param]

                assert all([p in env.params for p in olds_params])
                with open(os.path.join(model_dir,model),'wb') as f:
                    pickle.dump(olds_params,f)
Ejemplo n.º 2
0
    def test_action(self):
        """
        Checks that action is taken and updates the network, but only if
        load is not commited
        """
        flex = 0.1
        env = ActiveEnv(force_commitments=True)
        env.set_parameters({'flexibility': flex, 'demand_std': 0})
        env.set_demand_and_solar()
        demand = copy.copy(env.powergrid.load['p_mw'].values)
        action1 = np.ones_like(env.last_action)
        action1 = env.action_space.sample()
        scaled_action1 = flex * action1 * env.powergrid.load['p_mw']

        env._take_action(action1)

        assert norm(env.powergrid.load['p_mw'].values -
                    (demand + scaled_action1)) < 10e-4
        action2 = env.action_space.sample()
        env._take_action(action2)

        # action2 should by modified to cancel effect of action1
        assert norm(env.last_action + scaled_action1) < 10e-4
        assert norm(env.powergrid.load['p_mw'].values - demand) < 10e-4
Ejemplo n.º 3
0
# -*- coding: utf-8 -*-
"""

"""
import copy

import pytest
import copy
import numpy as np
from numpy.linalg import norm
from active_env.envs.active_network_env import ActiveEnv

__author__ = 'Vegard Solberg'
__email__ = '*****@*****.**'

ENV = ActiveEnv()


class TestForecasts:
    def test_initial_forecasts(self):
        """
        Checks that there are forecasts for every load, and that there always
        exist forecasts k hours in the future for all time steps.
        """
        env = copy.deepcopy(ENV)
        episode_load_forecasts = env.get_episode_demand_forecast()
        assert len(episode_load_forecasts) == 1  # len(ENV.powergrid.load)
        horizon = env.params['forecast_horizon']
        episode_length = env.params['episode_length']
        for load in episode_load_forecasts:
            assert load.shape[0] - episode_length >= horizon
Ejemplo n.º 4
0
# -*- coding: utf-8 -*-
"""
Training the reinforcement agent using stable baselines
"""
__author__ = 'Vegard Solberg'
__email__ = '*****@*****.**'

from active_env.envs.active_network_env import ActiveEnv
from stable_baselines.common.vec_env.dummy_vec_env import DummyVecEnv
from stable_baselines.ddpg.policies import LnMlpPolicy
from stable_baselines.ddpg.noise import OrnsteinUhlenbeckActionNoise
from stable_baselines import DDPG
from stable_baselines.ddpg.noise import AdaptiveParamNoiseSpec
import numpy as np

powerenv = ActiveEnv()
powerenv.set_parameters({
    'state_space': ['sun', 'demand', 'imbalance'],
    'reward_terms': ['voltage', 'current', 'imbalance']
})

powerenv = DummyVecEnv([lambda: powerenv])
action_mean = np.zeros(powerenv.action_space.shape)
action_sigma = 0.3 * np.ones(powerenv.action_space.shape)
action_noise = OrnsteinUhlenbeckActionNoise(mean=action_mean,
                                            sigma=action_sigma)

param_noise = AdaptiveParamNoiseSpec(initial_stddev=0.2,
                                     desired_action_stddev=0.01)

t_steps = 800000
Ejemplo n.º 5
0
from keras.models import Sequential, Model
from keras.layers import Dense, Activation, Flatten, Input, Concatenate
from keras.optimizers import Adam
from keras.layers.normalization import BatchNormalization

from rl.agents import DDPGAgent
from rl.memory import SequentialMemory
from rl.random import OrnsteinUhlenbeckProcess
from active_env.envs.twobus_env import TwoBusEnv, PowerEnvOld, PowerEnvOldNormalized
from active_env.envs.active_network_env import ActiveEnv

ENV_NAME = 'Pendulum-v0'

# Get the environment and extract the number of actions.
#powergrid = gym.make(ENV_NAME)
env = ActiveEnv()
env.set_parameters({
    'state_space': ['sun', 'demand', 'imbalance'],
    'voltage_weight': 10,
    'current_weight': 0.1,
    'reward_terms': ['voltage', 'current', 'imbalance']
})
#env = PowerEnvOldNormalized()
#env = PowerEnvStep()

np.random.seed(123)
env.seed(123)

assert len(env.action_space.shape) == 1
nb_actions = env.action_space.shape[0]