title: Jenetics custom gene/chromosome
body: <p>I have started experimenting with the Jenetics library, however I am having some issues with trying to make a very easy "custom" set of gene/chromosomes. 
  What I tried to do was to create a custom chromosome with a different (random) number of custom genes inside. The genes simply contain an integer value, just for the sake of simplicity. For the same simplicity, the contents of a gene can only be numbers ranging from 0 to 9 and a Gene is considered valid only if it does NOT contain the number 9 (again, retardedly simple, but I just wanted to make them custom)</p>
  
  <p>Here is my code:</p>
  
  <p>CustomGene:</p>
  
  <pre><code>public class CustomGene implements Gene&lt;Integer, CustomGene&gt; {
  
      private Integer value;
  
      private CustomGene(Integer value) {
          this.value = value;
      }
  
      public static CustomGene of(Integer value) {
          return new CustomGene(value);
      }
  
      public static ISeq&lt;CustomGene&gt; seq(Integer min, Integer max, int length) {
          Random r = RandomRegistry.getRandom();
          return MSeq.&lt;CustomGene&gt;ofLength(length).fill(() -&gt;
                  new CustomGene(random.nextInt(r, min, max))
          ).toISeq();
      }
  
      @Override
      public Integer getAllele() {
          return value;
      }
  
      @Override
      public CustomGene newInstance() {
          final Random random = RandomRegistry.getRandom();
          return new CustomGene(Math.abs(random.nextInt(9)));
      }
  
      @Override
      public CustomGene newInstance(Integer integer) {
          return new CustomGene(integer);
      }
  
      @Override
      public boolean isValid() {
          return value != 9;
      }
  }
  </code></pre>
  
  <p>CustomChromosome:</p>
  
  <pre><code>import org.jenetics.Chromosome;
  import org.jenetics.util.ISeq;
  import org.jenetics.util.RandomRegistry;
  
  import java.util.Iterator;
  import java.util.Random;
  
  public class CustomChromosome implements Chromosome&lt;CustomGene&gt; {
  
      private ISeq&lt;CustomGene&gt; iSeq;
      private final int length;
  
      public CustomChromosome(ISeq&lt;CustomGene&gt; genes) {
          this.iSeq = genes;
          this.length = iSeq.length();
      }
  
      public static CustomChromosome of(ISeq&lt;CustomGene&gt; genes) {
          return new CustomChromosome(genes);
      }
  
      @Override
      public Chromosome&lt;CustomGene&gt; newInstance(ISeq&lt;CustomGene&gt; iSeq) {
          this.iSeq = iSeq;
          return this;
      }
  
      @Override
      public CustomGene getGene(int i) {
          return iSeq.get(i);
      }
  
      @Override
      public int length() {
          return iSeq.length();
      }
  
      @Override
      public ISeq&lt;CustomGene&gt; toSeq() {
          return iSeq;
      }
  
      @Override
      public Chromosome&lt;CustomGene&gt; newInstance() {
          final Random random = RandomRegistry.getRandom();
          ISeq&lt;CustomGene&gt; genes = ISeq.empty();
          for (int i = 0; i &lt; length; i++) {
              genes = genes.append(CustomGene.of(Math.abs(random.nextInt(9))));
          }
          return new CustomChromosome(genes);
      }
  
      @Override
      public Iterator&lt;CustomGene&gt; iterator() {
          return iSeq.iterator();
      }
  
      @Override
      public boolean isValid() {
          return iSeq.stream().allMatch(CustomGene::isValid);
      }
  }
  </code></pre>
  
  <p>Main:</p>
  
  <pre><code>import org.jenetics.Genotype;
  import org.jenetics.Optimize;
  import org.jenetics.engine.Engine;
  import org.jenetics.engine.EvolutionResult;
  import org.jenetics.util.Factory;
  import org.jenetics.util.RandomRegistry;
  
  import java.util.Random;
  
  public class Main {
  
      private static int maxSum = - 100;
  
      private static Integer eval(Genotype&lt;CustomGene&gt; gt) {
          final int sum = gt.getChromosome().stream().mapToInt(CustomGene::getAllele).sum();
          if(sum &gt; maxSum)
              maxSum = sum;
          return sum;
  
      }
  
      public static void main(String[] args) {
          final Random random = RandomRegistry.getRandom();
  
          Factory&lt;Genotype&lt;CustomGene&gt;&gt; g =
                  Genotype.of(CustomChromosome.of(CustomGene.seq(0, 9, Math.abs(random.nextInt(9)) + 1)));
  
          Engine&lt;CustomGene, Integer&gt; engine = Engine
                  .builder(Main::eval, g)
                  .optimize(Optimize.MAXIMUM)
                  .populationSize(100)
                  .build();
  
          Genotype&lt;CustomGene&gt; result = engine.stream().limit(10000)
                  .collect(EvolutionResult.toBestGenotype());
  
          System.out.println(eval(result));
          result.getChromosome().stream().forEach(i -&gt; {
              System.out.print(i.getAllele() + " ");
          });
  
          System.out.println();
  
          System.out.println(maxSum);
      }
  }
  </code></pre>
  
  <p>I do not understand why I get this output:</p>
  
  <pre><code>13 (evaluated result)
  1 8 0 4 (all the alleles form the genes of the chosen chromosome)
  32 (the actual maximum fitness found)
  </code></pre>
  
  <p>We can clearly see a difference between the genotype which had the biggest fitness function and the chosen genotype. Why?
  I know I'm doing something wrong and it's probably a silly mistake, but I really can't seem to understand what I am doing wrong. Could you please help me out?</p>
  
  <p>Lots of thanks!</p>