update the csv file output code and add a progress bar in the code
This commit is contained in:
		
							
								
								
									
										598
									
								
								main.ipynb
									
									
									
									
									
								
							
							
						
						
									
										598
									
								
								main.ipynb
									
									
									
									
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							
							
								
								
									
										63
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										63
									
								
								main.py
									
									
									
									
									
								
							| @@ -1,7 +1,7 @@ | ||||
| #!/usr/bin/env python | ||||
| # coding: utf-8 | ||||
|  | ||||
| # In[39]: | ||||
| # In[83]: | ||||
|  | ||||
|  | ||||
| import os | ||||
| @@ -28,7 +28,7 @@ folder_path = 'plots' | ||||
| clear_folder_make_ess_pv(folder_path) | ||||
|  | ||||
|  | ||||
| # In[40]: | ||||
| # In[84]: | ||||
|  | ||||
|  | ||||
| import matplotlib.pyplot as plt | ||||
| @@ -39,7 +39,7 @@ from EnergySystem import EnergySystem | ||||
| from config import pv_config, grid_config, ess_config | ||||
|  | ||||
|  | ||||
| # In[41]: | ||||
| # In[85]: | ||||
|  | ||||
|  | ||||
| import json | ||||
| @@ -53,7 +53,7 @@ with open('config.json', 'r') as f: | ||||
|      | ||||
|  | ||||
| time_interval = js_data["time_interval"]["numerator"] / js_data["time_interval"]["denominator"] | ||||
| print(time_interval) | ||||
| # print(time_interval) | ||||
|  | ||||
| pv_loss = js_data["pv"]["loss"] | ||||
| pv_cost_per_kW = js_data["pv"]["cost_per_kW"] | ||||
| @@ -116,7 +116,7 @@ ess_capacities = np.linspace(ess_begin, ess_end, ess_groups) | ||||
| # overload_cnt = pd.DataFrame(index=pv_capacities, columns= ess_capacities) | ||||
|  | ||||
|  | ||||
| # In[42]: | ||||
| # In[86]: | ||||
|  | ||||
|  | ||||
| hour_demand = [] | ||||
| @@ -132,7 +132,7 @@ plt.savefig('plots/demand.png') | ||||
| plt.close() | ||||
|  | ||||
|  | ||||
| # In[43]: | ||||
| # In[87]: | ||||
|  | ||||
|  | ||||
| def draw_results(results, filename, title_benefit, annot_benefit=False, figure_size=(10, 10)): | ||||
| @@ -171,7 +171,7 @@ def draw_results(results, filename, title_benefit, annot_benefit=False, figure_s | ||||
|     plt.savefig(filename) | ||||
|  | ||||
|  | ||||
| # In[44]: | ||||
| # In[88]: | ||||
|  | ||||
|  | ||||
| def draw_roi(costs, results, filename, title_roi, days=365, annot_roi=False, figure_size=(10, 10)): | ||||
| @@ -184,7 +184,7 @@ def draw_roi(costs, results, filename, title_roi, days=365, annot_roi=False, fig | ||||
|     if 0 in df.index and 0 in df.columns: | ||||
|         df.loc[0,0] = 100 | ||||
|     df[df > 80] = 100 | ||||
|     print(df) | ||||
|     # print(df) | ||||
|  | ||||
|     df = df.astype(float) | ||||
|     df.index = df.index / 1000 | ||||
| @@ -193,7 +193,7 @@ def draw_roi(costs, results, filename, title_roi, days=365, annot_roi=False, fig | ||||
|     df.columns = df.columns.map(int) | ||||
|     min_value = df.min().min() | ||||
|     max_value = df.max().max() | ||||
|     print(max_value) | ||||
|     # print(max_value) | ||||
|     max_scale = max(abs(min_value), abs(max_value)) | ||||
|  | ||||
|     df[df.columns[-1] + 1] = df.iloc[:, -1]  | ||||
| @@ -222,7 +222,7 @@ def draw_roi(costs, results, filename, title_roi, days=365, annot_roi=False, fig | ||||
|     plt.close() | ||||
|  | ||||
|  | ||||
| # In[45]: | ||||
| # In[89]: | ||||
|  | ||||
|  | ||||
| def draw_cost(costs, filename, title_cost, annot_cost=False, figure_size=(10, 10)): | ||||
| @@ -257,17 +257,17 @@ def draw_cost(costs, filename, title_cost, annot_cost=False, figure_size=(10, 10 | ||||
|     plt.close() | ||||
|  | ||||
|  | ||||
| # In[46]: | ||||
| # In[90]: | ||||
|  | ||||
|  | ||||
| def draw_overload(overload_cnt, filename, title_unmet, annot_unmet=False, figure_size=(10, 10), days=365, granularity=15): | ||||
|     df = overload_cnt | ||||
|     print(days, granularity) | ||||
|     # print(days, granularity) | ||||
|     coef = 60 / granularity * days * 24 | ||||
|     print(coef) | ||||
|     print(df) | ||||
|     # print(coef) | ||||
|     # print(df) | ||||
|     df = ( coef - df) / coef | ||||
|     print(df) | ||||
|     # print(df) | ||||
|  | ||||
|     df = df.astype(float) | ||||
|     df.index = df.index / 1000 | ||||
| @@ -309,7 +309,7 @@ def draw_overload(overload_cnt, filename, title_unmet, annot_unmet=False, figure | ||||
|     plt.close() | ||||
|  | ||||
|  | ||||
| # In[47]: | ||||
| # In[91]: | ||||
|  | ||||
|  | ||||
| def cal_profit(es: EnergySystem, saved_money, days): | ||||
| @@ -317,7 +317,7 @@ def cal_profit(es: EnergySystem, saved_money, days): | ||||
|     return profit | ||||
|  | ||||
|  | ||||
| # In[48]: | ||||
| # In[92]: | ||||
|  | ||||
|  | ||||
| def generate_data(pv_capacity, pv_cost_per_kW, pv_lifetime, pv_loss, ess_capacity, ess_cost_per_kW, ess_lifetime, ess_loss, grid_capacity, grid_loss, sell_price, time_interval, data, days, storage=0): | ||||
| @@ -357,9 +357,11 @@ def generate_data(pv_capacity, pv_cost_per_kW, pv_lifetime, pv_loss, ess_capacit | ||||
|             energySystem.time) | ||||
|  | ||||
|  | ||||
| # In[49]: | ||||
|  | ||||
| # In[93]: | ||||
|  | ||||
|  | ||||
| from tqdm import tqdm | ||||
| months_results = [] | ||||
| months_costs = [] | ||||
| months_overload = [] | ||||
| @@ -368,7 +370,7 @@ months_gen_energy = [] | ||||
| months_gen_energy2 = [] | ||||
| months_ess_rest = pd.DataFrame(30, index=pv_capacities, columns= ess_capacities) | ||||
| months_csv_data = {} | ||||
| for index, month_data in enumerate(months_data): | ||||
| for index, month_data in tqdm(enumerate(months_data), total=len(months_data), position=0, leave= True): | ||||
|     results = pd.DataFrame(index=pv_capacities, columns= ess_capacities) | ||||
|     costs = pd.DataFrame(index=pv_capacities, columns= ess_capacities) | ||||
|     overload_cnt = pd.DataFrame(index=pv_capacities, columns= ess_capacities) | ||||
| @@ -381,7 +383,7 @@ for index, month_data in enumerate(months_data): | ||||
|     pv_generates = {} | ||||
|     grid_need_powers = {} | ||||
|     times = {} | ||||
|     for pv_capacity in pv_capacities: | ||||
|     for pv_capacity in tqdm(pv_capacities, total=len(pv_capacities), desc=f'generating pv for month {index + 1}',position=1, leave=False): | ||||
|         factory_demands[pv_capacity] = {} | ||||
|         buy_prices[pv_capacity] = {} | ||||
|         sell_prices[pv_capacity] = {} | ||||
| @@ -496,7 +498,7 @@ draw_overload(overload_cnt=annual_overload, | ||||
|                 figure_size=figure_size) | ||||
|  | ||||
|  | ||||
| # In[50]: | ||||
| # In[94]: | ||||
|  | ||||
|  | ||||
| def collapse_months_csv_data(months_csv_data, column_name,pv_capacies, ess_capacities): | ||||
| @@ -513,7 +515,7 @@ def collapse_months_csv_data(months_csv_data, column_name,pv_capacies, ess_capac | ||||
|     return data  | ||||
|  | ||||
|  | ||||
| # In[51]: | ||||
| # In[102]: | ||||
|  | ||||
|  | ||||
| annual_pv_gen = collapse_months_csv_data(months_csv_data, "pv_generate", pv_capacities, ess_capacities) | ||||
| @@ -523,16 +525,21 @@ annual_sell_price = collapse_months_csv_data(months_csv_data, "sell_price", pv_c | ||||
| annual_factory_demand = collapse_months_csv_data(months_csv_data, "factory_demand", pv_capacities, ess_capacities) | ||||
| annual_grid_need_power = collapse_months_csv_data(months_csv_data, "grid_need_power", pv_capacities, ess_capacities) | ||||
|  | ||||
| from datetime import datetime, timedelta | ||||
|  | ||||
| for pv_capacity in pv_capacities: | ||||
|     for ess_capacity in ess_capacities: | ||||
|         with open(f'data/annual_data-pv-{pv_capacity}-ess-{ess_capacity}.csv', 'w') as f: | ||||
|             f.write("time,pv_generate,factory_demand,buy_price,sell_price,grid_need_power\n") | ||||
|             f.write("date, time,pv_generate (kW),factory_demand (kW),buy_price (USD/MWh),sell_price (USD/MWh),grid_need_power (kW)\n") | ||||
|             start_date = datetime(2023, 1, 1, 0, 0, 0) | ||||
|             for i in range(len(annual_time[pv_capacity][ess_capacity])): | ||||
|                 f.write(f"{annual_time[pv_capacity][ess_capacity][i]}, {int(annual_pv_gen[pv_capacity][ess_capacity][i])}, {int(annual_factory_demand[pv_capacity][ess_capacity][i])}, {int(annual_buy_price[pv_capacity][ess_capacity][i]*1000)}, {int(annual_sell_price[pv_capacity][ess_capacity][i]*1000)}, {int(annual_grid_need_power[pv_capacity][ess_capacity][i])}\n") | ||||
|                 current_date = start_date + timedelta(hours=i) | ||||
|                 formate_date = current_date.strftime("%Y-%m-%d") | ||||
|                 f.write(f"{formate_date},{annual_time[pv_capacity][ess_capacity][i]},{int(annual_pv_gen[pv_capacity][ess_capacity][i])},{int(annual_factory_demand[pv_capacity][ess_capacity][i])},{int(annual_buy_price[pv_capacity][ess_capacity][i]*1000)},{int(annual_sell_price[pv_capacity][ess_capacity][i]*1000)},{int(annual_grid_need_power[pv_capacity][ess_capacity][i])} \n") | ||||
|  | ||||
|  | ||||
|  | ||||
| # In[52]: | ||||
| # In[96]: | ||||
|  | ||||
|  | ||||
| def save_data(data, filename): | ||||
| @@ -540,7 +547,7 @@ def save_data(data, filename): | ||||
|     data.to_json(filename + '.json') | ||||
|  | ||||
|  | ||||
| # In[53]: | ||||
| # In[97]: | ||||
|  | ||||
|  | ||||
| if not os.path.isdir('data'): | ||||
| @@ -551,13 +558,13 @@ save_data(annual_costs, f'data/{pv_begin}-{pv_end}-{pv_groups}-{ess_begin}-{ess_ | ||||
| save_data(annual_overload, f'data/{pv_begin}-{pv_end}-{pv_groups}-{ess_begin}-{ess_end}-{ess_groups}-overload_cnt') | ||||
|  | ||||
|  | ||||
| # In[54]: | ||||
| # In[98]: | ||||
|  | ||||
|  | ||||
| draw_results(annual_result, 'plots/test.png', 'test', False) | ||||
|  | ||||
|  | ||||
| # In[55]: | ||||
| # In[99]: | ||||
|  | ||||
|  | ||||
| draw_roi(annual_costs, annual_nettos, 'plots/annual_roi.png',  title_roi, 365, annot_benefit, figure_size) | ||||
|   | ||||
		Reference in New Issue
	
	Block a user