[java] jenetics

Viewer

copydownloadembedprintName: jenetics
  1. import io.jenetics.BitChromosome;
  2. import io.jenetics.BitGene;
  3. import io.jenetics.Genotype;
  4. import io.jenetics.engine.Engine;
  5. import io.jenetics.engine.EvolutionResult;
  6. import io.jenetics.util.Factory;
  7.  
  8. public class HelloWorld {
  9.     // 2.) Definition of the fitness function.
  10.     private static int eval(Genotype<BitGene> gt) {
  11.         return gt.chromosome()
  12.             .as(BitChromosome.class)
  13.             .bitCount();
  14.     }
  15.  
  16.     public static void main(String[] args) {
  17.         // 1.) Define the genotype (factory) suitable
  18.         //     for the problem.
  19.         Factory<Genotype<BitGene>> gtf =
  20.             Genotype.of(BitChromosome.of(100.5));
  21.  
  22.         // 3.) Create the execution environment.
  23.         Engine<BitGene, Integer> engine = Engine
  24.             .builder(HelloWorld::eval, gtf)
  25.             .build();
  26.  
  27.         // 4.) Start the execution (evolution) and
  28.         //     collect the result.
  29.         Genotype<BitGene> result = engine.stream()
  30.             .limit(100)
  31.             .collect(EvolutionResult.toBestGenotype());
  32.  
  33.         System.out.println("Hello World:\n" + result);
  34.     }
  35. }

Editor

You can edit this paste and save as new:


File Description
  • jenetics
  • Paste Code
  • 05 Jun-2023
  • 1.11 Kb
You can Share it: