Class IntChromosome

java.lang.Object
net.bmahe.genetics4j.core.chromosomes.IntChromosome
All Implemented Interfaces:
Chromosome

public class IntChromosome extends Object implements Chromosome
A chromosome implementation that represents genetic information as an array of integer values.

IntChromosome is widely used for discrete optimization problems where solutions can be encoded as sequences of integer values within specified ranges. Each position in the array represents a gene, and the integer value at that position represents the allele.

This chromosome type is particularly suitable for:

  • Combinatorial optimization: Problems with discrete decision variables
  • Parameter optimization: Integer hyperparameters, configuration settings
  • Permutation encoding: When combined with appropriate operators
  • Resource allocation: Assignment and scheduling problems
  • Graph problems: Node labeling, path encoding

Key features:

  • Bounded values: All integers are constrained to [minValue, maxValue]
  • Fixed length: Chromosome size is determined at creation time
  • Immutable: Values cannot be changed after construction
  • Type-safe: Compile-time guarantees for integer operations

The chromosome maintains bounds information which is used by genetic operators to ensure that crossover and mutation operations produce valid offspring within the specified constraints.

See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private final int
     
    private final int
     
    private final int
     
    private final int[]
     
  • Constructor Summary

    Constructors
    Constructor
    Description
    IntChromosome(int _size, int _minValue, int _maxValue, int[] _values)
    Creates a new integer chromosome with the specified parameters and values.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
     
    int
    getAllele(int index)
    Returns the integer value at the specified index.
    int
    Returns the maximum allowed value for integers in this chromosome.
    int
    Returns the minimum allowed value for integers in this chromosome.
    int
    Returns the number of alleles (genetic units) in this chromosome.
    int
    Returns the number of integer values in this chromosome.
    int[]
    Returns a copy of the integer values in this chromosome.
    int
     
     

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • size

      private final int size
    • minValue

      private final int minValue
    • maxValue

      private final int maxValue
    • values

      private final int[] values
  • Constructor Details

    • IntChromosome

      public IntChromosome(int _size, int _minValue, int _maxValue, int[] _values)
      Creates a new integer chromosome with the specified parameters and values.
      Parameters:
      _size - the number of integer values in this chromosome
      _minValue - the minimum allowed value for any integer in this chromosome
      _maxValue - the maximum allowed value for any integer in this chromosome
      _values - the array of integer values for this chromosome
      Throws:
      IllegalArgumentException - if size is not positive, if minValue > maxValue, if values array is null, or if the array length doesn't match the specified size
  • Method Details

    • getNumAlleles

      public int getNumAlleles()
      Description copied from interface: Chromosome
      Returns the number of alleles (genetic units) in this chromosome.

      The interpretation of an allele depends on the specific chromosome type:

      • For bit chromosomes: the number of bits
      • For numeric chromosomes: the number of numeric values
      • For tree chromosomes: the number of nodes in the tree
      Specified by:
      getNumAlleles in interface Chromosome
      Returns:
      the number of alleles in this chromosome, always positive
    • getAllele

      public int getAllele(int index)
      Returns the integer value at the specified index.
      Parameters:
      index - the index of the allele to retrieve (0-based)
      Returns:
      the integer value at the specified position
      Throws:
      IllegalArgumentException - if index is negative or greater than or equal to the chromosome size
    • getSize

      public int getSize()
      Returns the number of integer values in this chromosome.
      Returns:
      the chromosome size
    • getMinValue

      public int getMinValue()
      Returns the minimum allowed value for integers in this chromosome.
      Returns:
      the minimum value constraint
    • getMaxValue

      public int getMaxValue()
      Returns the maximum allowed value for integers in this chromosome.
      Returns:
      the maximum value constraint
    • getValues

      public int[] getValues()
      Returns a copy of the integer values in this chromosome.

      The returned array is a defensive copy; modifications to it will not affect this chromosome.

      Returns:
      a copy of the integer values array
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object