Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discrete Env unittests #181

Merged
merged 3 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions tests/control/test_mpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_run_with_load_pv_battery_grid(self):
pv = RenewableModule(time_series=pv_const*np.ones(100))
load = LoadModule(time_series=load_const*np.ones(100))

microgrid = get_modular_microgrid(remove_modules=["pv", "load", "genset"], additional_modules=[pv, load])
microgrid = get_modular_microgrid(remove_modules=["renewable", "load", "genset"], additional_modules=[pv, load])

mpc = ModelPredictiveControl(microgrid)
mpc_output = mpc.run_mpc_on_microgrid(forecast_steps=forecast_steps)
Expand All @@ -42,7 +42,7 @@ def test_run_with_load_pv_battery_genset(self):
pv = RenewableModule(time_series=pv_const*np.ones(100))
load = LoadModule(time_series=load_const*np.ones(100))

microgrid = get_modular_microgrid(remove_modules=["pv", "load", "grid"], additional_modules=[pv, load])
microgrid = get_modular_microgrid(remove_modules=["renewable", "load", "grid"], additional_modules=[pv, load])

mpc = ModelPredictiveControl(microgrid)
mpc_output = mpc.run_mpc_on_microgrid(forecast_steps=forecast_steps)
Expand All @@ -62,7 +62,7 @@ def test_run_twice_with_load_pv_battery_genset(self):
pv = RenewableModule(time_series=pv_const*np.ones(100))
load = LoadModule(time_series=load_const*np.ones(100))

microgrid = get_modular_microgrid(remove_modules=["pv", "load", "grid"], additional_modules=[pv, load])
microgrid = get_modular_microgrid(remove_modules=["renewable", "load", "grid"], additional_modules=[pv, load])

mpc = ModelPredictiveControl(microgrid)
mpc_output = mpc.run_mpc_on_microgrid(forecast_steps=forecast_steps)
Expand All @@ -88,7 +88,7 @@ def test_run_with_load_pv_battery_grid_different_names(self):
pv = RenewableModule(time_series=pv_const*np.ones(100))
load = LoadModule(time_series=load_const*np.ones(100))

