-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
125 lines (102 loc) · 3.6 KB
/
main.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
// © 2019-present nextmv.io inc
// package main holds the implementation of the mip-knapsack template.
package main
import (
"context"
"log"
"github.com/nextmv-io/go-highs"
"github.com/nextmv-io/go-mip"
"github.com/nextmv-io/sdk/run"
"github.com/nextmv-io/sdk/run/schema"
)
// This template demonstrates how to solve a Mixed Integer Programming problem.
// To solve a mixed integer problem is to optimize a linear objective function
// of many variables, subject to linear constraints. We demonstrate this by
// solving the well known knapsack problem.
func main() {
err := run.CLI(solver).Run(context.Background())
if err != nil {
log.Fatal(err)
}
}
// The options for the solver.
type options struct {
Solve mip.SolveOptions `json:"solve,omitempty"`
}
// Input of the problem.
type input struct {
Items []item `json:"items"`
WeightCapacity float64 `json:"weight_capacity"`
}
// An item has a Value and Weight. ID is used to identify the item.
type item struct {
ID string `json:"id,omitempty"`
Value float64 `json:"value"`
Weight float64 `json:"weight"`
}
// solution represents the decisions made by the solver.
type solution struct {
Items []item `json:"items,omitempty"`
}
// solver is the entrypoint of the program where a model is defined and solved.
func solver(_ context.Context, input input, options options) (schema.Output, error) {
// Translate the input to a MIP model.
model, variables := model(input)
// Create a solver using a provider. Please see the documentation on
// [mip.SolverProvider] for more information on the available providers.
solver := highs.NewSolver(model)
// Solve the model and get the solution.
solution, err := solver.Solve(options.Solve)
if err != nil {
return schema.Output{}, err
}
// Format the solution into the desired output format and add custom
// statistics.
output := mip.Format(options, format(input, solution, variables), solution)
output.Statistics.Result.Custom = mip.DefaultCustomResultStatistics(model, solution)
return output, nil
}
// model creates a MIP model from the input. It also returns the decision
// variables.
func model(input input) (mip.Model, map[string]mip.Bool) {
// We start by creating a MIP model.
model := mip.NewModel()
// Create a map of ID to decision variables for each item in the knapsack.
itemVariables := make(map[string]mip.Bool, len(input.Items))
for _, item := range input.Items {
// Create a new binary decision variable for each item in the knapsack.
itemVariables[item.ID] = model.NewBool()
}
// We want to maximize the value of the knapsack.
model.Objective().SetMaximize()
// This constraint ensures the weight capacity of the knapsack will not be
// exceeded.
capacityConstraint := model.NewConstraint(
mip.LessThanOrEqual,
input.WeightCapacity,
)
// For each item, set the term in the objective function and in the
// constraint.
for _, item := range input.Items {
// Sets the value of the item in the objective function.
model.Objective().NewTerm(item.Value, itemVariables[item.ID])
// Sets the weight of the item in the constraint.
capacityConstraint.NewTerm(item.Weight, itemVariables[item.ID])
}
return model, itemVariables
}
// format the solution from the solver into the desired output format.
func format(input input, solverSolution mip.Solution, itemVariables map[string]mip.Bool) solution {
if !solverSolution.IsOptimal() && !solverSolution.IsSubOptimal() {
return solution{}
}
items := make([]item, 0)
for _, item := range input.Items {
if solverSolution.Value(itemVariables[item.ID]) > 0.9 {
items = append(items, item)
}
}
return solution{
Items: items,
}
}