GithubHelp home page GithubHelp logo

shnarazk / mios Goto Github PK

View Code? Open in Web Editor NEW
39.0 5.0 3.0 4.41 MB

A SAT solver written in Haskell.

Home Page: https://gitlab.com/satisfiability01/mios/

License: GNU General Public License v3.0

Haskell 95.31% R 4.69%
sat-solver haskell

mios's Introduction

Mios -- Minisat-based Implementation and Optimization Study


Mios is yet another minisat-based SAT solver implementation in Haskell, as a part of my research theme.

> Features

  • fundamentally it is developed based on Minisat-1.14 and 2.2.0.
    • Firstly, version 1.0 was based on N. Een and N. Sorensson, “An extensible SAT-solver [extended version 1.2],” in 6th Int. Conf. on Theory and Applications of Satisfiability Testing (SAT2003), 2003, pp. 502–518.
    • Version 1.1 was a line-to-line translation of MiniSat 1.14.
    • Version 1.2 imported some idea used in Glucose 4.0.
    • Version 1.5 uses Literal Block Distance (LBD).
    • Version 1.6 adopts new ideas: EMA, ACIDS and so on.
  • runs in IO monad, uses Data.Primitive.ByteArray mainly and reallyUnsafePtrEquality.
  • very fast, compared with other SAT solvers written in Haskell; see below.

benchmark results

  • SAT-Competition 2017 Main track, running 3 jobs in parallel with a 510 second timeout on Intel Core i7-3930K @ 12x 3.8GHz (Therefore results near the threshold should be affected by other threads more or less.)

Cactus plot with Mios-1.6.1: SAT Competition 2017 main

> Install

Requirements
  • ghc-8.0.1 or upper (By deleting default-extensions from mios.cabal, you can use ghc-7.10.x.)
  • Stack
  • If you want to build with cabal, please use the cabal file under utils directory.
Stack
git clone https://github.com/shnarazk/mios
stack init --resolver lts-11.X  # for ghc-8.2.X
stack install
Hackage/Cabal

Mios is registered in hackage now.

cabal install mios

> Usage

* As a standalone program
$ mios a.cnf
an assignment :: [Int]

$ mios --help
mios 1.6.1 https://github.com/shnarazk/mios/
Usage: mios [OPTIONS] target.cnf
  -d 0.95   --variable-decay-rate=0.95  [solver] variable activity decay rate (0.0 - 1.0)
  -c 0.999  --clause-decay-rate=0.999   [solver] clause activity decay rate (0.0 - 1.0)
            --Rb=1.2                    [solver] expansion rate for blocking restart (>= 1.0)
            --Rf=1.01                   [solver] expansion rate for forcing restart (>= 1.0)
            --Rs=100.0                  [solver] a fixed number of conflicts between restarts
  -:        --validate-assignment       [solver] read an assignment from STDIN and validate it
            --validate                  [solver] self-check (satisfiable) assignment
  -o file   --output=file               [option] filename to store result
  -v        --verbose                   [option] display misc information
  -X        --hide-solution             [option] hide solution
            --benchmark=-1/0/N          [devel] No/Exhaustive/N-second timeout benchmark
            --sequence=NUM              [devel] set 2nd field of a CSV generated by benchmark
            --dump=0                    [devel] dump level; 1:solved, 2:reduction, 3:restart
  -h        --help                      [misc] display this message
            --version                   [misc] display program ID

If you have GNU parallel, Mios works well with it:

parallel "mios --benchmark=0 --sequence={#} -o {.cnf}.result {}" ::: *.cnf
* In Haskell
module Main where -- this is sample.hs in app/
import SAT.Mios (CNFDescription (..), solveSAT)

clauses = [[1, 2], [1, 3], [-1, -2], [1, -2, 3], [-3]] :: [[Int]]
desc = CNFDescription 3 5 Nothing    -- #vars, #clauses, Just pathname or Nothing

main = do
  asg <- solveSAT desc clauses    -- solveSAT :: Traversable m => CNFDescription -> m [Int] -> IO [Int]
  putStrLn $ if null asg then "unsatisfiable" else show asg
