Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor and extend History class #552

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
c829c75
Add direction argument to History class
timmens Nov 30, 2024
125d313
Create 3.13 compatible envs using pre-commit hooks
timmens Nov 30, 2024
d18b63c
Add attributes of HistoryArrays to History
timmens Nov 30, 2024
7ad2edd
Use History in convergence report creation
timmens Nov 30, 2024
bf6cbe6
Use History and not HistoryArrays in history_plots.py
timmens Nov 30, 2024
e6b6066
Remove history_tools.py
timmens Nov 30, 2024
d8fefc1
Update error message for direction init argument in History class
timmens Nov 30, 2024
6a991af
Add additional tests for history.
timmens Nov 30, 2024
b4a186d
Refactor History class
timmens Nov 30, 2024
0b7cfe0
Fix tests for refactored History class
timmens Dec 1, 2024
1ff4765
Add stop_time and init arg validation to History
timmens Dec 1, 2024
7bbc079
Implement cost model and use in History
timmens Dec 1, 2024
f15e173
Align codebase with new History class
timmens Dec 1, 2024
eac0bac
Add unit tests
timmens Dec 1, 2024
dff7c4c
Add tests for fun and params
timmens Dec 1, 2024
df8bb95
Finish test suite for History class
timmens Dec 1, 2024
e72608b
Fix mypy errors
timmens Dec 1, 2024
fe27a3b
Try to fix some warnings
timmens Dec 1, 2024
c86421c
Validate cost_model argument in _get_time function
timmens Dec 3, 2024
da279ac
Handle batch case
timmens Dec 3, 2024
e089347
Use plural
timmens Dec 3, 2024
bd50a5d
Add more test cases for invalid user funcs; check that batch time agg…
timmens Dec 5, 2024
0c5ead3
Some minor changes after first round of own review
timmens Dec 5, 2024
33c5a18
Add explicit assumption to _apply_to_batch docstring
timmens Dec 5, 2024
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
3 changes: 2 additions & 1 deletion src/optimagic/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from optimagic import constraints, mark, utilities
from optimagic import constraints, mark, timing, utilities
from optimagic.algorithms import algos
from optimagic.benchmarking.benchmark_reports import (
convergence_report,
Expand Down Expand Up @@ -102,4 +102,5 @@
"History",
"__version__",
"algos",
"timing",
]
2 changes: 1 addition & 1 deletion src/optimagic/benchmarking/run_benchmark.py
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ def _process_one_result(optimize_result, problem):
criterion_history = history.fun
criterion_history = np.clip(criterion_history, _solution_crit, np.inf)
batches_history = history.batches
time_history = history.time
time_history = history.start_time

return {
"params_history": params_history_flat,
Expand Down
18 changes: 8 additions & 10 deletions src/optimagic/optimization/convergence_report.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import numpy as np
from numpy.typing import NDArray

from optimagic.optimization.history_tools import get_history_arrays
from optimagic.optimization.history import History


def get_convergence_report(history, direction):
history_arrs = get_history_arrays(
history=history,
direction=direction,
)
def get_convergence_report(history: History) -> dict[str, dict[str, float]] | None:
is_accepted = history.is_accepted

critvals = history_arrs.fun[history_arrs.is_accepted]
params = history_arrs.params[history_arrs.is_accepted]
critvals = np.array(history.fun, dtype=np.float64)[is_accepted]
params = np.array(history.flat_params, dtype=np.float64)[is_accepted]

if len(critvals) < 2:
out = None
Expand All @@ -35,7 +33,7 @@ def get_convergence_report(history, direction):
return out


def _get_max_f_changes(critvals):
def _get_max_f_changes(critvals: NDArray[np.float64]) -> tuple[float, float]:
best_val = critvals[-1]
worst_val = critvals[0]

Expand All @@ -47,7 +45,7 @@ def _get_max_f_changes(critvals):
return max_change_rel, max_change_abs


def _get_max_x_changes(params):
def _get_max_x_changes(params: NDArray[np.float64]) -> tuple[float, float]:
best_x = params[-1]
diffs = params - best_x
denom = np.clip(np.abs(best_x), 0.1, np.inf)
Expand Down
Loading
Loading