/
vector_constraint.go
94 lines (75 loc) · 1.81 KB
/
vector_constraint.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
package optim
import "fmt"
/*
vector_constraint.go
Description:
*/
type VectorConstraint struct {
LeftHandSide VectorExpression
RightHandSide VectorExpression
Sense ConstrSense
}
/*
AtVec
Description:
Retrieves the constraint formed by one element of the "vector" constraint.
*/
func (vc VectorConstraint) AtVec(i int) (ScalarConstraint, error) {
// Input Processing
if vc.Check() != nil {
return ScalarConstraint{}, vc.Check()
}
vcLen := vc.LeftHandSide.Len()
if i >= vcLen {
return ScalarConstraint{},
fmt.Errorf(
"Cannot extract VectorConstraint element at %v; VectorConstraint has length %v.",
i, vcLen,
)
}
// Algorithm
lhsAtI := vc.LeftHandSide.AtVec(i)
rhsAtI := vc.RightHandSide.AtVec(i)
return ScalarConstraint{lhsAtI, rhsAtI, vc.Sense}, nil
}
/*
Check
Description:
Checks that the VectorConstraint is valid.
*/
func (vc VectorConstraint) Check() error {
// Constants
// Check dimensions of left and right hand sides.
if vc.LeftHandSide.Len() != vc.RightHandSide.Len() {
return fmt.Errorf(
"Left hand side has dimension %v, but right hand side has dimension %v!",
vc.LeftHandSide.Len(),
vc.RightHandSide.Len(),
)
}
// Check that the left and right hand sides are well-defined
err := vc.LeftHandSide.Check()
if err != nil {
return fmt.Errorf("left hand side of the constraint is not valid: %v", err)
}
err = vc.RightHandSide.Check()
if err != nil {
return fmt.Errorf("right hand side of the constraint is not valid: %v", err)
}
// All Checks Passed!
return nil
}
func (vc VectorConstraint) Left() Expression {
return vc.LeftHandSide
}
func (vc VectorConstraint) Right() Expression {
return vc.RightHandSide
}
/*
ConstrSense
Description:
Returns the sense of the constraint.
*/
func (vc VectorConstraint) ConstrSense() ConstrSense {
return vc.Sense
}