This repository has been archived by the owner on Nov 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
199 lines (175 loc) · 4.87 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package main
import (
"bufio"
"fmt"
"os"
"os/signal"
"runtime/pprof"
"syscall"
"time"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
const (
// SATEXITCODE is the exit code for UNKNOWN
SATEXITCODE = 10
// UNSATEXITCODE is the exit code for UNKNOWN
UNSATEXITCODE = 20
// UNKNOWNEXITCODE is the exit code for UNKNOWN
UNKNOWNEXITCODE = 0
)
var CurrentTime time.Time
var (
//DebugMode is an option that solver showes debug information
DebugMode = kingpin.Flag("debug", "Debug mode").Short('d').Bool()
//Verbose is an option that solver showes extra information
Verbose = kingpin.Flag("verbose", "Vervosity mode").Short('v').Default("true").Bool()
InputFile = kingpin.Arg("input-file", "Input cnf file for solving").Required().File()
OutputFile = kingpin.Arg("output-file", "Output result file").String()
CPUTimeLimit = kingpin.Flag("cpu-time-limit", "Limit on CPU time allowed in seconds").Int()
Profile = kingpin.Flag("profile", "Profiler file(pprof)").Short('p').String()
)
func printProblemStatistics(s *Solver) {
fmt.Printf("c ============================[ Problem Statistics ]=============================\n")
fmt.Printf("c | |\n")
fmt.Printf("c | Number of variables: %12d |\n", s.NumVars())
fmt.Printf("c | Number of clauses: %12d |\n", s.NumClauses())
fmt.Printf("c ================================================================================\n")
}
func printStatistics(s *Solver) {
elapsedTimeSeconds := time.Now().Sub(CurrentTime).Seconds()
fmt.Printf("c ================================================================================\n")
fmt.Printf("c restarts: %12d\n", s.Statistics.RestartCount)
fmt.Printf("c conflicts: %12d (%.02f / sec)\n", s.Statistics.ConflictCount, float64(s.Statistics.ConflictCount)/elapsedTimeSeconds)
fmt.Printf("c decisions: %12d (%.02f / sec)\n", s.Statistics.DecisionCount, float64(s.Statistics.DecisionCount)/elapsedTimeSeconds)
fmt.Printf("c propagations: %12d (%.02f / sec)\n", s.Statistics.PropagationCount, float64(s.Statistics.PropagationCount)/elapsedTimeSeconds)
fmt.Printf("c reduce DB: %12d\n", s.Statistics.ReduceDBCount)
fmt.Printf("c removed clause: %12d\n", s.Statistics.RemovedClauseCount)
fmt.Printf("c cpu time: %12f\n", elapsedTimeSeconds)
}
func setTimeOut(s *Solver, limitTimeSeconds int) {
if limitTimeSeconds <= 0 {
return
}
go func() {
<-time.After(time.Duration(limitTimeSeconds) * time.Second)
fmt.Println("c TIMEOUT")
if s.Verbosity {
printStatistics(s)
}
fmt.Println("\ns INDETERMINATE")
if *Profile != "" {
pprof.StopCPUProfile()
}
os.Exit(0)
}()
}
func setInterupt(s *Solver) {
c := make(chan os.Signal, 2)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
fmt.Println("c INTERUPT")
if s.Verbosity {
printStatistics(s)
}
fmt.Println("\ns INDETERMINATE")
if *Profile != "" {
pprof.StopCPUProfile()
}
os.Exit(0)
}()
}
func printModel(s *Solver) {
fmt.Print("v ")
for i := 0; i < s.NumVars(); i++ {
if s.Model[i] == LitBoolTrue {
fmt.Printf("%d ", i+1)
} else {
fmt.Printf("%d ", -(i + 1))
}
}
fmt.Print("0\n")
}
func writeOutputFile(file string, s *Solver, status LitBool) error {
var fp *os.File
if _, err := os.Stat(file); os.IsNotExist(err) {
fp, err = os.Create(file)
if err != nil {
return err
}
} else {
fp, err = os.Create(file)
if err != nil {
return err
}
}
defer fp.Close()
if status == LitBoolTrue {
for i := 0; i < s.NumVars(); i++ {
if s.Model[i] == LitBoolTrue {
fp.WriteString(fmt.Sprintf("%d ", i+1))
} else {
fp.WriteString(fmt.Sprintf("%d ", -(i + 1)))
}
}
fp.WriteString("0\n")
} else if status == LitBoolFalse {
fp.WriteString("UNSAT")
}
return nil
}
func init() {
CurrentTime = time.Now()
}
func run() int {
//input
inFp := *InputFile
defer inFp.Close()
in := bufio.NewScanner(inFp)
//Start profile
if *Profile != "" {
f, err := os.Create(*Profile)
if err != nil {
panic(err)
}
pprof.StartCPUProfile(f)
}
solver := NewSolver()
setTimeOut(solver, *CPUTimeLimit)
setInterupt(solver)
err := parseDimacs(in, solver)
if err != nil {
return UNKNOWNEXITCODE
}
if solver.Verbosity {
printProblemStatistics(solver)
}
status := solver.Solve()
//End profile
if *Profile != "" {
pprof.StopCPUProfile()
}
if solver.Verbosity {
printStatistics(solver)
}
if status == LitBoolTrue {
fmt.Println("\ns SATISFIABLE")
printModel(solver)
} else if status == LitBoolFalse {
fmt.Println("\ns UNSATISFIABLE")
}
if OutputFile != nil {
writeOutputFile(*OutputFile, solver, status)
}
if status == LitBoolTrue {
return SATEXITCODE
} else if status == LitBoolFalse {
return UNSATEXITCODE
}
return UNKNOWNEXITCODE
}
func main() {
kingpin.Version("0.0.1")
kingpin.Parse()
os.Exit(run())
}