Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
185 changes: 0 additions & 185 deletions bird/postprocess/SA_optimization/Xdata_study_scaleup_0_4vvm_3000W.csv

This file was deleted.

10 changes: 7 additions & 3 deletions bird/postprocess/SA_optimization/get_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ def get_config_result(study_fold: str = ".") -> None:
count = 0

# Save data into CSV files
xfname = f"Xdata_{study_fold}.csv"
yfname = f"ydata_{study_fold}.csv"
xfname = os.path.join(study_fold, f"Xdata.csv")
yfname = os.path.join(study_fold, f"ydata.csv")
with open(xfname, "w", newline="") as csvfile:
writer = csv.writer(csvfile)
for sims in results:
Expand All @@ -52,6 +52,10 @@ def get_config_result(study_fold: str = ".") -> None:


if __name__ == "__main__":
studies = {"study_scaleup_0_4vvm_3000W": r"0.0036$m^3$ 0.4vvm 0W"}
studies = {
"study_scaleup_0_4vvm_3000W": r"608$m^3$ 0.4vvm 3000W",
"study_scaleup_0_1vvm_6000W": r"608$m^3$ 0.1vvm 6000W",
"study_0_4vvm_1W": r"0.00361$m^3$ 0.4vvm 1W",
}
for study in studies:
get_config_result(study)
89 changes: 67 additions & 22 deletions bird/postprocess/SA_optimization/get_optimal.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import random
import warnings

Expand Down Expand Up @@ -90,6 +91,7 @@ def run_optimization(
n_runs: int = 10,
max_iters: int = 1000,
bootstrap_size: int = 100,
out_folder: str = ".",
):
"""
Bootstraps data, runs optimization and postprocesses the results.
Expand All @@ -115,6 +117,8 @@ def run_optimization(
maximum number of SA iterations
bootstrap_size : int
size of bootstrap samples
out_folder: str
folder where to output the results

Returns
----------
Expand Down Expand Up @@ -175,7 +179,10 @@ def run_optimization(
]
)
df.to_csv(
f"best_bootstrap_solution_{model_type}_size_{bootstrap_size}.csv",
os.path.join(
out_folder,
f"best_bootstrap_solution_{model_type}_size_{bootstrap_size}.csv",
),
index=False,
)

Expand All @@ -186,7 +193,7 @@ def run_optimization(
upper_bound = mean_trace + 1.96 * std_trace / np.sqrt(n_runs)
iterations = np.arange(max_iters + 1)

plt.figure(figsize=(8, 4))
plt.figure(figsize=(8, 6))
plt.plot(
iterations,
mean_trace,
Expand All @@ -203,19 +210,25 @@ def run_optimization(
)
pretty_labels(
"Iteration",
"Best Surrogate-Predicted Objective",
fontsize=16,
r"Predicted QOI [kg$^2$/kWh$^2$]",
fontsize=20,
title=f"Mean Convergence with 95% Confidence Interval ({model_type.upper()})",
grid=True,
fontname="Times",
)
pretty_legend(fontsize=16, fontname="Times")
pretty_legend(fontsize=20, fontname="Times")
plt.savefig(
f"Mean_Convergence_plot_{model_type}_size_{bootstrap_size}.png",
os.path.join(
out_folder,
f"Mean_Convergence_plot_{model_type}_size_{bootstrap_size}.png",
),
dpi=300,
)
plt.savefig(
f"Mean_Convergence_plot_{model_type}_size_{bootstrap_size}.pdf",
os.path.join(
out_folder,
f"Mean_Convergence_plot_{model_type}_size_{bootstrap_size}.pdf",
),
)
plt.show()

Expand Down Expand Up @@ -255,24 +268,56 @@ def run_optimization(
pretty_legend(fontsize=16, fontname="Times")
plt.tight_layout()
plt.savefig(
f"Mean_L1_distance_{model_type}_size_{bootstrap_size}.png", dpi=300
os.path.join(
out_folder,
f"Mean_L1_distance_{model_type}_size_{bootstrap_size}.png",
),
dpi=300,
)
plt.savefig(
os.path.join(
out_folder,
f"Mean_L1_distance_{model_type}_size_{bootstrap_size}.pdf",
)
)
plt.savefig(f"Mean_L1_distance_{model_type}_size_{bootstrap_size}.pdf")
plt.show()


if __name__ == "__main__":

# Read data from the csv file.
X_raw_data = pd.read_csv("Xdata_study_scaleup_0_4vvm_3000W.csv")
y_raw_data = pd.read_csv("ydata_study_scaleup_0_4vvm_3000W.csv")

X = X_raw_data.values
y = y_raw_data.iloc[:, :-1].values
# We want to maximize, so we minimize the opposite
y = y * -1

# The function will build, tune, and optimize the surrogate model and postprocess the results.
run_optimization(
X, y, model_type="rbf", n_runs=10, max_iters=1000, bootstrap_size=150
)
# studies = ["study_scaleup_0_4vvm_3000W", "study_scaleup_0_1vvm_6000W", "study_0_4vvm_1W"]
# studies = ["study_scaleup_0_1vvm_6000W", "study_0_4vvm_1W"]
studies = ["study_scaleup_0_4vvm_3000W", "study_0_4vvm_1W"]

for study in studies:
# Read data from the csv file.
X_raw_data = pd.read_csv(os.path.join(study, "Xdata.csv"))
y_raw_data = pd.read_csv(os.path.join(study, "ydata.csv"))

X = X_raw_data.values
y = y_raw_data.iloc[:, :-1].values
# We want to maximize, so we minimize the opposite
y = y * -1

# The function will build, tune, and optimize the surrogate model and postprocess the results.
run_optimization(
X,
y,
model_type="rbf",
n_runs=10,
max_iters=1000,
bootstrap_size=150,
out_folder=study,
)
# run_optimization(
# X,
# y,
# model_type="rf",
# n_runs=10,
# max_iters=1000,
# bootstrap_size=150,
# out_folder=study,
# )
# run_optimization(
# X, y, model_type="nn", n_runs=10, max_iters=1000, bootstrap_size=150, out_folder=study
# )
Loading