PyGAD
bug
documentation
Python
parameter handling

PyGAD is not receiving integer parameters according to documentation

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

PyGAD does support integer genes, but there is an easy mistake hidden in the wording. Some parameters are ordinary Python configuration values such as num_generations, while others control the data type of the genes themselves. If only one of those layers is configured as integer-friendly, the population can still behave like a float-based search space.

Separate "Integer Parameters" from "Integer Genes"

There are two different questions:

  • Are PyGAD configuration arguments like num_generations and num_genes Python integers
  • Are the solution genes themselves restricted to integer values

The first case is simple: those arguments should just be normal int values in Python.

The second case is where confusion usually starts. According to the PyGAD documentation, gene_type=int is supported, but that does not magically fix every source of floating-point values in your search space. You also need to make your initialization and mutation choices consistent with integer genes.

A Minimal Integer-Only Example

This example keeps the genes integer-valued by combining gene_type=int with an integer-valued gene_space:

python
1import pygad
2
3
4def fitness_func(ga_instance, solution, solution_idx):
5    return sum(solution)
6
7
8ga = pygad.GA(
9    num_generations=20,
10    sol_per_pop=8,
11    num_genes=4,
12    num_parents_mating=4,
13    fitness_func=fitness_func,
14    gene_type=int,
15    gene_space=[0, 1, 2, 3, 4, 5],
16    mutation_percent_genes=25,
17)
18
19ga.run()
20
21solution, fitness, solution_idx = ga.best_solution()
22print("best solution:", solution)
23print("python types:", [type(value).__name__ for value in solution])

This is the most reliable pattern when you truly need discrete integer genes.

Why Integer Intent Still Turns into Floats

Problems usually appear when users mix integer typing with continuous ranges. For example, if your gene space conceptually means "any value from zero to ten," there is a big difference between:

  • an explicit integer set such as range(11)
  • a continuous range description that the algorithm may treat numerically during mutation

Even if a later cast produces integers, the search behavior may no longer be the discrete search you thought you were configuring.

A safer approach is to make the search space itself discrete whenever the problem is discrete:

python
1ga = pygad.GA(
2    num_generations=10,
3    sol_per_pop=6,
4    num_genes=3,
5    num_parents_mating=2,
6    fitness_func=fitness_func,
7    gene_type=int,
8    gene_space=[list(range(0, 6)), list(range(10, 16)), [100, 200, 300]],
9)

Now each gene is selected from a clearly integer-valued domain.

Custom Mutation Can Reintroduce Floats

Another failure mode is custom code. Even if the initial population is integer-valued, a custom mutation callback or postprocessing hook can convert genes back to floats. If you extend PyGAD behavior yourself, make the integer contract explicit at the edge:

python
def clamp_to_ints(solution):
    return [int(value) for value in solution]

If your fitness function assumes integers but your operators quietly generate floats, you will get confusing results that look like a library bug.

What to Check When Debugging

When PyGAD appears not to honor integer intent, inspect these items in order:

  1. gene_type
  2. gene_space
  3. initialization range arguments
  4. mutation behavior
  5. any custom callbacks that alter solutions

Also print actual runtime types instead of trusting how the values look when displayed. A value printed as 3 may still be a NumPy float or a float that happened to have no visible fractional part.

Choose the Right Representation

If the optimization variable is categorical or discrete, use discrete gene_space. If it is truly continuous but later rounded for business rules, then integer genes may not be the right model at all. In that case it can be better to optimize continuously and round at evaluation time, while understanding that the search no longer operates on a strictly integer domain.

The important part is consistency between the mathematical problem and the gene representation.

Common Pitfalls

  • Assuming gene_type=int alone guarantees a discrete search space in every configuration.
  • Passing float-heavy ranges and then blaming PyGAD when mutation explores them numerically.
  • Forgetting that custom mutation or callbacks can reintroduce floats after initialization.
  • Confusing Python configuration integers with the data type of genes.
  • Debugging printed values instead of inspecting their actual runtime types.

Summary

  • PyGAD does support integer genes through gene_type=int.
  • For reliable integer behavior, pair gene_type=int with integer-valued gene_space.
  • A discrete optimization problem should usually use a discrete search space, not just a late integer cast.
  • Custom mutation logic can silently break integer guarantees.
  • When debugging, inspect the actual gene types and the full initialization and mutation path.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.