metaSMT git

metaSMT/backend/PicoSAT.hpp

Go to the documentation of this file.
00001 #pragma once
00002 
00003 #include "../tags/SAT.hpp"
00004 #include "../result_wrapper.hpp"
00005 
00006 extern "C"
00007 {
00008 #include <picosat.h>
00009 }
00010 #include <vector>
00011 #include <exception>
00012 
00013 #include <boost/fusion/sequence/intrinsic.hpp>
00014 #include <boost/fusion/support/is_sequence.hpp>
00015 #include <boost/variant.hpp>
00016 #include <boost/any.hpp> 
00017 #include <boost/foreach.hpp>
00018  
00019 
00020 namespace metaSMT {
00021   namespace solver {
00022 
00023     class PicoSAT
00024     {
00025       public:
00026         PicoSAT ()
00027         {
00028 //          if ( in_use == USED )
00029 //            throw std:runtime_error ("Picosat already in use. Only single instances supported."); 
00030          
00031           picosat_init (); 
00032 //          in_use = USED;
00033         }
00034 
00035         ~PicoSAT ()
00036         {
00037           picosat_reset (); 
00038  //         in_use = UNUSED;
00039         }
00040          
00041 
00042         int toLit ( SAT::tag::lit_tag lit )
00043         {
00044           return lit.id; 
00045         }
00046 
00047         void clause ( std::vector < SAT::tag::lit_tag > const& clause)
00048         {
00049           BOOST_FOREACH ( SAT::tag::lit_tag const& lit, clause )
00050             picosat_add ( toLit ( lit ) );
00051           picosat_add ( 0 ); 
00052         }
00053 
00054 
00055         void assertion ( SAT::tag::lit_tag lit )
00056         {
00057           picosat_add ( toLit ( lit ) ); 
00058           picosat_add ( 0 ); 
00059         }
00060 
00061         void assumption ( SAT::tag::lit_tag lit )
00062         {
00063           picosat_assume ( toLit ( lit ) ); 
00064         }
00065 
00066 
00067         bool solve ( )
00068         {
00069           switch (picosat_sat (-1))
00070           {
00071             case PICOSAT_UNSATISFIABLE:
00072               return false;
00073             case PICOSAT_SATISFIABLE:
00074               return true;
00075             case PICOSAT_UNKNOWN:
00076               throw std::runtime_error ( "unknown return type of picosat_sat "); 
00077             default:
00078               throw std::runtime_error ( "unsupported return type of picosat_sat "); 
00079           }
00080         }
00081 
00082         result_wrapper read_value ( SAT::tag::lit_tag lit ) 
00083         {
00084            
00085           switch ( picosat_deref ( toLit ( lit ) ) )
00086           {
00087             case -1:
00088               return result_wrapper ( '0' );
00089             case 1:
00090               return result_wrapper ( '1' );
00091             case 0:
00092               return result_wrapper ( 'X' );
00093             default:
00094               std::cerr << "Unknown result." << std::endl;
00095               return result_wrapper ( 'X' ); 
00096           }
00097         }
00098 
00099       private:
00100 //         enum { UNUSED, USED }; 
00101 //         static int in_use = UNUSED; 
00102     };
00103   } /* solver */
00104 } /* metaSMT */
00105 // vim: ts=2 sw=2 et
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines