예제 #1
0
def test_pairwise():
    list = [1, 2, 3, 4]

    pairs = general.pairwise(list)

    for p in pairs:
        pass

    assert type(pairs).__name__ == 'callable_iterator' or 'generator'
예제 #2
0
파일: ga.py 프로젝트: 321HG/opytimizer
    def _update(self, agents, function):
        """Method that wraps selection, crossover and mutation over all agents and variables.

        Args:
            agents (list): List of agents.
            function (Function): A Function object that will be used as the objective function.

        """

        # Creating a list to hold the new population
        new_agents = []

        # Retrieving the number of agents
        n_agents = len(agents)

        # Calculates a list of fitness from every agent
        fitness = [agent.fit + c.EPSILON for agent in agents]

        # Selects the parents
        selected = self._roulette_selection(n_agents, fitness)

        # For every pair of selected parents
        for s in g.pairwise(selected):
            # Performs the crossover
            alpha, beta = self._crossover(agents[s[0]], agents[s[1]])

            # Performs the mutation
            alpha, beta = self._mutation(alpha, beta)

            # Checking `alpha` limits
            alpha.clip_limits()

            # Checking `beta` limits
            beta.clip_limits()

            # Calculates new fitness for `alpha`
            alpha.fit = function(alpha.position)

            # Calculates new fitness for `beta`
            beta.fit = function(beta.position)

            # Appends the mutated agents to the children
            new_agents.extend([alpha, beta])

        # Joins both populations
        agents += new_agents

        # Sorting agents
        agents.sort(key=lambda x: x.fit)

        return agents[:n_agents]
예제 #3
0
    def _crossover(self, space):
        """Crossover a number of individuals pre-selected through a tournament procedure.

        Args:
            space (TreeSpace): A TreeSpace object.
            agents (list): Current iteration agents.
            trees (list): Current iteration trees.

        """

        # Calculates a list of current trees' fitness
        fitness = [agent.fit for agent in space.agents]

        # Number of individuals to be crossovered
        n_individuals = int(space.n_trees * self.p_crossover)

        # Checks if `n_individuals` is an odd number
        if n_individuals % 2 != 0:
            # If it is, increase it by one
            n_individuals += 1

        # Gathers a list of selected individuals to be replaced
        selected = g.tournament_selection(fitness, n_individuals)

        # For every pair in selected individuals
        for s in g.pairwise(selected):
            # Calculates the amount of father nodes
            father_nodes = space.trees[s[0]].n_nodes

            # Calculate the amount of mother nodes
            mother_nodes = space.trees[s[1]].n_nodes

            # Checks if both trees have more than one node
            if (father_nodes > 1) and (mother_nodes > 1):
                # Prunning father nodes
                max_f_nodes = self._prune_nodes(father_nodes)

                # Prunning mother nodes
                max_m_nodes = self._prune_nodes(mother_nodes)

                # Apply the crossover operation
                space.trees[s[0]], space.trees[s[1]] = self._cross(
                    space.trees[s[0]], space.trees[s[1]], max_f_nodes,
                    max_m_nodes)
예제 #4
0
import opytimizer.math.general as g

# Creating a list for pairwising
individuals = [1, 2, 3, 4]

# Creating pairwise from list
for pair in g.pairwise(individuals):
    # Outputting pairs
    print(f'Pair: {pair}')

# Performing a tournmanet selection over list
selected = g.tournament_selection(individuals, 2)

# Outputting selected individuals
print(f'Selected: {selected}')