$ stack ghc app/sample.hs
$ app/sample
[1,-2,-3]

Of course, you can use Mios in ghci similarly.

$ stack ghci
...>

mios's People

Contributors

shnarazk avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

mios's Issues

regression in 1.5 candidate

"mios-1.4.0 (2d91fb9@2016-09-12)", 50, "SR15m/manthey_DimacsSorterHalf_37_6.cnf",  772.28
"mios-1.5@08-24", 50, "SR15m131/manthey_DimacsSorterHalf_37_6.cnf",  818.46

TODO for 1.4.2

  • LBD implementation #36
  • Quick restart based on Luby or 'Glucose restart'
  • Removed Clause Management (GC) #34
  • performance boost

branches

  • WIP-for-1.2.0
  • lbd2
  • quick restart

more types

Some thought on types and kinds to use recent ghc features.

TODO after 1.0

  • better default parameters (1.0.0 has performance degression compared with 0.9)
  • LBD
  • phase-saving
  • literal flushing
  • reduce learnts based on one in 7.2.2.2 Satisfiabilty
  • community
  • suvery propagation
  • parallel computation
  • restart policy
  • clause freezing
  • validator

Unwrap aesthetic decorations!

_layered and functional version_

        Thu Feb 11 02:49 2016 Time and Allocation Profiling Report  (Final)

           mios-noinline-prof +RTS -p -RTS SR15m/manthey_DimacsSorter_30_4.cnf

        total time  =     2921.22 secs   (2921219 ticks @ 1000 us, 1 processor)
        total alloc = 1,669,657,684,504 bytes  (excludes profiling overheads)

COST CENTRE   MODULE                                 %time %alloc

getNthInt     SAT.Solver.Mios.Data.FixedVecInt        34.3   67.8
propagateLit  SAT.Solver.Mios.Solver                  11.8    7.4
nextWatcher   SAT.Solver.Mios.WatchList               11.0    2.4
pushWatcher   SAT.Solver.Mios.WatchList                9.7    2.3
propagate     SAT.Solver.Mios.Solver                   7.6    1.2
clauseNew     SAT.Solver.Mios.Solver                   5.5    0.0
selectWatcher SAT.Solver.Mios.Clause                   4.5    4.3
valueLit      SAT.Solver.Mios.Solver                   4.5    5.2
insertQueue   SAT.Solver.Mios.Data.QueueOfBoundedInt   1.7    1.4
enqueue       SAT.Solver.Mios.Solver                   1.4    0.3
undoOne       SAT.Solver.Mios.Solver                   0.9    2.1
setNthInt     SAT.Solver.Mios.Data.FixedVecInt         0.5    1.3

_low, raw and C-ish version_

        Thu Feb 11 19:07 2016 Time and Allocation Profiling Report  (Final)

           mios-noinline-prof +RTS -p -RTS SR15m/manthey_DimacsSorter_30_4.cnf

        total time  =     1363.96 secs   (1363957 ticks @ 1000 us, 1 processor)
        total alloc = 971,328,150,872 bytes  (excludes profiling overheads)

COST CENTRE   MODULE                                 %time %alloc

getNthInt     SAT.Solver.Mios.Data.FixedVecInt        24.3   21.2
propagateLit  SAT.Solver.Mios.Solver                  13.4   14.1
pushWatcher   SAT.Solver.Mios.WatchList               11.2    4.0
nextWatcher   SAT.Solver.Mios.WatchList               11.1    4.1
propagate     SAT.Solver.Mios.Solver                   9.5    2.1
clauseNew     SAT.Solver.Mios.Solver                   6.3    6.9
valueLit      SAT.Solver.Mios.Solver                   5.5   18.1
selectWatcher SAT.Solver.Mios.Clause                   4.5    7.4
insertQueue   SAT.Solver.Mios.Data.QueueOfBoundedInt   2.0    2.5
enqueue       SAT.Solver.Mios.Solver                   1.7    0.5
sizeOfClause  SAT.Solver.Mios.Clause                   1.5    2.3
undoOne       SAT.Solver.Mios.Solver                   1.1    3.7
setNthInt     SAT.Solver.Mios.Data.FixedVecInt         0.8    3.0
valueVar      SAT.Solver.Mios.Solver                   0.7    1.4
setNthClause  SAT.Solver.Mios.Clause                   0.3    1.2

