refactor models
This commit is contained in:
parent
425477fcab
commit
d0ea09d663
|
@ -1,9 +1,10 @@
|
||||||
import gym
|
import gym
|
||||||
|
import numpy as np
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
|
|
||||||
from data_manager import *
|
from models.data_manager import *
|
||||||
from module import *
|
from models.module import *
|
||||||
from parameters import *
|
from models.parameters import *
|
||||||
|
|
||||||
|
|
||||||
class WgzGym(gym.Env):
|
class WgzGym(gym.Env):
|
||||||
|
@ -29,18 +30,18 @@ class WgzGym(gym.Env):
|
||||||
self.heat_a = 0.6
|
self.heat_a = 0.6
|
||||||
self.power_a = 0.4
|
self.power_a = 0.4
|
||||||
self.EC_parameters = kwargs.get('EC_parameters', EC_parameters) # 电解水制氢器
|
self.EC_parameters = kwargs.get('EC_parameters', EC_parameters) # 电解水制氢器
|
||||||
self.HST_parameters = kwargs.get('dg_parameters', dg_parameters) # 储氢罐
|
self.HST_parameters = kwargs.get('HST_parameters', HST_parameters) # 储氢罐
|
||||||
|
|
||||||
self.grid = Grid()
|
self.grid = Grid()
|
||||||
self.EC = EC(self.EC_parameters)
|
self.EC = EC(self.EC_parameters)
|
||||||
self.HST = HST(self.HST_parameters)
|
self.HST = HST(self.HST_parameters)
|
||||||
|
|
||||||
self.action_space = gym.spaces.Box(low=-1, high=1, shape=(3,), dtype=np.float32)
|
self.action_space = gym.spaces.Box(low=-1, high=1, shape=(2,), dtype=np.float32)
|
||||||
'''
|
'''
|
||||||
时间 光伏 温度(湿度暂未考虑) 电需 热需(转化为对应热水所需瓦数) 人数 电价 7
|
时间 光伏 温度(湿度暂未考虑) 电需 热需(转化为对应热水所需瓦数) 人数 电价 7
|
||||||
电解水制氢功率 储氢罐容量占比 市电功率(注意标准化) 3
|
电解水制氢功率 储氢罐容量占比 2 市电功率(注意标准化)->舍(由供需控制)
|
||||||
'''
|
'''
|
||||||
self.state_space = gym.spaces.Box(low=0, high=1, shape=(10,), dtype=np.float32)
|
self.state_space = gym.spaces.Box(low=0, high=1, shape=(9,), dtype=np.float32)
|
||||||
|
|
||||||
def reset(self, *args):
|
def reset(self, *args):
|
||||||
self.month = np.random.randint(1, 13) # choose 12 month
|
self.month = np.random.randint(1, 13) # choose 12 month
|
||||||
|
@ -56,12 +57,12 @@ class WgzGym(gym.Env):
|
||||||
def _build_state(self):
|
def _build_state(self):
|
||||||
hst_soc = self.HST.current_soc
|
hst_soc = self.HST.current_soc
|
||||||
ec_out = self.EC.get_hydrogen()
|
ec_out = self.EC.get_hydrogen()
|
||||||
grid_ex = self.grid
|
grid_ex = self.grid.trade_energy
|
||||||
time_step = self.current_time
|
time_step = self.current_time
|
||||||
|
|
||||||
if self.TRAIN:
|
if self.TRAIN:
|
||||||
price = self.data_manager.get_price_data(self.month, self.day, self.current_time)
|
price = self.data_manager.get_price_data(self.month, self.day, self.current_time)
|
||||||
temper = self.data_manager.get_temperature_data(self.month, self.day, self.current_time)
|
temper = self.data_manager.get_temper_data(self.month, self.day, self.current_time)
|
||||||
solar = self.data_manager.get_solar_data(self.month, self.day, self.current_time)
|
solar = self.data_manager.get_solar_data(self.month, self.day, self.current_time)
|
||||||
load = self.data_manager.get_load_data(self.month, self.day, self.current_time)
|
load = self.data_manager.get_load_data(self.month, self.day, self.current_time)
|
||||||
heat = self.data_manager.get_heat_data(self.month, self.day, self.current_time)
|
heat = self.data_manager.get_heat_data(self.month, self.day, self.current_time)
|
||||||
|
@ -76,8 +77,7 @@ class WgzGym(gym.Env):
|
||||||
|
|
||||||
obs = np.concatenate((np.float32(time_step), np.float32(price), np.float32(temper),
|
obs = np.concatenate((np.float32(time_step), np.float32(price), np.float32(temper),
|
||||||
np.float32(solar), np.float32(load), np.float32(heat),
|
np.float32(solar), np.float32(load), np.float32(heat),
|
||||||
np.float32(people), np.float32(ec_out), np.float32(hst_soc), np.float32(grid_ex)),
|
np.float32(people), np.float32(ec_out), np.float32(hst_soc)), axis=None)
|
||||||
axis=None)
|
|
||||||
return obs
|
return obs
|
||||||
|
|
||||||
def step(self, action):
|
def step(self, action):
|
||||||
|
@ -85,7 +85,7 @@ class WgzGym(gym.Env):
|
||||||
current_obs = self._build_state()
|
current_obs = self._build_state()
|
||||||
self.EC.step(action[0])
|
self.EC.step(action[0])
|
||||||
self.HST.step(action[1])
|
self.HST.step(action[1])
|
||||||
self.grid.step(action[2])
|
# self.grid.step(action[2], self.EC.power_max)
|
||||||
price = current_obs[1]
|
price = current_obs[1]
|
||||||
temper = current_obs[2] # 用途待补充
|
temper = current_obs[2] # 用途待补充
|
||||||
solar = current_obs[3]
|
solar = current_obs[3]
|
||||||
|
@ -120,11 +120,11 @@ class WgzGym(gym.Env):
|
||||||
|
|
||||||
economic_cost = hst_cost + ec_cost + solar_cost - sell_benefit + buy_cost
|
economic_cost = hst_cost + ec_cost + solar_cost - sell_benefit + buy_cost
|
||||||
demand_cost = self.heat_a * heat_penalty + self.power_a * power_penalty
|
demand_cost = self.heat_a * heat_penalty + self.power_a * power_penalty
|
||||||
eco_benifit = self.EC.less_carbon() - self.grid.get_carbon()
|
eco_benifit = self.EC.less_carbon() - self.grid.get_carbon(power_gap)
|
||||||
reward = - self.a * demand_cost - self.b * economic_cost + self.c * eco_benifit
|
reward = - self.a * demand_cost - self.b * economic_cost + self.c * eco_benifit
|
||||||
|
|
||||||
self.unbalance = power_gap + heat_gap
|
self.unbalance = power_gap + heat_gap
|
||||||
final_step_outputs = [self.HST.current_soc, self.HST.get_power(), self.EC.current_power, self.grid.current_power]
|
final_step_outputs = [self.HST.current_soc, self.HST.get_power(), self.EC.current_power]
|
||||||
self.current_time += 1
|
self.current_time += 1
|
||||||
finish = (self.current_time == self.episode_length)
|
finish = (self.current_time == self.episode_length)
|
||||||
if finish:
|
if finish:
|
||||||
|
@ -140,7 +140,7 @@ class WgzGym(gym.Env):
|
||||||
solar = data_df['solar_power'].to_numpy(dtype=float)
|
solar = data_df['solar_power'].to_numpy(dtype=float)
|
||||||
temper = data_df['temper'].to_numpy(dtype=float)
|
temper = data_df['temper'].to_numpy(dtype=float)
|
||||||
energy = data_df['energy_demand'].to_numpy(dtype=float)
|
energy = data_df['energy_demand'].to_numpy(dtype=float)
|
||||||
water = data_df['water_demand'].to_numpy(dtype=float)
|
heat = data_df['water_demand'].to_numpy(dtype=float)
|
||||||
people = data_df['people_count'].to_numpy(dtype=float)
|
people = data_df['people_count'].to_numpy(dtype=float)
|
||||||
price = data_df['price'].to_numpy(dtype=float)
|
price = data_df['price'].to_numpy(dtype=float)
|
||||||
|
|
||||||
|
@ -151,9 +151,9 @@ class WgzGym(gym.Env):
|
||||||
transformed_e = transform_function(e)
|
transformed_e = transform_function(e)
|
||||||
add_function(transformed_e)
|
add_function(transformed_e)
|
||||||
|
|
||||||
process_elements(solar, lambda x: x, self.data_manager.add_load_element)
|
process_elements(solar, lambda x: x, self.data_manager.add_solar_element)
|
||||||
process_elements(temper, lambda x: x, self.data_manager.add_load_element)
|
process_elements(temper, lambda x: x, self.data_manager.add_temper_element)
|
||||||
process_elements(energy, lambda x: x, self.data_manager.add_irradiance_element)
|
process_elements(energy, lambda x: x, self.data_manager.add_electricity_element)
|
||||||
process_elements(water, lambda x: x, self.data_manager.add_temperature_element)
|
process_elements(heat, lambda x: x, self.data_manager.add_heat_element)
|
||||||
process_elements(people, lambda x: x, self.data_manager.add_wind_element)
|
process_elements(people, lambda x: x, self.data_manager.add_people_element)
|
||||||
process_elements(price, lambda x: x, self.data_manager.add_price_element)
|
process_elements(price, lambda x: x, self.data_manager.add_price_element)
|
||||||
|
|
|
@ -7,7 +7,7 @@ class EC:
|
||||||
self.ramp = params['ramp']
|
self.ramp = params['ramp']
|
||||||
self.lifetime = params['lifetime']
|
self.lifetime = params['lifetime']
|
||||||
self.equipment_cost = params['equipment_cost']
|
self.equipment_cost = params['equipment_cost']
|
||||||
self.electricity_efficiency = params['electricity_efficiency']
|
self.electrolysis_efficiency = params['electrolysis_efficiency']
|
||||||
self.carbon_reduce = params['carbon_reduce']
|
self.carbon_reduce = params['carbon_reduce']
|
||||||
|
|
||||||
def step(self, action_ec):
|
def step(self, action_ec):
|
||||||
|
@ -20,17 +20,14 @@ class EC:
|
||||||
return self.equipment_cost / self.lifetime * price * self.current_power / self.power_max
|
return self.equipment_cost / self.lifetime * price * self.current_power / self.power_max
|
||||||
|
|
||||||
def get_hydrogen(self):
|
def get_hydrogen(self):
|
||||||
return self.current_power * self.electricity_efficiency * self.hydrogen_produce
|
return self.current_power * self.electrolysis_efficiency * self.hydrogen_produce
|
||||||
|
|
||||||
def get_heat(self):
|
def get_heat(self):
|
||||||
return self.current_power * (1 - self.electricity_efficiency)
|
return self.current_power * (1 - self.electrolysis_efficiency)
|
||||||
|
|
||||||
def less_carbon(self):
|
def less_carbon(self):
|
||||||
return self.current_power * self.carbon_reduce
|
return self.current_power * self.carbon_reduce
|
||||||
|
|
||||||
def get_power_max(self):
|
|
||||||
return self.power_max
|
|
||||||
|
|
||||||
def reset(self):
|
def reset(self):
|
||||||
self.current_power = 0
|
self.current_power = 0
|
||||||
|
|
||||||
|
@ -42,7 +39,6 @@ class HST:
|
||||||
self.capacity = params['capacity']
|
self.capacity = params['capacity']
|
||||||
self.min_soc = params['min_soc']
|
self.min_soc = params['min_soc']
|
||||||
self.max_soc = params['max_soc']
|
self.max_soc = params['max_soc']
|
||||||
self.ramp = params['ramp']
|
|
||||||
self.lifetime = params['lifetime']
|
self.lifetime = params['lifetime']
|
||||||
self.equipment_cost = params['equipment_cost']
|
self.equipment_cost = params['equipment_cost']
|
||||||
self.charge_efficiency = params['charge_efficiency']
|
self.charge_efficiency = params['charge_efficiency']
|
||||||
|
@ -51,12 +47,12 @@ class HST:
|
||||||
|
|
||||||
'''
|
'''
|
||||||
储氢罐的充气速率 = 电解水制氢速率 (电解水制氢放的热会满足热水需求?)
|
储氢罐的充气速率 = 电解水制氢速率 (电解水制氢放的热会满足热水需求?)
|
||||||
|
如何控制上述待补充
|
||||||
储氢罐的放气速率 = 供电 (电价低时多电解,电价高时释放)
|
储氢罐的放气速率 = 供电 (电价低时多电解,电价高时释放)
|
||||||
'''
|
'''
|
||||||
|
|
||||||
def step(self, action_hst):
|
def step(self, action_hst):
|
||||||
energy = action_hst * self.ramp
|
energy = action_hst * self.capacity
|
||||||
updated_soc = max(self.min_soc, min(self.max_soc, (self.current_soc * self.capacity + energy) / self.capacity))
|
updated_soc = max(self.min_soc, min(self.max_soc, (self.current_soc * self.capacity + energy) / self.capacity))
|
||||||
self.hydrogen_charge = (updated_soc - self.current_soc) * self.capacity
|
self.hydrogen_charge = (updated_soc - self.current_soc) * self.capacity
|
||||||
self.current_soc = updated_soc
|
self.current_soc = updated_soc
|
||||||
|
@ -85,16 +81,16 @@ class Grid:
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.delta = 1
|
self.delta = 1
|
||||||
self.carbon_increace = 0.9
|
self.carbon_increace = 0.9
|
||||||
self.trade_energy = None
|
# self.trade_energy = None
|
||||||
|
|
||||||
def step(self, action_grid):
|
# def step(self, action_grid, ec_power_max):
|
||||||
self.trade_energy = (action_grid + 1) / 2 * EC.get_power_max() # 反标准化
|
# self.trade_energy = (action_grid + 1) / 2 * ec_power_max # 反标准化
|
||||||
|
|
||||||
def get_cost(self, price):
|
def get_cost(self, price, trade_energy):
|
||||||
return price * self.trade_energy * self.delta
|
return price * trade_energy * self.delta
|
||||||
|
|
||||||
def get_carbon(self):
|
def get_carbon(self, trade_energy):
|
||||||
return self.trade_energy * self.carbon_increace
|
return trade_energy * self.carbon_increace
|
||||||
|
|
||||||
def retrieve_past_price(self):
|
def retrieve_past_price(self):
|
||||||
result = []
|
result = []
|
||||||
|
|
|
@ -15,12 +15,10 @@ def test_one_episode(env, act, device):
|
||||||
action = a_tensor.detach().cpu().numpy()[0]
|
action = a_tensor.detach().cpu().numpy()[0]
|
||||||
state, next_state, reward, done = env.step(action)
|
state, next_state, reward, done = env.step(action)
|
||||||
record_system_info.append([state[1], state[2], env.HST.current_soc(), env.HST.get_power(),
|
record_system_info.append([state[1], state[2], env.HST.current_soc(), env.HST.get_power(),
|
||||||
env.EC.current_power, env.grid.current_power, action, reward])
|
env.EC.current_power, action, reward])
|
||||||
state = next_state
|
state = next_state
|
||||||
# add information of last step EC, HST.current_soc, HST.power, grid
|
# add information of last step EC, HST.current_soc, HST.power, grid
|
||||||
final_step_outputs = [self.HST.current_soc, self.HST.get_power(), self.EC.current_power, self.grid.current_power]
|
record_system_info[-1][2:5] = [env.final_step_outputs[0], env.final_step_outputs[1], env.final_step_outputs[2]]
|
||||||
record_system_info[-1][2:6] = [env.final_step_outputs[0], env.final_step_outputs[1], env.final_step_outputs[2],
|
|
||||||
env.final_step_outputs[3]]
|
|
||||||
record = {'init_info': record_init_info, 'system_info': record_system_info}
|
record = {'init_info': record_init_info, 'system_info': record_system_info}
|
||||||
return record
|
return record
|
||||||
|
|
||||||
|
|
5
train.py
5
train.py
|
@ -3,11 +3,12 @@ import pickle
|
||||||
|
|
||||||
os.environ['OMP_WAIT_POLICY'] = 'PASSIVE' # 确保在pytorch前设置
|
os.environ['OMP_WAIT_POLICY'] = 'PASSIVE' # 确保在pytorch前设置
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
import pandas as pd
|
import numpy as np
|
||||||
|
import torch
|
||||||
import torch.nn.functional as F
|
import torch.nn.functional as F
|
||||||
from models.env import WgzGym
|
from models.env import WgzGym
|
||||||
from models.net import ActorPPO, CriticAdv
|
from models.net import ActorPPO, CriticAdv
|
||||||
from models.tools import get_episode_return, test_one_episode
|
from models.tools import get_episode_return
|
||||||
|
|
||||||
|
|
||||||
def smooth_rewards(rewards, window=10):
|
def smooth_rewards(rewards, window=10):
|
||||||
|
|
Loading…
Reference in New Issue