├── README.md └── First code /README.md: -------------------------------------------------------------------------------- 1 | # Energy-Efficient Building Design Recommendations using Deep Reinforcement Learning 2 | 3 | This project leverages **Deep Reinforcement Learning (DRL)** techniques to recommend energy-efficient building designs. The goal is to minimize energy consumption by optimizing building parameters such as **window size**, **insulation thickness**, and **HVAC setpoint**. 4 | 5 | ## Project Overview 6 | 7 | - **Environment**: A simulated building environment that models energy consumption based on design parameters. 8 | - **Agent**: A Deep Q-Network (DQN) is used to learn optimal design strategies. 9 | - **Goal**: Reduce energy consumption and provide actionable design recommendations for sustainable building development. 10 | 11 | ## Project Structure 12 | 13 | ``` 14 | 📁 Energy-Efficient-Building-Design-DRL 15 | ├── building_env.py # Custom Gym environment for building simulation 16 | ├── dqn_agent.py # Deep Q-Network agent 17 | ├── train.py # Training loop 18 | ├── building_design_training_data.xlsx # Training log data 19 | ├── README.md # Project documentation 20 | ├── requirements.txt # Python dependencies 21 | └── .gitignore # Ignored files and folders 22 | ``` 23 | 24 | ## Requirements 25 | 26 | Install all dependencies using: 27 | 28 | ```bash 29 | pip install -r requirements.txt 30 | ``` 31 | 32 | **`requirements.txt` includes:** 33 | 34 | ``` 35 | torch 36 | gym 37 | numpy 38 | pandas 39 | openpyxl 40 | ``` 41 | 42 | ## How to Run 43 | 44 | 1. Clone the repository and navigate into the project directory: 45 | 46 | ```bash 47 | git clone https://github.com/your-username/Energy-Efficient-Building-Design-DRL.git 48 | cd Energy-Efficient-Building-Design-DRL 49 | ``` 50 | 51 | 2. Train the DRL agent: 52 | 53 | ```bash 54 | python train.py 55 | ``` 56 | 57 | 3. After training: 58 | - Evaluation metrics such as **energy consumption** and **rewards** will be printed. 59 | - Training logs will be saved to `building_design_training_data.xlsx`. 60 | 61 | ## Sample Environment Setup 62 | 63 | Each building design consists of: 64 | - **Window Size**: Continuous value between 0 and 1 65 | - **Insulation Thickness**: Continuous value between 0 and 1 66 | - **HVAC Setpoint**: Continuous value between 0 and 1 67 | 68 | The DRL agent learns to recommend optimal settings to minimize the building's energy consumption. 69 | 70 | ## Dataset Example 71 | 72 | ```csv 73 | Episode,Window_Size,Insulation_Thickness,HVAC_Setpoint,Energy_Consumption,Reward 74 | 1,0.3745,0.9507,0.7319,134.58,-72.36 75 | 2,0.5986,0.1560,0.1559,96.45,-88.21 76 | ... 77 | ``` 78 | 79 | ## Output 80 | 81 | - **`building_design_training_data.xlsx`**: Contains the design parameters, energy consumption, and reward for each episode. 82 | - **Trained Model**: Saved as `building_design_dqn.pth`. 83 | 84 | ## Acknowledgements 85 | 86 | - Inspired by energy-efficient building standards and sustainable design principles. 87 | - Future work can integrate advanced simulation tools like **EnergyPlus** or **OpenStudio** for realistic energy modeling. 88 | 89 | ## Author 90 | 91 | **Ayebawanaemi Geraldine Winston** 92 | 93 | -------------------------------------------------------------------------------- /First code: -------------------------------------------------------------------------------- 1 | import gym 2 | import numpy as np 3 | import torch 4 | import torch.nn as nn 5 | import torch.optim as optim 6 | from collections import deque 7 | import random 8 | 9 | # Define a simple custom Environment (Placeholder for building simulation) 10 | class BuildingEnv(gym.Env): 11 | def __init__(self): 12 | super(BuildingEnv, self).__init__() 13 | # Example: Window size [0,1], Insulation thickness [0,1], HVAC setpoint [0,1] 14 | self.observation_space = gym.spaces.Box(low=0, high=1, shape=(3,), dtype=np.float32) 15 | self.action_space = gym.spaces.Box(low=0, high=1, shape=(3,), dtype=np.float32) 16 | 17 | def reset(self): 18 | self.state = np.random.rand(3) 19 | return self.state 20 | 21 | def step(self, action): 22 | # Dummy energy consumption model 23 | energy_consumption = (1.5 - action[0])**2 + (1.2 - action[1])**2 + (0.8 - action[2])**2 24 | reward = -energy_consumption # Minimize consumption = maximize negative reward 25 | done = True # One-step environment 26 | return np.random.rand(3), reward, done, {} 27 | 28 | # Deep Q-Network (can replace with DDPG Actor-Critic for continuous action space) 29 | class DQN(nn.Module): 30 | def __init__(self, state_dim, action_dim): 31 | super(DQN, self).__init__() 32 | self.fc1 = nn.Linear(state_dim, 128) 33 | self.fc2 = nn.Linear(128, 128) 34 | self.fc3 = nn.Linear(128, action_dim) 35 | 36 | def forward(self, x): 37 | x = torch.relu(self.fc1(x)) 38 | x = torch.relu(self.fc2(x)) 39 | return self.fc3(x) 40 | 41 | # Replay Buffer 42 | class ReplayBuffer: 43 | def __init__(self, max_size=10000): 44 | self.buffer = deque(maxlen=max_size) 45 | 46 | def add(self, transition): 47 | self.buffer.append(transition) 48 | 49 | def sample(self, batch_size): 50 | batch = random.sample(self.buffer, batch_size) 51 | state, action, reward, next_state = zip(*batch) 52 | return np.array(state), np.array(action), np.array(reward), np.array(next_state) 53 | 54 | # Hyperparameters 55 | gamma = 0.99 56 | lr = 1e-3 57 | batch_size = 64 58 | episodes = 1000 59 | epsilon = 1.0 60 | epsilon_decay = 0.995 61 | epsilon_min = 0.01 62 | 63 | # Initialize 64 | env = BuildingEnv() 65 | state_dim = env.observation_space.shape[0] 66 | action_dim = env.action_space.shape[0] 67 | device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 68 | 69 | q_network = DQN(state_dim, action_dim).to(device) 70 | target_network = DQN(state_dim, action_dim).to(device) 71 | target_network.load_state_dict(q_network.state_dict()) 72 | 73 | optimizer = optim.Adam(q_network.parameters(), lr=lr) 74 | replay_buffer = ReplayBuffer() 75 | 76 | # Training Loop 77 | for episode in range(episodes): 78 | state = env.reset() 79 | done = False 80 | episode_reward = 0 81 | 82 | while not done: 83 | state_tensor = torch.FloatTensor(state).unsqueeze(0).to(device) 84 | if np.random.rand() < epsilon: 85 | action = np.random.uniform(0, 1, size=action_dim) 86 | else: 87 | with torch.no_grad(): 88 | q_values = q_network(state_tensor) 89 | action = q_values.cpu().numpy()[0] 90 | 91 | next_state, reward, done, _ = env.step(action) 92 | replay_buffer.add((state, action, reward, next_state)) 93 | state = next_state 94 | episode_reward += reward 95 | 96 | if len(replay_buffer.buffer) > batch_size: 97 | states, actions, rewards, next_states = replay_buffer.sample(batch_size) 98 | 99 | states = torch.FloatTensor(states).to(device) 100 | actions = torch.FloatTensor(actions).to(device) 101 | rewards = torch.FloatTensor(rewards).unsqueeze(1).to(device) 102 | next_states = torch.FloatTensor(next_states).to(device) 103 | 104 | current_q = q_network(states) 105 | next_q = target_network(next_states).detach() 106 | max_next_q = next_q.max(1)[0].unsqueeze(1) 107 | target_q = rewards + gamma * max_next_q 108 | 109 | loss = nn.MSELoss()(current_q, target_q) 110 | 111 | optimizer.zero_grad() 112 | loss.backward() 113 | optimizer.step() 114 | 115 | epsilon = max(epsilon * epsilon_decay, epsilon_min) 116 | 117 | if episode % 10 == 0: 118 | target_network.load_state_dict(q_network.state_dict()) 119 | print(f"Episode {episode}, Reward: {episode_reward:.2f}, Epsilon: {epsilon:.3f}") 120 | 121 | print("Training Completed!") 122 | 123 | # Save the model 124 | torch.save(q_network.state_dict(), "building_design_dqn.pth") 125 | --------------------------------------------------------------------------------