Skip to content

Commit 6eb69e9

Browse files
authored
Merge pull request #209 from nbelakovski/fixes
Fix a couple errors caught by the SciPy build process
2 parents c368823 + d11a883 commit 6eb69e9

File tree

7 files changed

+32
-29
lines changed

7 files changed

+32
-29
lines changed

.github/workflows/build_python.yml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ jobs:
2727
matrix:
2828
# As of 20240501, macos-12/13 are AMD64, and macOS-14 is ARM64.
2929
os: [ubuntu-20.04, ubuntu-22.04, ubuntu-24.04, windows-2019, windows-2022, macos-12, macos-13, macos-14]
30-
3130
steps:
3231
- name: Clone Repository (Latest)
3332
uses: actions/checkout@v4
@@ -46,11 +45,20 @@ jobs:
4645
- name: Checkout pybind11 submodule
4746
run: git submodule update --init python/pybind11
4847

48+
- name: Set the MACOSX_DEPLOYMENT_TARGET
49+
if: ${{ runner.os == 'macOS' }}
50+
run: |
51+
MACOSX_DEPLOYMENT_TARGET=$(sw_vers -productVersion | cut -d'.' -f 1)
52+
echo "MACOSX_DEPLOYMENT_TARGET is $MACOSX_DEPLOYMENT_TARGET"
53+
echo "MACOSX_DEPLOYMENT_TARGET=$MACOSX_DEPLOYMENT_TARGET" >> $GITHUB_ENV
54+
55+
4956
- name: Set up Fortran
5057
uses: fortran-lang/setup-fortran@main
5158
if: ${{ runner.os == 'macOS' }}
5259
with:
5360
compiler: gcc
61+
version: 12
5462

5563
# Copied from https://github.com/scipy/scipy/blob/main/.github/workflows/wheels.yml
5664
# For rtools, see https://github.com/r-windows/rtools-installer/releases, which has been

.github/workflows/cmake.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ jobs:
4242
toolchain:
4343
- {compiler: gcc, version: 11, cflags: '-Wall -Wextra -Wpedantic -Werror', fflags: '-Wall -Wextra -Wpedantic -Werror -fimplicit-none -fcheck=all -fstack-check -Wno-function-elimination'}
4444
- {compiler: gcc, version: 12, cflags: '-Wall -Wextra -Wpedantic -Werror', fflags: '-Wall -Wextra -Wpedantic -Werror -fimplicit-none -fcheck=all -fstack-check -Wno-function-elimination'}
45-
- {compiler: gcc, version: 13, cflags: '-Wall -Wextra -Wpedantic -Werror', fflags: '-Wall -Wextra -Wpedantic -Werror -fimplicit-none -fcheck=all -fstack-check -Wno-function-elimination'}
45+
# As of 20240616 gcc13 has issues importing math.h on macOS.
46+
# - {compiler: gcc, version: 13, cflags: '-Wall -Wextra -Wpedantic -Werror', fflags: '-Wall -Wextra -Wpedantic -Werror -fimplicit-none -fcheck=all -fstack-check -Wno-function-elimination'}
4647
- {compiler: intel-classic, version: '2021.8', cflags: '-diag-disable=10441 -Wall -w3 -Werror-all', fflags: '-warn all -debug extended -fimplicit-none -standard-semantics'}
4748
- {compiler: intel-classic, version: '2021.9', cflags: '-diag-disable=10441 -Wall -w3 -Werror-all', fflags: '-warn all -debug extended -fimplicit-none -standard-semantics'}
4849
- {compiler: intel-classic, version: '2021.10', cflags: '-diag-disable=10441 -Wall -w3 -Werror-all', fflags: '-warn all -debug extended -fimplicit-none -standard-semantics'}

c/prima.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,12 +260,14 @@ prima_rc_t prima_minimize(const prima_algorithm_t algorithm, const prima_problem
260260
default:
261261
return PRIMA_INVALID_INPUT;
262262
}
263+
} else {
264+
return info;
263265
}
264266

265267
result->status = info;
266268
result->success = ((result->status == PRIMA_SMALL_TR_RADIUS && result->cstrv <= options.ctol) ||
267269
(result->status == PRIMA_FTARGET_ACHIEVED));
268270
result->message = prima_get_rc_string(info);
269271

270-
return info;
272+
return PRIMA_RC_DFT;
271273
}

