Repository: scipy/scipy

Issue:
[Bug?] Example of Bland's Rule for optimize.linprog (simplex) cycling/preventing termination
Forcing Bland's Rule with the "bland":True options seems to prevent the code example below from terminating, while the purpose of Bland's Rule is to make sure it terminates. The problem is solved quickly when not forcing Bland's Rule, giving an optimal value of -6.044533469014448.

By "not terminating" i mean that it's still running after running for a couple of hours on a MacBook Pro, while my own implementation of Bland's Rule finds it in seconds.

### Reproducing code example:
```
import numpy as np
import scipy.optimize as opt
import sys

np.random.seed(4)
m = int(np.round(10 * np.exp(np.log(50) * np.random.rand())))
n = int(np.round(10 * np.exp(np.log(50) * np.random.rand())))
c, A, b = np.round(10*np.random.randn(n)),np.round(10*np.random.randn(m,n)),np.round(10*np.abs(np.random.randn(m)))
opt.linprog(-1*c, A, b, options={"maxiter": sys.maxsize, "bland": True})
```

### Scipy/Numpy/Python version information:
1.0.0 1.14.0 sys.version_info(major=3, minor=6, micro=4, releaselevel='final', serial=0)




Discussion:
Looks like a clear bug, thanks for reporting @mikkelmk 

Resolving patch:
diff --git a/scipy/optimize/_linprog.py b/scipy/optimize/_linprog.py
--- a/scipy/optimize/_linprog.py
+++ b/scipy/optimize/_linprog.py
@@ -171,7 +171,7 @@ def _pivot_col(T, tol=1.0E-12, bland=False):
     return True, np.ma.where(ma == ma.min())[0][0]
 
 
-def _pivot_row(T, pivcol, phase, tol=1.0E-12):
+def _pivot_row(T, basis, pivcol, phase, tol=1.0E-12, bland=False):
     """
     Given a linear programming simplex tableau, determine the row for the
     pivot operation.
@@ -180,6 +180,8 @@ def _pivot_row(T, pivcol, phase, tol=1.0E-12):
     ----------
     T : 2D ndarray
         The simplex tableau.
+    basis : array
+        A list of the current basic variables.
     pivcol : int
         The index of the pivot column.
     phase : int
@@ -188,6 +190,9 @@ def _pivot_row(T, pivcol, phase, tol=1.0E-12):
         Elements in the pivot column smaller than tol will not be considered
         for pivoting.  Nominally this value is zero, but numerical issues
         cause a tolerance about zero to be necessary.
+    bland : bool
+        If True, use Bland's rule for selection of the row (if more than one
+        row can be used, choose the one with the lowest variable index).
 
     Returns
     -------
@@ -207,7 +212,10 @@ def _pivot_row(T, pivcol, phase, tol=1.0E-12):
         return False, np.nan
     mb = np.ma.masked_where(T[:-k, pivcol] <= tol, T[:-k, -1], copy=False)
     q = mb / ma
-    return True, np.ma.where(q == q.min())[0][0]
+    min_rows = np.ma.where(q == q.min())[0]
+    if bland:
+        return True, min_rows[np.argmin(np.take(basis, min_rows))]
+    return True, min_rows[0]
 
 
 def _solve_simplex(T, n, basis, maxiter=1000, phase=2, callback=None,
@@ -355,7 +363,7 @@ def _solve_simplex(T, n, basis, maxiter=1000, phase=2, callback=None,
             complete = True
         else:
             # Find the pivot row
-            pivrow_found, pivrow = _pivot_row(T, pivcol, phase, tol)
+            pivrow_found, pivrow = _pivot_row(T, basis, pivcol, phase, tol, bland)
             if not pivrow_found:
                 status = 3
                 complete = True


Test patch:
diff --git a/scipy/optimize/tests/test_linprog.py b/scipy/optimize/tests/test_linprog.py
--- a/scipy/optimize/tests/test_linprog.py
+++ b/scipy/optimize/tests/test_linprog.py
@@ -240,6 +240,25 @@ def test_linprog_cyclic_bland(self):
                           method=self.method)
         _assert_success(res, desired_x=[1, 0, 1, 0])
 
+    def test_linprog_cyclic_bland_bug_8561(self):
+        # Test that pivot row is chosen correctly when using Bland's rule
+        c = np.array([7, 0, -4, 1.5, 1.5])
+        A_ub = np.array([
+            [4, 5.5, 1.5, 1.0, -3.5],
+            [1, -2.5, -2, 2.5, 0.5],
+            [3, -0.5, 4, -12.5, -7],
+            [-1, 4.5, 2, -3.5, -2],
+            [5.5, 2, -4.5, -1, 9.5]])
+        b_ub = np.array([0, 0, 0, 0, 1])
+        if self.method == "simplex":
+            res = linprog(c, A_ub=A_ub, b_ub=b_ub,
+                          options=dict(maxiter=100, bland=True),
+                          method=self.method)
+        else:
+            res = linprog(c, A_ub=A_ub, b_ub=b_ub, options=dict(maxiter=100),
+                          method=self.method)
+        _assert_success(res, desired_x=[0, 0, 19, 16/3, 29/3])
+
     def test_linprog_unbounded(self):
         # Test linprog response to an unbounded problem
         c = np.array([1, 1]) * -1  # maximize