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) minError = newMinError helper.updateGraphs(i + 1, population, results, matrix, ecuaParms)
# 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) if (acumError == results[minInd]): addChrom = helper.createPopulation(39, 5)
child_3 = helper.flipBits(parent_1, parent_2, 48, 8) print("position:", 48, child_3) child_4 = helper.flipBits(parent_1, parent_2, 20, 8) print("position:", 20, child_4) # Test 'getBestInTournament' print("Testing getBestInTournament") testErrors = [12, 23, 24, 54, 9, 2, 43, 31, 59, 100, 121, 38] print("errors:", testErrors) print("5%:", helper.getBestInTournament(testErrors, 5)) print("3%:", helper.getBestInTournament(testErrors, 3)) print("2%:", helper.getBestInTournament(testErrors, 2)) print("1%:", helper.getBestInTournament(testErrors, 1)) # Test 'bitExchangeReproduction' print("Testing bitExchangeReproduction") results = helper.calculateResults(population, matrix, 3) helper.printPopulation(population, results) chosen = helper.getChosenParents(population, results, 5, 2) print("chosen parents:", chosen) newPopulation = helper.bitExchangeReproduction(population, chosen, 2, 8) newResults = helper.calculateResults(newPopulation, matrix, 3) helper.printPopulation(newPopulation, newResults) # Test 'mutation' print("Testing mutation") mutationPopulation = [[1, 2, 3, 4], [32, 45, 65, 76], [109, 209, 250, 199]] print(mutationPopulation) helper.mutation(mutationPopulation, 2, 8) print(mutationPopulation)