naohiro701 commited on
Commit
fe4d036
·
verified ·
1 Parent(s): 4e039d2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -30
app.py CHANGED
@@ -62,13 +62,7 @@ def optimize_energy_system(city_code, solar_cost, onshore_wind_cost, offshore_wi
62
  renewable_capacity = pulp.LpVariable.dicts("renewable_capacity",
63
  [(r, g) for r in regions for g in technologies],
64
  lowBound=0, cat='Continuous')
65
- curtailment = pulp.LpVariable.dicts("curtailment",
66
- [(r, t) for r in regions for t in time_steps],
67
- lowBound=0, cat='Continuous')
68
  battery_capacity = pulp.LpVariable("battery_capacity", lowBound=0, cat='Continuous')
69
- battery_charge = pulp.LpVariable.dicts("battery_charge", time_steps, lowBound=0, cat='Continuous')
70
- battery_discharge = pulp.LpVariable.dicts("battery_discharge", time_steps, lowBound=0, cat='Continuous')
71
- SOC = pulp.LpVariable.dicts("SOC", time_steps, lowBound=0, cat='Continuous')
72
 
73
  model = pulp.LpProblem("EnergySystemOptimizationWithBattery", pulp.LpMinimize)
74
 
@@ -77,27 +71,6 @@ def optimize_energy_system(city_code, solar_cost, onshore_wind_cost, offshore_wi
77
  for r in regions for g in technologies]) + \
78
  battery_capacity * battery_cost_per_mwh, "TotalCost"
79
 
80
- # Constraints: meet demand, manage battery SOC
81
- for r in regions:
82
- for t in time_steps:
83
- model += pulp.lpSum([renewable_capacity[(r, g)] * capacity_factor[g][t]
84
- for g in technologies]) + battery_discharge[t] == demand[t] + battery_charge[t] + curtailment[(r, t)], f"DemandConstraint_{r}_{t}"
85
- if t == 0:
86
- model += SOC[t] == battery_charge[t] * battery_efficiency - battery_discharge[t] * (1 / battery_efficiency), f"SOCUpdate_{t}"
87
- else:
88
- model += SOC[t] == SOC[t - 1] + battery_charge[t] * battery_efficiency - battery_discharge[t] * (1 / battery_efficiency), f"SOCUpdate_{t}"
89
- model += SOC[t] <= battery_capacity, f"SOCUpperBound_{t}"
90
-
91
- # Capacity range constraints
92
- model += renewable_capacity[('region1', 'solar')] >= solar_range[0]
93
- model += renewable_capacity[('region1', 'solar')] <= solar_range[1]
94
- model += renewable_capacity[('region1', 'onshore_wind')] >= wind_range[0]
95
- model += renewable_capacity[('region1', 'onshore_wind')] <= wind_range[1]
96
- model += renewable_capacity[('region1', 'offshore_wind')] >= offshore_wind_range[0]
97
- model += renewable_capacity[('region1', 'offshore_wind')] <= offshore_wind_range[1]
98
- model += renewable_capacity[('region1', 'river')] >= river_range[0]
99
- model += renewable_capacity[('region1', 'river')] <= river_range[1]
100
-
101
  # Solve the initial model to find the optimal solution
102
  model.solve()
103
  optimal_cost = pulp.value(model.objective)
@@ -168,12 +141,27 @@ def plot_capacity_distribution(alternative_solutions, selected_technologies):
168
  capacity_df = pd.DataFrame(capacity_data)
169
 
170
  # Create violin plot with Plotly Express
171
- fig_violin = px.violin(capacity_df, x="Threshold (%)", y="Capacity (MW)", color="Technology",
172
- box=True, points="all", title="Capacity Distribution by Technology and Threshold")
173
- fig_violin.update_layout(xaxis_title="Cost Deviation Threshold (%)", yaxis_title="Installed Capacity (MW)")
174
 
175
  return fig_violin
176
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
177
  # Streamlit UI setup
178
  st.set_page_config(page_title='Renewable Energy System Optimization with MGA', layout='wide')
179
  st.title('Modeling to Generate Alternatives (MGA) in Renewable Energy System Optimization')
@@ -206,3 +194,6 @@ if st.button("Run MGA Optimization"):
206
  # Display capacity distribution using violin plots
207
  fig_violin = plot_capacity_distribution(alternative_solutions, selected_technologies)
208
  st.plotly_chart(fig_violin, use_container_width=True)
 
 
 
 
62
  renewable_capacity = pulp.LpVariable.dicts("renewable_capacity",
63
  [(r, g) for r in regions for g in technologies],
64
  lowBound=0, cat='Continuous')
 
 
 
65
  battery_capacity = pulp.LpVariable("battery_capacity", lowBound=0, cat='Continuous')
 
 
 
66
 
67
  model = pulp.LpProblem("EnergySystemOptimizationWithBattery", pulp.LpMinimize)
68
 
 
71
  for r in regions for g in technologies]) + \
72
  battery_capacity * battery_cost_per_mwh, "TotalCost"
73
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
74
  # Solve the initial model to find the optimal solution
75
  model.solve()
76
  optimal_cost = pulp.value(model.objective)
 
141
  capacity_df = pd.DataFrame(capacity_data)
142
 
143
  # Create violin plot with Plotly Express
144
+ fig_violin = px.violin(capacity_df, x="Technology", y="Capacity (MW)", color="Technology",
145
+ box=True, points="all", title="Capacity Distribution by Technology")
146
+ fig_violin.update_layout(xaxis_title="Technology", yaxis_title="Installed Capacity (MW)")
147
 
148
  return fig_violin
149
 
150
+ # Function to create cost breakdown stacked bar plot for each threshold
151
+ def plot_cost_breakdown(alternative_solutions, selected_technologies):
152
+ # Generate a bar plot for each case based on threshold and technology type
153
+ for sol in alternative_solutions:
154
+ cost_data = {
155
+ 'Technology': selected_technologies + ['Battery'],
156
+ 'Cost': [sol['solution'][tech] * renewable_capacity_cost[tech] for tech in selected_technologies] + [sol['battery_capacity'] * battery_cost_per_mwh]
157
+ }
158
+ cost_df = pd.DataFrame(cost_data)
159
+
160
+ # Create stacked bar chart
161
+ fig_bar = px.bar(cost_df, x='Technology', y='Cost', title=f"Cost Breakdown (Threshold: {sol['threshold'] * 100}%, Type: {sol['type']})",
162
+ labels={'Cost': 'Cost (¥)'})
163
+ st.plotly_chart(fig_bar, use_container_width=True)
164
+
165
  # Streamlit UI setup
166
  st.set_page_config(page_title='Renewable Energy System Optimization with MGA', layout='wide')
167
  st.title('Modeling to Generate Alternatives (MGA) in Renewable Energy System Optimization')
 
194
  # Display capacity distribution using violin plots
195
  fig_violin = plot_capacity_distribution(alternative_solutions, selected_technologies)
196
  st.plotly_chart(fig_violin, use_container_width=True)
197
+
198
+ # Display cost breakdown stacked bar plots for each case
199
+ plot_cost_breakdown(alternative_solutions, selected_technologies)