From f7afee2a64a2b2c25eccaabcb5eafc3fa27e1a80 Mon Sep 17 00:00:00 2001 From: mhrooz Date: Fri, 3 May 2024 15:11:58 +0200 Subject: [PATCH] write 2 random data scripts --- generatedata.py | 56 ++++++++++++++++++++++++++++++++++++++++ generatepriceschedule.py | 24 +++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 generatedata.py create mode 100644 generatepriceschedule.py diff --git a/generatedata.py b/generatedata.py new file mode 100644 index 0000000..3f852ea --- /dev/null +++ b/generatedata.py @@ -0,0 +1,56 @@ +import pandas as pd +import numpy as np + +# 设置随机种子以重现结果 +np.random.seed(43) + +def simulate_sunlight(hour, month): + # 假设最大日照强度在正午,根据月份调整最大日照强度 + max_intensity = 1.0 # 夏季最大日照强度 + if month in [12, 1, 2]: # 冬季 + max_intensity = 0.6 + elif month in [3, 4, 10, 11]: # 春秋 + max_intensity = 0.8 + + # 计算日照强度,模拟早晚日照弱,中午日照强 + intensity = max_intensity * np.sin(np.pi * (hour - 6) / 12)**2 if 6 <= hour <= 18 else 0 + return intensity + +def simulate_factory_demand(hour, day_of_week): + # 周末工厂需求可能减少 + if day_of_week in [5, 6]: # 周六和周日 + base_demand = 3000 + else: + base_demand = 6000 + + # 日常波动 + if 8 <= hour <= 20: + return base_demand + np.random.randint(100, 200) # 白天需求量大 + else: + return base_demand - np.random.randint(0, 100) # 夜间需求量小 + +def generate_data(days=10): + records = [] + month_demand = 0 + for day in range(days): + month = (day % 365) // 30 + 1 + day_of_week = day % 7 + day_demand = 0 + for hour in range(24): + for minute in [0, 10, 20, 30, 40, 50]: + time = f'{hour:02d}:{minute:02d}' + sunlight = simulate_sunlight(hour, month) + demand = simulate_factory_demand(hour, day_of_week) + day_demand+=demand + records.append({'time': time, 'sunlight': sunlight, 'demand': demand}) + print(f"day:{day}, day_demand: {day_demand}") + month_demand += day_demand + if day%30 == 0: + print(f"month:{month}, month_demand:{month_demand}") + month_demand = 0 + return pd.DataFrame(records) + +# 生成数据 +data = generate_data(365) # 模拟一年的数据 +data.to_csv('simulation_data.csv', index=False) +print("Data generated and saved to simulation_data.csv.") diff --git a/generatepriceschedule.py b/generatepriceschedule.py new file mode 100644 index 0000000..b6de2cc --- /dev/null +++ b/generatepriceschedule.py @@ -0,0 +1,24 @@ +import pandas as pd +import numpy as np + +def generate_price_schedule(): + records = [] + # 假设一天分为三个时段:谷时、平时、峰时 + times = [('00:00', '06:00', 0.25), + ('06:00', '18:00', 0.3), + ('18:00', '24:00', 0.35)] + + # 随机调整每天的电价以增加现实性 + for time_start, time_end, base_price in times: + # 随机浮动5%以内 + fluctuation = np.random.uniform(-0.005, 0.005) + price = round(base_price + fluctuation, 3) + records.append({'time_start': time_start, 'time_end': time_end, 'price': price}) + + return pd.DataFrame(records) + +# 生成电价计划 +price_schedule = generate_price_schedule() +price_schedule.to_csv('price_schedule.csv', index=False) +print("Price schedule generated and saved to price_schedule.csv.") +print(price_schedule) \ No newline at end of file