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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import requests
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import pulp
|
| 5 |
+
import plotly.graph_objs as go
|
| 6 |
+
import plotly.express as px
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
+
# Function to fetch renewable energy data
|
| 10 |
+
def get_renewable_energy_data(city_code):
|
| 11 |
+
"""
|
| 12 |
+
Fetch renewable energy data for a given city code.
|
| 13 |
+
|
| 14 |
+
Args:
|
| 15 |
+
city_code (int): The city code to fetch data for.
|
| 16 |
+
|
| 17 |
+
Returns:
|
| 18 |
+
tuple: A pandas DataFrame containing energy data, and a string indicating any error (if applicable).
|
| 19 |
+
"""
|
| 20 |
+
url = f"https://energy-sustainability.jp/_ajax/renewable_energy/get/?code={city_code}"
|
| 21 |
+
response = requests.get(url)
|
| 22 |
+
if response.status_code != 200:
|
| 23 |
+
return None, "Failed to retrieve data."
|
| 24 |
+
|
| 25 |
+
data = response.json()
|
| 26 |
+
if not data:
|
| 27 |
+
return None, "No data found."
|
| 28 |
+
|
| 29 |
+
base_times = data[next(iter(data))]['x']
|
| 30 |
+
result_df = pd.DataFrame({"Time": base_times})
|
| 31 |
+
|
| 32 |
+
for energy_type, energy_data in data.items():
|
| 33 |
+
if 'x' in energy_data and 'y' in energy_data:
|
| 34 |
+
values = energy_data['y']
|
| 35 |
+
result_df[f"{energy_type} hourly capacity factor"] = values
|
| 36 |
+
|
| 37 |
+
return result_df, None
|
| 38 |
+
|
| 39 |
+
# Function to optimize the energy system and create visualizations
|
| 40 |
+
def optimize_energy_system(city_code, solar_cost, onshore_wind_cost, offshore_wind_cost, river_cost, battery_cost, yearly_demand, solar_range, wind_range, river_range, offshore_wind_range):
|
| 41 |
+
"""
|
| 42 |
+
Optimize the energy system by determining the optimal capacity for different renewable technologies.
|
| 43 |
+
|
| 44 |
+
Args:
|
| 45 |
+
city_code (int): The city code to fetch data for.
|
| 46 |
+
solar_cost (float): Cost per MW for solar.
|
| 47 |
+
onshore_wind_cost (float): Cost per MW for onshore wind.
|
| 48 |
+
offshore_wind_cost (float): Cost per MW for offshore wind.
|
| 49 |
+
river_cost (float): Cost per MW for river.
|
| 50 |
+
battery_cost (float): Cost per MWh for battery storage.
|
| 51 |
+
yearly_demand (float): Yearly power demand in TWh/year.
|
| 52 |
+
solar_range (tuple): Minimum and maximum capacity for solar.
|
| 53 |
+
wind_range (tuple): Minimum and maximum capacity for onshore wind.
|
| 54 |
+
river_range (tuple): Minimum and maximum capacity for river.
|
| 55 |
+
offshore_wind_range (tuple): Minimum and maximum capacity for offshore wind.
|
| 56 |
+
|
| 57 |
+
Returns:
|
| 58 |
+
tuple: Visualization figures and optimization results.
|
| 59 |
+
"""
|
| 60 |
+
data, error = get_renewable_energy_data(city_code)
|
| 61 |
+
if error:
|
| 62 |
+
st.error(error)
|
| 63 |
+
return None, None, None, None, None, None
|
| 64 |
+
|
| 65 |
+
for col in data.columns[1:]:
|
| 66 |
+
data[col] = pd.to_numeric(data[col], errors='coerce')
|
| 67 |
+
data = data.fillna(0)
|
| 68 |
+
|
| 69 |
+
time_steps = range(len(data['Time']))
|
| 70 |
+
solar_cf = data['solar hourly capacity factor']
|
| 71 |
+
onshore_wind_cf = data['onshore_wind hourly capacity factor']
|
| 72 |
+
offshore_wind_cf = data['offshore_wind hourly capacity factor']
|
| 73 |
+
river_cf = data['river hourly capacity factor']
|
| 74 |
+
demand_cf = data['demand hourly capacity factor']
|
| 75 |
+
|
| 76 |
+
regions = ['region1']
|
| 77 |
+
technologies = ['solar', 'onshore_wind', 'offshore_wind', 'river']
|
| 78 |
+
capacity_factor = {
|
| 79 |
+
'solar': solar_cf,
|
| 80 |
+
'onshore_wind': onshore_wind_cf,
|
| 81 |
+
'offshore_wind': offshore_wind_cf,
|
| 82 |
+
'river': river_cf
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
renewable_capacity_cost = {'solar': solar_cost, 'onshore_wind': onshore_wind_cost, 'offshore_wind': offshore_wind_cost, 'river': river_cost}
|
| 86 |
+
battery_cost_per_mwh = battery_cost
|
| 87 |
+
battery_efficiency = 0.9
|
| 88 |
+
|
| 89 |
+
demand = demand_cf * yearly_demand / 100 * 1000 * 1000
|
| 90 |
+
|
| 91 |
+
renewable_capacity = pulp.LpVariable.dicts("renewable_capacity",
|
| 92 |
+
[(r, g) for r in regions for g in technologies],
|
| 93 |
+
lowBound=0, cat='Continuous')
|
| 94 |
+
curtailment = pulp.LpVariable.dicts("curtailment",
|
| 95 |
+
[(r, t) for r in regions for t in time_steps],
|
| 96 |
+
lowBound=0, cat='Continuous')
|
| 97 |
+
battery_capacity = pulp.LpVariable("battery_capacity", lowBound=0, cat='Continuous')
|
| 98 |
+
battery_charge = pulp.LpVariable.dicts("battery_charge", time_steps, lowBound=0, cat='Continuous')
|
| 99 |
+
battery_discharge = pulp.LpVariable.dicts("battery_discharge", time_steps, lowBound=0, cat='Continuous')
|
| 100 |
+
SOC = pulp.LpVariable.dicts("SOC", time_steps, lowBound=0, cat='Continuous')
|
| 101 |
+
|
| 102 |
+
model = pulp.LpProblem("EnergySystemOptimizationWithBattery", pulp.LpMinimize)
|
| 103 |
+
|
| 104 |
+
model += pulp.lpSum([renewable_capacity[(r, g)] * renewable_capacity_cost[g]
|
| 105 |
+
for r in regions for g in technologies]) + \
|
| 106 |
+
battery_capacity * battery_cost_per_mwh, "TotalCost"
|
| 107 |
+
|
| 108 |
+
for r in regions:
|
| 109 |
+
for t in time_steps:
|
| 110 |
+
model += pulp.lpSum([renewable_capacity[(r, g)] * capacity_factor[g][t]
|
| 111 |
+
for g in technologies]) + battery_discharge[t] == demand[t] + battery_charge[t] + curtailment[(r, t)], f"DemandConstraint_{r}_{t}"
|
| 112 |
+
|
| 113 |
+
if t == 0:
|
| 114 |
+
model += SOC[t] == battery_charge[t] * battery_efficiency - battery_discharge[t] * (1 / battery_efficiency), f"SOCUpdate_{t}"
|
| 115 |
+
else:
|
| 116 |
+
model += SOC[t] == SOC[t - 1] + battery_charge[t] * battery_efficiency - battery_discharge[t] * (1 / battery_efficiency), f"SOCUpdate_{t}"
|
| 117 |
+
|
| 118 |
+
model += SOC[t] <= battery_capacity, f"SOCUpperBound_{t}"
|
| 119 |
+
|
| 120 |
+
model += renewable_capacity[('region1', 'solar')] >= solar_range[0], "SolarMinConstraint"
|
| 121 |
+
model += renewable_capacity[('region1', 'solar')] <= solar_range[1], "SolarMaxConstraint"
|
| 122 |
+
model += renewable_capacity[('region1', 'onshore_wind')] >= wind_range[0], "WindMinConstraint"
|
| 123 |
+
model += renewable_capacity[('region1', 'onshore_wind')] <= wind_range[1], "WindMaxConstraint"
|
| 124 |
+
model += renewable_capacity[('region1', 'offshore_wind')] >= offshore_wind_range[0], "OffshoreWindMinConstraint"
|
| 125 |
+
model += renewable_capacity[('region1', 'offshore_wind')] <= offshore_wind_range[1], "OffshoreWindMaxConstraint"
|
| 126 |
+
model += renewable_capacity[('region1', 'river')] >= river_range[0], "RiverMinConstraint"
|
| 127 |
+
model += renewable_capacity[('region1', 'river')] <= river_range[1], "RiverMaxConstraint"
|
| 128 |
+
|
| 129 |
+
result = model.solve()
|
| 130 |
+
if pulp.LpStatus[model.status] != 'Optimal':
|
| 131 |
+
st.error(f"Optimization problem was not solved to optimality: {pulp.LpStatus[model.status]}")
|
| 132 |
+
return None, None, None, None, None, None
|
| 133 |
+
|
| 134 |
+
supply_solar = solar_cf * renewable_capacity[('region1', 'solar')].varValue
|
| 135 |
+
supply_onshore_wind = onshore_wind_cf * renewable_capacity[('region1', 'onshore_wind')].varValue
|
| 136 |
+
supply_offshore_wind = offshore_wind_cf * renewable_capacity[('region1', 'offshore_wind')].varValue
|
| 137 |
+
supply_river = river_cf * renewable_capacity[('region1', 'river')].varValue
|
| 138 |
+
|
| 139 |
+
battery_discharge_values = [battery_discharge[t].varValue for t in time_steps]
|
| 140 |
+
battery_charge_values = [-battery_charge[t].varValue for t in time_steps]
|
| 141 |
+
SOC_values = [SOC[t].varValue for t in time_steps]
|
| 142 |
+
curtailment_values = [-curtailment[(r, t)].varValue for r in regions for t in time_steps]
|
| 143 |
+
|
| 144 |
+
max_SOC = max(SOC_values)
|
| 145 |
+
SOC_normalized = [(soc / max_SOC) * 100 for soc in SOC_values] if max_SOC > 0 else [0] * len(SOC_values)
|
| 146 |
+
|
| 147 |
+
fig_energy = go.Figure()
|
| 148 |
+
fig_energy.add_trace(go.Scatter(x=data['Time'], y=supply_solar, mode='lines', stackgroup='one', name='Solar', line=dict(color='#FFD700', width=0)))
|
| 149 |
+
fig_energy.add_trace(go.Scatter(x=data['Time'], y=supply_onshore_wind, mode='lines', stackgroup='one', name='Onshore Wind', line=dict(color='#1F78B4', width=0)))
|
| 150 |
+
fig_energy.add_trace(go.Scatter(x=data['Time'], y=supply_offshore_wind, mode='lines', stackgroup='one', name='Offshore Wind', line=dict(color='#66C2A5', width=0)))
|
| 151 |
+
fig_energy.add_trace(go.Scatter(x=data['Time'], y=supply_river, mode='lines', stackgroup='one', name='Run of River', line=dict(color='#FF7F00', width=0)))
|
| 152 |
+
fig_energy.add_trace(go.Scatter(x=data['Time'], y=battery_discharge_values, mode='lines', stackgroup='one', name='Battery Discharge', fill='tonexty', line=dict(color='#6A3D9A', width=0)))
|
| 153 |
+
fig_energy.add_trace(go.Scatter(x=data['Time'], y=battery_charge_values, mode='lines', stackgroup='two', name='Battery Charge', fill='tonexty', line=dict(color='#6A3D9A', width=0)))
|
| 154 |
+
fig_energy.add_trace(go.Scatter(x=data['Time'], y=-demand, mode='lines', stackgroup='two', name='Demand', line=dict(color='black', width=0)))
|
| 155 |
+
fig_energy.add_trace(go.Scatter(x=data['Time'], y=curtailment_values, mode='lines', stackgroup='two', name='Curtailment', line=dict(color='#aaaaaa', width=0)))
|
| 156 |
+
|
| 157 |
+
fig_energy.update_layout(
|
| 158 |
+
title_text='Power Supply and Demand',
|
| 159 |
+
title_x=0.5,
|
| 160 |
+
yaxis_title='Power dispatch (MW)',
|
| 161 |
+
legend_title='Source',
|
| 162 |
+
font=dict(size=12),
|
| 163 |
+
margin=dict(l=40, r=40, t=40, b=40),
|
| 164 |
+
hovermode='x unified',
|
| 165 |
+
plot_bgcolor='white',
|
| 166 |
+
xaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray'),
|
| 167 |
+
yaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray')
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
# Heatmap generation for each renewable energy source
|
| 171 |
+
heatmaps = []
|
| 172 |
+
for energy_source in ['solar', 'onshore_wind', 'offshore_wind', 'river']:
|
| 173 |
+
df_heatmap = data[['Time', f'{energy_source} hourly capacity factor']].copy()
|
| 174 |
+
df_heatmap['Time'] = pd.to_datetime(df_heatmap['Time'], errors='coerce')
|
| 175 |
+
df_heatmap['day_of_year'] = df_heatmap['Time'].dt.dayofyear
|
| 176 |
+
df_heatmap['hour_of_day'] = df_heatmap['Time'].dt.hour
|
| 177 |
+
|
| 178 |
+
pivot_df = df_heatmap.pivot_table(
|
| 179 |
+
index='hour_of_day',
|
| 180 |
+
columns='day_of_year',
|
| 181 |
+
values=f'{energy_source} hourly capacity factor',
|
| 182 |
+
aggfunc='mean'
|
| 183 |
+
)
|
| 184 |
+
|
| 185 |
+
fig_heatmap = px.imshow(
|
| 186 |
+
pivot_df.values,
|
| 187 |
+
labels=dict(x="Day of Year", y="Hour of Day", color=f"{energy_source.replace('_', ' ').title()} Capacity Factor"),
|
| 188 |
+
x=pivot_df.columns,
|
| 189 |
+
y=pivot_df.index,
|
| 190 |
+
aspect="auto",
|
| 191 |
+
color_continuous_scale='Plasma'
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
fig_heatmap.update_layout(
|
| 195 |
+
title=f'{energy_source.replace("_", " ").title()} Hourly Capacity Factor (24 Hours x 365 Days)',
|
| 196 |
+
xaxis_title='Day of Year',
|
| 197 |
+
yaxis_title='Hour of Day',
|
| 198 |
+
font=dict(size=12),
|
| 199 |
+
plot_bgcolor='white',
|
| 200 |
+
margin=dict(l=40, r=40, t=40, b=40),
|
| 201 |
+
)
|
| 202 |
+
heatmaps.append(fig_heatmap)
|
| 203 |
+
|
| 204 |
+
# Create capacity range visualization for each technology
|
| 205 |
+
fig_capacity_ranges = go.Figure()
|
| 206 |
+
technologies = ['solar', 'onshore_wind', 'offshore_wind', 'river']
|
| 207 |
+
capacity_ranges = [solar_range, wind_range, offshore_wind_range, river_range]
|
| 208 |
+
optimized_capacities = [
|
| 209 |
+
renewable_capacity[('region1', 'solar')].varValue,
|
| 210 |
+
renewable_capacity[('region1', 'onshore_wind')].varValue,
|
| 211 |
+
renewable_capacity[('region1', 'offshore_wind')].varValue,
|
| 212 |
+
renewable_capacity[('region1', 'river')].varValue
|
| 213 |
+
]
|
| 214 |
+
|
| 215 |
+
for tech, cap_range, optimized_cap in zip(technologies, capacity_ranges, optimized_capacities):
|
| 216 |
+
fig_capacity_ranges.add_trace(go.Scatter(
|
| 217 |
+
x=[tech, tech],
|
| 218 |
+
y=cap_range,
|
| 219 |
+
mode='lines',
|
| 220 |
+
name=f'{tech} capacity range',
|
| 221 |
+
line=dict(color='blue', width=4)
|
| 222 |
+
))
|
| 223 |
+
fig_capacity_ranges.add_trace(go.Scatter(
|
| 224 |
+
x=[tech],
|
| 225 |
+
y=[optimized_cap],
|
| 226 |
+
mode='markers',
|
| 227 |
+
name=f'{tech} optimized capacity',
|
| 228 |
+
marker=dict(color='red', symbol='x', size=10)
|
| 229 |
+
))
|
| 230 |
+
|
| 231 |
+
fig_capacity_ranges.update_layout(
|
| 232 |
+
title_text='Optimized Capacity vs. Capacity Ranges',
|
| 233 |
+
title_x=0.5,
|
| 234 |
+
yaxis_title='Capacity (MW)',
|
| 235 |
+
xaxis_title='Technology',
|
| 236 |
+
font=dict(size=12),
|
| 237 |
+
margin=dict(l=40, r=40, t=40, b=40),
|
| 238 |
+
hovermode='x unified',
|
| 239 |
+
plot_bgcolor='white',
|
| 240 |
+
xaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray'),
|
| 241 |
+
yaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray')
|
| 242 |
+
)
|
| 243 |
+
|
| 244 |
+
return fig_energy, heatmaps, curtailment_values, SOC_normalized, fig_capacity_ranges, renewable_capacity
|
| 245 |
+
|
| 246 |
+
# 資源コストの感度解析を行う関数
|
| 247 |
+
def analyze_cost_sensitivity(renewable_capacity_cost, technologies, renewable_capacity):
|
| 248 |
+
"""
|
| 249 |
+
Perform cost sensitivity analysis for different renewable technologies.
|
| 250 |
+
|
| 251 |
+
Args:
|
| 252 |
+
renewable_capacity_cost (dict): Cost of each renewable technology.
|
| 253 |
+
technologies (list): List of renewable technologies.
|
| 254 |
+
renewable_capacity (dict): Optimized capacities for each technology.
|
| 255 |
+
|
| 256 |
+
Returns:
|
| 257 |
+
fig (plotly.graph_objs.Figure): Plotly figure showing cost sensitivity analysis.
|
| 258 |
+
"""
|
| 259 |
+
# コストの変動範囲(0.5倍から1.5倍)
|
| 260 |
+
cost_multipliers = np.linspace(0.5, 1.5, 11)
|
| 261 |
+
|
| 262 |
+
# 結果を格納する辞書
|
| 263 |
+
sensitivity_results = {}
|
| 264 |
+
|
| 265 |
+
for tech in technologies:
|
| 266 |
+
# 各技術ごとのコスト変動に対する総コストの変化を計算
|
| 267 |
+
original_cost = renewable_capacity_cost[tech]
|
| 268 |
+
total_costs = []
|
| 269 |
+
|
| 270 |
+
for multiplier in cost_multipliers:
|
| 271 |
+
# コストを変更
|
| 272 |
+
modified_cost = original_cost * multiplier
|
| 273 |
+
# 総コスト = 変更後のコスト * 設備容量
|
| 274 |
+
total_cost = modified_cost * renewable_capacity[('region1', tech)].varValue
|
| 275 |
+
total_costs.append(total_cost)
|
| 276 |
+
|
| 277 |
+
# 技術ごとに結果を保存
|
| 278 |
+
sensitivity_results[tech] = total_costs
|
| 279 |
+
|
| 280 |
+
# 可視化
|
| 281 |
+
fig = go.Figure()
|
| 282 |
+
|
| 283 |
+
for tech, total_costs in sensitivity_results.items():
|
| 284 |
+
fig.add_trace(go.Scatter(
|
| 285 |
+
x=cost_multipliers,
|
| 286 |
+
y=total_costs,
|
| 287 |
+
mode='lines+markers',
|
| 288 |
+
name=f'{tech} Cost Sensitivity'
|
| 289 |
+
))
|
| 290 |
+
|
| 291 |
+
# グラフのレイアウト
|
| 292 |
+
fig.update_layout(
|
| 293 |
+
title='Cost Sensitivity Analysis: Impact of Cost Changes on Total System Cost',
|
| 294 |
+
xaxis_title='Cost Multiplier (0.5x to 1.5x)',
|
| 295 |
+
yaxis_title='Total System Cost (¥)',
|
| 296 |
+
hovermode='x unified',
|
| 297 |
+
plot_bgcolor='white',
|
| 298 |
+
xaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray'),
|
| 299 |
+
yaxis=dict(showgrid=True, gridwidth=0.5, gridcolor='lightgray')
|
| 300 |
+
)
|
| 301 |
+
|
| 302 |
+
return fig
|
| 303 |
+
|
| 304 |
+
# Streamlit UI setup
|
| 305 |
+
st.set_page_config(page_title='Renewable Energy System Optimization', layout='wide')
|
| 306 |
+
st.title('Renewable Energy System Optimization')
|
| 307 |
+
|
| 308 |
+
st.markdown("""
|
| 309 |
+
### Model Overview
|
| 310 |
+
|
| 311 |
+
This application is designed to optimize renewable energy systems for a specific region. The model allows the user to set the costs for different renewable energy technologies and battery storage, as well as minimum and maximum capacity limits for each technology. The optimization uses linear programming to minimize the total cost while ensuring demand is met, incorporating energy storage to help manage intermittency.
|
| 312 |
+
|
| 313 |
+
The renewable technologies considered are:
|
| 314 |
+
- Solar PV
|
| 315 |
+
- Onshore Wind
|
| 316 |
+
- Offshore Wind
|
| 317 |
+
- Run of River (Hydro)
|
| 318 |
+
|
| 319 |
+
The optimization problem aims to balance supply and demand at minimal cost, while also providing flexibility in the form of battery energy storage. Curtailment and battery state of charge are also considered in the model.
|
| 320 |
+
|
| 321 |
+
""")
|
| 322 |
+
|
| 323 |
+
with st.sidebar:
|
| 324 |
+
st.header('Input Parameters')
|
| 325 |
+
city_code = st.text_input("Enter City Code", value=999999)
|
| 326 |
+
solar_cost = st.number_input("Solar Capacity Cost (¥/MW)", value=80.0)
|
| 327 |
+
onshore_wind_cost = st.number_input("Onshore Wind Capacity Cost (¥/MW)", value=120.0)
|
| 328 |
+
offshore_wind_cost = st.number_input("Offshore Wind Capacity Cost (¥/MW)", value=180.0)
|
| 329 |
+
river_cost = st.number_input("River Capacity Cost (¥/MW)", value=1000.0)
|
| 330 |
+
battery_cost = st.number_input("Battery Cost (¥/MWh)", value=80.0)
|
| 331 |
+
yearly_demand = st.number_input("Yearly Power Demand (TWh/year)", value=15.0)
|
| 332 |
+
solar_range = st.slider("Solar Capacity Range (MW)", 0, 10000, (0, 10000))
|
| 333 |
+
wind_range = st.slider("Onshore Wind Capacity Range (MW)", 0, 10000, (0, 10000))
|
| 334 |
+
offshore_wind_range = st.slider("Offshore Wind Capacity Range (MW)", 0, 10000, (0, 10000))
|
| 335 |
+
river_range = st.slider("River Capacity Range (MW)", 0, 10000, (0, 10000))
|
| 336 |
+
|
| 337 |
+
calculated_optimal_energy_mix = False
|
| 338 |
+
|
| 339 |
+
if st.button('Calculate Optimal Energy Mix'):
|
| 340 |
+
fig_energy, heatmaps, curtailment_values, soc_per_hour, fig_capacity_ranges, renewable_capacity = optimize_energy_system(
|
| 341 |
+
city_code, solar_cost, onshore_wind_cost, offshore_wind_cost, river_cost, battery_cost, yearly_demand, solar_range, wind_range, river_range, offshore_wind_range
|
| 342 |
+
)
|
| 343 |
+
|
| 344 |
+
if fig_energy:
|
| 345 |
+
st.plotly_chart(fig_energy, use_container_width=True, height=800)
|
| 346 |
+
|
| 347 |
+
# Additional visualizations
|
| 348 |
+
st.markdown("### Hourly Capacity Factor Heatmaps")
|
| 349 |
+
for fig_heatmap in heatmaps:
|
| 350 |
+
st.plotly_chart(fig_heatmap, use_container_width=True, height=800)
|
| 351 |
+
|
| 352 |
+
st.markdown("### Additional Analysis")
|
| 353 |
+
st.markdown("The following plots provide additional insights into the renewable energy mix, curtailment, and electricity price variations.")
|
| 354 |
+
|
| 355 |
+
# Plot curtailment over time
|
| 356 |
+
curtailment_df = pd.DataFrame({"Time": fig_energy.data[0].x, "Curtailment (MW)": curtailment_values})
|
| 357 |
+
fig_curtailment = px.line(curtailment_df, x='Time', y='Curtailment (MW)', title='Curtailment Over Time', template='plotly_white')
|
| 358 |
+
st.plotly_chart(fig_curtailment, use_container_width=True, height=800)
|
| 359 |
+
|
| 360 |
+
# Plot electricity price variation over time
|
| 361 |
+
soc_df = pd.DataFrame({"Time": fig_energy.data[0].x, "State of charge [%]": soc_per_hour})
|
| 362 |
+
fig_battery_operation = px.line(soc_df, x='Time', y='State of charge [%]', title='State of charge in battery', template='plotly_white')
|
| 363 |
+
st.plotly_chart(fig_battery_operation, use_container_width=True, height=800)
|
| 364 |
+
|
| 365 |
+
# Plot optimized capacity vs. capacity ranges
|
| 366 |
+
st.plotly_chart(fig_capacity_ranges, use_container_width=True, height=800)
|
| 367 |
+
|
| 368 |
+
calculated_optimal_energy_mix = True
|
| 369 |
+
|
| 370 |
+
# Streamlit UIに感度解析ボタンを追加
|
| 371 |
+
if st.button('Analyze Cost Sensitivity'):
|
| 372 |
+
if calculated_optimal_energy_mix:
|
| 373 |
+
fig_sensitivity = analyze_cost_sensitivity({
|
| 374 |
+
'solar': solar_cost,
|
| 375 |
+
'onshore_wind': onshore_wind_cost,
|
| 376 |
+
'offshore_wind': offshore_wind_cost,
|
| 377 |
+
'river': river_cost
|
| 378 |
+
}, ['solar', 'onshore_wind', 'offshore_wind', 'river'], renewable_capacity)
|
| 379 |
+
st.plotly_chart(fig_sensitivity, use_container_width=True, height=800)
|
| 380 |
+
else:
|
| 381 |
+
st.error("Please calculate the optimal energy mix first before running the cost sensitivity analysis.")
|