Update app.py
Browse files
app.py
CHANGED
|
@@ -5,153 +5,10 @@ import pulp
|
|
| 5 |
import plotly.graph_objs as go
|
| 6 |
import plotly.express as px
|
| 7 |
import numpy as np
|
|
|
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
url = f"https://energy-sustainability.jp/_ajax/renewable_energy/get/?code={city_code}"
|
| 12 |
-
response = requests.get(url)
|
| 13 |
-
if response.status_code != 200:
|
| 14 |
-
return None, "Failed to retrieve data."
|
| 15 |
-
|
| 16 |
-
data = response.json()
|
| 17 |
-
if not data:
|
| 18 |
-
return None, "No data found."
|
| 19 |
-
|
| 20 |
-
base_times = data[next(iter(data))]['x']
|
| 21 |
-
result_df = pd.DataFrame({"Time": base_times})
|
| 22 |
-
|
| 23 |
-
for energy_type, energy_data in data.items():
|
| 24 |
-
if 'x' in energy_data and 'y' in energy_data: # 修正箇所
|
| 25 |
-
values = energy_data['y']
|
| 26 |
-
result_df[f"{energy_type} hourly capacity factor"] = values
|
| 27 |
-
|
| 28 |
-
return result_df, None
|
| 29 |
-
|
| 30 |
-
# Function to optimize the energy system and create visualizations with MGA
|
| 31 |
-
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, thresholds):
|
| 32 |
-
data, error = get_renewable_energy_data(city_code)
|
| 33 |
-
if error:
|
| 34 |
-
st.error(error)
|
| 35 |
-
return None, None, None, None, None, None, None
|
| 36 |
-
|
| 37 |
-
for col in data.columns[1:]:
|
| 38 |
-
data[col] = pd.to_numeric(data[col], errors='coerce')
|
| 39 |
-
data = data.fillna(0)
|
| 40 |
-
|
| 41 |
-
time_steps = range(len(data['Time']))
|
| 42 |
-
solar_cf = data['solar hourly capacity factor']
|
| 43 |
-
onshore_wind_cf = data['onshore_wind hourly capacity factor']
|
| 44 |
-
offshore_wind_cf = data['offshore_wind hourly capacity factor']
|
| 45 |
-
river_cf = data['river hourly capacity factor']
|
| 46 |
-
demand_cf = data['demand hourly capacity factor']
|
| 47 |
-
|
| 48 |
-
regions = ['region1']
|
| 49 |
-
technologies = ['solar', 'onshore_wind', 'offshore_wind', 'river']
|
| 50 |
-
capacity_factor = {
|
| 51 |
-
'solar': solar_cf,
|
| 52 |
-
'onshore_wind': onshore_wind_cf,
|
| 53 |
-
'offshore_wind': offshore_wind_cf,
|
| 54 |
-
'river': river_cf
|
| 55 |
-
}
|
| 56 |
-
|
| 57 |
-
renewable_capacity_cost = {'solar': solar_cost, 'onshore_wind': onshore_wind_cost, 'offshore_wind': offshore_wind_cost, 'river': river_cost}
|
| 58 |
-
battery_cost_per_mwh = battery_cost
|
| 59 |
-
battery_efficiency = 0.9
|
| 60 |
-
|
| 61 |
-
demand = demand_cf * yearly_demand / 100 * 1000 * 1000
|
| 62 |
-
|
| 63 |
-
renewable_capacity = pulp.LpVariable.dicts("renewable_capacity",
|
| 64 |
-
[(r, g) for r in regions for g in technologies],
|
| 65 |
-
lowBound=0, cat='Continuous')
|
| 66 |
-
curtailment = pulp.LpVariable.dicts("curtailment",
|
| 67 |
-
[(r, t) for r in regions for t in time_steps],
|
| 68 |
-
lowBound=0, cat='Continuous')
|
| 69 |
-
battery_capacity = pulp.LpVariable("battery_capacity", lowBound=0, cat='Continuous')
|
| 70 |
-
battery_charge = pulp.LpVariable.dicts("battery_charge", time_steps, lowBound=0, cat='Continuous')
|
| 71 |
-
battery_discharge = pulp.LpVariable.dicts("battery_discharge", time_steps, lowBound=0, cat='Continuous')
|
| 72 |
-
SOC = pulp.LpVariable.dicts("SOC", time_steps, lowBound=0, cat='Continuous')
|
| 73 |
-
|
| 74 |
-
model = pulp.LpProblem("EnergySystemOptimizationWithBattery", pulp.LpMinimize)
|
| 75 |
-
|
| 76 |
-
# Objective: minimize total cost (renewable capacities and battery)
|
| 77 |
-
model += pulp.lpSum([renewable_capacity[(r, g)] * renewable_capacity_cost[g]
|
| 78 |
-
for r in regions for g in technologies]) + \
|
| 79 |
-
battery_capacity * battery_cost_per_mwh, "TotalCost"
|
| 80 |
-
|
| 81 |
-
# Constraints: meet demand, manage battery SOC
|
| 82 |
-
for r in regions:
|
| 83 |
-
for t in time_steps:
|
| 84 |
-
model += pulp.lpSum([renewable_capacity[(r, g)] * capacity_factor[g][t]
|
| 85 |
-
for g in technologies]) + battery_discharge[t] == demand[t] + battery_charge[t] + curtailment[(r, t)], f"DemandConstraint_{r}_{t}"
|
| 86 |
-
if t == 0:
|
| 87 |
-
model += SOC[t] == battery_charge[t] * battery_efficiency - battery_discharge[t] * (1 / battery_efficiency), f"SOCUpdate_{t}"
|
| 88 |
-
else:
|
| 89 |
-
model += SOC[t] == SOC[t - 1] + battery_charge[t] * battery_efficiency - battery_discharge[t] * (1 / battery_efficiency), f"SOCUpdate_{t}"
|
| 90 |
-
model += SOC[t] <= battery_capacity, f"SOCUpperBound_{t}"
|
| 91 |
-
|
| 92 |
-
# Capacity range constraints
|
| 93 |
-
model += renewable_capacity[('region1', 'solar')] >= solar_range[0]
|
| 94 |
-
model += renewable_capacity[('region1', 'solar')] <= solar_range[1]
|
| 95 |
-
model += renewable_capacity[('region1', 'onshore_wind')] >= wind_range[0]
|
| 96 |
-
model += renewable_capacity[('region1', 'onshore_wind')] <= wind_range[1]
|
| 97 |
-
model += renewable_capacity[('region1', 'offshore_wind')] >= offshore_wind_range[0]
|
| 98 |
-
model += renewable_capacity[('region1', 'offshore_wind')] <= offshore_wind_range[1]
|
| 99 |
-
model += renewable_capacity[('region1', 'river')] >= river_range[0]
|
| 100 |
-
model += renewable_capacity[('region1', 'river')] <= river_range[1]
|
| 101 |
-
|
| 102 |
-
# Solve the initial model to find the optimal solution
|
| 103 |
-
model.solve()
|
| 104 |
-
optimal_cost = pulp.value(model.objective)
|
| 105 |
-
|
| 106 |
-
# MGA: Generate alternative solutions
|
| 107 |
-
mga_models = []
|
| 108 |
-
alternative_solutions = []
|
| 109 |
-
for threshold in thresholds:
|
| 110 |
-
relaxed_cost = optimal_cost * (1 + threshold)
|
| 111 |
-
for tech in technologies:
|
| 112 |
-
# Minimize capacity of each technology
|
| 113 |
-
alt_model_min = pulp.LpProblem(f"AlternativeModel_Min_{tech}_{threshold}", pulp.LpMinimize)
|
| 114 |
-
alt_model_min += pulp.lpSum([renewable_capacity[(r, g)] * renewable_capacity_cost[g]
|
| 115 |
-
for r in regions for g in technologies]) + battery_capacity * battery_cost_per_mwh <= relaxed_cost
|
| 116 |
-
|
| 117 |
-
# Copy original constraints with unique names
|
| 118 |
-
for name, constraint in model.constraints.items():
|
| 119 |
-
alt_model_min += constraint.copy(), f"{name}_min_{tech}_{threshold}"
|
| 120 |
-
|
| 121 |
-
# Minimize the capacity of the selected technology
|
| 122 |
-
alt_model_min += renewable_capacity[('region1', tech)], f"Minimize_{tech}_Capacity"
|
| 123 |
-
alt_model_min.solve()
|
| 124 |
-
if pulp.LpStatus[alt_model_min.status] == 'Optimal':
|
| 125 |
-
alternative_solutions.append({
|
| 126 |
-
'threshold': threshold,
|
| 127 |
-
'type': 'min',
|
| 128 |
-
'technology': tech,
|
| 129 |
-
'solution': {g: renewable_capacity[('region1', g)].varValue for g in technologies},
|
| 130 |
-
'battery_capacity': battery_capacity.varValue
|
| 131 |
-
})
|
| 132 |
-
|
| 133 |
-
# Maximize capacity of each technology
|
| 134 |
-
alt_model_max = pulp.LpProblem(f"AlternativeModel_Max_{tech}_{threshold}", pulp.LpMinimize)
|
| 135 |
-
alt_model_max += pulp.lpSum([renewable_capacity[(r, g)] * renewable_capacity_cost[g]
|
| 136 |
-
for r in regions for g in technologies]) + battery_capacity * battery_cost_per_mwh <= relaxed_cost
|
| 137 |
-
|
| 138 |
-
# Copy original constraints with unique names
|
| 139 |
-
for name, constraint in model.constraints.items():
|
| 140 |
-
alt_model_max += constraint.copy(), f"{name}_max_{tech}_{threshold}"
|
| 141 |
-
|
| 142 |
-
# Maximize the capacity of the selected technology
|
| 143 |
-
alt_model_max += -renewable_capacity[('region1', tech)], f"Maximize_{tech}_Capacity"
|
| 144 |
-
alt_model_max.solve()
|
| 145 |
-
if pulp.LpStatus[alt_model_max.status] == 'Optimal':
|
| 146 |
-
alternative_solutions.append({
|
| 147 |
-
'threshold': threshold,
|
| 148 |
-
'type': 'max',
|
| 149 |
-
'technology': tech,
|
| 150 |
-
'solution': {g: renewable_capacity[('region1', g)].varValue for g in technologies},
|
| 151 |
-
'battery_capacity': battery_capacity.varValue
|
| 152 |
-
})
|
| 153 |
-
|
| 154 |
-
return alternative_solutions
|
| 155 |
|
| 156 |
# Streamlit UI setup
|
| 157 |
st.set_page_config(page_title='Renewable Energy System Optimization with MGA', layout='wide')
|
|
@@ -202,4 +59,4 @@ if st.button("Run MGA Optimization"):
|
|
| 202 |
ax.grid(True, linestyle='--', alpha=0.7)
|
| 203 |
|
| 204 |
# Streamlitにプロットを表示
|
| 205 |
-
st.pyplot(fig)
|
|
|
|
| 5 |
import plotly.graph_objs as go
|
| 6 |
import plotly.express as px
|
| 7 |
import numpy as np
|
| 8 |
+
import matplotlib.pyplot as plt # ここでpltをインポート
|
| 9 |
|
| 10 |
+
# 先ほどのコードからの関数: optimize_energy_system()
|
| 11 |
+
# ここに alternative_solutions が返されるコードが既に含まれていると仮定しています。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# Streamlit UI setup
|
| 14 |
st.set_page_config(page_title='Renewable Energy System Optimization with MGA', layout='wide')
|
|
|
|
| 59 |
ax.grid(True, linestyle='--', alpha=0.7)
|
| 60 |
|
| 61 |
# Streamlitにプロットを表示
|
| 62 |
+
st.pyplot(fig)
|