-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
112 lines (92 loc) · 2.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
package main
/**
* ===== GO-SAT - SIMPLE SAT SOLVER IN GO =====
* == By Marc van Zee ([email protected]) ==
* ============================================
* = www.github.com/marcvanzee/go-sat/ =
* = =
* ============================================
*
* View a more detailed explanation of this solver in Python:
* http://sahandsaba.com/understanding-sat-by-implementing-a-simple-sat-solver-in-python.html
*
*
* Run the SAT solver from here with optional arguments:
* -all
* Output all possible solutions (default true)
* -brief
* Only output variables assigned true
* -i string
* Read from given file instead of stdin
* -recursive
* Use recursive algorithm instead of iterative
* -starting_with string
* Only output variables with names starting with the given string
* -verbose
* Verbose output (default true)
*/
import (
"flag"
"fmt"
"github.com/marcvanzee/go-solver/satproblem"
"github.com/marcvanzee/go-solver/solvers"
"os"
)
var verbose = flag.Bool("verbose", true, "Verbose output")
var allSolutions = flag.Bool("all", true, "Output all possible solutions")
var brief = flag.Bool("brief", false, "Only output variables assigned true")
var startingWith = flag.String("starting_with", "", "Only output variables with names starting with the given string")
var recursive = flag.Bool("recursive", false, "Use recursive algorithm instead of iterative")
var file = flag.String("i", "", "Read from given file instead of stdin")
func exit(err error) {
if err != nil {
fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
// no error
}
func solve(s satproblem.SATInstance) [][]int {
n := len(s.Vars)
// initialize the watchlist, where each clauses in the SATInstance initially
// watches it first literal
watchlist := satproblem.NewWatchlist(s)
if len(watchlist) == 0 {
return nil
}
// initially we do not assignment a truth value to any of the literals
assignment := make([]int, n, n)
for i := range assignment {
assignment[i] = satproblem.NONE
}
ret := solvers.NewSolver(*recursive).
Solve(s, watchlist, assignment, 0, *verbose)
return ret
}
func main() {
flag.Parse()
var err error
defer func() { exit(err) }()
instance := satproblem.NewSATInstance()
// initalize tries to read from file, if file is empty it reads from stdin
if err := instance.Init(file, *verbose); err != nil {
return
}
// solve returns a slide of solution assignments,
// which assign truth values to literals
assignments := solve(instance)
// print the assignments
count := 0
for _, assignment := range assignments {
if *verbose {
fmt.Printf("Found satisfying assignment #%v: ", count)
fmt.Println(instance.AssignmentToString(assignment, *brief, *startingWith))
}
count += 1
if !*allSolutions {
break
}
}
if *verbose && count == 0 {
fmt.Println("No satisfying assignment exists.")
}
}