Пример #1
0
def reproducePopulation(population, numberOfReproductions, mutationProbability,
                        gammaK, gammaTheta):
    populationSize = len(population)

    while numberOfReproductions > 0:
        # escolher pais utilizando distribuicao gamma
        fatherIndex = int(gammavariate(gammaK, gammaTheta)) % populationSize
        motherIndex = fatherIndex
        while motherIndex == fatherIndex:
            motherIndex = int(gammavariate(gammaK,
                                           gammaTheta)) % populationSize
        father = population[fatherIndex]
        mother = population[fatherIndex]

        # determinar qual o pai mais forte
        if father.value < mother.value:
            stronger = father
            weaker = mother
        else:
            stronger = mother
            weaker = father

        # quanto mais forte o pai mais cromossomos passa, pais fracos (valor > 8) passam em media metade dos cromossomos
        separationRandomization = gammavariate(stronger.value % 8 + 1, 1 / 2)
        separationIndex = int(round(separationRandomization)) % 4 + 1

        # chance de swap de maneira que ambos extremos dos cromossomos do mais forte possam ser passados
        if random() < 0.5:
            stronger, weaker = weaker, stronger
            separationIndex = 8 - separationIndex

        # gera filho
        child = State()
        i = 0
        while i < separationIndex:
            child.queen[i] = stronger.queen[i]
            i += 1
        while i < 8:
            child.queen[i] = weaker.queen[i]
            i += 1
        # mutacao
        if random() < mutationProbability:
            child.queen[randrange(8)] = randrange(8)

        child.evaluate()

        # filho eh inserido na populacao no lugar de alguem escolhido de acordo com uma distribuicao gamma
        replaceIndex = populationSize - int(gammavariate(
            gammaK, gammaTheta)) % populationSize - 1
        #print(populationSize, replaceIndex)	#debug
        population.pop(replaceIndex)
        population.insert(0, child)

        numberOfReproductions -= 1
Пример #2
0
# 		Recommended values: <initial temperature> = 10-1000, <cooling rate> = 0.005-0.01
#	python3 main.py GENETIC_ALGORITHM <population size> <reproduction rate> <mutation probability> <gamma k> <gamma theta>
#		Recommended values: <population size> = 1000, <reproduction rate> = 0.5, <mutation probability> = 0.15, <gamma k> = 2, <gamma theta> = <population size>/(4 x <gamma k>)
#		The values of k and theta determine who will be selected for reproduction and substitution. The recomended values will make so that, in general, the 25% best will reproduce and the 25% worst will be replaced (Average = k x theta). Smaller values of k increase the elitism, while smaller values get the distribution closer to a normal distribution
#
#
# The file contains 8 integers where each represent the colum the queen in that line is
#
# In case it's not possible to find a solution with the given initial state, the state is shuffled and the choses algorithm runs again using the shuffled state.
	
solution = State()
tries = 0
	
if sys.argv[2] == 'HILL_CLIMBING':
	solution.readFromFile( open(sys.argv[1], 'r') )
	solution.evaluate()
	sidewayLimit = int(sys.argv[3])
	
	while solution.value > 0:
		tries += 1
		solution = hillClimb(solution, sidewayLimit)
		
		if solution.value > 0:
			solution.randomize()
			solution.evaluate()
		
elif sys.argv[2] == 'SIMULATED_ANNEALING':
	solution.readFromFile( open(sys.argv[1], 'r') )
	solution.evaluate()
	temperature = float(sys.argv[3])
	coolingRate = float(sys.argv[4])