|
| 1 | +""" |
| 2 | +Contain a minmax problem solver routine. |
| 3 | +
|
| 4 | +""" |
| 5 | +import numpy as np |
| 6 | +from numba import jit |
| 7 | +from .linprog_simplex import _set_criterion_row, solve_tableau, PivOptions |
| 8 | +from .pivoting import _pivoting |
| 9 | + |
| 10 | + |
| 11 | +@jit(nopython=True, cache=True) |
| 12 | +def minmax(A, max_iter=10**6, piv_options=PivOptions()): |
| 13 | + r""" |
| 14 | + Given an m x n matrix `A`, return the value :math:`v^*` of the |
| 15 | + minmax problem: |
| 16 | +
|
| 17 | + .. math:: |
| 18 | +
|
| 19 | + v^* = \max_{x \in \Delta_m} \min_{y \in \Delta_n} x^T A y |
| 20 | + = \min_{y \in \Delta_n}\max_{x \in \Delta_m} x^T A y |
| 21 | +
|
| 22 | + and the optimal solutions :math:`x^* \in \Delta_m` and |
| 23 | + :math:`y^* \in \Delta_n`: :math:`v^* = x^{*T} A y^*`, where |
| 24 | + :math:`\Delta_k = \{z \in \mathbb{R}^k_+ \mid z_1 + \cdots + z_k = |
| 25 | + 1\}`, :math:`k = m, n`. |
| 26 | +
|
| 27 | + This routine is jit-compiled by Numba, using |
| 28 | + `optimize.linprog_simplex` routines. |
| 29 | +
|
| 30 | + Parameters |
| 31 | + ---------- |
| 32 | + A : ndarray(float, ndim=2) |
| 33 | + ndarray of shape (m, n). |
| 34 | +
|
| 35 | + max_iter : int, optional(default=10**6) |
| 36 | + Maximum number of iteration in the linear programming solver. |
| 37 | +
|
| 38 | + piv_options : PivOptions, optional |
| 39 | + PivOptions namedtuple to set tolerance values used in the linear |
| 40 | + programming solver. |
| 41 | +
|
| 42 | + Returns |
| 43 | + ------- |
| 44 | + v : float |
| 45 | + Value :math:`v^*` of the minmax problem. |
| 46 | +
|
| 47 | + x : ndarray(float, ndim=1) |
| 48 | + Optimal solution :math:`x^*`, of shape (,m). |
| 49 | +
|
| 50 | + y : ndarray(float, ndim=1) |
| 51 | + Optimal solution :math:`y^*`, of shape (,n). |
| 52 | +
|
| 53 | + """ |
| 54 | + m, n = A.shape |
| 55 | + |
| 56 | + min_ = A.min() |
| 57 | + const = 0. |
| 58 | + if min_ <= 0: |
| 59 | + const = min_ * (-1) + 1 |
| 60 | + |
| 61 | + tableau = np.zeros((m+2, n+1+m+1)) |
| 62 | + |
| 63 | + for i in range(m): |
| 64 | + for j in range(n): |
| 65 | + tableau[i, j] = A[i, j] + const |
| 66 | + tableau[i, n] = -1 |
| 67 | + tableau[i, n+1+i] = 1 |
| 68 | + |
| 69 | + tableau[-2, :n] = 1 |
| 70 | + tableau[-2, -1] = 1 |
| 71 | + |
| 72 | + # Phase 1 |
| 73 | + pivcol = 0 |
| 74 | + |
| 75 | + pivrow = 0 |
| 76 | + max_ = tableau[0, pivcol] |
| 77 | + for i in range(1, m): |
| 78 | + if tableau[i, pivcol] > max_: |
| 79 | + pivrow = i |
| 80 | + max_ = tableau[i, pivcol] |
| 81 | + |
| 82 | + _pivoting(tableau, n, pivrow) |
| 83 | + _pivoting(tableau, pivcol, m) |
| 84 | + |
| 85 | + basis = np.arange(n+1, n+1+m+1) |
| 86 | + basis[pivrow] = n |
| 87 | + basis[-1] = 0 |
| 88 | + |
| 89 | + # Modify the criterion row for Phase 2 |
| 90 | + c = np.zeros(n+1) |
| 91 | + c[-1] = -1 |
| 92 | + _set_criterion_row(c, basis, tableau) |
| 93 | + |
| 94 | + # Phase 2 |
| 95 | + solve_tableau(tableau, basis, max_iter-2, skip_aux=False, |
| 96 | + piv_options=piv_options) |
| 97 | + |
| 98 | + # Obtain solution |
| 99 | + x = np.empty(m) |
| 100 | + y = np.zeros(n) |
| 101 | + |
| 102 | + for i in range(m+1): |
| 103 | + if basis[i] < n: |
| 104 | + y[basis[i]] = tableau[i, -1] |
| 105 | + |
| 106 | + for j in range(m): |
| 107 | + x[j] = tableau[-1, n+1+j] |
| 108 | + if x[j] != 0: |
| 109 | + x[j] *= -1 |
| 110 | + |
| 111 | + v = tableau[-1, -1] - const |
| 112 | + |
| 113 | + return v, x, y |
0 commit comments