Path: blob/master/src/sage/sat/solvers/cryptominisat/solverconf.pyx
8823 views
"""1Configuration for CryptoMiniSat.23EXAMPLE:45We construct a new configuration object::67sage: from sage.sat.solvers.cryptominisat import SolverConf # optional - cryptominisat8sage: s = SolverConf() # optional - cryptominisat9sage: s.doxorsubsumption # optional - cryptominisat10True1112and modify it such that we disable xor subsumption::1314sage: s.doxorsubsumption = False # optional - cryptominisat1516Finally, we pass it on to CryptoMiniSat::1718sage: from sage.sat.solvers.cryptominisat import CryptoMiniSat # optional - cryptominisat19sage: cms = CryptoMiniSat(s) # optional - cryptominisat2021Note that we can achieve the same effect by::2223sage: from sage.sat.solvers.cryptominisat import CryptoMiniSat # optional - cryptominisat24sage: cms = CryptoMiniSat(doxorsubsumption=False) # optional - cryptominisat2526AUTHORS:2728- Martin Albrecht (2012): first version29"""30##############################################################################31# Copyright (C) 2012 Martin Albrecht <[email protected]>32# Distributed under the terms of the GNU General Public License (GPL)33# The full text of the GPL is available at:34# http://www.gnu.org/licenses/35##############################################################################3637###38# NOTE: To add new options edit solverconf_helper.cpp which maintains39# a map from names to C++ attributes.40###4142from libc.stdint cimport uint32_t, uint64_t4344include "sage/ext/stdsage.pxi"4546cdef class SolverConf(object):47"""48Configuration for cls:`CryptoMiniSat`4950This class implements an interface to the C++ SolverConf class51which allows to configure the behaviour of the cls:`CryptoMiniSat`52solver.53"""54def __cinit__(self, **kwds):55"""56Construct a new configuration object.5758INPUT:5960- ``kwds`` - see string representation of any instance of this61class for a list of options.6263EXAMPLE::6465sage: from sage.sat.solvers.cryptominisat import SolverConf # optional - cryptominisat66sage: s = SolverConf() # optional - cryptominisat67sage: s # optional - cryptominisat68random_var_freq: 0.001000 # the frequency with which the decision heuristic tries to choose a random variable. (default 0.02)69...70greedyunbound: True # if set, then variables will be greedily unbounded (set to l_undef). this is experimental71origseed: 0 #72"""73self._conf = new SolverConfC()74self._nopts = setup_map(self._map, self._conf[0], 100)7576for k,v in kwds.iteritems():77self[k] = v7879def __dealloc__(self):80"""81TESTS::8283sage: from sage.sat.solvers.cryptominisat import SolverConf # optional - cryptominisat84sage: for i in range(100): # optional - cryptominisat85... s = SolverConf()86... del s8788"""89del self._conf9091def __setitem__(self, name, value):92"""93Set options using dictionary notation.9495EXAMPLE::9697sage: from sage.sat.solvers.cryptominisat import SolverConf # optional - cryptominisat98sage: s = SolverConf() # optional - cryptominisat99sage: s['polarity_mode'] = 3 # optional - cryptominisat100sage: s['polarity_mode'] # optional - cryptominisat1013102103TESTS::104105sage: s['foobar'] = 1.2 # optional - cryptominisat106Traceback (most recent call last):107...108AttributeError: SolverConf has no option 'foobar'109110"""111for i in range(self._nopts):112name_i = self._map[i].name.lower()113if name != name_i:114continue115if self._map[i].type == t_int:116(<int*>self._map[i].target)[0] = value117elif self._map[i].type == t_float:118(<float*>self._map[i].target)[0] = value119elif self._map[i].type == t_double:120(<double*>self._map[i].target)[0] = value121elif self._map[i].type == t_Var:122(<uint32_t*>self._map[i].target)[0] = value123elif self._map[i].type == t_bool:124(<bint*>self._map[i].target)[0] = value125elif self._map[i].type == t_uint32_t:126(<uint32_t*>self._map[i].target)[0] = value127elif self._map[i].type == t_uint64_t:128(<uint64_t*>self._map[i].target)[0] = value129else:130raise NotImplementedError("Type %d of CryptoMiniSat is not supported yet."%self._map[i].type)131return132raise AttributeError("SolverConf has no option '%s'"%name)133134def __setattr__(self, name, value):135"""136Set options using attributes.137138EXAMPLE::139140sage: from sage.sat.solvers.cryptominisat import SolverConf # optional - cryptominisat141sage: s = SolverConf() # optional - cryptominisat142sage: s.dovarelim = False # optional - cryptominisat143sage: s.dovarelim # optional - cryptominisat144False145146TESTS::147148sage: s.foobar = 1.2 # optional - cryptominisat149Traceback (most recent call last):150...151AttributeError: SolverConf has no option 'foobar'152"""153self[name] = value154155def __getitem__(self, name):156"""157Read options using dictionary notation.158159EXAMPLE::160161sage: from sage.sat.solvers.cryptominisat import SolverConf # optional - cryptominisat162sage: s = SolverConf() # optional - cryptominisat163sage: s['simpstartmult'] # optional - cryptominisat164300.0165166TESTS::167168sage: s['foobar'] # optional - cryptominisat169Traceback (most recent call last):170...171AttributeError: SolverConf has no option 'foobar'172173"""174for i in range(self._nopts):175name_i = self._map[i].name.lower()176if name_i != name:177continue178if self._map[i].type == t_int:179return int((<int*>self._map[i].target)[0])180elif self._map[i].type == t_float:181return float((<float*>self._map[i].target)[0])182elif self._map[i].type == t_double:183return float((<double*>self._map[i].target)[0])184elif self._map[i].type == t_Var:185return int((<uint32_t*>self._map[i].target)[0])186elif self._map[i].type == t_bool:187return bool((<bint*>self._map[i].target)[0])188elif self._map[i].type == t_uint32_t:189return int((<uint32_t*>self._map[i].target)[0])190elif self._map[i].type == t_uint64_t:191return int((<uint64_t*>self._map[i].target)[0])192else:193raise NotImplementedError("Type %d of CryptoMiniSat is not supported."%self._map[i].type)194raise AttributeError("SolverConf has no option '%s'"%name)195196def __getattr__(self, name):197"""198Read options using dictionary notation.199200EXAMPLE::201202sage: from sage.sat.solvers.cryptominisat import SolverConf # optional - cryptominisat203sage: s = SolverConf() # optional - cryptominisat204sage: s.restrictpickbranch # optional - cryptominisat2050206207TESTS::208209sage: s.foobar # optional - cryptominisat210Traceback (most recent call last):211...212AttributeError: SolverConf has no option 'foobar'213"""214215return self[name]216217def __repr__(self):218"""219Print the current configuration.220221EXAMPLE::222223sage: from sage.sat.solvers.cryptominisat import SolverConf # optional - cryptominisat224sage: s = SolverConf(); s # optional - cryptominisat225random_var_freq: 0.001000 # the frequency with which the decision heuristic tries to choose a random variable. (default 0.02)226...227greedyunbound: True # if set, then variables will be greedily unbounded (set to l_undef). this is experimental228origseed: 0 #229"""230rep = []231for i in range(self._nopts):232name = self._map[i].name.lower()233doc = self._map[i].doc.lower()234if self._map[i].type == t_int:235val = "%10d"%(<int*>self._map[i].target)[0]236elif self._map[i].type == t_float:237val = "%10f"%(<float*>self._map[i].target)[0]238elif self._map[i].type == t_double:239val = "%10f"%(<double*>self._map[i].target)[0]240elif self._map[i].type == t_Var:241val = "%10d"%(<uint32_t*>self._map[i].target)[0]242elif self._map[i].type == t_bool:243val = "%10s"%bool((<bint*>self._map[i].target)[0])244elif self._map[i].type == t_uint32_t:245val = "%10d"%(<uint32_t*>self._map[i].target)[0]246elif self._map[i].type == t_uint64_t:247val = "%10d"%(<uint64_t*>self._map[i].target)[0]248else:249val = "UNKNOWN"250rep.append("%20s: %10s # %50s"%(name,val,doc))251return "\n".join(rep)252253def trait_names(self):254"""255Return list of all option names.256257EXAMPLE::258259sage: from sage.sat.solvers.cryptominisat import SolverConf # optional - cryptominisat260sage: s = SolverConf() # optional - cryptominisat261sage: s.trait_names() # optional - cryptominisat262['random_var_freq', 'clause_decay', 'restart_first', 'restart_inc', 'learntsize_factor', 'learntsize_inc',263'expensive_ccmin', 'polarity_mode', 'verbosity', 'restrictpickbranch', 'simpburstsconf', 'simpstartmult',264'simpstartmmult', 'doperformpresimp', 'failedlitmultiplier', 'dofindxors', 'dofindeqlits',265'doregfindeqlits', 'doreplace', 'doconglxors', 'doheuleprocess', 'doschedsimp', 'dosatelite',266'doxorsubsumption', 'dohyperbinres', 'doblockedclause', 'dovarelim', 'dosubsume1', 'doclausvivif',267'dosortwatched', 'dominimlearntmore', 'dominimlmorerecur', 'dofailedlit', 'doremuselessbins',268'dosubswbins', 'dosubswnonexistbins', 'doremuselesslbins', 'doprintavgbranch', 'docacheotfssr',269'docacheotfssrset', 'doextendedscc', 'docalcreach', 'dobxor', 'dootfsubsume', 'maxconfl', 'isplain',270'maxrestarts', 'needtodumplearnts', 'needtodumporig', 'maxdumplearntssize', 'libraryusage', 'greedyunbound', 'origseed']271"""272return [self._map[i].name.lower() for i in range(self._nopts)]273274def __richcmp__(self, other, op):275"""276TESTS::277278sage: from sage.sat.solvers.cryptominisat import SolverConf # optional - cryptominisat279sage: s = SolverConf() # optional - cryptominisat280sage: t = copy(s) # optional - cryptominisat281sage: t == s # optional - cryptominisat282True283sage: t.dobxor = False # optional - cryptominisat284sage: t == s # optional - cryptominisat285False286sage: t < s # optional - cryptominisat287Traceback (most recent call last):288...289TypeError: Configurations are not ordered.290"""291if op not in (2,3):292raise TypeError("Configurations are not ordered.")293res = all(self[name] == other[name] for name in self.trait_names())294if op == 2: # ==295return res296if op == 3: # !=297return not res298299def __copy__(self):300"""301Return a copy.302303EXAMPLE::304305sage: from sage.sat.solvers.cryptominisat import SolverConf # optional - cryptominisat306sage: s = SolverConf() # optional - cryptominisat307sage: t = copy(s) # optional - cryptominisat308sage: t.verbosity = 1 # optional - cryptominisat309sage: t['verbosity'] # optional - cryptominisat3101311sage: s.verbosity # optional - cryptominisat3120313"""314cdef SolverConf other = PY_NEW(SolverConf)315other._conf = new SolverConfC()316other._nopts = setup_map(other._map, other._conf[0], 100)317for name in self.trait_names():318other[name] = self[name]319return other320321322