示例#1
0
def firefly_paper(constParams, tuneParams):
    evalFun = constParams[0]
    evalConstParams = constParams[1]
    paramRanges = constParams[2]
    stopCond = constParams[3]
    stopVal = constParams[4]
    repetitions = constParams[5]
    verbose = constParams[6]
    numFlies = tuneParams[0]
    alpha = tuneParams[1]
    beta0 = tuneParams[2]
    gamma = tuneParams[3]

    numDim = len(paramRanges)
    posns = [util.uniform_random(paramRanges) for i in range(numFlies)]
    bestSol = None
    bestSolFitness = sys.float_info.max
    bestSolReps = None
    stopLoop = False
    numEvals = 0
    iterations = 0

    while not stopLoop:

        #Note that, under this implementation, the best solution is always one iteration behind the movement
        bestSol, bestSolFitness, bestSolReps, fitness, fitnessReps, newEvals = evaluate_sols(
            evalFun, evalConstParams, posns, repetitions)
        numEvals += newEvals

        posns = firefly_move_paper(posns, fitness, alpha, beta0, gamma,
                                   paramRanges)

        iterations += 1

        if stopCond == "fitness":
            if bestSolFitness < stopVal[0]:
                stopLoop = True
        elif stopCond == "fitnessOrEvals":
            if bestSolFitness < stopVal[0] or numEvals > stopVal[1]:
                stopLoop = True
        elif stopCond == "fitnessOrGlobalEvals":
            if bestSolFitness < stopVal[
                    0] or test_funs.numEvaluations > stopVal[1]:
                stopLoop = True
        elif stopCond == "generations":
            if iterations >= stopVal[0]:
                stopLoop = True
        else:
            print "ERROR: Stop condition not recognized"
            break

        if verbose:
            print "Iteration: " + str(iterations)
            print "Evaluations: " + str(numEvals)
            print "Best Solution: " + str(bestSol)
            print "Best Mean Fitness: " + str(bestSolFitness)
            print "Best Fitness Set: " + str(bestSolReps) + "\n"

    return (numEvals, bestSol, bestSolFitness, bestSolReps)
示例#2
0
def empty_nests_paper(nests, pa, bestNest, paramRanges, fmin, fminReps, evalFun, evalConstParams, oldFitness, oldFitnessReps, repetitions):
    numNests = len(nests)
    numDim = len(nests[0])

    #sort nests by fitness
    sortList = [[nests[i], oldFitness[i], oldFitnessReps[i]] for i in range(numNests)]
    sortList.sort(key=lambda x: x[1])

    #determine the cutoff past which nests will be replaced
    breakpoint = int(round((1-pa)*numNests))
    if breakpoint == numNests:
        breakpoint -= 1
    
    returnNests = []
    returnFitness = []
    returnReps = []
    #Keep nests above the cutoff
    for i in range(breakpoint):
        returnNests.append(sortList[i][0])
        returnFitness.append(sortList[i][1])
        returnReps.append(sortList[i][2])
    
    newEvals = 0
    newBestFitness = fmin
    newBestNest = bestNest
    newBestReps = fminReps
    #Replace nests below cutoff and calculate their fitness
    for i in range(breakpoint, numNests):
        newNest = util.uniform_random(paramRanges)
        newNest = util.boundary_clamp(newNest, paramRanges)

        #Calculate fitness of new nest
        fitnessReps = []
        for j in range(repetitions):
            currFitness,_,_,_ = evalFun(evalConstParams, newNest)
            newEvals += 1
            fitnessReps.append(currFitness)
        newFitness = np.mean(fitnessReps)

        #Add new nest to lists
        returnNests.append(newNest)
        returnFitness.append(newFitness)
        returnReps.append(fitnessReps)

        #Check if global bests need to be changed
        if returnFitness[i] < newBestFitness:
            newBestFitness = returnFitness[i]
            newBestNest = returnNests[i]
            newBestReps = returnReps[i]

    return (newBestNest,newBestFitness,newBestReps,returnNests,returnFitness,returnReps,newEvals)
