from helper import helper from constants import travelParms import matplotlib.pyplot as plt # ----- START OF EXECUTUION # 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
import random from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter zResult = [[4, 2, 0, 0, 0], [5, 6, 5, 2, 0], [6, 6, 5, 5, 3], [5, 6, 6, 6, 4], [4, 7, 6, 5, 5]] x, y = [0, 1, 2, 3, 4], [0, 1, 2, 3, 4] mutationPercent, iterations, tournamentPercent = 7, 500, 3 # Positions in genetic algorithm # [0-5 xyMean, 6-11 xyVariance, 12-20 pValues, 21-29 qValues, 30-38 rValues] # Values between 0 and 5 so if 8 bit = 255 then 255/51 = max 5 population = helper.createPopulation(39, 100) #helper.printPopulation(popoulation) # Evaluate aptitud functions and get min error 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])
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")
from helper import helper from constants import ecuaParms import matplotlib.pyplot as plt # ----- START OF EXECUTUION # 1.- Create random population 100 chromosomes with 6 genes each population = helper.createPopulation(ecuaParms.minR, 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)
# Test 'aptitudFunction' print("Testing aptitudFunction") y = helper.aptitudFunction(ecuaParms.result, 2.9) print("Ecuation:", ecuaParms.result, "x:", 2.9, "y:", y) # Test 'divideByFactor' print("Testing divideByFactor") chrom = [3, 30, 56, 70, 250] divChrom = helper.divideByFactor(chrom, 3) print(chrom) print(divChrom) # Test 'createPopulation' print("Testing createPopulation") population = helper.createPopulation(1, 255, 6, 10) print(population) # Test 'calculateMatrixResults' print("Testing calculateMatrixResults") matrix = helper.calculateMatrixResults(ecuaParms.result) print(matrix[0], matrix[1], len(matrix)) # Test 'calculateResults' print("Testing calculateResults") testPopulation = [[10, 25, 3, 70, 10, 6]] results = helper.calculateResults(testPopulation, matrix, 3) helper.printPopulation(testPopulation, results) testPopulation = [[10, 25, 3, 70, 10, 8]] results = helper.calculateResults(testPopulation, matrix, 3) helper.printPopulation(testPopulation, results)