This repository was archived by the owner on Nov 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Add package for linear programming solving and Simplex implementation #165
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
// Copyright ©2016 The gonum Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package lp | ||
|
||
import ( | ||
"github.com/gonum/floats" | ||
"github.com/gonum/matrix/mat64" | ||
) | ||
|
||
// TODO(btracey): Have some sort of preprocessing step for helping to fix A to make it | ||
// full rank? | ||
// TODO(btracey): Reduce rows? Get rid of all zeros, places where only one variable | ||
// is there, etc. Could be implemented with a Reduce function. | ||
// TODO(btracey): Provide method of artificial variables for help when problem | ||
// is infeasible? | ||
// TODO(btracey): Add an lp.Solve that solves an LP in non-standard form. | ||
|
||
// Convert converts a General-form LP into a standard form LP. | ||
// The general form of an LP is: | ||
// minimize c^T * x | ||
// s.t G * x <= h | ||
// A * x = b | ||
// And the standard form is: | ||
// minimize cNew^T * x | ||
// s.t aNew * x = bNew | ||
// x >= 0 | ||
// If there are no constraints of the given type, the inputs may be nil. | ||
func Convert(c []float64, g mat64.Matrix, h []float64, a mat64.Matrix, b []float64) (cNew []float64, aNew *mat64.Dense, bNew []float64) { | ||
nVar := len(c) | ||
nIneq := len(h) | ||
|
||
// Check input sizes. | ||
if g == nil { | ||
if nIneq != 0 { | ||
panic(badShape) | ||
} | ||
} else { | ||
gr, gc := g.Dims() | ||
if gr != nIneq { | ||
panic(badShape) | ||
} | ||
if gc != nVar { | ||
panic(badShape) | ||
} | ||
} | ||
|
||
nEq := len(b) | ||
if a == nil { | ||
if nEq != 0 { | ||
panic(badShape) | ||
} | ||
} else { | ||
ar, ac := a.Dims() | ||
if ar != nEq { | ||
panic(badShape) | ||
} | ||
if ac != nVar { | ||
panic(badShape) | ||
} | ||
} | ||
|
||
// Convert the general form LP. | ||
// Derivation: | ||
// 0. Start with general form | ||
// min. c^T * x | ||
// s.t. G * x <= h | ||
// A * x = b | ||
// 1. Introduce slack variables for each constraint | ||
// min. c^T * x | ||
// s.t. G * x + s = h | ||
// A * x = b | ||
// s >= 0 | ||
// 2. Add non-negativity constraints for x by splitting x | ||
// into positive and negative components. | ||
// x = xp - xn | ||
// xp >= 0, xn >= 0 | ||
// This makes the LP | ||
// min. c^T * xp - c^T xn | ||
// s.t. G * xp - G * xn + s = h | ||
// A * xp - A * xn = b | ||
// xp >= 0, xn >= 0, s >= 0 | ||
// 3. Write the above in standard form: | ||
// xt = [xp | ||
// xn | ||
// s ] | ||
// min. [c^T, -c^T, 0] xt | ||
// s.t. [G, -G, I] xt = h | ||
// [A, -A, 0] xt = b | ||
// x >= 0 | ||
|
||
// In summary: | ||
// Original LP: | ||
// min. c^T * x | ||
// s.t. G * x <= h | ||
// A * x = b | ||
// Standard Form: | ||
// xt = [xp; xn; s] | ||
// min. [c^T, -c^T, 0] xt | ||
// s.t. [G, -G, I] xt = h | ||
// [A, -A, 0] xt = b | ||
// x >= 0 | ||
|
||
// New size of x is [xp, xn, s] | ||
nNewVar := nVar + nVar + nIneq | ||
|
||
// Construct cNew = [c; -c; 0] | ||
cNew = make([]float64, nNewVar) | ||
copy(cNew, c) | ||
copy(cNew[nVar:], c) | ||
floats.Scale(-1, cNew[nVar:2*nVar]) | ||
|
||
// New number of equality constraints is the number of total constraints. | ||
nNewEq := nIneq + nEq | ||
|
||
// Construct bNew = [h, b]. | ||
bNew = make([]float64, nNewEq) | ||
copy(bNew, h) | ||
copy(bNew[nIneq:], b) | ||
|
||
// Construct aNew = [G, -G, I; A, -A, 0]. | ||
aNew = mat64.NewDense(nNewEq, nNewVar, nil) | ||
if nIneq != 0 { | ||
aView := (aNew.View(0, 0, nIneq, nVar)).(*mat64.Dense) | ||
aView.Copy(g) | ||
aView = (aNew.View(0, nVar, nIneq, nVar)).(*mat64.Dense) | ||
aView.Scale(-1, g) | ||
aView = (aNew.View(0, 2*nVar, nIneq, nIneq)).(*mat64.Dense) | ||
for i := 0; i < nIneq; i++ { | ||
aView.Set(i, i, 1) | ||
} | ||
} | ||
if nEq != 0 { | ||
aView := (aNew.View(nIneq, 0, nEq, nVar)).(*mat64.Dense) | ||
aView.Copy(a) | ||
aView = (aNew.View(nIneq, nVar, nEq, nVar)).(*mat64.Dense) | ||
aView.Scale(-1, a) | ||
} | ||
return cNew, aNew, bNew | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
license header?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.