Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Self treated like a normal argument

So, I have the following class:

class GeneticAlgorithm():
    def breed_population(self, agents, fitness=0.10, crossover_rate=0.50, mutation_rate=0.10, mutation_degree=0.10, mutate=True):
        '''
        Crossover the weights and biases of the fittest members of the population,
        then randomly mutate weights and biases.
        '''
        # Sort by highest to lowest score
        agents.sort(key=operator.attrgetter("top_score"))

        # Get the number of breeding agents
        pop_size = len(agents)
        cutoff = (int)(fitness * pop_size)
        if cutoff < 2: return agents

        # Get number of times each parent pair needs to breed
        num_children = pop_size // cutoff

        # Initialize children
        children = [Agent() for i in range(pop_size)]

        # Breed population
        for i in range(0, cutoff, 2):
            for c in range(num_children):
                children[i*c] = self.crossover(children[i*c], agents[i], agents[i+1], crossover_rate, mutation_rate, mutation_degree, mutate)

        return children


    def crossover(self, child, parent_one, parent_two, crossover_rate, mutation_rate, mutation_degree, mutate):
        ''' Apply crossover and mutation between two parents in order to get a child. '''
        # Crossover and mutate each layer
        for i in range(len(parent_one.layers)):
            # Get weights
            p1_weights = parent_one.layers[i].get_weights()[0]
            p2_weights = parent_two.layers[i].get_weights()[0]
            
            # Cycle through layer's weights
            for x, row in enumerate(p1_weights):
                for y, _ in enumerate(row):
                    # Apply crossover
                    if (random.random() < crossover_rate):
                        p1_weights[x][y] = p2_weights[x][y]
                    
                    # Apply mutation
                    if mutate:
                        if (random.random() < mutation_rate):
                            if (random.random() > 0.50):
                                p1_weights[x][y] += p1_weights[x][y] * mutation_degree
                            else:
                                p1_weights[x][y] -= p1_weights[x][y] * mutation_degree
            
            # Set weights in child
            child.layers[i].set_weights(p1_weights)
        return child

When I try to run it, I get this error message:

agents = GeneticAlgorithm.breed_population(agents)
TypeError: breed_population() missing 1 required positional argument: 'agents'

Meaning that, for some reason, self is being treated like an actual argument instead of just giving the class access to all functions within the class.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

How do I fix this?

>Solution :

The problem you are having is that you trying to call the method on the class without creating an object.

agents = GeneticAlgorithm.breed_population(agents)

Should be:

ga_obj = GeneticAlgorithm()
agents = ga_obj.breed_population(agents)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading