Created at 9am, Apr 19
HangytongArtificial Intelligence
0
Evolving Solutions With Genetic Algorithms
pgtXN60TNlFaRRzKuvcJ2xCb0fmUHyYhEMwI3_9FE6c
File Type
PDF
Entry Count
30
Embed. Model
jina_embeddings_v2_base_en
Index Type
hnsw

a detailed explanation of what genetic algorithm is. this article explains the details of each component of the genetic algorithm and follows along with the code.

best_fitness = sorted_fitnesses[] amount_new = int((1 self.elitism) * len(self.population) ) new_population = [] for _ in range(amount_new): i = np.random.choice(sorted_population.shape[], p=fitnesses_weighting) il = np.random.choice(sorted_population.shape[], p=fitnesses_weighting) new_dna = self.__crossover(self.population[i], self.population[i1]) new_dna = self.__mutate(new_dna, self.mutation_sigma, self.mutation_rate) new_population. append(new_dna) amount_old = population_size amount_new new_population = np.array(new_population + sorted_population[:amount_old].tolist()) assert new_population.shape == self.population.shape self.population = np.clip(new_population, self.dna_bounds[], self.dna_bounds) get_best(self): return self.best_dna, self.best_fitness __create_random_population(self, dna_size, dna_sigma, dna_start_position, population_size): population = np.random.standard_normal((population_size, dna_size)) * dna_sigma
id: c17d6bfffce96fdca93c23f86f8ee85b - page: 10
mutate_fn is not None: 93 return self.mutate_fn(dna) 94 95 if np.random.random_sample() < mutation_rate: 96 dna += np.random.standard_normal(size=dna.shape) * mutation_sigma 97 98 return dna 99 100 101 def __crossover(self, dnal, dna2): 102 103 assert len(dna1) == len(dna2) 104 105 if self.crossover_fn is not None: 106 return self.crossover_fn(dna1, dna2) 107 108 new_dna = np.copy(dna1) 109 indices = np.where(np.random.randint(2, size=new_dna.size)) 110 new_dna[indices] = dna2[indices]
id: 2ab14ac4ef037952cd42220992d5de56 - page: 10
111 return new_dna GeneticAlgorithm.py hosted with by GitHub view raw Using it is very straightforward. The optimisation process is two fold, we firstly get the dna solutions via the .get_solutions() method, we evaluate the genepool with some external method related to the problem we are trying to solve, and then we supply the fitness list to the class so it knows which chromosomes are worth keeping and which are not. 11/23 4/19/24, 5:27 PM Evolving Solutions With Genetic Algorithms | by Leon Fedden | Medium
id: ca392bd9aecc21d24efdb9efc1c02cee - page: 11
1 = # Construct the class 2 ga = GA(dna_size=2, # Each dna will have size elements 3 dna_bounds=(-5.11, 5.11), # DNA cannot optimise outside of these bounds 4 dna_start_position=[3.1, 3.1], # DNA begins randomly around this position 5 elitism=0.5, # We keep the 50% fittest individuals 6 population_size=500, # We have 5 genes in the gene pool 7 mutation_rate=0.01, # Roguhly every 100 dice rolls we mutate 8 mutation_sigma=0.1, # The gene will mutated at a bound of (-0.1, .1) 9 mutation_decay=0.999, # The mutation size will decay each step 10 mutation_limit=e.1) # The mutation size will never get smaller than this 11 12 # Loop and optimise each step 13. for optimisation_step in range(amount_optimisation_steps): 14 15 # Get the current gene pool
id: 0d1e0f7e07a8c5ef746aa37337700e80 - page: 12
How to Retrieve?
# Search

curl -X POST "https://search.dria.co/hnsw/search" \
-H "x-api-key: <YOUR_API_KEY>" \
-H "Content-Type: application/json" \
-d '{"rerank": true, "top_n": 10, "contract_id": "pgtXN60TNlFaRRzKuvcJ2xCb0fmUHyYhEMwI3_9FE6c", "query": "What is alexanDRIA library?"}'
        
# Query

curl -X POST "https://search.dria.co/hnsw/query" \
-H "x-api-key: <YOUR_API_KEY>" \
-H "Content-Type: application/json" \
-d '{"vector": [0.123, 0.5236], "top_n": 10, "contract_id": "pgtXN60TNlFaRRzKuvcJ2xCb0fmUHyYhEMwI3_9FE6c", "level": 2}'