intelligent-environments-lab / CityLearn

Official reinforcement learning environment for demand response and load shaping
MIT License
468 stars 171 forks source link

[BUG] env.observation_names does not provide names for the entire observation space #74

Closed KandBM closed 1 year ago

KandBM commented 1 year ago

Issue Description

The list of observations names is smaller than the observation space returned by the environment. My observation space has 31 elements, but I only have the names for 28, and don't know which are unnamed.

Expected Behavior

I expect the environment would provide the name of each observation/feature in the observation space, so there'd be a name for every feature in the observation space.

Actual Behavior

The environment does not provide the name of each observation/feature in the observation space, as there are fewer names than features

Steps to Reproduce

from citylearn.citylearn import CityLearnEnv
from citylearn.wrappers import NormalizedObservationWrapper, StableBaselines3Wrapper, DiscreteActionWrapper
from citylearn.data import DataSet

dataset_name = 'citylearn_challenge_2022_phase_1'
schema = DataSet.get_schema(dataset_name)
env = CityLearnEnv(schema, 
        central_agent=True, 
        buildings='building_1')
env = DiscreteActionWrapper(env)
env = NormalizedObservationWrapper(env)
env = StableBaselines3Wrapper(env)

print(len(env.observation_names)) #28
print(env.observation_space.shape[0])  #31

Environment

Possible Solution

Sorry, no idea

Additional Notes

env.observation_names lists the observations which are active in the schema, but the env lists a larger observation space.

kingsleynweye commented 1 year ago

@KandBM thanks for raising this issue. The extra three observation in env.observation_space.shape[0] are as a result of the NormalizedObservationWrapper wrapper. The hour, day and month observations are transformed to cyclic vectors. So they have sin and cos parts and 28 + three extra observations will give you 31.

So your observation space is correct. I just did not implement an observation_names property in NormalizedObservationWrapper to account for these three extra observations that get added during normalization.

You should be fine to use as-is if the agent you are using doesn't do anything with the observation_names. I will work on updating NormalizedObservationWrapper to return the correct list of observation names but it might not happen soon.

KandBM commented 1 year ago

@kingsleynweye You're on the ball! I'm indeed still able to train and use the agent, but I'm masking part of the observations when producing adversarial examples and don't want to touch the calendar features. So if I've understood you correctly, observations[0:6] are the sin and cos for the calendar features?

kingsleynweye commented 1 year ago

@kingsleynweye You're on the ball! I'm indeed still able to train and use the agent, but I'm masking part of the observations when producing adversarial examples and don't want to touch the calendar features. So if I've understood you correctly, observations[0:6] are the sin and cos for the calendar features?

Yeah! The first six observations are the cos, sin of the three calendar features

kingsleynweye commented 1 year ago

@KandBM I think I fixed the problem in the mentioned pull request. However, I have not extensively tested it beyond using your example. I will hold off on releasing a new version until I test. Should happen sometime mid-September.

I will leave the issue open so that I remember.

kingsleynweye commented 1 year ago

This issue has been fixed since CityLearn==2.0.0