Update app.py
Browse files
app.py
CHANGED
|
@@ -79,7 +79,7 @@ def optimize_energy_system(city_code, solar_cost, onshore_wind_cost, offshore_wi
|
|
| 79 |
alternative_solutions = []
|
| 80 |
for threshold in thresholds:
|
| 81 |
relaxed_cost = optimal_cost * (1 + threshold)
|
| 82 |
-
for tech in selected_tech: #
|
| 83 |
# Minimize capacity of each technology
|
| 84 |
alt_model_min = pulp.LpProblem(f"AlternativeModel_Min_{tech}_{threshold}", pulp.LpMinimize)
|
| 85 |
alt_model_min += pulp.lpSum([renewable_capacity[(r, g)] * renewable_capacity_cost[g]
|
|
@@ -140,27 +140,27 @@ def plot_capacity_distribution(alternative_solutions, selected_technologies):
|
|
| 140 |
|
| 141 |
capacity_df = pd.DataFrame(capacity_data)
|
| 142 |
|
| 143 |
-
# Create violin plot with Plotly Express
|
| 144 |
-
fig_violin = px.violin(capacity_df,
|
| 145 |
-
box=True, points="all", title="Capacity Distribution by Technology")
|
| 146 |
-
fig_violin.update_layout(
|
| 147 |
|
| 148 |
return fig_violin
|
| 149 |
|
| 150 |
# Function to create cost breakdown stacked bar plot for each threshold and technology type
|
| 151 |
def plot_cost_breakdown(alternative_solutions, selected_technologies, renewable_capacity_cost, battery_cost_per_mwh):
|
| 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')
|
|
|
|
| 79 |
alternative_solutions = []
|
| 80 |
for threshold in thresholds:
|
| 81 |
relaxed_cost = optimal_cost * (1 + threshold)
|
| 82 |
+
for tech in selected_tech: # Run only for selected technologies
|
| 83 |
# Minimize capacity of each technology
|
| 84 |
alt_model_min = pulp.LpProblem(f"AlternativeModel_Min_{tech}_{threshold}", pulp.LpMinimize)
|
| 85 |
alt_model_min += pulp.lpSum([renewable_capacity[(r, g)] * renewable_capacity_cost[g]
|
|
|
|
| 140 |
|
| 141 |
capacity_df = pd.DataFrame(capacity_data)
|
| 142 |
|
| 143 |
+
# Create violin plot with Plotly Express (horizontal)
|
| 144 |
+
fig_violin = px.violin(capacity_df, y="Technology", x="Capacity (MW)", color="Technology",
|
| 145 |
+
box=True, points="all", orientation="h", title="Capacity Distribution by Technology")
|
| 146 |
+
fig_violin.update_layout(yaxis_title="Technology", xaxis_title="Installed Capacity (MW)")
|
| 147 |
|
| 148 |
return fig_violin
|
| 149 |
|
| 150 |
# Function to create cost breakdown stacked bar plot for each threshold and technology type
|
| 151 |
def plot_cost_breakdown(alternative_solutions, selected_technologies, renewable_capacity_cost, battery_cost_per_mwh):
|
| 152 |
# Generate a bar plot for each case based on threshold and technology type
|
| 153 |
+
for idx, sol in enumerate(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 with unique key to avoid StreamlitDuplicateElementId error
|
| 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, key=f"cost_plot_{idx}")
|
| 164 |
|
| 165 |
# Streamlit UI setup
|
| 166 |
st.set_page_config(page_title='Renewable Energy System Optimization with MGA', layout='wide')
|