def __init__(self, inN, inK, inQ, fileName = None): self.q = inQ self.n = inN self.k = inK # self.genNeigh() # self.genFunc() # self.genFuncQ() # self.exportToFile(fileName) nk.NKLandscape.__init__(self, inN, inK, fileName) if fileName == None: self.genFuncQ() else: self.readFile(fileName) self.Kbits = tl.genSeqBits(self.k+1)
def __init__(self,inN,inK, fileName = None): self.n = inN self.k = inK # for run experiments if fileName == None: self.genNeigh() self.genFunc() else: self.readFile(fileName) # # for generating benchmarks # self.genNeigh() # self.genFunc() # self.exportToFile(fileName) # self.Kbits = tl.genSeqBits(self.k+1)
def genHyperWalVote(self): """ using the voting strategy where only best hyperplane have the chance to vote selecting hyperplane template on the basis of nonzero Walsh coefficients """ self.transWal() # bit,fit = tl.compFit(self) # a = sorted(zip(bit,fit), key=lambda a_entry: a_entry[1]) # optBit = a[0][0] # optFit = a[0][1] # print 'opti\n',optBit, optFit #for i in range(len(a)): # for i in range(10): # print '%s\t%.3f' %(a[i][0],a[i][1]) # initialize sumFitA self.sumFitA = [] evalSubFunc = [] for i in range(self.n): self.sumFitA.append(Struct(one=0,zero=0)) for i in self.WA: subBit = i.arr if subBit not in evalSubFunc and i.arr: evalSubFunc.append(subBit) # check every template that matches the subfunction seqBits = tl.genSeqBits(len(subBit)) schFitArr = [] for j in seqBits: schFit = 0 # convert bit string to array representation schTpl = [] for k in range(len(j)): if j[k] == '1': schTpl.append(subBit[k]) # compute schema fitness for k in self.WA: subset = True for l in k.arr: if l not in subBit: subset = False break if subset == True: schFit = schFit + int(math.pow(-1, self.binCountArr(k.arr, schTpl))) * k.w schFitArr.append(Struct(fit=schFit,arr=schTpl)) # print subBit, j, schFit # print schFitArrSort = sorted(schFitArr, key = lambda i: i.fit) # perform voting from the best hyperplane associated with the subfunction #for k in range(self.k+1): for k in range(1): #for k in range(self.k*2): for j in subBit: if j in schFitArrSort[k].arr: #self.sumFitA[j].one = self.sumFitA[j].one + schFitArrSort[k].fit self.sumFitA[j].one = self.sumFitA[j].one + 1 else: #self.sumFitA[j].zero = self.sumFitA[j].zero + schFitArrSort[k].fit self.sumFitA[j].zero = self.sumFitA[j].zero + 1
def genHyperSqVote(self): """ using the voting strategy where only best hyperplane have the chance to vote compose the template on the bases of union of two subfunction, in this way each variable can have more than one vote """ self.transWal() # print 'genHyperSqVote' # # bit,fit = tl.compFit(self) # a = sorted(zip(bit,fit), key=lambda a_entry: a_entry[1]) # optBit = a[0][0] # optFit = a[0][1] # print 'opti\n',optBit, optFit # # for i in range(len(a)): ## for i in range(10): # print '%s\t%.3f' %(a[i][0],a[i][1]) # initialize sumFitA self.sumFitA = [] for i in range(self.n): self.sumFitA.append(Struct(one=0,zero=0)) # scan = 0 # reuse = 0 evalOuterFunc = [] mergeFunc = [] for i in range(self.n): subBitOut = self.neighs[i][:] subBitOut.append(i) subBitOut.sort() # print 'subBitOut', subBitOut if subBitOut not in evalOuterFunc: evalOuterFunc.append(subBitOut) evalInnerFunc = [] for ii in range(i+1,self.n): subBitIn = self.neighs[ii][:] subBitIn.append(ii) subBitIn.sort() # print '\tsubBitIn', subBitIn if subBitIn != subBitOut and subBitIn not in evalInnerFunc: evalInnerFunc.append(subBitIn) subBitIn = tl.listMerge(subBitOut,subBitIn) subBitIn.sort() if subBitIn not in mergeFunc: mergeFunc.append(subBitIn) # print '\t\tsubMerge', subBitIn # check every template that matches the subfunction seqBits = tl.genSeqBits(len(subBitIn)) schFitArr = [] walTouch = [] init = False for j in seqBits: schFit = 0 # convert bit string to array representation schTpl = [] for k in range(len(j)): if j[k] == '1': schTpl.append(subBitIn[k]) if init == False: # compute schema fitness from scan over all wal cof for k in self.WA: subset = True for l in k.arr: if l not in subBitIn: subset = False break if subset == True: schFit = schFit + int(math.pow(-1, self.binCountArr(k.arr, schTpl))) * k.w walTouch.append(k) init = True # scan = scan + 1 else: for k in walTouch: schFit = schFit + int(math.pow(-1, self.binCountArr(k.arr, schTpl))) * k.w # reuse = reuse + 1 schFitArr.append(Struct(fit=schFit,arr=schTpl)) #print subBitIn, j, schFit # print schFitArrSort = sorted(schFitArr, key = lambda i: i.fit) # perform voting from the best hyperplane associated with the subfunction #for k in range(self.k+1): for k in range(1): #for k in range(self.k*2): for j in subBitIn: if j in schFitArrSort[k].arr: #self.sumFitA[j].one = self.sumFitA[j].one + schFitArrSort[k].fit self.sumFitA[j].one = self.sumFitA[j].one + 1 else: #self.sumFitA[j].zero = self.sumFitA[j].zero + schFitArrSort[k].fit self.sumFitA[j].zero = self.sumFitA[j].zero + 1