<p dir="auto">From working on <a class="issue-link js-issue-link" data-error-text="Failed to load title" data-id="254786692" data-permission-text="Title is private" data-url="https://github.com/scipy/scipy/issues/7819" data-hovercard-type="pull_request" data-hovercard-url="/scipy/scipy/pull/7819/hovercard?comment_id=327995047&amp;comment_type=issue_comment" href="https://github.com/scipy/scipy/pull/7819#issuecomment-327995047">#7819 (comment)</a>, here's a case where having both <code class="notranslate">bounds</code> and <code class="notranslate">constraints</code> enabled leads to bizarre behavior that violates both.  I don't know where the actual problem is.  Yellow dots are the local minima found during each step.  Blue is an (exclusion) constraint, and red is out of bounds:</p>
<p dir="auto"><strong>Neither:</strong></p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/58611/30195865-832bd920-942a-11e7-9f07-bdb739050338.png"><img src="https://user-images.githubusercontent.com/58611/30195865-832bd920-942a-11e7-9f07-bdb739050338.png" alt="no constraints no bounds" style="max-width: 100%;"></a></p>
<p dir="auto"><strong>Bounds only (red):</strong></p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/58611/30195873-8e8e249e-942a-11e7-96ff-98a7dd4eb0a8.png"><img src="https://user-images.githubusercontent.com/58611/30195873-8e8e249e-942a-11e7-96ff-98a7dd4eb0a8.png" alt="bounds yes constraints no" style="max-width: 100%;"></a></p>
<p dir="auto"><strong>Constraints only (blue):</strong></p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/58611/30195885-9c88b640-942a-11e7-98a1-1025b4002afa.png"><img src="https://user-images.githubusercontent.com/58611/30195885-9c88b640-942a-11e7-98a1-1025b4002afa.png" alt="constraints yes bounds no" style="max-width: 100%;"></a></p>
<p dir="auto"><strong>Bounds (red) and constraints (blue):</strong></p>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/58611/30195899-b4a16498-942a-11e7-8cdf-e8b16da3dcaa.png"><img src="https://user-images.githubusercontent.com/58611/30195899-b4a16498-942a-11e7-8cdf-e8b16da3dcaa.png" alt="bounds yes constraints yes" style="max-width: 100%;"></a></p>
<p dir="auto">It finds lots of incorrect minima, many of which violate the bounds or the constraints or both.</p>
<p dir="auto">I think these are using different minimizers, though.  The "both" case seems to be slsqp.</p>
<h3 dir="auto">Reproducing code example:</h3>
<p dir="auto">Code for above: <a href="https://gist.github.com/endolith/6f89b1cb0baa98688a5bc49e590ce196">https://gist.github.com/endolith/6f89b1cb0baa98688a5bc49e590ce196</a></p>
<h3 dir="auto">Scipy/Numpy/Python version information:</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="0.19.0 1.13.1 sys.version_info(major=3, minor=6, micro=1, releaselevel='final', serial=0)"><pre class="notranslate"><code class="notranslate">0.19.0 1.13.1 sys.version_info(major=3, minor=6, micro=1, releaselevel='final', serial=0)
</code></pre></div>
<p dir="auto"><code class="notranslate">scipy.optimize.basinhopping</code> fails when constraints are specified, returning a result that violates the constraints, even though the minimizer can easily find the solution that fits the constraints.</p>
<h3 dir="auto">Reproducing code example:</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="import numpy as np
import matplotlib.pyplot as plt
from scipy import optimize


def func(x):
    return (x**2-8)**2+(x+2)**2


x0 = -4
x = np.linspace(-5, 5, 10000)
plt.plot(x, func(x))
plt.plot(x0, func(x0), 'x')

# Constrain to func value &gt;= 50
limit = 50
cons = ({'type': 'ineq', 'fun': lambda x: func(x) - limit},)
plt.axhspan(limit, np.amax(func(x)), alpha=0.1)

# This returns an invalid result
result_bh = optimize.basinhopping(func, x0, disp=True,
                                  minimizer_kwargs={'constraints': cons})

plt.plot(result_bh['x'], result_bh['fun'], 'ro')
print(result_bh)
print('---------------------------------')

# This finds the correct result easily
result_min = optimize.minimize(func, x0, constraints=cons)

plt.plot(result_min['x'], result_min['fun'], 'go')
print(result_min)"><pre class="notranslate"><code class="notranslate">import numpy as np
import matplotlib.pyplot as plt
from scipy import optimize


def func(x):
    return (x**2-8)**2+(x+2)**2


x0 = -4
x = np.linspace(-5, 5, 10000)
plt.plot(x, func(x))
plt.plot(x0, func(x0), 'x')

# Constrain to func value &gt;= 50
limit = 50
cons = ({'type': 'ineq', 'fun': lambda x: func(x) - limit},)
plt.axhspan(limit, np.amax(func(x)), alpha=0.1)

# This returns an invalid result
result_bh = optimize.basinhopping(func, x0, disp=True,
                                  minimizer_kwargs={'constraints': cons})

plt.plot(result_bh['x'], result_bh['fun'], 'ro')
print(result_bh)
print('---------------------------------')

# This finds the correct result easily
result_min = optimize.minimize(func, x0, constraints=cons)

plt.plot(result_min['x'], result_min['fun'], 'go')
print(result_min)
</code></pre></div>
<h3 dir="auto">Results:</h3>
<p dir="auto"><code class="notranslate">scipy.optimize.basinhopping</code> (red dot at -3.816, 46.353306367220874):</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="                        fun: 46.353306367220874
 lowest_optimization_result:      fun: 46.353306367220874
     jac: array([-103.789])
 message: 'Positive directional derivative for linesearch'
    nfev: 113
     nit: 28
    njev: 24
  status: 8
 success: False
       x: array([-3.816])
                    message: ['requested number of basinhopping iterations completed successfully']
      minimization_failures: 49
                       nfev: 7064
                        nit: 100
                       njev: 1444
                          x: array([-3.816])"><pre class="notranslate"><code class="notranslate">                        fun: 46.353306367220874
 lowest_optimization_result:      fun: 46.353306367220874
     jac: array([-103.789])
 message: 'Positive directional derivative for linesearch'
    nfev: 113
     nit: 28
    njev: 24
  status: 8
 success: False
       x: array([-3.816])
                    message: ['requested number of basinhopping iterations completed successfully']
      minimization_failures: 49
                       nfev: 7064
                        nit: 100
                       njev: 1444
                          x: array([-3.816])
</code></pre></div>
<p dir="auto"><code class="notranslate">scipy.optimize.minimize</code> (green dot at -3.85, 49.99991873706809):</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="    fun: 49.99991873706809
     jac: array([-108.809])
 message: 'Optimization terminated successfully.'
    nfev: 28
     nit: 10
    njev: 6
  status: 0
 success: True
       x: array([-3.85])"><pre class="notranslate"><code class="notranslate">    fun: 49.99991873706809
     jac: array([-108.809])
 message: 'Optimization terminated successfully.'
    nfev: 28
     nit: 10
    njev: 6
  status: 0
 success: True
       x: array([-3.85])
</code></pre></div>
<p dir="auto"><a target="_blank" rel="noopener noreferrer nofollow" href="https://user-images.githubusercontent.com/58611/29752116-0586cc8a-8b26-11e7-97bb-94cf9ad44b4b.png"><img src="https://user-images.githubusercontent.com/58611/29752116-0586cc8a-8b26-11e7-97bb-94cf9ad44b4b.png" alt="green good red bad" style="max-width: 100%;"></a></p>
<h3 dir="auto">Scipy/Numpy/Python version information:</h3>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="0.19.0 1.13.1 sys.version_info(major=3, minor=6, micro=1, releaselevel='final', serial=0)"><pre class="notranslate"><code class="notranslate">0.19.0 1.13.1 sys.version_info(major=3, minor=6, micro=1, releaselevel='final', serial=0)
</code></pre></div>