Package gPy :: Module WSATUtils
[hide private]
[frames] | no frames]

Module WSATUtils

source code

Various utilities for interfacing with weighted SAT solvers, particularly (altered versions of) MaxWalkSAT

Functions [hide private]
 
best_n(wcnf_filename, output_filename, best, set_true=(), flags='')
Get n best ADGs
source code
Boolean
find_it(wcnf_filename, output_filename, true_adg, inv_atom_ids, predicate='has_parents', set_true=(), flags='')
Method for searching for a true ADG
source code
 
make_sample(wcnf_filename, output_filename, best, set_true=(), flags='')
Generate a sample
source code
List
parse_maxwalksat(fobj, inv_atom_ids, predicate='has_parents')
Return (cost_of_ADG,ADG) pairs from MaxWalkSAT output
source code
 
runwalksat(wcnf_filename, output_filename, set_true=(), flags='', printatoms=False)
Basic routine to run maxwalksat
source code
Tuple
write_cnf(file, clause_generators, hard_clause_cost=None, comments=True)
Write a CNF file using the provided clause_generators
source code
 
write_initfile(num_atoms, set_true=frozenset([]))
Writes a file called 'initfile' in the current directory.
source code
 
write_printatomsinitfile(printatoms)
Writes a file called 'printatomsinitfile' in the current directory.
source code
Variables [hide private]
  _version = '$Id: WSATUtils.py,v 1.1 2008/10/07 09:15:01 jc Exp $'
Function Details [hide private]

best_n(wcnf_filename, output_filename, best, set_true=(), flags='')

source code 

Get n best ADGs

Parameters:
  • wcnf_filename (String) - Name of WCNF file
  • output_filename (String) - Where true_adg will be if it is found in the search.
  • best (Integer) - How many ADGs to return
  • set_true (Sequence) - Sequence of atoms to initialise to true (only for cheating!)
  • flags (String) - Options to give newmaxwalksat

find_it(wcnf_filename, output_filename, true_adg, inv_atom_ids, predicate='has_parents', set_true=(), flags='')

source code 

Method for searching for a true ADG

Parameters:
  • wcnf_filename (String) - Name of WCNF file
  • output_filename (String) - Where true_adg will be if it is found in the search.
  • true_adg (Graphs.ADG) - The true ADG
  • inv_atom_ids (Dictionary) - Mapping from logical atoms to numbers
  • predicate (String) - The predicate symbol used for the relation beween a child and its parent set
  • set_true (Sequence) - Sequence of atoms to initialise to true (only for cheating!)
  • flags (String) - Options to give newmaxwalksat
Returns: Boolean
Whether true_adg is amongst those found in the search

make_sample(wcnf_filename, output_filename, best, set_true=(), flags='')

source code 

Generate a sample

Parameters:
  • wcnf_filename (String) - Name of WCNF file
  • output_filename (String) - Where true_adg will be if it is found in the search.
  • best (Integer) - How many ADGs to return
  • set_true (Sequence) - Sequence of atoms to initialise to true (only for cheating!)
  • flags (String) - Options to give newmaxwalksat

parse_maxwalksat(fobj, inv_atom_ids, predicate='has_parents')

source code 

Return (cost_of_ADG,ADG) pairs from MaxWalkSAT output

The method looks for output between 'Begin assign' and 'End assign'

Parameters:
  • fobj (Readable file object (not the filename)) - MaxWalkSAT output
  • inv_atom_ids (List) - Maps integers to ground logical atoms. This will have typically been produced by write_cnf.
  • predicate (String) - The ground logical atoms recovered using inv_atom_ids are assumed to be tuples of the form (predicate,child,pas) where child is a vertex and pas is a frozenset giving its parents.
Returns: List
Returns (cost_of_ADG,ADG) pairs for all ADGs in the output.
Raises:
  • IndexError - If an integer is encountered for which inv_atom_ids can find no ground logical atom.

runwalksat(wcnf_filename, output_filename, set_true=(), flags='', printatoms=False)

source code 

Basic routine to run maxwalksat

Actually calls 'newmaxwalksat', an altered version of MaxWalkSAT

Parameters:
  • wcnf_filename (String) - Name of WCNF file
  • output_filename (String) - Name of output file
  • set_true (Iterable) - Atoms to set initially true (useful for cheating)
  • flags (String) - Additional flags to give to MaxWalkSAT
  • printatoms (Iterable) - Which atoms to select for printing during a run

write_cnf(file, clause_generators, hard_clause_cost=None, comments=True)

source code 

Write a CNF file using the provided clause_generators

Ideally the cost of a hard clause is +infinity, but ... If the cost of hard clauses is too high then an assignment with an overflowing total cost may be produced, possibly confusing a SAT solver. If it is too low, then an assignment which breaks a hard clause may be optimal.

The default is to set it to x such that cost_of_soft_clauses * x|hard clauses| = sys.maxint. Then even breaking all clauses will not give an overflow. This is reasonable as long as the SAT solver will be running on the same machine as this Python process. If this value of x is below the cost of a soft clause an error is raised.

This method returns the tuple (atom_ids, inv_atom_ids, extra_cost). atom_ids is a dictionary mapping logical ground atoms to the integers representing them inv_atom_ids is the inverse of atom_ids. It is a list (note that inv_atom_ids[0]=None, since 0 is never used to represent an atom in WCNF files.) extra_cost is the total cost of all soft empty clauses which (ideally) would be in the WCNF file. Such clauses are not written to the WCNF file since solvers (eg UBCSAT) don't like them. This cost should be added to the cost of any assigment found by a SAT solver.

Parameters:
  • file (Writable file object) - Where to write the WCNF file
  • clause_generators (Sequence) - Sequence of clause generator iterators. For example, clause_generators = ( probadg.clauses_justorder(float_weights=True), probadg.clauses_no3cycles()) is a valid choice, where probadg is a RandomGraphs.RandomADG object.
  • hard_clause_cost (Number of None) - The cost of breaking a 'hard' clause. If None a suitable value is computed internally.
  • comments (Boolean) - Whether to include comments in the generated WCNF stating what the atoms and clauses mean
Returns: Tuple
atom_ids, inv_atom_ids, extra_cost (see above)
Raises:
  • RuntimeError - If a hard empty clause is generated. (Such a clause can never be satisfied.)
  • ValueError - If soft clauses have such high costs that a sensible hard clause cost cannot be chosen.

write_initfile(num_atoms, set_true=frozenset([]))

source code 

Writes a file called 'initfile' in the current directory. This file will be used to set all atoms, except those in set_true initially to false in a MaxWalkSAT run

Parameters:
  • num_atoms (Integer) - The number of atoms to set. The set of atoms is 1,2, ... num_atoms
  • set_true (Iterable) - Atoms to set true (useful for cheating)

write_printatomsinitfile(printatoms)

source code 

Writes a file called 'printatomsinitfile' in the current directory. This file will be used to tell MaxWalkSAT which atoms to print out during its run

Parameters:
  • printatoms (Iterable) - Which atoms to print out