-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathlinear_expr.go
74 lines (62 loc) · 1.93 KB
/
linear_expr.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
package goop
// LinearExpr represents a linear general expression of the form
// c0 * x0 + c1 * x1 + ... + cn * xn + k where ci are coefficients and xi are
// variables and k is a constant
type LinearExpr struct {
vars []uint64
coeffs []float64
constant float64
}
// NewLinearExpr returns a new expression with a single additive constant
// value, c, and no variables.
func NewLinearExpr(c float64) Expr {
return &LinearExpr{constant: c}
}
// NumVars returns the number of variables in the expression
func (e *LinearExpr) NumVars() int {
return len(e.vars)
}
// Vars returns a slice of the Var ids in the expression
func (e *LinearExpr) Vars() []uint64 {
return e.vars
}
// Coeffs returns a slice of the coefficients in the expression
func (e *LinearExpr) Coeffs() []float64 {
return e.coeffs
}
// Constant returns the constant additive value in the expression
func (e *LinearExpr) Constant() float64 {
return e.constant
}
// Plus adds the current expression to another and returns the resulting
// expression
func (e *LinearExpr) Plus(other Expr) Expr {
e.vars = append(e.vars, other.Vars()...)
e.coeffs = append(e.coeffs, other.Coeffs()...)
e.constant += other.Constant()
return e
}
// Mult multiplies the current expression to another and returns the
// resulting expression
func (e *LinearExpr) Mult(c float64) Expr {
for i, coeff := range e.coeffs {
e.coeffs[i] = coeff * c
}
e.constant *= c
return e
}
// LessEq returns a less than or equal to (<=) constraint between the
// current expression and another
func (e *LinearExpr) LessEq(other Expr) *Constr {
return LessEq(e, other)
}
// GreaterEq returns a greater than or equal to (>=) constraint between the
// current expression and another
func (e *LinearExpr) GreaterEq(other Expr) *Constr {
return GreaterEq(e, other)
}
// Eq returns an equality (==) constraint between the current expression
// and another
func (e *LinearExpr) Eq(other Expr) *Constr {
return Eq(e, other)
}