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.
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_generationsandnum_genesPython 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:
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:
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:
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:
gene_typegene_space- initialization range arguments
- mutation behavior
- 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=intalone 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=intwith integer-valuedgene_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
- PyLint message logging-format-interpolation
- Pylint, PyChecker or PyFlakes?
- PyLint Unable to import error - how to set PYTHONPATH?
- Pylint unresolved import error in Visual Studio Code
- pymongo - dnspython module must be installed to use mongodbsrv// URIs
- PyPy -- How can it possibly beat CPython?
- pytest assert almost equal
- pytest cannot import module while python can
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.