示例#3
0
def cuckoo_paper(constParams, tuneParams):
    evalFun = constParams[0]
    evalConstParams = constParams[1]
    paramRanges = constParams[2]
    stopCond = constParams[3]
    stopVal = constParams[4]
    repetitions = constParams[5]
    verbose = constParams[6]
    numNests = tuneParams[0]
    pa = tuneParams[1]

    numEvals = 0
    nests = [util.uniform_random(paramRanges) for i in range(numNests)]
    fitness = [sys.float_info.max for i in range(numNests)]
    fitnessReps = [[sys.float_info.max for j in range(repetitions)] for i in range(numNests)]
    bestNest, fmin, fminReps, nests, fitness, fitnessReps, newEvals = find_best_nests(evalFun, evalConstParams, nests, nests, fitness, fitnessReps, repetitions)
    numEvals += newEvals
    
    iterations = 0
    stopLoop = False
    while(not stopLoop):
        #Do single Levy flight
        newBestNest, fnew, fnewReps, nests, fitness, fitnessReps, newEvals = levy_flight_paper(nests, bestNest, paramRanges, fmin, fminReps, evalFun, evalConstParams, fitness, fitnessReps, repetitions)
        numEvals += newEvals

        #Determine new best solution
        if fnew < fmin:
            fmin = fnew
            bestNest = newBestNest
            fminReps = fnewReps

        #if repetitions > 1:
        #    print "**********"
        
        #Remove proportion pa of lowest quality nests
        newBestNest, fnew, fnewReps, nests, fitness, fitnessReps, newEvals = empty_nests_paper(nests, pa, bestNest, paramRanges, fmin, fminReps, evalFun, evalConstParams, fitness, fitnessReps, repetitions)
        numEvals += newEvals

        #if repetitions > 1:
        #    print "**********"
            
        #Determine new best solution
        if fnew < fmin:
            fmin = fnew
            bestNest = newBestNest
            fminReps = fnewReps
        
        iterations += 1

        if stopCond == "fitness":
            if fmin < stopVal[0]:
                stopLoop = True
        elif stopCond == "fitnessOrEvals":
            if fmin < stopVal[0] or numEvals > stopVal[1]:
                stopLoop = True
        elif stopCond == "fitnessOrGlobalEvals":
            if fmin < stopVal[0] or test_funs.numEvaluations > stopVal[1]:
                stopLoop = True
        elif stopCond == "generations":
            if iterations >= stopVal[0]:
                stopLoop = True 
        else:
            print "ERROR: Stop condition not recognized"
            break

        if verbose:
            print "Iteration: " + str(iterations)
            print "Evaluations: " + str(numEvals)
            print "Best Solution: " + str(bestNest)
            print "Best Mean Fitness: " + str(fmin)
            print "Best Fitness Set: " + str(fminReps) + "\n"

    return (numEvals, bestNest, fmin, fminReps)
