GeneticAlgorithmPython icon indicating copy to clipboard operation
GeneticAlgorithmPython copied to clipboard

Integer precision issues

Open huku- opened this issue 1 year ago • 1 comments

Passing gene_type=int in the GA class constructor, will result in internal numpy arrays holding 64-bit integer values. This is well known to numpy users:

>>> type(numpy.array([1], dtype=int)[0])
<class 'numpy.int64'>

This, however, has two major problems:

  1. It contradicts the fact that Python ints are arbitrary precision integers
  2. It prohibits users from using pygad to explore bigger state-spaces (e.g. bit-vectors of 256-bits, or even larger in my case)

To solve this problem, a one-liner fix is to add object in GA.supported_int_types here. Then, users can pass gene_type=object in the GA constructor and handle Python integers in objective functions without worrying about numpy getting in their way.

huku- avatar Apr 13 '23 10:04 huku-

Added in a recent commit https://github.com/ahmedfgad/GeneticAlgorithmPython/commit/d06223483386af0a4d1f24de57e6117c0b83e917. Will be supported in the next release.

ahmedfgad avatar Jan 28 '24 05:01 ahmedfgad

Finally found some time to port my code to the new version and run some tests.

Works like charm, thanks a lot @ahmedfgad :)

huku- avatar Jul 11 '24 10:07 huku-