comparison target: mios-WIP(unreleased 1.0.2)

       Fri Feb 12 18:42 2016 Time and Allocation Profiling Report  (Final)

           mios-WIP-prof +RTS -p -RTS manthey_DimacsSorter_30_4.cnf

        total time  =     2477.18 secs   (2477183 ticks @ 1000 us, 1 processor)
        total alloc = 20,225,903,792 bytes  (excludes profiling overheads)

COST CENTRE MODULE                 %time %alloc

solve       SAT.Solver.Mios.Solver  99.5   95.6
select      SAT.Solver.Mios.Solver   0.2    4.2

Wrong interpretation of comment lines after DIMACS header part

f.e.: manthey_DimacsSorter_28.cnf (SAT RACE 2015) contains:

p cnf 3553 11381
c Exactly One of a0_0_2380 (3) a0_1_2380 (4) a1_0_2380 (5) a1_1_2380 (6) 
c -a0_0_2380 -a0_1_2380
-3 -4 0
c -a0_0_2380 -a1_0_2380
-3 -5 0
c -a0_0_2380 -a1_1_2380
-3 -6 0
c -a0_1_2380 -a1_0_2380

mios-1.0.1 and upper can't handle it correctly.

release plan

LBD was implemented. But before committing it to master, I must make a release of a vanilla minisat implementation.

  • move codes on LBD from the HEAD to a new branch
  • move codes on my heuristics on clause activity to somewhere
  • bump to closed version 0.9 in my private repository -- a solid minisat implementation
  • swtich to this repository from a private one by Dec. 10
    • prune everything but mios 🎄
    • rebase 🎋
  • bump to closed version 1.0 in my private repository
    • switch to stack from cabal
    • comparison of IORef, Vector.Unboxed.Mutable singleton, Data.Mutable
  • reimplement old version based on 1.0 codebases to make version comparisons meaningful. (Old versions have critical mistakes in various functions)
    • salvage old classes and types ⚓
    • add some mechanism to switch versions easily
    • update cabal file in order to generate all versions
  • Test! Experiment! Record! Summarize! 🏫
  • refactoring to RC3
    • commit everything
    • rebase
    • activate all options
  • refactoring to 1.0
    • update REAME.md
    • commit everything
    • rebase
    • final check
  • update statistics
  • open version 1.0 -- slow but true minisat by Dec. 30 2016-01-13 🎉

GADTs and Kind

{-# LANGUAGE TypeFamilies, DataKinds, GADTs, StandaloneDeriving, FlexibleInstances #-}
module Clause where
-- import Unsafe.Coerce

data ClauseKind = OfNormal | NullCaluse | BinaryClause

data Clause (a :: ClauseKind) where
  MkClause :: [Int] -> Clause 'OfNormal
  MkBinaryClause :: Int -> Clause 'BinaryClause

instance Eq (Clause a) where
  (MkBinaryClause a) == (MkBinaryClause b) = a == b
  _ == _ = undefined

instance Show (Clause a) where
  show (MkBinaryClause l) = "B{" ++ show l ++ "}"
  show (MkClause _) = show "Clause"
  
-- data Clause (a :: ClauseKind) = Clause [Int] deriving (Eq, Show)

c :: [Clause 'OfNormal]
c = []

Segmentation fault when used as a library

On complicated problems, mios works fine when used as an executable like this:

$ stack run mios162 -- sudoku.dimacs
SATISFIABLE, saved to .ans_sudoku.dimacs for sudoku.dimacs

Example problem: https://gist.github.com/potocpav/97c84a24a45797b44a63a565295aec8b

It, however, fails on the same file when used as a library:

import SAT.Mios.Util.DIMACS.Reader
import SAT.Mios (CNFDescription(..), solveSAT)

main :: IO ()
main = do
    clauses <- clauseListFromFile "sudoku.dimacs"
    let nVars = (maximum . map abs $ concat clauses)
    let descr = CNFDescription nVars (length clauses) "file"
    l <- solveSAT descr clauses
    print l

This sometimes shows Segmentation fault (core dumped), sometimes this:

simple: Data.Vector.Mutable: uninitialised element
CallStack (from HasCallStack):
  error, called at ./Data/Vector/Mutable.hs:188:17 in vector-0.12.0.2-AoZ9EwUsgIW1yrOc105QXH:Data.Vector.Mutable

On large problems (>10k variables) it fails reliably. For less complicated problems (~2000 variables), the failures are intermittent. For small problems (~10 variables), it works.

TODO for 1.0.3

  • Implement the following missing functions cancel
    • analyze_removable
    • analyzeFinal
  • no more pointer; uses only vector of clauses from ClauseVector branch

Phantom

{-# LANGUAGE FlexibleInstances #-}
module Main where

newtype VecFam a = V [Int]

data AsVector
data AsStack
data AsQueue

class Gettable a where
  get :: a -> [Int]
  push :: a -> Int -> a
  peep :: a -> Int
  pop :: a -> a

type Vector = VecFam AsVector
type Stack = VecFam AsStack
type Queue = VecFam AsQueue

instance Gettable Vector where
  get (V a) = a

instance Gettable Stack where
  get (V a) = tail a

instance Gettable Queue where
  get (V a) = reverse $ tail a

q = V [1 .. 5]

main :: IO ()
main = do
  print $ get (q :: Vector)
  print $ get (q :: Stack)
  print $ get (q :: Queue)

TODO for 1.2

It deserves to release.

  • validity (soundness) checking by SR15m131
  • delete a lot of working branches
  • validator should display filename
  • replace ClauseManager with WatcherList; or rename to a better name
  • delete useless comments
  • redo glucose (single thread) benchmark
  • update documents
  • release by June 24 27

TODO for 1.4

  • replace VSIDS with a better heuristics.

branches

  • chb
  • WIP-for-1.3.0

TODO for 1.0.2

Feature: a bug fix on DIMACS reader + the following micro tunings (or code-cleanup) from nowrap-with-inline branch

  • change level, activities, reason and seen to var-indexed (1-based) vectors
  • add more type declarations
  • no heap for var selection

release procedure

  • 🔍 Implemenation Validation by Feb. 21.
    • 🏫 on UF250 with mios
    • on UF250 with sih4
    • ⛪ on SAT-Race 2015 subsets with mios
    • on SAT-Race 2015 subsets with sih4
  • ⌚ Performance Comparion by Feb 21.
    • ⏳ experiment on 3-SATs
    • 🚥 experiment on SAT-Race 2015 subsets
  • 📦 Packaging by Feb. 22.
    • 🕥 rebase on master branch
    • ♻️ delete wip branches:
      • noinline
      • noinline-nowrap
      • noinline-nowrap2
      • nowrap
      • nowrap-with-inline
      • reader
      • validator
      • wl3
      • coredump
      • 1.0.0-eval
      • 1.0.2-eval
    • cancel 1.0.1

TODO for 1.1 and 1.2

Though the purpose of 1.0 is to translate the codes in the paper (Eén, Niklas, and Niklas Sörensson. "An extensible SAT-solver." Theory and applications of satisfiability testing. Springer Berlin Heidelberg, 2003.) and some parts from MiniSat 1.14 into a program written in Haskell, I found there are a lot of differences between the paper and MiniSat 1.14; version 1.14 is not close to the paper but to MiniSat 2.2. It makes difficult to develop mios further; most part of the solver core should be updated.
So stopping to improve the performance of the current mios 1.0.X based on the paper, I decided to switch the reference code to MiniSat 2.2 after the release of 1.0.3.

  • mios 1.0 is a study on the paper.
  • mios 1.1 is a study based on MiniSat 2.2. This is the current branch M22 and a one month sprint I hope.

It is a shorter way to archive an expected high-performance SAT solver in Haskell.


random note

  • reorder functions
  • GClause for binary clauses
  • analyzeRemovable and 3 variables for analyze family
  • current target is not simp but core
  • var heap is ready
  • Luby is ready
  • LBD is ready
  • write the output to a file in DIMACS CNF format

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.