Skip to content

Commit c273bd9

Browse files
committed
ENH: Add iprint support to COBYLA
1 parent 1ef7db5 commit c273bd9

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

scipy/optimize/_cobyla_py.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def fmin_cobyla(func, x0, cons, args=(), consargs=None, rhobeg=1.0,
192192
@synchronized
193193
def _minimize_cobyla(fun, x0, args=(), constraints=(),
194194
rhobeg=1.0, tol=1e-6, maxiter=1000,
195-
disp=False, catol=np.sqrt(np.finfo(float).eps),
195+
disp=0, catol=np.sqrt(np.finfo(float).eps),
196196
ftarget=-np.inf, callback=None, bounds=None,
197197
**unknown_options):
198198
"""
@@ -206,9 +206,15 @@ def _minimize_cobyla(fun, x0, args=(), constraints=(),
206206
tol : float
207207
Final accuracy in the optimization (not precisely guaranteed).
208208
This is a lower bound on the size of the trust region.
209-
disp : bool
210-
Set to True to print convergence messages. If False,
211-
`verbosity` is ignored as set to 0.
209+
disp : int
210+
0: there will be no printing;
211+
1: a message will be printed to the screen at the return, showing the
212+
best vector of variables found and its objective function value
213+
2: in addition to 1, each new value of RHO is printed to the screen,
214+
with the best vector of variables so far and its objective function
215+
value.
216+
3: in addition to 2, each function evaluation with its variables will
217+
be printed to the screen.
212218
maxiter : int
213219
Maximum number of function evaluations.
214220
catol : float
@@ -225,7 +231,10 @@ def _minimize_cobyla(fun, x0, args=(), constraints=(),
225231
_check_unknown_options(unknown_options)
226232
maxfun = maxiter
227233
rhoend = tol
228-
iprint = int(bool(disp))
234+
iprint = disp if disp is not None else 0
235+
if iprint != 0 and iprint != 1 and iprint != 2 and iprint != 3:
236+
raise ValueError(f'disp argument to minimize must be 0, 1, 2, or 3,\
237+
received {iprint}')
229238

230239
# create the ScalarFunction, cobyla doesn't require derivative function
231240
def _jac(x, *args):

scipy/optimize/tests/test_cobyla.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class TestCobyla:
1111
def setup_method(self):
1212
self.x0 = [4.95, 0.66]
1313
self.solution = [math.sqrt(25 - (2.0/3)**2), 2.0/3]
14-
self.opts = {'disp': False, 'rhobeg': 1, 'tol': 1e-5,
14+
self.opts = {'disp': 0, 'rhobeg': 1, 'tol': 1e-5,
1515
'maxiter': 100}
1616

1717
def fun(self, x):
@@ -24,9 +24,9 @@ def con2(self, x):
2424
return -self.con1(x)
2525

2626
def test_simple(self):
27-
# use disp=True as smoke test for gh-8118
27+
# use disp=1 as smoke test for gh-8118
2828
x = fmin_cobyla(self.fun, self.x0, [self.con1, self.con2], rhobeg=1,
29-
rhoend=1e-5, maxfun=100, disp=True)
29+
rhoend=1e-5, maxfun=100, disp=1)
3030
assert_allclose(x, self.solution, atol=1e-4)
3131

3232
def test_minimize_simple(self):

0 commit comments

Comments
 (0)