PV

Documentacao RCE Evolution

cofre-2024 • 05/06/2024

newCriterio Function:

The newCriterio function implements a two-step approach for selecting elite individuals:

· 

Step 1: Identify the Best Fitness and Maximum Difference:

· 

a. Find the best individual (HOF) from the current population. b. Calculate the maximum difference allowed for fitness values, based on the porcentagem parameter.

· 

· 

Step 2: Select Elite Individuals Based on Fitness and Diversity:

· 

a. Iterate through the population: i. Check if the individual’s fitness is within the maximum difference range. ii. If so, calculate the difference between its decision variables and those of the previously selected elite individuals. iii. If the difference exceeds the delta threshold, consider it an elite individual. iv. Add the elite individual to the pop_RCE list and the CONJUNTO_ELITE_RCE set.

· 

2. Applying RCE Periodically:

· The run method now includes a check for RCE application based on the num_repopulation parameter and the current generation.

· If RCE is enabled: a. Call the aplicar_RCE function to generate a new population using the newCriterio function for elite selection. b. Replace the current population with the modified population generated by RCE.

3. Additional Modifications:

· The checkDecisionVariablesAndFitnessFunction method now accepts decision_variables and fitness_function arguments.

· The elitismoSimples function updates the HOF and clones the best individual to the top of the population.

· The criterio2 function compares decision variables and selects elite individuals based on the delta threshold.

· The avaliarFitnessIndividuos function evaluates the fitness of all individuals in the population.

· The calculateFitnessGeneration function calculates the fitness for the new population.

· The visualizarPopAtual function updates the allIndividualValuesArray with data from the current population.

· The cout function adds a decorative border to printed messages.

Key Improvements:

· The newCriterio function provides a more flexible and efficient approach for selecting elite individuals, considering both fitness and diversity.

· Applying RCE periodically allows for the introduction of new genetic material and exploration of diverse regions of the search space.

· The updated code includes additional helper functions and improved data handling.

Código Python para Otimização com Algoritmo Evolutivo e RCE

Este código implementa um algoritmo evolutivo com Estratégia de Reposição por Diversidade de Cadeia (RCE) para otimização de funções.

Bibliotecas utilizadas:

· numpy

· math

· deap

· random

· matplotlib.pyplot

· time

· scipy.optimize

· json

· pandas

Classe AlgoritimoEvolutivoRCE:

· Inicializa o algoritmo com parâmetros de configuração (setup).

· Define estatísticas de acompanhamento (stats).

· Armazena o logbook (logbook) para registrar o histórico de gerações.

· Cria a população inicial (POPULATION) e a população de otimização (opcional: POP_OPTIMIZATION).

· Armazena dados do RCE (pop_RCE, best_solutions_array, best_individual_array, allIndividualValuesArray, data, repopulation_counter, allFitnessValues, CONJUNTO_ELITE_RCE).

· Define métodos para:

o Registrar estatísticas e melhores soluções (registrarDados).

o Verificar clones na população (checkClonesInPop).

o Gerar informações de indivíduos (generateInfoIndividual).

o Exibir dataframe com informações dos indivíduos (show_ind_df).

o Aplicar os critérios RCE para selecionar indivíduos (criterio1, criterio2_alternative, newCriterio).

o Realizar o RCE para gerar uma nova população (aplicar_RCE).

o Implementar elitismo simples (elitismoSimples).

o Avaliar o fitness dos indivíduos (avaliarFitnessIndividuos).

o Calcular o fitness da geração (calculateFitnessGeneration).

o Verificar variáveis de decisão e função fitness (checkDecisionVariablesAndFitnessFunction).

o Executar o algoritmo principal (run).

o Visualizar a população atual (visualizarPopAtual).

o Imprimir mensagens (cout).

Função load_params:

· Carrega parâmetros de configuração de um arquivo JSON.

Como utilizar o código:

1. Configure os parâmetros de otimização no objeto setup.

2. Defina as variáveis de decisão e a função fitness (opcionalmente, use set_decision_variables_and_fitness_function).

3. Execute o algoritmo (algoritmo.run()).

4. O algoritmo retornará a população final (pop), o logbook (logbook) e o melhor indivíduo (hof[0]).