Exemple #1
0
def colSelection(col_list, c0, c1):
    nCols = len(col_list)
    fitness_list = []
    for colony in col_list:
        fitness_list.append(fitness(colony, c0, c1))

    totFit = sum(fitness_list)
    # This array holds votes for each colony in col_list
    colonyVotes = np.zeros(nCols)
    # Iterate over the total number of new colonies to be created
    for j in range(nCols):
        # Assign random number over size of total fitness
        temp1 = random.uniform(0, totFit)
        # Initialize an accumulator
        tot = 0
        # Get cumulative sum of fitnesses to decide on colony
        for i in range(nCols):
            # Update cumulative sum
            tot = tot + fitness_list[i]
            # If less than the random number, increase vote
            if temp1<tot:
                colonyVotes[i] = colonyVotes[i] + 1
                break

    return colonyVotes        
Exemple #2
0
# Initialize a list of ant colonies
colony_list = colCreate(colCreateParms)

for i in range(evo_cycles):
    # create a colony index
    j = 0
    # Initialize minimum to a high number
    tempMin = 1e5
    # Cycle each colony through TSP 'iters' times
    for colony in colony_list:
        # Submit to TSP cycling
        colony = tsp(colony, iters, n_mat)
        # Display update and store fitness of current colony/evo
        print("evo= " + str(i) + " , colony= "+str(j))
        fitness_mat[i,j]=fitness(colony, c0, c1)
        # Get colony minimum for this evo and store it
        iterMin = colony.minL
        lMin_mat[i,j]=iterMin
        lAvg_mat[i,j]=colony.L/colony.N
        # Check if it's best so far from all colonies
        # If so, store the index of the colony
        if iterMin<tempMin:
            tempMin = iterMin
            bestIdx = j
        # Increment colony index    
        j = j+1

    # Store the alpha dist. for the best L_min
    alphaBestMin.append(colony_list[bestIdx].getAlphaDist())