genetic/genetic_algo.py
def generate_children(parent, data, take_best,
                      population_size = 20,
                      mutation_rate = 0.05):
    """
    produce children with mutations

    parent: PopulationPool class
    mutation_rate: float probability that value will be mutated
    crossover_rate: float probability that genes will cross with another gene
    """
    child_genes = []

    best_parent_genes = parent.genes[:take_best]
    while len(child_genes) < population_size:
        # randomly pick a parent
        parentA_gene = random.choice(best_parent_genes)

        # randomly pick another parent
        parentB_gene = random.choice(best_parent_genes)

        # crossover the gene at a random point
        rand_point = random.randint(0, parentA_gene.shape[0])

        if random.random() <= mutation_rate:
            # Crossover
            if parentB_gene.shape[0] < rand_point:
                child_gene = np.vstack((parentA_gene[0:rand_point],
                                        parentB_gene[rand_point, parentB_gene.shape[0]]))

            else :
                child_gene = parentA_gene

            # randomly change values in the array
            mask = np.random.randint(0,2,size=child_gene.shape).astype(np.bool)
            r = np.random.uniform(-np.pi, np.pi, size = child_gene.shape)
            child_gene[mask] = r[mask]

        else:
            child_gene = parentA_gene

        child_genes.append(child_gene)

    fitness, acc = get_fitness(child_genes, data)

    return PopulationPool(child_genes, fitness, acc)

    produce children with mutations

    parent: PopulationPool class
    mutation_rate: float probability that value will be mutated
    crossover_rate: float probability that genes will cross with another gene
    
produce children with mutations
parent: PopulationPool class
mutation_rate: float probability that value will be mutated
crossover_rate: float probability that genes will cross with another gene
produce children with mutations parent : PopulationPool class mutation_rate : float probability that value will be mutated crossover_rate : float probability that genes will cross with another gene
def generate_children(parent, data, take_best,
                      population_size = 20,
                      mutation_rate = 0.05):
    child_genes = []
    best_parent_genes = parent.genes[:take_best]
    while len(child_genes) < population_size:
        parentA_gene = random.choice(best_parent_genes)
        parentB_gene = random.choice(best_parent_genes)
        rand_point = random.randint(0, parentA_gene.shape[0])
        if random.random() <= mutation_rate:
            if parentB_gene.shape[0] < rand_point:
                child_gene = np.vstack((parentA_gene[0:rand_point],
                                        parentB_gene[rand_point, parentB_gene.shape[0]]))
            else :
                child_gene = parentA_gene
            mask = np.random.randint(0,2,size=child_gene.shape).astype(np.bool)
            r = np.random.uniform(-np.pi, np.pi, size = child_gene.shape)
            child_gene[mask] = r[mask]
        else:
            child_gene = parentA_gene
        child_genes.append(child_gene)
    fitness, acc = get_fitness(child_genes, data)
    return PopulationPool(child_genes, fitness, acc)
def generate_children ( parent , data , take_best , population_size = 20 , mutation_rate = 0.05 ) : child_genes = [ ] best_parent_genes = parent . genes [ : take_best ] while len ( child_genes ) < population_size : parentA_gene = random . choice ( best_parent_genes ) parentB_gene = random . choice ( best_parent_genes ) rand_point = random . randint ( 0 , parentA_gene . shape [ 0 ] ) if random . random ( ) <= mutation_rate : if parentB_gene . shape [ 0 ] < rand_point : child_gene = np . vstack ( ( parentA_gene [ 0 : rand_point ] , parentB_gene [ rand_point , parentB_gene . shape [ 0 ] ] ) ) else : child_gene = parentA_gene mask = np . random . randint ( 0 , 2 , size = child_gene . shape ) . astype ( np . bool ) r = np . random . uniform ( - np . pi , np . pi , size = child_gene . shape ) child_gene [ mask ] = r [ mask ] else : child_gene = parentA_gene child_genes . append ( child_gene ) fitness , acc = get_fitness ( child_genes , data ) return PopulationPool ( child_genes , fitness , acc )
produce children with mutations
parent: PopulationPool class
mutation_rate: float probability that value will be mutated
crossover_rate: float probability that genes will cross with another gene
produce children with mutations parent : PopulationPool class mutation_rate : float probability that value will be mutated crossover_rate : float probability that genes will cross with another gene
"""
    produce children with mutations

    parent: PopulationPool class
    mutation_rate: float probability that value will be mutated
    crossover_rate: float probability that genes will cross with another gene
    """ # randomly pick a parent # randomly pick another parent # crossover the gene at a random point # Crossover # randomly change values in the array
parent
data
take_best
population_size
mutation_rate