-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbounds.go
67 lines (58 loc) · 1.67 KB
/
bounds.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package clpwrapper
import (
"errors"
"math"
"github.com/lanl/clp"
"gonum.org/v1/gonum/mat"
)
// ConvertBounds from a gonum mat into clp bounds
func ConvertBounds(varBounds mat.Matrix) ([]clp.Bounds, error) {
rows, cols := varBounds.Dims()
if cols != 2 {
return nil, errors.New("bounds matrix must have 2 columns")
}
cb := make([]clp.Bounds, rows)
for i := 0; i < rows; i++ {
cb[i].Lower = varBounds.At(i, 0)
cb[i].Upper = varBounds.At(i, 1)
}
return cb, nil
}
// BuildAllPositiveBounds returns a vector of 0->math.Inf(1) bounds
func BuildAllPositiveBounds(nv int) []clp.Bounds {
zeroVec := make([]float64, nv)
return BuildAboveBounds(zeroVec)
}
// BuildAllNegativeBounds returns a vector of math.Inf(-1)->0 bounds
func BuildAllNegativeBounds(nv int) []clp.Bounds {
zeroVec := make([]float64, nv)
return BuildBelowBounds(zeroVec)
}
// BuildEqualityBounds returns a vector of v<->v bounds
func BuildEqualityBounds(boundsVec []float64) []clp.Bounds {
varBounds := make([]clp.Bounds, len(boundsVec))
for i, v := range boundsVec {
varBounds[i].Lower = v
varBounds[i].Upper = v
}
return varBounds
}
// BuildBelowBounds returns a vector of math.Inf(-1)->v bounds
func BuildBelowBounds(boundsVec []float64) []clp.Bounds {
varBounds := make([]clp.Bounds, len(boundsVec))
for i, v := range boundsVec {
varBounds[i].Lower = math.Inf(-1)
varBounds[i].Upper = v
}
return varBounds
}
// BuildAboveBounds returns a vector of v->math.Inf(1) bounds
func BuildAboveBounds(boundsVec []float64) []clp.Bounds {
varBounds := make([]clp.Bounds, len(boundsVec))
for i, v := range boundsVec {
varBounds[i].Lower = v
varBounds[i].Upper = math.Inf(1)
}
return varBounds
}
// eof