-
Notifications
You must be signed in to change notification settings - Fork 6
/
vars.go
110 lines (94 loc) · 2.69 KB
/
vars.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package goop
// Var represnts a variable in a optimization problem. The variable is
// identified with an uint64.
type Var struct {
id uint64
lower float64
upper float64
vtype VarType
}
// NumVars returns the number of variables in the expression. For a variable, it
// always returns one.
func (v *Var) NumVars() int {
return 1
}
// Vars returns a slice of the Var ids in the expression. For a variable, it
// always returns a singleton slice with the given variable ID.
func (v *Var) Vars() []uint64 {
return []uint64{v.id}
}
// Coeffs returns a slice of the coefficients in the expression. For a variable,
// it always returns a singleton slice containing the value one.
func (v *Var) Coeffs() []float64 {
return []float64{1}
}
// Constant returns the constant additive value in the expression. For a
// variable, it always returns zero.
func (v *Var) Constant() float64 {
return 0
}
// Plus adds the current expression to another and returns the resulting
// expression.
func (v *Var) Plus(e Expr) Expr {
vars := append([]uint64{v.id}, e.Vars()...)
coeffs := append([]float64{1}, e.Coeffs()...)
newExpr := &LinearExpr{
vars: vars,
coeffs: coeffs,
constant: e.Constant(),
}
return newExpr
}
// Mult multiplies the current expression to another and returns the
// resulting expression
func (v *Var) Mult(c float64) Expr {
vars := []uint64{v.id}
coeffs := []float64{c}
newExpr := &LinearExpr{
vars: vars,
coeffs: coeffs,
constant: 0,
}
return newExpr
}
// LessEq returns a less than or equal to (<=) constraint between the
// current expression and another
func (v *Var) LessEq(other Expr) *Constr {
return LessEq(v, other)
}
// GreaterEq returns a greater than or equal to (>=) constraint between the
// current expression and another
func (v *Var) GreaterEq(other Expr) *Constr {
return GreaterEq(v, other)
}
// Eq returns an equality (==) constraint between the current expression
// and another
func (v *Var) Eq(other Expr) *Constr {
return Eq(v, other)
}
// ID returns the ID of the variable
func (v *Var) ID() uint64 {
return v.id
}
// Lower returns the lower value limit of the variable
func (v *Var) Lower() float64 {
return v.lower
}
// Upper returns the upper value limit of the variable
func (v *Var) Upper() float64 {
return v.upper
}
// Type returns the type of variable (continuous, binary, integer, etc)
func (v *Var) Type() VarType {
return v.vtype
}
// VarType represents the type of the variable (continuous, binary,
// integer, etc) and uses Gurobi's encoding.
type VarType byte
// Multiple common variable types have been included as constants that conform
// to Gurobi's encoding.
const (
Continuous VarType = 'C'
Binary = 'B'
Integer = 'I'
)