def create_env(building_uids, **kwargs): data_folder = Path("data/") demand_file = data_folder / "AustinResidential_TH.csv" weather_file = data_folder / 'Austin_Airp_TX-hour.csv' max_action_val = kwargs["max_action_val"] min_action_val = kwargs["min_action_val"] target_cooling = kwargs["target_cooling"] heat_pump, heat_tank, cooling_tank = {}, {}, {} loss_coeff, efficiency = 0.19 / 24, 1. # Ref: Assessment of energy efficiency in electric storage water heaters (2008 Energy and Buildings) buildings = [] for uid in building_uids: heat_pump[uid] = HeatPump(nominal_power=9e12, eta_tech=0.22, t_target_heating=45, t_target_cooling=target_cooling) heat_tank[uid] = EnergyStorage(capacity=9e12, loss_coeff=loss_coeff) cooling_tank[uid] = EnergyStorage(capacity=9e12, loss_coeff=loss_coeff) buildings.append( Building(uid, heating_storage=heat_tank[uid], cooling_storage=cooling_tank[uid], heating_device=heat_pump[uid], cooling_device=heat_pump[uid], sub_building_uids=[uid])) buildings[-1].state_space(np.array([24.0, 40.0, 1.001]), np.array([1.0, 17.0, -0.001])) buildings[-1].action_space(np.array([max_action_val]), np.array([min_action_val])) building_loader(demand_file, weather_file, buildings) auto_size(buildings, t_target_heating=45, t_target_cooling=target_cooling) env = CityLearn(demand_file, weather_file, buildings=buildings, time_resolution=1, simulation_period=(kwargs["start_time"] - 1, kwargs["end_time"])) return env, buildings, heat_pump, heat_tank, cooling_tank
weather_file = data_path / 'weather_data.csv' solar_profile = data_path / 'solar_generation_1kW.csv' building_state_actions = 'buildings_state_action_space.json' building_ids = [ "Building_1", "Building_2", "Building_3", "Building_4", "Building_5", "Building_6", "Building_7", "Building_8", "Building_9" ] objective_function = [ 'ramping', '1-load_factor', 'average_daily_peak', 'peak_demand', 'net_electricity_consumption' ] env = CityLearn(data_path, building_attributes, weather_file, solar_profile, building_ids, buildings_states_actions=building_state_actions, cost_function=objective_function) observations_spaces, actions_spaces = env.get_state_action_spaces() # Provides information on Building type, Climate Zone, Annual DHW demand, Annual Cooling Demand, Annual Electricity Demand, Solar Capacity, and correllations among buildings building_info = env.get_building_information() # RL CONTROLLER #Instantiating the control agent(s) agents = Agent(env, building_info, observations_spaces, actions_spaces) # Select many episodes for training. In the final run we will set this value to 1 (the buildings run for one year) episodes = 10
cooling_storage=cooling_tank[uid], heating_device=heat_pump[uid], cooling_device=heat_pump[uid]) buildings[uid].state_action_space(np.array([24.0, 40.0, 1.001]), np.array([1.0, 17.0, -0.001]), np.array([0.5]), np.array([-0.5])) building_loader(demand_file, weather_file, buildings) auto_size(buildings, t_target_heating=45, t_target_cooling=10) env = {} for uid in building_ids: env[uid] = CityLearn(demand_file, weather_file, buildings={uid: buildings[uid]}, time_resolution=1, simulation_period=(3500, 6000)) env[uid](uid) if __name__ == "__main__": N_AGENTS = 2 GAMMA = 0.99 BATCH_SIZE = 5000 LEARNING_RATE_ACTOR = 1e-4 LEARNING_RATE_CRITIC = 1e-3 REPLAY_SIZE = 5000 REPLAY_INITIAL = 100 TEST_ITERS = 120 EPSILON_DECAY_LAST_FRAME = 1000 EPSILON_START = 1.2
data_path = Path("data/Climate_Zone_" + str(climate_zone)) building_attributes = data_path / 'building_attributes.json' weather_file = data_path / 'weather_data.csv' solar_profile = data_path / 'solar_generation_1kW.csv' building_state_actions = 'buildings_state_action_space.json' #building_ids = ["Building_1","Building_2","Building_3","Building_4","Building_5","Building_6","Building_7","Building_8","Building_9"] building_ids = ["Building_1"] objective_function = [ 'ramping', '1-load_factor', 'average_daily_peak', 'peak_demand', 'net_electricity_consumption' ] env = CityLearn(data_path, building_attributes, weather_file, solar_profile, building_ids, buildings_states_actions=building_state_actions, cost_function=objective_function, central_agent=True, verbose=1) # Contain the lower and upper bounds of the states and actions, to be provided to the agent to normalize the variables between 0 and 1. # Can be obtained using observations_spaces[i].low or .high observations_spaces, actions_spaces = env.get_state_action_spaces() # Provides information on Building type, Climate Zone, Annual DHW demand, Annual Cooling Demand, Annual Electricity Demand, Solar Capacity, and correllations among buildings building_info = env.get_building_information() # In[ ]: tf.compat.v1.reset_default_graph()
# Load environment climate_zone = 1 data_path = Path("data/Climate_Zone_" + str(climate_zone)) building_attributes = data_path / 'building_attributes.json' weather_file = data_path / 'weather_data.csv' solar_profile = data_path / 'solar_generation_1kW.csv' #building_state_actions = 'buildings_state_action_space_full.json' building_state_actions = 'buildings_state_action_space_central_full.json' building_id = ["Building_1","Building_2","Building_3","Building_4","Building_5","Building_6","Building_7","Building_8","Building_9"] #building_id = ["Building_1","Building_2"] objective_function = ['ramping', '1-load_factor', 'average_daily_peak', 'peak_demand', 'net_electricity_consumption'] # Contain the lower and upper bounds of the states and actions, to be provided to the agent to normalize the variables between 0 and 1. # Can be obtained using observations_spaces[i].low or .high env = CityLearn(data_path, building_attributes, weather_file, solar_profile, building_id, buildings_states_actions=building_state_actions, cost_function=objective_function, verbose=0, central_agent=True, simulation_period=(0, 8760 - 1)) #observations_spaces, actions_spaces = env.get_state_action_spaces() observations_spaces = env.observation_space actions_spaces = env.action_space torch.manual_seed(args.seed) random.seed(args.seed) np.random.seed(args.seed) env.seed(args.seed) # Provides information on Building type, Climate Zone, Annual DHW demand, Annual Cooling Demand, Annual Electricity Demand, Solar Capacity, and correllations among buildings building_info = env.get_building_information() #print(building_info)
data_path = Path("data/Climate_Zone_" + str(climate_zone)) building_attributes = data_path / 'building_attributes.json' weather_file = data_path / 'weather_data.csv' solar_profile = data_path / 'solar_generation_1kW.csv' building_state_actions = 'buildings_state_action_space.json' # building_ids = ['Building_1',"Building_2","Building_3","Building_4","Building_5","Building_6","Building_7","Building_8","Building_9"] building_ids = ['Building_1', 'Building_2'] objective_function = [ 'ramping', '1-load_factor', 'average_daily_peak', 'peak_demand', 'net_electricity_consumption' ] env = CityLearn(data_path, building_attributes, weather_file, solar_profile, building_ids, buildings_states_actions=building_state_actions, cost_function=objective_function, central_agent=True, verbose=1) # Store the weights and scores in a new directory parent_dir = "alg/ddpg_{}/".format( time.strftime("%Y%m%d-%H%M%S")) # apprends the timedate os.makedirs(parent_dir, exist_ok=True) # Create log dir log_dir = parent_dir + "monitor" os.makedirs(log_dir, exist_ok=True) # Set the interval and their count
data_path = Path("data/Climate_Zone_" + str(climate_zone)) building_attributes = data_path / 'building_attributes.json' weather_file = data_path / 'weather_data.csv' solar_profile = data_path / 'solar_generation_1kW.csv' building_state_actions = 'buildings_state_action_space.json' #building_ids = ["Building_1","Building_2","Building_3","Building_4","Building_5","Building_6","Building_7","Building_8","Building_9"] building_ids = ["Building_1"] objective_function = [ 'ramping', '1-load_factor', 'average_daily_peak', 'peak_demand', 'net_electricity_consumption' ] env = CityLearn(data_path, building_attributes, weather_file, solar_profile, building_ids, buildings_states_actions=building_state_actions, cost_function=objective_function, central_agent=False, verbose=0) # Contain the lower and upper bounds of the states and actions, to be provided to the agent to normalize the variables between 0 and 1. # Can be obtained using observations_spaces[i].low or .high observations_spaces, actions_spaces = env.get_state_action_spaces() # Provides information on Building type, Climate Zone, Annual DHW demand, Annual Cooling Demand, Annual Electricity Demand, Solar Capacity, and correllations among buildings building_info = env.get_building_information() # In[ ]: """ ###################################
["Building_" + str(i) for i in [1, 2, 3, 4, 5, 6, 7, 8, 9]], 'buildings_states_actions': 'buildings_state_action_space.json', 'simulation_period': (0, 8760 * 4 - 1), 'cost_function': [ 'ramping', '1-load_factor', 'average_daily_peak', 'peak_demand', 'net_electricity_consumption', 'carbon_emissions' ], 'central_agent': False, 'save_memory': False } # Contain the lower and upper bounds of the states and actions, to be provided to the agent to normalize the variables between 0 and 1. env = CityLearn(**params) observations_spaces, actions_spaces = env.get_state_action_spaces() # Provides information on Building type, Climate Zone, Annual DHW demand, Annual Cooling Demand, Annual Electricity Demand, Solar Capacity, and correllations among buildings building_info = env.get_building_information() params_agent = { 'building_ids': ["Building_" + str(i) for i in [1, 2, 3, 4, 5, 6, 7, 8, 9]], 'buildings_states_actions': 'buildings_state_action_space.json', 'building_info': building_info, 'observation_spaces': observations_spaces, 'action_spaces': actions_spaces } # Instantiating the control agent(s)
import numpy as np # Load environment data_folder = Path("data/") building_attributes = data_folder / 'building_attributes.json' solar_profile = data_folder / 'solar_generation_1kW.csv' building_state_actions = 'buildings_state_action_space.json' building_ids = [ "Building_1", "Building_2", "Building_3", "Building_4", "Building_5", "Building_6", "Building_7", "Building_8", "Building_9" ] env = CityLearn(building_attributes, solar_profile, building_ids, buildings_states_actions=building_state_actions, cost_function=[ 'ramping', '1-load_factor', 'peak_to_valley_ratio', 'peak_demand', 'net_electricity_consumption', 'quadratic' ]) observations_spaces, actions_spaces = env.get_state_action_spaces() # Provides information on Building type, Climate Zone, Annual DHW demand, Annual Cooling Demand, Annual Electricity Demand, Solar Capacity, and correllations among buildings building_info = env.get_building_information() # RL CONTROLLER #Instantiating the control agent(s) agents = RL_Agents(building_info, observations_spaces, actions_spaces) episodes = 300 k, c = 0, 0 cost, cum_reward = {}, {}
def run(config): data_folder = Path(config.data_path) building_attributes = data_folder / 'building_attributes.json' solar_profile = data_folder / 'solar_generation_1kW.csv' building_state_actions = 'buildings_state_action_space.json' # building_ids = ["Building_" + str(i) for i in range(1, config.num_buildings + 1)] config.num_buildings = 6 # customized log directory hidden = config.hidden_dim lr = config.lr tau = config.tau gamma = config.gamma batch_size = config.batch_size buffer_length = config.buffer_length to_print = lambda x: str(x) log_path = "log"+"_hidden"+to_print(hidden)+"_lr"+to_print(lr)+"_tau"+to_print(tau)+"_gamma"+to_print(gamma)+\ "_batch_size"+to_print(batch_size)+"_buffer_length"+to_print(buffer_length)+"_TIME_PERIOD_1008_MAXACTION_25"+"/" logger = SummaryWriter(log_dir=log_path) # TODO fix here building_ids = ["Building_" + str(i) for i in [1, 2, 5, 6, 7, 8]] #[1,2,5,6,7,8] env = CityLearn(building_attributes, solar_profile, building_ids, buildings_states_actions=building_state_actions, cost_function=[ 'ramping', '1-load_factor', 'peak_to_valley_ratio', 'peak_demand', 'net_electricity_consumption' ]) observations_spaces, actions_spaces = env.get_state_action_spaces() # Instantiating the control agent(s) if config.agent_alg == 'MADDPG': agents = MA_DDPG(observations_spaces, actions_spaces, hyper_params=vars(config)) else: raise NotImplementedError k, c = 0, 0 cost, cum_reward = {}, {} buffer = ReplayBuffer(max_steps=config.buffer_length, num_agents=config.num_buildings, obs_dims=[s.shape[0] for s in observations_spaces], ac_dims=[a.shape[0] for a in actions_spaces]) # TODO: store np or tensor in buffer? start = time.time() for e in range(config.n_episodes): cum_reward[e] = 0 rewards = [] state = env.reset() statecast = lambda x: [torch.FloatTensor(s) for s in x] done = False ss = 0 while not done: if k % (40000 * 4) == 0: print('hour: ' + str(k) + ' of ' + str(TIME_PERIOD * config.n_episodes)) action = agents.select_action(statecast(state), explore=False) action = [a.detach().numpy() for a in action] # if batch norm: action = [np.squeeze(a, axis=0) for a in action] ss += 1 #print("action is ", action) #print(action[0].shape) #raise NotImplementedError next_state, reward, done, _ = env.step(action) reward = reward_function( reward) # See comments in reward_function.py #buffer_reward = [-r for r in reward] # agents.add_to_buffer() buffer.push(statecast(state), action, reward, statecast(next_state), done) # if (len(buffer) >= config.batch_size and # (e % config.steps_per_update) < config.n_rollout_threads): if len(buffer) >= config.batch_size: if USE_CUDA: agents.to_train(device='gpu') else: agents.to_train(device='cpu') for a_i in range(agents.n_buildings): sample = buffer.sample(config.batch_size, to_gpu=USE_CUDA) agents.update(sample, a_i, logger=logger, global_step=e * TIME_PERIOD + ss) logger.add_scalar(tag='net electric consumption', scalar_value=env.net_electric_consumption[-1], global_step=e * TIME_PERIOD + ss) logger.add_scalar(tag='env cost total', scalar_value=env.cost()['total'], global_step=e * TIME_PERIOD + ss) logger.add_scalar(tag="1 load factor", scalar_value=env.cost()['1-load_factor'], global_step=e * TIME_PERIOD + ss) logger.add_scalar(tag="peak to valley ratio", scalar_value=env.cost()['peak_to_valley_ratio'], global_step=e * TIME_PERIOD + ss) logger.add_scalar(tag="peak demand", scalar_value=env.cost()['peak_demand'], global_step=e * TIME_PERIOD + ss) logger.add_scalar( tag="net energy consumption", scalar_value=env.cost()['net_electricity_consumption'], global_step=e * TIME_PERIOD + ss) net_energy_consumption_wo_storage = env.net_electric_consumption[ -1] + env.electric_generation[ -1] - env.electric_consumption_cooling_storage[ -1] - env.electric_consumption_dhw_storage[-1] logger.add_scalar(tag="net energy consumption without storage", scalar_value=net_energy_consumption_wo_storage, global_step=e * TIME_PERIOD + ss) for id, r in enumerate(reward): logger.add_scalar(tag="agent {} reward ".format(id), scalar_value=r, global_step=e * TIME_PERIOD + ss) state = next_state cum_reward[e] += reward[0] k += 1 cur_time = time.time() # print("average time : {}s/iteration at iteration {}".format((cur_time - start) / (60.0 * k), k)) cost[e] = env.cost() if c % 1 == 0: print(cost[e]) # add env total cost and reward logger logger.add_scalar(tag='env cost total final', scalar_value=env.cost()['total'], global_step=e) logger.add_scalar(tag="1 load factor final", scalar_value=env.cost()['1-load_factor'], global_step=e) logger.add_scalar(tag="peak to valley ratio final", scalar_value=env.cost()['peak_to_valley_ratio'], global_step=e) logger.add_scalar(tag="peak demand final", scalar_value=env.cost()['peak_demand'], global_step=e) logger.add_scalar( tag="net energy consumption final", scalar_value=env.cost()['net_electricity_consumption'], global_step=e) net_energy_consumption_wo_storage = env.net_electric_consumption[ -1] + env.electric_generation[ -1] - env.electric_consumption_cooling_storage[ -1] - env.electric_consumption_dhw_storage[-1] logger.add_scalar(tag="net energy consumption without storage", scalar_value=net_energy_consumption_wo_storage, global_step=e) c += 1 rewards.append(reward) end = time.time() print((end - start) / 60.0)
building_id = [ "Building_1", "Building_2", "Building_3", "Building_4", "Building_5", "Building_6", "Building_7", "Building_8", "Building_9" ] objective_function = [ 'ramping', '1-load_factor', 'average_daily_peak', 'peak_demand', 'net_electricity_consumption' ] # Contain the lower and upper bounds of the states and actions, to be provided to the agent to normalize the variables between 0 and 1. # Can be obtained using observations_spaces[i].low or .high env = CityLearn(data_path, building_attributes, weather_file, solar_profile, building_id, buildings_states_actions=building_state_actions, cost_function=objective_function, verbose=0, simulation_period=(0, 8760 - 1)) observations_spaces, actions_spaces = env.get_state_action_spaces() # Provides information on Building type, Climate Zone, Annual DHW demand, Annual Cooling Demand, Annual Electricity Demand, Solar Capacity, and correllations among buildings building_info = env.get_building_information() # Hyperparameters bs = 256 tau = 0.005 gamma = 0.99 lr = 0.0003 hid = [128, 128]
weather_file = data_path / 'weather_data.csv' solar_profile = data_path / 'solar_generation_1kW.csv' building_state_actions = 'buildings_state_action_space.json' building_ids = [ 'Building_1', "Building_2", "Building_3", "Building_4", "Building_5", "Building_6", "Building_7", "Building_8", "Building_9" ] objective_function = [ 'ramping', '1-load_factor', 'average_daily_peak', 'peak_demand', 'net_electricity_consumption' ] env = CityLearn(data_path, building_attributes, weather_file, solar_profile, building_ids, buildings_states_actions=building_state_actions, cost_function=objective_function, simulation_period=(3624, 5832), central_agent=False, verbose=0) RBC_env = CityLearn(data_path, building_attributes, weather_file, solar_profile, building_ids, buildings_states_actions=building_state_actions, cost_function=objective_function, simulation_period=(3624, 5832), central_agent=False, normalise=True, verbose=0)
from torch.utils.tensorboard import writer # Ignore the float32 bound precision warning warnings.simplefilter("ignore", UserWarning) # Central agent controlling the buildings using the OpenAI Stable Baselines climate_zone = 1 data_path = Path("data/Climate_Zone_"+str(climate_zone)) building_attributes = data_path / 'building_attributes.json' weather_file = data_path / 'weather_data.csv' solar_profile = data_path / 'solar_generation_1kW.csv' building_state_actions = 'buildings_state_action_space.json' building_ids = ['Building_1',"Building_2","Building_3","Building_4","Building_5","Building_6","Building_7","Building_8","Building_9"] # building_ids = ['Building_1', 'Building_2'] objective_function = ['ramping','1-load_factor','average_daily_peak','peak_demand','net_electricity_consumption'] env = CityLearn(data_path, building_attributes, weather_file, solar_profile, building_ids, buildings_states_actions = building_state_actions, cost_function = objective_function, central_agent = True, verbose = 1) # Store the weights and scores in a new directory parent_dir = "alg/sac_{}/".format(time.strftime("%Y%m%d-%H%M%S")) # apprends the timedate os.makedirs(parent_dir, exist_ok=True) # Create log dir log_dir = parent_dir+"monitor" os.makedirs(log_dir, exist_ok=True) # Set the interval and their count interval = 8760 icount = int(sys.argv[1]) if len(sys.argv) > 1 else 10 log_interval = 1 check_interval = 1
# Autoencoder parameters NEW_STATE_SIZE = 8 AE_EPOCHS = 5000 # Environment # Central agent controlling the buildings using the OpenAI Stable Baselines climate_zone = 1 data_path = Path("data/Climate_Zone_"+str(climate_zone)) building_attributes = data_path / 'building_attributes.json' weather_file = data_path / 'weather_data.csv' solar_profile = data_path / 'solar_generation_1kW.csv' building_state_actions = 'buildings_state_action_space.json' building_ids = ['Building_1',"Building_2","Building_3","Building_4","Building_5","Building_6","Building_7","Building_8","Building_9"] # building_ids = ['Building_3'] objective_function = ['ramping','1-load_factor','average_daily_peak','peak_demand','net_electricity_consumption'] env = CityLearn(data_path, building_attributes, weather_file, solar_profile, building_ids, buildings_states_actions = building_state_actions, cost_function = objective_function, central_agent = True, verbose = 0) RBC_env = CityLearn(data_path, building_attributes, weather_file, solar_profile, building_ids, buildings_states_actions = building_state_actions, cost_function = objective_function, central_agent = False, verbose = 0) # Contain the lower and upper bounds of the states and actions, to be provided to the agent to normalize the variables between 0 and 1. # Can be obtained using observations_spaces[i].low or .high observations_spaces, actions_spaces = env.get_state_action_spaces() observations_spacesRBC, actions_spacesRBC = RBC_env.get_state_action_spaces() # Provides information on Building type, Climate Zone, Annual DHW demand, Annual Cooling Demand, Annual Electricity Demand, Solar Capacity, and correllations among buildings building_info = env.get_building_information() """ ############################################# STEP 2: Determine the size of the Action and State Spaces and the Number of Agents The observation space consists of various variables corresponding to the
building_attributes = data_path / 'building_attributes.json' weather_file = data_path / 'weather_data.csv' solar_profile = data_path / 'solar_generation_1kW.csv' building_state_actions = 'buildings_state_action_space.json' building_ids = [ "Building_1", "Building_2", "Building_3", "Building_4", "Building_5", "Building_6", "Building_7", "Building_8", "Building_9" ] objective_function = [ 'ramping', '1-load_factor', 'average_daily_peak', 'peak_demand', 'net_electricity_consumption' ] env = CityLearn(data_path, building_attributes, weather_file, solar_profile, building_ids, buildings_states_actions=building_state_actions, cost_function=objective_function, central_agent=True) # Contain the lower and upper bounds of the states and actions, to be provided to the agent to normalize the variables between 0 and 1. # Can be obtained using observations_spaces[i].low or .high observations_spaces, actions_spaces = env.get_state_action_spaces() # Provides information on Building type, Climate Zone, Annual DHW demand, Annual Cooling Demand, Annual Electricity Demand, Solar Capacity, and correllations among buildings building_info = env.get_building_information() # Store the weights and scores in a new directory parent_dir = "alg/sac_{}/".format( time.strftime("%Y%m%d-%H%M%S")) # apprends the timedate os.makedirs(parent_dir, exist_ok=True)