Skip to content

Instantly share code, notes, and snippets.

@jenetics
Created June 11, 2019 18:28
Show Gist options
  • Save jenetics/261ea4bbffb9aba003f303b6b672a244 to your computer and use it in GitHub Desktop.
Save jenetics/261ea4bbffb9aba003f303b6b672a244 to your computer and use it in GitHub Desktop.
private static final Regression<Double> REGRESSION = Regression.of(
Regression.codecOf(OPERATIONS, TERMINALS, 5),
Error.of(LossFunction::mse),
Sample.ofDouble(-1.0, -8.0000),
// ...
Sample.ofDouble(0.9, 1.3860),
Sample.ofDouble(1.0, 2.0000)
);
public static void main(final String[] args) {
final Engine<ProgramGene<Double>, Double> engine = Engine
.builder(REGRESSION)
.minimizing()
.alterers(
new SingleNodeCrossover<>(0.1),
new Mutator<>())
.build();
final EvolutionResult<ProgramGene<Double>, Double> result = engine
.stream()
.limit(Limits.byFitnessThreshold(0.01))
.collect(EvolutionResult.toBestEvolutionResult());
final ProgramGene<Double> program = result.getBestPhenotype()
.getGenotype()
.getGene();
final TreeNode<Op<Double>> tree = program.toTreeNode();
MathExpr.rewrite(tree); // Simplify result program.
System.out.println("Generations: " + result.getTotalGenerations());
System.out.println("Function: " + new MathExpr(tree));
System.out.println("Error: " + REGRESSION.error(tree));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment