示例#1
0
# 1.- Create random population 100 chromosomes with 20 genes each
population = helper.createPopulation(list(range(1, 21)),
                                     travelParms.population)
distances = helper.calculateDistancesInPopulation(population)

#print("-------INITIAL STATE------")
#helper.printPopulation(population, distances)

# 1.a- Call graphs and graph the best
# First generation
helper.createGraphs(population, distances)

for i in range(travelParms.iterations):

    # 2.- Compete, get chosen parents
    chosenParents = helper.getChosenParents(population, distances)

    # 3.- Reproduction, replace population with children
    population = helper.getNewPopulation(population, chosenParents)
    distances = helper.calculateDistancesInPopulation(population)

    # 3.a- Get the best and graph
    helper.updateGraphs(i + 1, population, distances)

# 4.- Print and graph the best of all times
helper.printBest()

plt.show()

#print("-------FINAL STATE------")
#helper.printPopulation(population, distances)
示例#2
0
    ecuaParms.maxR, ecuaParms.chromLength, ecuaParms.popuLength)

matrix = helper.calculateMatrixResults(ecuaParms.result)

results = helper.calculateResults(population, matrix, 
    ecuaParms.divisor)

# 2.- Create graph
helper.createGraphs(population, results, matrix, ecuaParms)

minError = min(results)
mutationP = ecuaParms.mutationPercent

# 3.- Iterate
for i in range(ecuaParms.iterations):
    chosenParents = helper.getChosenParents(population, results, 
        ecuaParms.percent, ecuaParms.parentNum)

    population = helper.bitExchangeReproduction(population, 
        chosenParents, ecuaParms.parentNum, ecuaParms.bits)
    
    helper.mutation(population, mutationP, ecuaParms.bits)

    results = helper.calculateResults(population, matrix, 
        ecuaParms.divisor)
    
    newMinError = min(results)
    if(minError == newMinError):
        #mutationP = 20
        helper.addRandomnes_1(population, ecuaParms)        
        #results = helper.calculateResults(population, matrix, ecuaParms.divisor)
示例#3
0
results = helper.calculateResults(x, y, population, zResult, 51)
minInd = results.index(min(results))

# Get best matrix to graph
bestChrom = helper.divideByFactor(population[minInd], 51)
zBestChrom = helper.aptitudFunction(bestChrom, x, y)

# 3D printing
#print(zBestChrom)
fig = plt.figure(figsize=plt.figaspect(0.5))
ax = helper.graph3D(fig, x, y, zResult, zBestChrom, results[minInd])

acumError = results[minInd]
# 3.- Iterate
for i in range(iterations):
    chosenParents = helper.getChosenParents(population, results,
                                            tournamentPercent, 2)

    population = helper.bitExchangeReproduction(population, chosenParents, 2,
                                                8)

    helper.mutation(population, mutationPercent, 8)

    results = helper.calculateResults(x, y, population, zResult, 51)
    minInd = results.index(min(results))

    # Get best matrix to graph
    bestChrom = helper.divideByFactor(population[minInd], 51)
    zBestChrom = helper.aptitudFunction(bestChrom, x, y)
    helper.updateGraph3D(ax, x, y, zBestChrom, results[minInd])
    print("Iteration:", i, "Error:", results[minInd])
    #print(bestChrom)
示例#4
0
from helper import helper
from constants import ecuaParms

# Test 'createPopulation'
print("Testing createPopulation")
population = helper.createPopulation(0, 255, 3, 10)
results = helper.calculateResults(population, 10, 2, 13)
print(len(population), len(results))
helper.printPopulation(population, results)

# Test 'getChosenParents'
print("Testing getChosenParents")
chosenParents = helper.getChosenParents(population, results, 5, 2)
for i in range(len(chosenParents)):
    print(i, chosenParents[i], results[chosenParents[i]])

# Test 'intToBitArray'
print("Testing intToBitArray")
bitArray_1 = helper.intToBitArray(3, 8)
bitArray_2 = helper.intToBitArray(15, 8)
bitArray_3 = helper.intToBitArray(8, 8)
bitArray_4 = helper.intToBitArray(255, 8)
bitArray_5 = helper.intToBitArray(0, 8)
print("3", bitArray_1)
print("15", bitArray_2)
print("8", bitArray_3)
print("255", bitArray_4)
print("0", bitArray_5)

# Test 'bitArrayToInt'
print("Testing bitArrayToInt")