def path_sat(self, pathname): sat = {} for name in pathname: nov, val = nov_val(name) bits = Center.snodes[nov].choice['bits'] vals = [get_bit(val, 2), get_bit(val, 1), get_bit(val, 0)] for ind, b in enumerate(bits): sat[b] = vals[ind] return sat
def get_sats(self, val): assert (val < (2**self.ln)) satdic = {} for ind, vn in enumerate(self.varray): v = get_bit(val, ind) satdic[vn] = v return satdic
def reduce_vk(self, vk): ''' return: vk12, vk3, cvr ''' hit_bits = set(self.varray).intersection(set(vk.bits)) ln = len(hit_bits) if ln == 0: # vk is totally outside of sh vk3 = vk.clone() return None, vk3, None if ln == 3: # vk is totally overlapping with sh return None, None, vk.compressed_value() # vk is partially in sh, producing a vk12, and cvs vk12 = None dic = {} # part of vk.dic lying outside of sh for b, v in vk.dic.items(): if b not in hit_bits: dic[b] = v vk12 = vk.__class__(vk.kname, dic, vk.nov) cvs = set([]) vlst = range(2**self.ln) hit_dic = {hbit: self.varray.index(hbit) for hbit in hit_bits} for val in vlst: covered = True for hit_bit, position in hit_dic.items(): if get_bit(val, position) != vk.dic[hit_bit]: covered = False break if covered: cvs.add(val) return vk12, None, list(cvs)
def fill_rest(self, sat): s = set(range(Center.maxnov)) - set(sat.keys()) if len(s) > 0: lst = tuple(s) for v in range(2**len(lst)): ssat = sat.copy() for ind, k in enumerate(lst): ssat[k] = get_bit(v, ind) Center.sats.append(ssat) else: Center.sats.append(sat)
def morph(self, n12): n12.vk12dic = {} chs = {} excl_cvs = set([]) # all possible values: for 2 bits:(0,1,2,3); for 1 bit: (0,1) allvalues = [(0, 1), (0, 1, 2, 3)][n12.sh.ln == 2] tdic = {} for kn, vk in self.vkdic.items(): bs = vk.bits[:] out_bits = set(bs) - set(n12.sh.varray) if len(out_bits) == 0: if vk.nob == n12.sh.ln: excl_cvs.add(vk.compressed_value()) elif vk.hit(n12.hsat): raise Exception(f'Wrong vk: {vk.kname}') else: pass # drop this vk elif len(out_bits) == vk.nob: # vk is totally outside of sh tdic.setdefault(allvalues, []).append(vk) else: # vk divided: in sh 1 bit, out 1 bit outb = out_bits.pop() vk12 = VKlause(vk.kname, {outb: vk.dic[outb]}, vk.nov) bs.remove(outb) # bs now has only 1 of vk that is in sh in_index = n12.sh.varray.index(bs[0]) vs = [] for v in allvalues: if get_bit(v, in_index) == vk.dic[bs[0]]: vs.append(v) tdic.setdefault(tuple(vs), []).append(vk12) for val in allvalues: if val in excl_cvs: continue sub_vk12dic = {} for cvr in tdic: if val in cvr: # kv does have outside bit vks = tdic[cvr] for vk in vks: sub_vk12dic[vk.kname] = vk.clone() vkm = VK12Manager(self.nov - n12.sh.ln, sub_vk12dic) if vkm.valid: node = n12.__class__( n12, # n12 is parent-node n12.next_sh, # sh n12.sh.get_sats(val), # val -> hsat vkm) chs[val] = node return chs # for making chdic with tnodes
def get_segment_sats(self, chil_keyvalue): ''' assert check: varray length can be 1, 2 or 3, where sat-value can be: (0,1), (0,1,2,3) or (0,1,2,3,4,5,6,7) operational example: varray: [231, 8]: names of two variables, varray[0] == 231, varray[1] == 8 And sat chil_keyvalue: 2 == bin( 10 ) output: return value is a list of tuples, as like: [(231,0), (8,1)]: variable-231 has boolean chil_keyvalue 0 variable-8 has boolean chil_keyvalue 1 ''' assert (chil_keyvalue < (2**self.ln)) lst = [] for ind, v in enumerate(self.varray): # every bit in varray represent a variable(original name) lst.append((v, get_bit(chil_keyvalue, ind))) return lst