python/_prima.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ struct PRIMAResult {
5252
"nfev=" + std::to_string(nfev) + ", " +
5353
"maxcv=" + std::to_string(maxcv) + ", " +
5454
"nlconstr=" + std::string(pybind11::repr(nlconstr)) + ", " +
55-
"method=" + "\'" + method + "\'" + ")";
55+
"method=" + "\'" + method + "\'" +
5656
")";
5757
return repr;
5858
}
@@ -325,9 +325,14 @@ PYBIND11_MODULE(_prima, m) {
325325
// Initialize the result, call the function, convert the return type, and return it.
326326
prima_result_t result;
327327
const prima_rc_t rc = prima_minimize(algorithm, problem, options, &result);
328-
PRIMAResult result_copy(result, py_x0.size(), problem.m_nlcon, method.cast<std::string>());
329-
prima_free_result(&result);
330-
return result_copy;
328+
if (rc == PRIMA_RC_DFT) {
329+
PRIMAResult result_copy(result, py_x0.size(), problem.m_nlcon, method.cast<std::string>());
330+
prima_free_result(&result);
331+
return result_copy;
332+
} else {
333+
prima_free_result(&result);
334+
throw std::runtime_error("PRIMA failed with error code " + std::to_string(rc));
335+
}
331336
}, "fun"_a, "x0"_a, "args"_a=py::tuple(), "method"_a=py::none(),
332337
"lb"_a=py::none(), "ub"_a=py::none(), "A_eq"_a=py::none(), "b_eq"_a=py::none(),
333338
"A_ineq"_a=py::none(), "b_ineq"_a=py::none(),

python/pybind11

Submodule pybind11 updated 91 files

python/tests/test_combining_constraints.py

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def test_providing_bounds_and_linear_constraints():
2828
def test_providing_bounds_and_nonlinear_constraints():
2929
nlc = prima_NLC(lambda x: x[0]**2, lb=[25], ub=[100])
3030
bounds = prima_Bounds([None, 1], [None, 1])
31-
x0 = [0, 0]
31+
x0 = [6, 1] # Unfortunately the test is very fragile if we do not start near the optimal point
3232
res = prima_minimize(fun, x0, constraints=nlc, bounds=bounds)
3333
assert np.isclose(res.x[0], 5, atol=1e-6, rtol=1e-6)
3434
assert np.isclose(res.x[1], 1, atol=1e-6, rtol=1e-6)
@@ -46,26 +46,10 @@ def newfun(x):
4646
nlc = NLC(lambda x: x[0]**2, lb=[25], ub=[100])
4747
bounds = Bounds([-np.inf, 1, -np.inf], [np.inf, 1, np.inf])
4848
lc = LC(np.array([1,1,1]), lb=10, ub=15)
49-
x0 = [0, 0, 0]
50-
# macOS seems to stop just short of the optimal solution, so we help it along by
51-
# taking a larger initial trust region radius and requiring a smaller final radius
52-
# before stopping. The different packages have different names for these options.
53-
if package == 'prima':
54-
options = {'rhobeg': 2, 'rhoend': 1e-8}
55-
method = None
56-
elif package == 'pdfo':
57-
options = {'radius_init': 2, 'radius_final': 1e-8}
58-
method = None
59-
elif package == 'scipy':
60-
options = {'rhobeg': 2, 'tol': 1e-8}
61-
# PDFO and PRIMA will select COBYLA but SciPy may select something else, so we tell it to select COBYLA
62-
method = 'COBYLA'
63-
else:
64-
# Since this is test infrastructure under the control of the developers we
65-
# should never get here except for a typo or something like that
66-
raise ValueError(f"Unknown package: {package}")
67-
68-
res = minimize(newfun, x0, method=method, constraints=[nlc, lc], bounds=bounds, options=options)
49+
x0 = [6, 1, 3.5] # The test becomes very fragile if we do not start near the optimal point
50+
# PDFO and PRIMA will select COBYLA but SciPy may select something else, so we tell it to select COBYLA
51+
method = 'COBYLA' if package == 'scipy' else None
52+
res = minimize(newfun, x0, method=method, constraints=[nlc, lc], bounds=bounds)
6953

7054
# 32 bit builds of PRIMA reach the optimal solution with the same level of precision as 64 bit builds
7155
# so we lower the atol/rtol to 1e-3 so that 32 bit builds will pass.

python/tests/test_compatibility_pdfo.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ def test_pdfo():
2020
scipy = pytest.importorskip("scipy")
2121
if version.parse(scipy.__version__) < version.parse("1.11.0"):
2222
pytest.skip("scipy version too old for this test (its version of COBYLA does not accept bounds)")
23+
numpy = pytest.importorskip("numpy")
24+
if version.parse(numpy.__version__) >= version.parse("2.0.0"):
25+
pytest.skip("numpy version too new for this test (pdfo does not yet support numpy v2)")
2326

2427
from pdfo import pdfo
2528
from scipy.optimize import NonlinearConstraint as NLC, LinearConstraint as LC, Bounds

0 commit comments

Comments
 (0)