Skip to content

Commit

Permalink
Formatting fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
shuds13 committed Feb 26, 2025
1 parent 1fe7221 commit 93f0846
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 25 deletions.
1 change: 1 addition & 0 deletions libensemble/gen_funcs/persistent_ax_multitask.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
TaskChoiceToIntTaskChoice,
]


# get_MTGP based on https://ax.dev/docs/tutorials/multi_task/
def get_MTGP(
experiment: Experiment,
Expand Down
1 change: 1 addition & 0 deletions libensemble/sim_funcs/rosenbrock.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

__all__ = ["rosenbrock_eval"]


def EvaluateFunction(x, component=None):
"""
Evaluates the chained Rosenbrock function
Expand Down
26 changes: 13 additions & 13 deletions scripts/aposmm_min_loc.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

N = 6 # number of opt runs to show.

x_name = 'x0'
y_name = 'x1'
z_name = 'x2'
x_name = "x0"
y_name = "x1"
z_name = "x2"

full_bounds = False # For entire input space enter bounds below

Expand All @@ -35,7 +35,7 @@
# Find the most recent .npy and pickle files
try:
H_file = max(glob.glob("*.npy"), key=os.path.getmtime)
persis_info_file = max(glob.iglob('*.pickle'), key=os.path.getctime)
persis_info_file = max(glob.iglob("*.pickle"), key=os.path.getctime)
except Exception:
sys.exit("Need a *.npy and a *.pickle files in run dir. Exiting...")

Expand All @@ -45,30 +45,30 @@
index_sets = pickle.load(f)["run_order"]

# Filter best N opt runs for clearer graph
trimmed_index_sets = {key: indices[:-1] for key, indices in index_sets.items()}
min_f_per_set = [(key, indices, H['f'][indices].min()) for key, indices in trimmed_index_sets.items() if len(indices) > 0]
trim_sets = {key: indices[:-1] for key, indices in index_sets.items()}
min_f_per_set = [(key, indices, H["f"][indices].min()) for key, indices in trim_sets.items() if len(indices) > 0]
min_f_per_set_sorted = sorted(min_f_per_set, key=lambda x: x[2])[:N]

# Plotting
fig = plt.figure(figsize=(6, 6))
ax = fig.add_subplot(111, projection='3d')
ax = fig.add_subplot(111, projection="3d")

for key, indices, _ in min_f_per_set_sorted:
min_f_index = indices[np.argmin(H['f'][indices])]
min_f_index = indices[np.argmin(H["f"][indices])]

# Extract the corresponding 3D x position from H
try:
x, y, z = H['x'][min_f_index]
x, y, z = H["x"][min_f_index]
except ValueError:
x = H[x_name][min_f_index]
y = H[y_name][min_f_index]
z = H[z_name][min_f_index]

# Plot the 3D point
ax.scatter(x, y, z, marker='o', s=50, label=f'Opt run {key}')
ax.scatter(x, y, z, marker="o", s=50, label=f"Opt run {key}")

# Draw a line from the point to the XY plane (z=0)
ax.plot([x, x], [y, y], [0, z], color='grey', linestyle='--')
ax.plot([x, x], [y, y], [0, z], color="grey", linestyle="--")

if full_bounds:
ax.set_xlim(x0_min, x0_max)
Expand All @@ -79,6 +79,6 @@
ax.set_xlabel(x_name)
ax.set_ylabel(y_name)
ax.set_zlabel(z_name)
ax.set_title('Locations of best points from each optimization run')
ax.legend(bbox_to_anchor=(-0.1, 0.9), loc='upper left', borderaxespad=0)
ax.set_title("Locations of best points from each optimization run")
ax.legend(bbox_to_anchor=(-0.1, 0.9), loc="upper left", borderaxespad=0)
plt.savefig(f"location_min_best{N}.png")
24 changes: 12 additions & 12 deletions scripts/aposmm_plot_local_runs.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# Find the most recent .npy and pickle files
try:
H_file = max(glob.glob("*.npy"), key=os.path.getmtime)
persis_info_file = max(glob.iglob('*.pickle'), key=os.path.getctime)
persis_info_file = max(glob.iglob("*.pickle"), key=os.path.getctime)
except Exception:
sys.exit("Need a *.npy and a *.pickle files in run dir. Exiting...")

Expand All @@ -31,38 +31,38 @@
index_sets = pickle.load(f)["run_order"]

# Filter best N opt runs for clearer graph
trimmed_index_sets = {key: indices[:-1] for key, indices in index_sets.items()}
min_f_per_set = [(key, indices, H['f'][indices].min()) for key, indices in trimmed_index_sets.items() if len(indices) > 0]
trim_sets = {key: indices[:-1] for key, indices in index_sets.items()}
min_f_per_set = [(key, indices, H["f"][indices].min()) for key, indices in trim_sets.items() if len(indices) > 0]
min_f_per_set_sorted = sorted(min_f_per_set, key=lambda x: x[2])[:N]

# Plotting
plt.figure(figsize=(10, 6))
plt.scatter(range(len(H['f'])), H['f'], color='lightgrey', label='Other points', zorder=1)
plt.scatter(range(len(H["f"])), H["f"], color="lightgrey", label="Other points", zorder=1)

num_runs = len(index_sets)
# for key, indices in index_sets.items():
for key, indices, _ in min_f_per_set_sorted:
# Extract the 'f' values for each index set
f_values = H['f'][indices]
f_values = H["f"][indices]

ratio = (f_values[-1] - f_values[-2])/ f_values[-2]
ratio = (f_values[-1] - f_values[-2]) / f_values[-2]
# print(f"Tolerance of the last two values for {key}: {ratio}")

plt.plot(indices, f_values, marker='o', label=f'Opt run {key}', zorder=2)
plt.plot(indices, f_values, marker="o", label=f"Opt run {key}", zorder=2)

# Identify the index and value of the minimum f
min_index = indices[np.argmin(f_values)]
min_f_value = np.min(f_values)

# Mark the minimum f value with a distinct marker
plt.scatter(min_index, min_f_value, color='red', edgecolor='black', s=50, zorder=3)
plt.scatter(min_index, min_f_value, color="red", edgecolor="black", s=50, zorder=3)

# Add a dummy point to the legend for "minima of opt run"
plt.scatter([], [], color='red', edgecolor='black', s=50, label='Best value of opt run')
plt.scatter([], [], color="red", edgecolor="black", s=50, label="Best value of opt run")

plt.xlabel('Simulation ID')
plt.ylabel('Objective value')
plt.title(f'Objective values by Optimization runs. Best {N} runs from {num_runs}')
plt.xlabel("Simulation ID")
plt.ylabel("Objective value")
plt.title(f"Objective values by Optimization runs. Best {N} runs from {num_runs}")
plt.legend()
plt.grid(True)
plt.savefig(f"opt_runs_best{N}.png")

0 comments on commit 93f0846

Please sign in to comment.