[Python] How to run GA in pygad package?

Discussão em 'Python' iniciado por Stack, Agosto 5, 2025.

  1. Stack

    Stack Membro Participativo

    I am running pygad.GA and getting an error:

    1337 self.logger.exception(e)
    1338 # sys.exit(-1)
    -> 1339 raise e

    File c:\Users\...:431, in GA.__init__(self, num_generations, num_parents_mating, fitness_func, fitness_batch_size, initial_population, sol_per_pop, num_genes, init_range_low, init_range_high, gene_type, parent_selection_type, keep_parents, keep_elitism, K_tournament, crossover_type, crossover_probability, mutation_type, mutation_probability, mutation_by_replacement, mutation_percent_genes, mutation_num_genes, random_mutation_min_val, random_mutation_max_val, gene_space, gene_constraint, sample_size, allow_duplicate_genes, on_start, on_fitness, on_parents, on_crossover, on_mutation, on_generation, on_stop, save_best_solutions, save_solutions, suppress_warnings, stop_criteria, parallel_processing, random_seed, logger)
    429 # Number of solutions in the population.
    430 self.sol_per_pop = sol_per_pop
    ...
    --> 369 if not seq:
    370 raise IndexError('Cannot choose from an empty sequence')
    371 return seq[self._randbelow(len(seq))]

    ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()


    The problem is in gene_space. When I comment it out, the GA works.

    [[0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2], [0, 1, 2]]


    What is unusual, my friend can run it with the same script and output on his side.

    Continue reading...

Compartilhe esta Página