Skip to content

Commit

Permalink
Fix results processing of nag optimizers (#544)
Browse files Browse the repository at this point in the history
  • Loading branch information
timmens authored Oct 31, 2024
1 parent 4dca0bc commit cc049d1
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .tools/envs/testenv-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ dependencies:
- jinja2 # dev, tests
- annotated-types # dev, tests
- pip: # dev, tests, docs
- DFO-LS # dev, tests
- DFO-LS>=1.5.3 # dev, tests
- Py-BOBYQA # dev, tests
- fides==0.7.4 # dev, tests
- kaleido # dev, tests
Expand Down
2 changes: 1 addition & 1 deletion .tools/envs/testenv-numpy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dependencies:
- jinja2 # dev, tests
- annotated-types # dev, tests
- pip: # dev, tests, docs
- DFO-LS # dev, tests
- DFO-LS>=1.5.3 # dev, tests
- Py-BOBYQA # dev, tests
- fides==0.7.4 # dev, tests
- kaleido # dev, tests
Expand Down
2 changes: 1 addition & 1 deletion .tools/envs/testenv-others.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dependencies:
- jinja2 # dev, tests
- annotated-types # dev, tests
- pip: # dev, tests, docs
- DFO-LS # dev, tests
- DFO-LS>=1.5.3 # dev, tests
- Py-BOBYQA # dev, tests
- fides==0.7.4 # dev, tests
- kaleido # dev, tests
Expand Down
2 changes: 1 addition & 1 deletion .tools/envs/testenv-pandas.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ dependencies:
- jinja2 # dev, tests
- annotated-types # dev, tests
- pip: # dev, tests, docs
- DFO-LS # dev, tests
- DFO-LS>=1.5.3 # dev, tests
- Py-BOBYQA # dev, tests
- fides==0.7.4 # dev, tests
- kaleido # dev, tests
Expand Down
2 changes: 2 additions & 0 deletions docs/source/algorithms.md
Original file line number Diff line number Diff line change
Expand Up @@ -1083,6 +1083,8 @@ install each of them separately:
```{eval-rst}
.. dropdown:: nag_dfols
*Note*: We recommend to install `DFO-LS` version 1.5.3 or higher. Versions of 1.5.0 or lower also work but the versions `1.5.1` and `1.5.2` contain bugs that can lead to errors being raised.
.. code-block::
"nag_dfols"
Expand Down
4 changes: 4 additions & 0 deletions docs/source/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ pip install Py-BOBYQA
pip install DFO-LS
```

*Note*: We recommend to install `DFO-LS` version 1.5.3 or higher. Versions of 1.5.0 or
lower also work but the versions `1.5.1` and `1.5.2` contain bugs that can lead to
errors being raised.

```
conda install -c conda-forge petsc4py
```
Expand Down
2 changes: 1 addition & 1 deletion environment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ dependencies:
- furo # dev, docs
- annotated-types # dev, tests
- pip: # dev, tests, docs
- DFO-LS # dev, tests
- DFO-LS>=1.5.3 # dev, tests
- Py-BOBYQA # dev, tests
- fides==0.7.4 # dev, tests
- kaleido # dev, tests
Expand Down
9 changes: 4 additions & 5 deletions src/optimagic/optimizers/nag_optimizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ def nag_dfols_internal(
fun=res["solution_criterion"],
success=res["success"],
message=res["message"],
n_iterations=int(res["n_iterations"]),
n_iterations=res["n_iterations"],
n_fun_evals=res["n_fun_evals"],
)
return out
Expand Down Expand Up @@ -857,7 +857,7 @@ def nag_pybobyqa_internal(
fun=res["solution_criterion"],
success=res["success"],
message=res["message"],
n_iterations=int(res["n_iterations"]),
n_iterations=res["n_iterations"],
)

return out
Expand Down Expand Up @@ -890,9 +890,8 @@ def _process_nag_result(nag_result_obj, len_x):
"diagnostic_info": nag_result_obj.diagnostic_info,
}
try:
processed["n_iterations"] = nag_result_obj.diagnostic_info["iters_total"].iloc[
-1
]
n_iterations = int(nag_result_obj.diagnostic_info["iters_total"].iloc[-1])
processed["n_iterations"] = n_iterations
except (KeyboardInterrupt, SystemExit):
raise
except Exception:
Expand Down
12 changes: 12 additions & 0 deletions tests/optimagic/optimization/test_many_algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,15 @@ def test_global_algorithms_on_sum_of_squares(algorithm):
)
assert res.success in [True, None]
aaae(res.params, np.array([0.2, 0]), decimal=1)


def test_nag_dfols_starting_at_optimum():
# From issue: https://github.com/optimagic-dev/optimagic/issues/538
params = np.zeros(2, dtype=float)
res = minimize(
fun=sos,
params=params,
algorithm="nag_dfols",
bounds=Bounds(-1 * np.ones_like(params), np.ones_like(params)),
)
aaae(res.params, params)

0 comments on commit cc049d1

Please sign in to comment.