-
Notifications
You must be signed in to change notification settings - Fork 0
/
term.go
184 lines (160 loc) · 3.71 KB
/
term.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// © 2019-present nextmv.io inc
package mip
import (
"fmt"
)
// Term is the building block of a constraint and an objective. A term consist
// of a coefficient and a var and should be interpreted as the product
// of coefficient and the var in the context of the constraint or
// objective.
type Term interface {
// Coefficient returns the coefficient value of the invoking term.
Coefficient() float64
// Var returns the variable of the term.
Var() Var
}
// Terms is a slice of Term instances.
type Terms []Term
// QuadraticTerm consists of a coefficient and two vars. It should be
// interpreted as the product of a coefficient and the two vars.
type QuadraticTerm interface {
// Coefficient returns the coefficient value of the invoking term.
Coefficient() float64
// Var1 returns the first variable.
Var1() Var
// Var2 returns the second variable.
Var2() Var
}
// QuadraticTerms is a slice of QuadraticTerm instances.
type QuadraticTerms []QuadraticTerm
func makeLinearTermsUnique(terms Terms) Terms {
uniqueTerms := make(map[Var]Term)
for _, t := range terms {
if t.Coefficient() == 0 {
continue
}
if _, ok := uniqueTerms[t.Var()]; !ok {
uniqueTerms[t.Var()] = t
} else {
coefficient := uniqueTerms[t.Var()].Coefficient() +
t.Coefficient()
if coefficient == 0.0 {
delete(uniqueTerms, t.Var())
} else {
uniqueTerms[t.Var()] = &term{
coefficient: coefficient,
variable: t.Var(),
}
}
}
}
uniqueTermsAsSlice := make(Terms, 0, len(uniqueTerms))
for _, t := range uniqueTerms {
uniqueTermsAsSlice = append(uniqueTermsAsSlice, t)
}
return uniqueTermsAsSlice
}
type term struct {
variable Var
coefficient float64
}
func (t *term) Coefficient() float64 {
return t.coefficient
}
func (t *term) Var() Var {
return t.variable
}
func (t *term) String() string {
return fmt.Sprintf("%v %v",
t.coefficient,
t.variable)
}
// quadraticTerm maintains the invariant that variable1.Index() <=
// variable2.Index(). use newQuadraticTerm to ensure you don't forget that.
type quadraticTerm struct {
variable1 Var
variable2 Var
coefficient float64
}
func (t *quadraticTerm) Coefficient() float64 {
return t.coefficient
}
func (t *quadraticTerm) Var1() Var {
return t.variable1
}
func (t *quadraticTerm) Var2() Var {
return t.variable2
}
func (t *quadraticTerm) String() string {
if t.variable1.Index() == t.variable2.Index() {
return fmt.Sprintf("%v %v^2",
t.coefficient,
t.variable1)
}
return fmt.Sprintf("%v %v*%v",
t.coefficient,
t.variable1,
t.variable2)
}
func makeQuadraticTermsUnique(
quadraticTerms QuadraticTerms,
) QuadraticTerms {
terms := make(map[Var]map[Var]QuadraticTerm)
termCounter := 0
for _, v := range quadraticTerms {
v1 := v.Var1()
v2 := v.Var2()
if v.Coefficient() == 0.0 {
continue
}
mqt, ok := terms[v1]
if !ok {
terms[v1] = make(map[Var]QuadraticTerm)
terms[v1][v2] = v
termCounter++
continue
}
qt, ok := mqt[v2]
if !ok {
mqt[v2] = v
termCounter++
continue
}
newCoef := v.Coefficient() + qt.Coefficient()
if newCoef == 0.0 {
delete(mqt, v2)
termCounter--
continue
}
mqt[v2] = &quadraticTerm{
coefficient: newCoef,
variable1: v1,
variable2: v2,
}
}
rTerms := make(QuadraticTerms, 0, termCounter)
for k := range terms {
for _, v := range terms[k] {
rTerms = append(rTerms, v)
}
}
return rTerms
}
func newQuadraticTerm(
coefficient float64,
variable1 Var,
variable2 Var,
) QuadraticTerm {
if variable1.Index() <= variable2.Index() {
return &quadraticTerm{
coefficient: coefficient,
variable1: variable1,
variable2: variable2,
}
}
return &quadraticTerm{
coefficient: coefficient,
variable1: variable2,
variable2: variable1,
}
}