// replaceBland uses the Bland rule to find the indices to swap if the minimum // move is 0. The indices to be swapped are replace and minIdx (following the // nomenclature in the main routine). func replaceBland(A mat64.Matrix, ab *mat64.Dense, xb []float64, basicIdxs, nonBasicIdx []int, r, move []float64) (replace, minIdx int, err error) { // Use the traditional bland rule, except don't replace a constraint which // causes the new ab to be singular. for i, v := range r { if v > -blandNegTol { continue } minIdx = i err = computeMove(move, minIdx, A, ab, xb, nonBasicIdx) if err != nil { // Either unbounded or something went wrong. return -1, -1, err } replace = floats.MinIdx(move) if math.Abs(move[replace]) > blandZeroTol { // Large enough that it shouldn't be a problem return replace, minIdx, nil } // Find a zero index where replacement is non-singular. biCopy := make([]int, len(basicIdxs)) for replace, v := range move { if v > blandZeroTol { continue } copy(biCopy, basicIdxs) biCopy[replace] = nonBasicIdx[minIdx] abTmp := extractColumns(A, biCopy) // If the condition number is reasonable, use this index. if mat64.Cond(abTmp, 1) < 1e16 { return replace, minIdx, nil } } } return -1, -1, ErrBland }
func simplex(initialBasic []int, c []float64, A mat64.Matrix, b []float64, tol float64) (float64, []float64, []int, error) { err := verifyInputs(initialBasic, c, A, b) if err != nil { if err == ErrUnbounded { return math.Inf(-1), nil, nil, ErrUnbounded } return math.NaN(), nil, nil, err } m, n := A.Dims() // There is at least one optimal solution to the LP which is at the intersection // to a set of constraint boundaries. For a standard form LP with m variables // and n equality constraints, at least m-n elements of x must equal zero // at optimality. The Simplex algorithm solves the standard-form LP by starting // at an initial constraint vertex and successively moving to adjacent constraint // vertices. At every vertex, the set of non-zero x values is the "basic // feasible solution". The list of non-zero x's are maintained in basicIdxs, // the respective columns of A are in ab, and the actual non-zero values of // x are in xb. // // The LP is equality constrained such that A * x = b. This can be expanded // to // ab * xb + an * xn = b // where ab are the columns of a in the basic set, and an are all of the // other columns. Since each element of xn is zero by definition, this means // that for all feasible solutions xb = ab^-1 * b. // // Before the simplex algorithm can start, an initial feasible solution must // be found. If initialBasic is non-nil a feasible solution has been supplied. // Otherwise the "Phase I" problem must be solved to find an initial feasible // solution. var basicIdxs []int // The indices of the non-zero x values. var ab *mat64.Dense // The subset of columns of A listed in basicIdxs. var xb []float64 // The non-zero elements of x. xb = ab^-1 b if initialBasic != nil { // InitialBasic supplied. Panic if incorrect length or infeasible. if len(initialBasic) != m { panic("lp: incorrect number of initial vectors") } ab = extractColumns(A, initialBasic) xb, err = initializeFromBasic(ab, b) if err != nil { panic(err) } basicIdxs = make([]int, len(initialBasic)) copy(basicIdxs, initialBasic) } else { // No inital basis supplied. Solve the PhaseI problem. basicIdxs, ab, xb, err = findInitialBasic(A, b) if err != nil { return math.NaN(), nil, nil, err } } // basicIdxs contains the indexes for an initial feasible solution, // ab contains the extracted columns of A, and xb contains the feasible // solution. All x not in the basic set are 0 by construction. // nonBasicIdx is the set of nonbasic variables. nonBasicIdx := make([]int, 0, n-m) inBasic := make(map[int]struct{}) for _, v := range basicIdxs { inBasic[v] = struct{}{} } for i := 0; i < n; i++ { _, ok := inBasic[i] if !ok { nonBasicIdx = append(nonBasicIdx, i) } } // cb is the subset of c for the basic variables. an and cn // are the equivalents to ab and cb but for the nonbasic variables. cb := make([]float64, len(basicIdxs)) for i, idx := range basicIdxs { cb[i] = c[idx] } cn := make([]float64, len(nonBasicIdx)) for i, idx := range nonBasicIdx { cn[i] = c[idx] } an := extractColumns(A, nonBasicIdx) bVec := mat64.NewVector(len(b), b) cbVec := mat64.NewVector(len(cb), cb) // Temporary data needed each iteration. (Described later) r := make([]float64, n-m) move := make([]float64, m) // Solve the linear program starting from the initial feasible set. This is // the "Phase 2" problem. // // Algorithm: // 1) Compute the "reduced costs" for the non-basic variables. The reduced // costs are the lagrange multipliers of the constraints. // r = cn - an^T * ab^-T * cb // 2) If all of the reduced costs are positive, no improvement is possible, // and the solution is optimal (xn can only increase because of // non-negativity constraints). Otherwise, the solution can be improved and // one element will be exchanged in the basic set. // 3) Choose the x_n with the most negative value of r. Call this value xe. // This variable will be swapped into the basic set. // 4) Increase xe until the next constraint boundary is met. This will happen // when the first element in xb becomes 0. The distance xe can increase before // a given element in xb becomes negative can be found from // xb = Ab^-1 b - Ab^-1 An xn // = Ab^-1 b - Ab^-1 Ae xe // = bhat + d x_e // xe = bhat_i / - d_i // where Ae is the column of A corresponding to xe. // The constraining basic index is the first index for which this is true, // so remove the element which is min_i (bhat_i / -d_i), assuming d_i is negative. // If no d_i is less than 0, then the problem is unbounded. // 5) If the new xe is 0 (that is, bhat_i == 0), then this location is at // the intersection of several constraints. Use the Bland rule instead // of the rule in step 4 to avoid cycling. for { // Compute reduced costs -- r = cn - an^T ab^-T cb var tmp mat64.Vector err = tmp.SolveVec(ab.T(), cbVec) if err != nil { break } data := make([]float64, n-m) tmp2 := mat64.NewVector(n-m, data) tmp2.MulVec(an.T(), &tmp) floats.SubTo(r, cn, data) // Replace the most negative element in the simplex. If there are no // negative entries then the optimal solution has been found. minIdx := floats.MinIdx(r) if r[minIdx] >= -tol { break } for i, v := range r { if math.Abs(v) < rRoundTol { r[i] = 0 } } // Compute the moving distance. err = computeMove(move, minIdx, A, ab, xb, nonBasicIdx) if err != nil { if err == ErrUnbounded { return math.Inf(-1), nil, nil, ErrUnbounded } break } // Replace the basic index along the tightest constraint. replace := floats.MinIdx(move) if move[replace] <= 0 { replace, minIdx, err = replaceBland(A, ab, xb, basicIdxs, nonBasicIdx, r, move) if err != nil { if err == ErrUnbounded { return math.Inf(-1), nil, nil, ErrUnbounded } break } } // Replace the constrained basicIdx with the newIdx. basicIdxs[replace], nonBasicIdx[minIdx] = nonBasicIdx[minIdx], basicIdxs[replace] cb[replace], cn[minIdx] = cn[minIdx], cb[replace] tmpCol1 := mat64.Col(nil, replace, ab) tmpCol2 := mat64.Col(nil, minIdx, an) ab.SetCol(replace, tmpCol2) an.SetCol(minIdx, tmpCol1) // Compute the new xb. xbVec := mat64.NewVector(len(xb), xb) err = xbVec.SolveVec(ab, bVec) if err != nil { break } } // Found the optimum successfully or died trying. The basic variables get // their values, and the non-basic variables are all zero. opt := floats.Dot(cb, xb) xopt := make([]float64, n) for i, v := range basicIdxs { xopt[v] = xb[i] } return opt, xopt, basicIdxs, err }
// findInitialBasic finds an initial basic solution, and returns the basic // indices, ab, and xb. func findInitialBasic(A mat64.Matrix, b []float64) ([]int, *mat64.Dense, []float64, error) { m, n := A.Dims() basicIdxs := findLinearlyIndependent(A) if len(basicIdxs) != m { return nil, nil, nil, ErrSingular } // It may be that this linearly independent basis is also a feasible set. If // so, the Phase I problem can be avoided. ab := extractColumns(A, basicIdxs) xb, err := initializeFromBasic(ab, b) if err == nil { return basicIdxs, ab, xb, nil } // This set was not feasible. Instead the "Phase I" problem must be solved // to find an initial feasible set of basis. // // Method: Construct an LP whose optimal solution is a feasible solution // to the original LP. // 1) Introduce an artificial variable x_{n+1}. // 2) Let x_j be the most negative element of x_b (largest constraint violation). // 3) Add the artificial variable to A with: // a_{n+1} = b - \sum_{i in basicIdxs} a_i + a_j // swap j with n+1 in the basicIdxs. // 4) Define a new LP: // minimize x_{n+1} // subject to [A A_{n+1}][x_1 ... x_{n+1}] = b // x, x_{n+1} >= 0 // 5) Solve this LP. If x_{n+1} != 0, then the problem is infeasible, otherwise // the found basis can be used as an initial basis for phase II. // // The extra column in Step 3 is defined such that the vector of 1s is an // initial feasible solution. // Find the largest constraint violator. // Compute a_{n+1} = b - \sum{i in basicIdxs}a_i + a_j. j is in basicIDx, so // instead just subtract the basicIdx columns that are not minIDx. minIdx := floats.MinIdx(xb) aX1 := make([]float64, m) copy(aX1, b) col := make([]float64, m) for i, v := range basicIdxs { if i == minIdx { continue } mat64.Col(col, v, A) floats.Sub(aX1, col) } // Construct the new LP. // aNew = [A, a_{n+1}] // bNew = b // cNew = 1 for x_{n+1} aNew := mat64.NewDense(m, n+1, nil) aNew.Copy(A) aNew.SetCol(n, aX1) basicIdxs[minIdx] = n // swap minIdx with n in the basic set. c := make([]float64, n+1) c[n] = 1 // Solve the Phase 2 linear program. _, xOpt, newBasic, err := simplex(basicIdxs, c, aNew, b, 1e-10) if err != nil { return nil, nil, nil, errors.New(fmt.Sprintf("lp: error finding feasible basis: %s", err)) } // If n+1 is part of the solution basis then the problem is infeasible. If // not, then the problem is feasible and newBasic is an initial feasible // solution. if math.Abs(xOpt[n]) > phaseIZeroTol { return nil, nil, nil, ErrInfeasible } // The value is zero. First, see if it's not in the basis (feasible solution). basicIdx := -1 basicMap := make(map[int]struct{}) for i, v := range newBasic { if v == n { basicIdx = i } basicMap[v] = struct{}{} xb[i] = xOpt[v] } if basicIdx == -1 { // Not in the basis. ab = extractColumns(A, newBasic) return newBasic, ab, xb, nil } // The value is zero, but it's in the basis. See if swapping in another column // finds a feasible solution. for i := range xOpt { if _, inBasic := basicMap[i]; inBasic { continue } newBasic[basicIdx] = i ab := extractColumns(A, newBasic) xb, err := initializeFromBasic(ab, b) if err == nil { return newBasic, ab, xb, nil } } return nil, nil, nil, ErrInfeasible }