-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexample_test.go
51 lines (41 loc) · 1.25 KB
/
example_test.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
package goop_test
import (
"fmt"
"github.com/mit-drl/goop"
"github.com/mit-drl/goop/solvers"
)
// This example shows how goop can be used to solve a simple MIP:
// // maximize x + y + 2 z
// // subject to x + 2 y + 3 z <= 4
// // x + y >= 1
// // x, y, z binary
// MIP being modelled is the same as in http://www.gurobi.com/documentation/7.5/examples/mip1_cpp_cpp.html
func ExampleModel_simple() {
// Instantiate a new model
m := goop.NewModel()
// Add your variables to the model
x := m.AddBinaryVar()
y := m.AddBinaryVar()
z := m.AddBinaryVar()
// Add your constraints
m.AddConstr(goop.Sum(x, y.Mult(2), z.Mult(3)).LessEq(goop.K(4)))
m.AddConstr(goop.Sum(x, y).GreaterEq(goop.One))
// Set a linear objective using your variables
obj := goop.Sum(x, y, z.Mult(2))
m.SetObjective(obj, goop.SenseMaximize)
// Optimize the variables according to the model
sol, err := m.Optimize(solvers.NewLPSolveSolver())
// Check if there is an error from the solver. No error should be returned
// for this model
if err != nil {
panic("Should not have an error")
}
// Print out the solution
fmt.Println("x =", sol.Value(x))
fmt.Println("y =", sol.Value(y))
fmt.Println("z =", sol.Value(z))
// Output:
// x = 1
// y = 0
// z = 1
}