Example #1
0
 def try_sampling(self,subspace_dimension):
     for j in range(subspace_dimension):
         self.random_projection()
     result = self.try_solving()
     if result:
         print "Random projection satisfied"
         tp = self.holes2tape(result)
         print parse_tape(tp)[0]
         if self.is_solution_unique(tp):
             print "Unique. Accepted."
         else:
             print "Sample rejected"
Example #2
0
 def uniqueness_clause(self,tape):
     p,bit_mask = parse_tape(tape)
     clause = []
     for j in range(len(tape)):
         if bit_mask[j] == 1:
             # jth tape position
             v = self.tape2variable[j]
             if tape[j] == 1: v = -v
             clause += [v]
     return clause
Example #3
0
 def is_solution_unique(self,tape):
     d = self.generate_variable()
     clause = [d] + self.uniqueness_clause(tape)
     print "uniqueness clause",clause
     self.s.add_clause(clause)
     result = self.try_solving([-d])
     self.s.add_clause([d]) # make the clause documents they satisfied
     if result:
         tp = self.holes2tape(result)
         print "alternative:",parse_tape(tp)
         print "alternative tape:",tp
         return False
     else:
         return True
Example #4
0
 def adaptive_sample(self):
     subspace_dimension = 1
     result = self.try_solving()
     if result:
         print "Formula satisfied"
         for j in range(subspace_dimension):
             self.random_projection()
         while True:
             print "\n\niterating:"
             result = self.try_solving()
             if result:
                 print "Satisfied %d constraints" % subspace_dimension
                 tp = self.holes2tape(result)
                 print parse_tape(tp)
                 print "tape = ",tp
                 if self.is_solution_unique(tp):
                     print "UNIQUE"
                     print "<<< ==  ==  == >>>"
                 self.random_projection()
                 subspace_dimension += 1
             else:
                 print "Rejected %d projections" % subspace_dimension
                 print "total time = ",self.tt
                 break
Example #5
0
 def enumerate_solutions(self):
     solutions = []
     result = self.try_solving()
     d = self.generate_variable()
     logZ = float('-inf')
     while result:
         tp = self.holes2tape(result)
         program,mask = parse_tape(tp)
         solutions = solutions + [program]
         specified = sum(mask)
         logZ = lse(logZ, -specified * 0.693)
         print "Enumerated program", program, "with", specified, "specified bits."
         self.s.add_clause([d] + self.uniqueness_clause(tp))
         result = self.try_solving([-d])
     print "log(z) = ",logZ, "\t1/p = ", math.exp(-logZ)
     return solutions