microgrid = get_modular_microgrid(remove_modules=["pv", "load", "genset"],
microgrid = get_modular_microgrid(remove_modules=["renewable", "load", "genset"],
additional_modules=[("pv_with_name", pv), ("load_with_name", load)])

mpc = ModelPredictiveControl(microgrid)
Expand Down
111 changes: 101 additions & 10 deletions tests/envs/discrete.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,125 @@
import numpy as np

from copy import deepcopy

from tests.helpers.test_case import TestCase
from tests.helpers.modular_microgrid import get_modular_microgrid

from pymgrid.envs import DiscreteMicrogridEnv
from pymgrid.envs.base import BaseMicrogridEnv
from pymgrid.algos.priority_list import PriorityListAlgo, PriorityListElement
from pymgrid.modules import BatteryModule, GensetModule


class TestDiscreteEnv(TestCase):

def test_init_from_microgrid(self):
microgrid = get_modular_microgrid()
env = DiscreteMicrogridEnv(microgrid)
def _check_env(self, env, source_microgrid):
self.assertIsInstance(env, PriorityListAlgo)
self.assertIsInstance(env, BaseMicrogridEnv)

self.assertEqual(env.modules, microgrid.modules)
self.assertIsNot(env.modules.module_tuples(), microgrid.modules.module_tuples())
self.assertEqual(env.modules, source_microgrid.modules)
self.assertIsNot(env.modules.module_tuples(), source_microgrid.modules.module_tuples())

n_obs = sum([x.observation_spaces['normalized'].shape[0] for x in microgrid.module_list])
n_obs = sum([x.observation_spaces['normalized'].shape[0] for x in source_microgrid.module_list])

self.assertEqual(env.observation_space.shape, (n_obs,))

def test_init_from_microgrid(self):
microgrid = get_modular_microgrid()
env = DiscreteMicrogridEnv(microgrid)

self._check_env(env, microgrid)

def test_init_from_modules(self):
microgrid = get_modular_microgrid()
env = DiscreteMicrogridEnv(microgrid.modules.module_tuples(), add_unbalanced_module=False)

self.assertEqual(env.modules, microgrid.modules)
self.assertIsNot(env.modules.module_tuples(), microgrid.modules.module_tuples())
self._check_env(env, microgrid)

n_obs = sum([x.observation_spaces['normalized'].shape[0] for x in microgrid.module_list])
def test_populate_action_battery_grid(self, battery_first=True):
battery = BatteryModule(min_capacity=0,
max_capacity=60,
max_charge=30,
max_discharge=60,
efficiency=1.0,
init_soc=1)

self.assertEqual(env.observation_space.shape, (n_obs,))
microgrid = get_modular_microgrid(retain_only=('load', 'grid'), additional_modules=[battery])
env = DiscreteMicrogridEnv.from_microgrid(microgrid)

battery_element = PriorityListElement(module=('battery', 0), module_actions=1, action=0)
grid_element = PriorityListElement(module=('grid', 0), module_actions=1, action=0)

if battery_first:
priority_list = (battery_element, grid_element)
battery_val, grid_val = 60.0, 0.0
else:
priority_list = (grid_element, battery_element)
grid_val, battery_val = 60.0, 0.0

action = env._populate_action(priority_list)

self.assertEqual(action['battery'], [battery_val])
self.assertEqual(action['grid'], [grid_val])

def test_populate_action_grid_battery(self):
self.test_populate_action_battery_grid(battery_first=False)

def test_populate_action_battery_genset_0_1(self, order=('battery', 'genset_0', 'genset_1')):
battery = BatteryModule(min_capacity=0,
max_capacity=60,
max_charge=30,
max_discharge=60,
efficiency=1.0,
init_soc=1)

genset = GensetModule(running_min_production=10, running_max_production=60, genset_cost=0.5)

microgrid = get_modular_microgrid(retain_only=('load',), additional_modules=[battery, genset])
env = DiscreteMicrogridEnv.from_microgrid(microgrid)

elements = {
'battery': PriorityListElement(module=('battery', 0), module_actions=1, action=0),
'genset_0': PriorityListElement(module=('genset', 0), module_actions=2, action=0),
'genset_1': PriorityListElement(module=('genset', 0), module_actions=2, action=1)
}

priority_list = [elements[element] for element in order]

"""
battery_val should be 0 if genset_1 is ahead of battery AND genset_1 is ahead of genset_0"""
if order.index('genset_1') < order.index('battery') and order.index('genset_1') < order.index('genset_0'):
battery_val = 0.0
else:
battery_val = 60.0

genset_val = np.array([
int(order.index('genset_1') < order.index('genset_0')),
60.0-battery_val
])

expected_action = {'battery': [battery_val],
'genset': [genset_val]}

action = env._populate_action(priority_list)

self.assertEqual(action, expected_action)


def test_populate_action_battery_genset_1_0(self):
return self.test_populate_action_battery_genset_0_1(('battery', 'genset_1', 'genset_0'))

def test_populate_action_genset_1_0_battery(self):
return self.test_populate_action_battery_genset_0_1(('genset_1', 'battery', 'genset_0'))

def test_populate_action_genset_0_1_battery(self):
return self.test_populate_action_battery_genset_0_1(('genset_0', 'genset_1', 'battery'))

def test_populate_action_genset_0_battery_genset_1(self):
return self.test_populate_action_battery_genset_0_1(('genset_0', 'battery', 'genset_1'))

def test_populate_action_genset_1_battery_genset_0(self):
return self.test_populate_action_battery_genset_0_1(('genset_1', 'battery', 'genset_0'))


class TestDiscreteEnvScenario(TestCase):
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers/modular_microgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def get_modular_microgrid(remove_modules=(), retain_only=None, additional_module
efficiency=1.0,
init_soc=0.5),

pv=RenewableModule(time_series=50*np.ones(100)),
renewable=RenewableModule(time_series=50*np.ones(100)),

load=LoadModule(time_series=60*np.ones(100)),

Expand Down