Show optimal value in the plot
This commit is contained in:
parent
9274b9ca69
commit
46bb8234cd
1 changed files with 40 additions and 12 deletions
52
src/ui.py
52
src/ui.py
|
@ -153,25 +153,53 @@ class OptimizationUI(QWidget):
|
|||
This is a call-back for pressing the run button.
|
||||
"""
|
||||
settings = self.current_settings
|
||||
print("-" * 80)
|
||||
print(self.function_dropdown.currentText())
|
||||
print(settings)
|
||||
|
||||
values: list[float] = []
|
||||
x = None
|
||||
for x in min_search(**settings):
|
||||
y = settings["function"](x)
|
||||
values.append(y)
|
||||
print(f"{x} -> {y}")
|
||||
|
||||
self.plot_results(values)
|
||||
if x is None:
|
||||
raise RuntimeError("Number of iterations can't be 0")
|
||||
|
||||
def plot_results(self, values: list[float]) -> None:
|
||||
self.plot_results(values, x)
|
||||
|
||||
def plot_results(self, values: list[float], optimal_x: INPUT_VECTOR) -> None:
|
||||
"""Plot the decreasing values using Matplotlib."""
|
||||
_ = plt.figure(figsize=(8, 5))
|
||||
_ = plt.plot(values, marker="o", linestyle="-", color="b", label="Function Value")
|
||||
_ = plt.xlabel("Iteration")
|
||||
_ = plt.ylabel("Function Value")
|
||||
_ = plt.title("Optimization Progress")
|
||||
_ = plt.legend()
|
||||
plt.grid(True)
|
||||
func_name = self.function_dropdown.currentText()
|
||||
|
||||
_fig, ax = plt.subplots(figsize=(8, 5))
|
||||
_ = ax.plot(values, marker="o", linestyle="-", color="b", label="Function Value")
|
||||
_ = ax.set_xlabel("Iteration")
|
||||
_ = ax.set_ylabel("Minimum Value")
|
||||
_ = ax.set_title(f"Optimization Progress ({func_name})")
|
||||
_ = ax.legend()
|
||||
ax.grid(True)
|
||||
|
||||
# Convert optimal_x to a readable string
|
||||
max_display_dim = 5 # Truncate if X has too many components (a lot of dimensions)
|
||||
if optimal_x.size > max_display_dim:
|
||||
optimal_x_str = np.array2string(optimal_x[:max_display_dim], precision=4, separator=", ")
|
||||
optimal_x_str = f"{optimal_x_str[:-1]}, ...]" # Add ellipsis at the end
|
||||
else:
|
||||
optimal_x_str = np.array2string(optimal_x, precision=4, separator=", ")
|
||||
|
||||
min_value = values[-1]
|
||||
|
||||
table_data = [
|
||||
["Min Value", f"{min_value:.6f}"],
|
||||
["Optimal X", optimal_x_str],
|
||||
]
|
||||
|
||||
# Add table
|
||||
_ = plt.table(
|
||||
cellText=table_data,
|
||||
cellLoc="center",
|
||||
loc="bottom",
|
||||
bbox=[0.1, -0.4, 0.8, 0.2], # Position table below plot
|
||||
)
|
||||
|
||||
plt.subplots_adjust(bottom=0.35) # Adjust layout to make room for the table
|
||||
plt.show()
|
||||
|
|
Loading…
Add table
Reference in a new issue