/
solution_format.go
67 lines (58 loc) · 1.91 KB
/
solution_format.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
package mip
import (
"github.com/nextmv-io/sdk/run/schema"
"github.com/nextmv-io/sdk/run/statistics"
)
// CustomResultStatistics is an example of custom statistics that can be added
// to the output and used in experiments.
type CustomResultStatistics struct {
// Columns in the matrix, i.e. the number of variables.
Columns int `json:"columns,omitempty"`
// Rows in the matrix, i.e. the number of constraints.
Rows int `json:"rows,omitempty"`
// Status of the solution.
Status string `json:"status,omitempty"`
}
// Format the MIP solution into the output format that the runner expects. The
// solution represents the decisions that were made by the solver, translated
// into the domain of the problem.
func Format(
options any,
solution any,
solverSolution Solution,
) schema.Output {
output := schema.NewOutput(options, []any{})
output.Statistics = statistics.NewStatistics()
output.Statistics.Run = &statistics.Run{}
output.Statistics.Result = &statistics.Result{}
if solverSolution == nil || !solverSolution.HasValues() || solverSolution.IsInfeasible() {
return output
}
duration := solverSolution.RunTime().Seconds()
output.Statistics.Result.Duration = &duration
output.Statistics.Run.Duration = &duration
value := statistics.Float64(solverSolution.ObjectiveValue())
output.Statistics.Result.Value = &value
output.Solutions = []any{solution}
return output
}
// DefaultCustomResultStatistics creates default custom statistics for a given
// solution.
func DefaultCustomResultStatistics(model Model, solution Solution) CustomResultStatistics {
status := "unknown"
switch {
case solution.IsOptimal():
status = "optimal"
case solution.IsUnbounded():
status = "unbounded"
case solution.IsSubOptimal():
status = "suboptimal"
case solution.IsInfeasible():
status = "infeasible"
}
return CustomResultStatistics{
Status: status,
Columns: len(model.Vars()),
Rows: len(model.Constraints()),
}
}