Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
inox
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
LARA
inox
Commits
43182d14
Commit
43182d14
authored
13 years ago
by
Ali Sinan Köksal
Browse files
Options
Downloads
Patches
Plain Diff
Sat solver as in paper
parent
fcde1045
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
cp-demo/SatSolver.scala
+20
-10
20 additions, 10 deletions
cp-demo/SatSolver.scala
with
20 additions
and
10 deletions
cp-demo/SatSolver.scala
+
20
−
10
View file @
43182d14
...
@@ -9,22 +9,32 @@ object SatSolver {
...
@@ -9,22 +9,32 @@ object SatSolver {
List
(
4
)
List
(
4
)
)
)
def
satSolve
(
problem
:
Seq
[
Seq
[
Int
]])
=
{
problem
.
map
(
l
=>
l
.
map
(
i
=>
{
val
id
=
scala
.
math
.
abs
(
i
)
val
isPos
=
i
>
0
val
c
:
Constraint1
[
Map
[
Int
,
Boolean
]]
=
((
m
:
Map
[
Int
,
Boolean
])
=>
m
(
id
)
==
isPos
)
c
}).
reduceLeft
(
_
||
_
)).
reduceLeft
(
_
&&
_
).
find
}
def
main
(
args
:
Array
[
String
])
:
Unit
=
{
def
main
(
args
:
Array
[
String
])
:
Unit
=
{
val
problem
:
List
[
List
[
Int
]]
=
if
(
args
.
length
>
0
)
(
for
(
line
<-
Source
.
fromFile
(
args
(
0
)).
getLines
if
(!
line
.
startsWith
(
"c"
)
&&
!
line
.
startsWith
(
"p"
)))
yield
{
val
problem
:
List
[
List
[
Int
]]
=
if
(
args
.
length
>
0
)
(
for
(
line
<-
Source
.
fromFile
(
args
(
0
)).
getLines
if
(!
line
.
startsWith
(
"c"
)
&&
!
line
.
startsWith
(
"p"
)))
yield
{
line
.
split
(
" "
).
toList
.
map
(
_
.
toInt
)
line
.
split
(
" "
).
toList
.
map
(
_
.
toInt
)
}).
toList
else
defaultProblem
}).
toList
else
defaultProblem
type
CM
=
Constraint1
[
Map
[
Int
,
Boolean
]]
//
type CM = Constraint1[Map[Int,Boolean]]
val
solution
:
CM
=
//
val solution : CM =
problem
.
foldLeft
[
CM
]((
m
:
Map
[
Int
,
Boolean
])
=>
true
)((
cons
:
CM
,
clause
:
List
[
Int
])
=>
cons
&&
clause
.
foldLeft
[
CM
]((
m
:
Map
[
Int
,
Boolean
])
=>
false
)((
cons
:
CM
,
lit
:
Int
)
=>
{
//
problem.foldLeft[CM]((m:Map[Int,Boolean]) => true)((cons:CM,clause:List[Int]) => cons && clause.foldLeft[CM]((m:Map[Int,Boolean]) => false)((cons:CM,lit:Int) => {
val
isPos
=
lit
>
0
//
val isPos = lit > 0
val
abs
=
scala
.
math
.
abs
(
lit
)
//
val abs = scala.math.abs(lit)
cons
||
((
m
:
Map
[
Int
,
Boolean
])
=>
m
(
abs
)
==
isPos
)
//
cons || ((m:Map[Int,Boolean]) => m(abs) == isPos)
}))
//
}))
val
answer
=
solution
.
solve
// val answer = solution.solve
println
(
"Solution map : "
+
answer
)
// println("Solution map : " + answer)
(
1
to
4
).
foreach
(
i
=>
println
(
"i : "
+
answer
(
i
)))
// (1 to 4).foreach(i => println("i : " + answer(i)))
println
(
"satSolve(p)"
,
satSolve
(
problem
))
}
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment