示例#1
0
def checkNodePossibilities(node, indy, newSSS, cellNum, model, params, KOlist,
                           KIlist, boolC):
    tol = .01 * len(newSSS)  # set tolerance for equivalence
    end = findEnd(node, model)  # find end of model for this node
    start = model.individualParse[node]  # find start of model for this node
    truth = list(indy[start:end])
    equivs = [truth]  #add if
    if (end - start) == 0:
        return truth, equivs, 0.
    indOptions = []
    indErrors = []
    # iterate over possibilities for this node
    for i in range(1, 2**(end - start)):
        tempultimate = list(indy)
        tempInd = bitList(i, len(truth))
        tempultimate[start:end] = tempInd  # set rule to one being checked
        currentsumtemp = evaluateByNode(tempultimate, cellNum, model, newSSS,
                                        params, KOlist, KIlist, boolC)
        currentsum = currentsumtemp[node]  # save the error found
        indOptions.append(tempInd)
        indErrors.append(currentsum)
        gc.collect()
    minny = min(indErrors)
    equivs = []
    # find the minimum error individual
    for i in range(len(indOptions)):
        if indErrors[i] < minny + tol:
            equivs.append(indOptions[i])
    truth = equivs[0]
    return truth, equivs, minny
示例#2
0
文件: GA.py 项目: rpalli/ruleMakerGA
def checkNodePossibilities(node, indy, newSSS, cellNum, model, params, KOlist,
                           KIlist, boolC):
    tol = .01 * len(newSSS)
    end = findEnd(node, model)
    start = model.individualParse[node]
    truth = list(indy[start:end])
    equivs = [truth]  #add if
    if (end - start) == 0:
        return truth, equivs, 0.
    indOptions = []
    indErrors = []
    for i in range(1, 2**(end - start)):
        tempultimate = list(indy)
        tempInd = bitList(i, len(truth))
        tempultimate[start:end] = tempInd
        currentsumtemp = evaluateByNode(tempultimate, cellNum, model, newSSS,
                                        params, KOlist, KIlist, boolC)
        currentsum = currentsumtemp[node]
        indOptions.append(tempInd)
        indErrors.append(currentsum)
        gc.collect()
    minny = min(indErrors)
    equivs = []
    for i in range(len(indOptions)):
        if indErrors[i] < minny + tol:
            equivs.append(indOptions[i])
    truth = equivs[0]
    return truth, equivs, minny
示例#3
0
def genBits(model):
    # generate random bitlist
    startInd = list(genRandBits(model.size))
    counter = 0
    # make sure bitlist isn't zero
    while numpy.sum(startInd) == 0 and counter < 10000:
        startInd = list(genRandBits(model.size))
        counter += 1
    # go through nodes and make sure that there are 1-5 ones in the random list
    for node in range(0, len(model.nodeList)):
        end = findEnd(node, model)
        start = model.individualParse[node]
        if (end - start) > 1:
            counter = 0
            while numpy.sum(startInd[start:end]) > 5 and counter < 10000:
                chosen = math.floor(random() * (end - start))
                startInd[start + int(chosen)] = 0
                counter += 1
            if numpy.sum(startInd[start:end]) == 0:
                chosen = math.floor(random() * (end - start))
                startInd[start + int(chosen)] = 1
        elif (end - start) == 1:
            startInd[start] = 1
    return [copy.deepcopy(model), startInd]
示例#4
0
def mutFlipBitAdapt(indyIn, genfrac, mutModel):
    errors = list(indyIn.fitness.values)  # get errors
    individual = indyIn[1]
    model = indyIn[0]
    # get rid of errors in nodes that can't be changed
    errorNodes = 0
    for j in xrange(len(errors)):
        if model.andLenList[j] < 2:
            errors[j] = 0
        else:
            errorNodes = errorNodes + 1

    if numpy.sum(errors) < .05 * errorNodes or errorNodes == 0:
        # condition selection on number of incoming edges + downstream edges
        pseudoerrors = [
            len(model.possibilityList[i]) if model.successorNums[i] == 0 else
            len(model.possibilityList[i]) * model.successorNums[i]
            for i in range(len(model.nodeList))
        ]
        # zero out nodes that can't be changed
        for j in xrange(len(pseudoerrors)):
            if model.andLenList[j] < 2:
                pseudoerrors[j] = 0
        focusNode = selectMutNode(pseudoerrors)
    else:
        # if errors are relatively high, focus on nodes that fit the worst and have highest in-degree
        # calculate probabilities for mutating each node
        for i in range(len(errors)):
            temper = model.successorNums[i]
            if temper == 0:
                errors[i] = errors[i] * len(model.possibilityList[i])
            else:
                errors[i] = errors[i] * len(model.possibilityList[i]) * temper
        focusNode = selectMutNode(errors)
    # perform mutation
    if model.andLenList[focusNode] > 1:
        # find ends of the node of interest in the individual
        start = model.individualParse[focusNode]
        end = findEnd(focusNode, model)
        # mutate the inputs some of the time
        if len(model.possibilityList[focusNode]) > 3 and random() < mutModel:
            temppermup = []
            upstreamAdders = list(model.possibilityList[focusNode])
            rvals = list(model.rvalues[focusNode])
            while len(temppermup) < 3:
                randy = random()  # randomly select a node to mutate
                tempsum = sum(rvals)
                if tempsum == 0:
                    addNoder = int(math.floor(random() * len(upstreamAdders)))
                else:
                    recalc = numpy.cumsum(
                        [1. * rval / tempsum for rval in rvals])
                    addNoder = next(i for i in range(len(recalc))
                                    if recalc[i] > randy)
                temppermup.append(upstreamAdders.pop(addNoder))
                rvals.pop(addNoder)
            model.update_upstream(focusNode, temppermup)
            model.updateCpointers()
        for i in range(start, end):
            if random() < 2 / (end - start + 1):
                individual[i] = 1
            else:
                individual[i] = 0
        #ensure that there is at least one shadow and node turned on
        if numpy.sum(individual[start:end]) == 0:
            individual[start] = 1
        indyIn[0] = model
        indyIn[1] = individual
    else:
        print('did not actually check')
    return indyIn,