Ejemplo n.º 1
0
print("xGausians", xGausians)
print("yGausians", yGausians)

# Evaluate
print("Evaluate Fuzzy3D")
print(
    "x=0, y=0",
    helper.evaluateFuzzy3D(0, 0, pValues, qValues, rValues, xGausians,
                           yGausians))
print(
    "x=2, y=2",
    helper.evaluateFuzzy3D(2, 2, pValues, qValues, rValues, xGausians,
                           yGausians))
print(
    "x=4, y=4",
    helper.evaluateFuzzy3D(4, 4, pValues, qValues, rValues, xGausians,
                           yGausians))

# Funcion de aptitud
print("Evaluate aptitud")
zMatrix = helper.aptitudFunction(population[1], x, y)
for i in range(len(zMatrix)):
    print(zMatrix[i])
zMatrix = helper.aptitudFunction(population[0], x, y)
for i in range(len(zMatrix)):
    print(zMatrix[i])

# Test calculate results
print("Calculate results")
results = helper.calculateResults(x, y, population[0:1], zResult, 1)
print(results)
Ejemplo n.º 2
0
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])

acumError = results[minInd]
# 3.- Iterate
for i in range(iterations):
    chosenParents = helper.getChosenParents(population, results,
Ejemplo n.º 3
0
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)
Ejemplo n.º 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")
Ejemplo n.º 5
0
from helper import helper
from constants import ecuaParms
import matplotlib.pyplot as plt

# ----- START OF EXECUTUION

# 1.- Create random population 100 chromosomes with 3 genes each
population = helper.createPopulation(ecuaParms.minR, ecuaParms.maxR,
                                     ecuaParms.chromLength,
                                     ecuaParms.popuLength)
results = helper.calculateResults(population, ecuaParms.divisor, ecuaParms.x,
                                  ecuaParms.y)

#print("-------INITIAL STATE------")

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

for i in range(ecuaParms.iterations):

    # 2.- Compete, get chosen parents
    chosenParents = helper.getChosenParents(population, results,
                                            ecuaParms.percent,
                                            ecuaParms.parentNum)

    # 3.- Reproduction, replace population with children
    population = helper.bitExchangeReproduction(population, chosenParents,
                                                ecuaParms.parentNum,
                                                ecuaParms.bits)
    results = helper.calculateResults(population, ecuaParms.divisor,
Ejemplo n.º 6
0
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)

# Test 'intToBitArray' and 'bitArrayToInt'
print("Testing intToBitArray and bitArrayToInt")
bit_1 = helper.intToBitArray(89, 8)
bit_2 = helper.intToBitArray(7, 8)
print("bit:", bit_1, "number:", helper.bitArrayToInt(bit_1))
print("bit:", bit_2, "number:", helper.bitArrayToInt(bit_2))

# Test 'flipBits'
print("Testing flipBits")
parent_1 = [100, 250, 89, 7, 10, 8]