-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
146 lines (124 loc) · 2.86 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"flag"
"fmt"
"log"
"os"
"runtime/pprof"
"time"
"github.com/rhartert/yass/internal/dimacs"
"github.com/rhartert/yass/internal/sat"
)
var flagCPUProfile = flag.Bool(
"cpuprof",
false,
"save pprof CPU profile in cpuprof",
)
var flagMemProfile = flag.Bool(
"memprof",
false,
"save pprof memory profile in memprof",
)
var flagMaxConflict = flag.Int64(
"max_conflicts",
-1,
"maximum number of conflicts allowed to solve the problem (-1 = no maximum)",
)
var flagTimeout = flag.Duration(
"timeout",
-1,
"search timeout (-1 = no timeout)",
)
var flagPhaseSaving = flag.Bool(
"phase",
false,
"enable phase saving in search strategy",
)
var flagGzipInput = flag.Bool(
"gzip",
false,
"gzipped input DIMACS file",
)
func parseConfig() (*config, error) {
flag.Parse()
if flag.NArg() == 0 || flag.Arg(0) == "" {
return nil, fmt.Errorf("missing instance file")
}
return &config{
instanceFile: flag.Arg(0),
gzippedFile: *flagGzipInput,
memProfile: *flagMemProfile,
cpuProfile: *flagCPUProfile,
maxConflicts: *flagMaxConflict,
timeout: *flagTimeout,
phaseSaving: *flagPhaseSaving,
}, nil
}
type config struct {
instanceFile string
gzippedFile bool
memProfile bool
cpuProfile bool
maxConflicts int64
timeout time.Duration
phaseSaving bool
}
func solverOptions(cfg *config) sat.Options {
options := sat.DefaultOptions
options.PhaseSaving = cfg.phaseSaving
if cfg.maxConflicts >= 0 {
options.MaxConflicts = cfg.maxConflicts
}
if cfg.timeout >= 0 {
options.Timeout = cfg.timeout
}
return options
}
func run(cfg *config) error {
s := sat.NewSolver(solverOptions(cfg))
tRead := time.Now()
if err := dimacs.LoadDIMACS(cfg.instanceFile, cfg.gzippedFile, s); err != nil {
return fmt.Errorf("could not load instance: %s", err)
}
tSolve := time.Now()
status := s.Solve()
tCompleted := time.Now()
stats := s.Statistics
readDur := tSolve.Sub(tRead).Seconds()
solveDur := tCompleted.Sub(tSolve).Seconds()
propagationsFreq := float64(stats.Propagations) / solveDur
conflictsFreq := float64(stats.Conflicts) / solveDur
fmt.Printf("c\n")
fmt.Printf("c read time: %.3f sec\n", readDur)
fmt.Printf("c solve time: %.3f sec\n", solveDur)
fmt.Printf("c conflicts: %d (%.2f /sec)\n", stats.Conflicts, conflictsFreq)
fmt.Printf("c propagations: %d (%.2f M/sec)\n", stats.Propagations, propagationsFreq/1e6)
fmt.Printf("c status: %s\n", status.String())
return nil
}
func main() {
cfg, err := parseConfig()
if err != nil {
log.Fatal(err)
}
if cfg.cpuProfile {
f, err := os.Create("cpuprof")
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if err := run(cfg); err != nil {
log.Fatal(err)
}
if cfg.memProfile {
f, err := os.Create("memprof")
if err != nil {
log.Fatal(err)
}
pprof.WriteHeapProfile(f)
f.Close()
return
}
}