示例#4
0
def uniform_random_search(constParams, tuneParams):
    evalFun = constParams[0]
    evalConstParams = constParams[1]
    paramRanges = constParams[2]
    stopCond = constParams[3]
    stopVal = constParams[4]
    repetitions = constParams[5]
    verbose = constParams[6]

    bestSol = None
    bestSolFitness = sys.float_info.max
    bestSolReps = None
    stopLoop = False
    numDim = len(paramRanges)

    numEvals = 0
    iterations = 0
    while (not stopLoop):
        newSol = util.uniform_random(paramRanges)

        #Determine fitness of new solution
        fitnessReps = []
        for j in range(repetitions):
            currFitness, _, _, _ = evalFun(evalConstParams, newSol)
            numEvals += 1
            fitnessReps.append(currFitness)
        newFitness = np.mean(fitnessReps)

        #Check if global bests need to be changed
        if newFitness < bestSolFitness:
            bestSolFitness = newFitness
            bestSol = newSol
            bestSolReps = fitnessReps

        iterations += 1

        if stopCond == "fitness":
            if bestSolFitness < stopVal[0]:
                stopLoop = True
        elif stopCond == "fitnessOrEvals":
            if bestSolFitness < stopVal[0] or numEvals > stopVal[1]:
                stopLoop = True
        elif stopCond == "fitnessOrGlobalEvals":
            if bestSolFitness < stopVal[
                    0] or test_funs.numEvaluations > stopVal[1]:
                stopLoop = True
        elif stopCond == "generations":
            if float(iterations) / 25.0 >= stopVal[0]:
                stopLoop = True
        else:
            print "ERROR: Stop condition not recognized"
            break

    if verbose:
        print "Iterations: " + str(iterations)
        print "Evaluations: " + str(numEvals)
        print "Best Solution: " + str(bestSol)
        print "Best Mean Fitness: " + str(bestSolFitness)
        print "Best Fitness Set: " + str(bestSolReps) + "\n"

    return (numEvals, bestSol, bestSolFitness, bestSolReps)
示例#5
0
def pso_basic(constParams, tuneParams):
    evalFun = constParams[0]
    evalConstParams = constParams[1]
    paramRanges = constParams[2]
    stopCond = constParams[3]
    stopVal = constParams[4]
    repetitions = constParams[5]
    verbose = constParams[6]
    numParticles = tuneParams[0]
    inertia = tuneParams[1]
    selfAdjust = tuneParams[2]
    socialAdjust = tuneParams[3]

    numDim = len(paramRanges)
    initPosns = [util.uniform_random(paramRanges) for i in range(numParticles)]
    paramWidths = [
        abs(paramRanges[i][-1] - paramRanges[i][0]) for i in range(numDim)
    ]
    velocityRanges = [[-paramWidths[i], paramWidths[i]] for i in range(numDim)]
    initVelocities = [
        util.uniform_random(velocityRanges) for i in range(numParticles)
    ]
    particles = [
        Particle(initPosns[i], initVelocities[i]) for i in range(numParticles)
    ]

    bestSol = None
    bestSolFitness = sys.float_info.max
    bestSolReps = None
    stopLoop = False
    numEvals = 0
    iterations = 0
    bestSol, bestSolFitness, bestSolReps, newEvals = evaluate_sols(
        evalFun, evalConstParams, particles, repetitions, bestSol,
        bestSolFitness, bestSolReps)
    numEvals += newEvals

    while (not stopLoop):

        for i in range(numParticles):
            update_particles(particles, bestSol, inertia, selfAdjust,
                             socialAdjust, paramRanges, velocityRanges)

        bestSol, bestSolFitness, bestSolReps, newEvals = evaluate_sols(
            evalFun, evalConstParams, particles, repetitions, bestSol,
            bestSolFitness, bestSolReps)
        numEvals += newEvals

        iterations += 1

        if stopCond == "fitness":
            if bestSolFitness < stopVal[0]:
                stopLoop = True
        elif stopCond == "fitnessOrEvals":
            if bestSolFitness < stopVal[0] or numEvals > stopVal[1]:
                stopLoop = True
        elif stopCond == "fitnessOrGlobalEvals":
            if bestSolFitness < stopVal[
                    0] or test_funs.numEvaluations > stopVal[1]:
                stopLoop = True
        elif stopCond == "generations":
            if float(iterations) / 25.0 >= stopVal[0]:
                stopLoop = True
        else:
            print "ERROR: Stop condition not recognized"
            break

        if verbose:
            print "Iteration: " + str(iterations)
            print "Evaluations: " + str(numEvals)
            print "Best Solution: " + str(bestSol)
            print "Best Mean Fitness: " + str(bestSolFitness)
            print "Best Fitness Set: " + str(bestSolReps) + "\n"

    return (numEvals, bestSol, bestSolFitness, bestSolReps)