Repository navigation
Expand file tree
/
Copy pathexample_constraint_test.go
More file actions
120 lines (96 loc) · 2.17 KB
/
Copy pathexample_constraint_test.go
File metadata and controls
120 lines (96 loc) · 2.17 KB
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
// © 2019-present nextmv.io inc
package mip_test
import (
"fmt"
"testing"
mip "github.com/nextmv-io/go-mip"
)
func ExampleConstraint_greaterThanEqual() {
model := mip.NewModel()
c := model.NewConstraint(mip.GreaterThanOrEqual, 1.0)
fmt.Println(c.Sense())
fmt.Println(c.RightHandSide())
fmt.Println(c)
// Output:
// 2
// 1
// >= 1
}
func ExampleConstraint_equal() {
model := mip.NewModel()
c := model.NewConstraint(mip.Equal, 1.0)
fmt.Println(c.Sense())
fmt.Println(c.RightHandSide())
fmt.Println(c)
// Output:
// 1
// 1
// = 1
}
func ExampleConstraint_lessThanOrEqual() {
model := mip.NewModel()
c := model.NewConstraint(mip.LessThanOrEqual, 1.0)
c.SetName("constraint")
fmt.Println(c.Sense())
fmt.Println(c.RightHandSide())
fmt.Println(c)
fmt.Println(c.Name())
// Output:
// 0
// 1
// <= 1
// constraint
}
func ExampleConstraint_terms() {
model := mip.NewModel()
v := model.NewBool()
c := model.NewConstraint(mip.Equal, 1.0)
t1 := c.NewTerm(1.0, v)
t2 := c.NewTerm(2.0, v)
fmt.Println(t1.Var().Index())
fmt.Println(t1.Coefficient())
fmt.Println(t2.Coefficient())
fmt.Println(len(c.Terms()))
fmt.Println(c.Terms()[0].Coefficient())
fmt.Println(c)
fmt.Println(c.Term(v))
// Output:
// 0
// 1
// 2
// 1
// 3
// 3 B0 = 1
// 3 B0 2
}
func benchmarkNewConstraintNewTerms(nrTerms int, b *testing.B) {
model := mip.NewModel()
v := model.NewFloat(1.0, 2.0)
for i := 0; i < b.N; i++ {
c := model.NewConstraint(mip.Equal, 1.0)
for i := 0; i < nrTerms; i++ {
c.NewTerm(1.0, v)
}
}
}
func BenchmarkNewConstraintNewTerms0(b *testing.B) {
benchmarkNewConstraintNewTerms(0, b)
}
func BenchmarkNewConstraintNewTerms1(b *testing.B) {
benchmarkNewConstraintNewTerms(1, b)
}
func BenchmarkNewConstraintNewTerms2(b *testing.B) {
benchmarkNewConstraintNewTerms(2, b)
}
func BenchmarkNewConstraintNewTerms4(b *testing.B) {
benchmarkNewConstraintNewTerms(4, b)
}
func BenchmarkNewConstraintNewTerms8(b *testing.B) {
benchmarkNewConstraintNewTerms(8, b)
}
func BenchmarkNewConstraintNewTerms16(b *testing.B) {
benchmarkNewConstraintNewTerms(16, b)
}
func BenchmarkNewConstraintNewTerms32(b *testing.B) {
benchmarkNewConstraintNewTerms(32, b)
}