Class Regression<T>

java.lang.Object
io.jenetics.prog.regression.Regression<T>
Type Parameters:
T - the operation type
All Implemented Interfaces:
Problem<Tree<Op<T>,?>,ProgramGene<T>,Double>

public final class Regression<T> extends Object implements Problem<Tree<Op<T>,?>,ProgramGene<T>,Double>
This class implements a symbolic regression problem. The example below shows a typical usage of the Regression class.
public class SymbolicRegression {
    private static final ISeq<Op<Double>> OPERATIONS =
        ISeq.of(MathOp.ADD, MathOp.SUB, MathOp.MUL);

    private static final ISeq<Op<Double>> TERMINALS = ISeq.of(
        Var.of("x", 0),
        EphemeralConst.of(() -> (double)RandomRegistry.random().nextInt(10))
    );

    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.bestPhenotype()
            .genotype()
            .gene();

        final TreeNode<Op<Double>> tree = program.toTreeNode();
        MathExpr.rewrite(tree); // Simplify result program.
        System.out.println("Generations: " + result.totalGenerations());
        System.out.println("Function:    " + new MathExpr(tree));
        System.out.println("Error:       " + REGRESSION.error(tree));
    }
}
Since:
5.0
Version:
6.0
See Also:
  • Method Details

    • fitness

      public Function<Tree<Op<T>,?>,Double> fitness()
      Description copied from interface: Problem
      Return the fitness function of the problem in the native problem domain.
      Specified by:
      fitness in interface Problem<Tree<Op<T>,?>,ProgramGene<T>,Double>
      Returns:
      the fitness function
    • codec

      public Codec<Tree<Op<T>,?>,ProgramGene<T>> codec()
      Description copied from interface: Problem
      Return the codec, which translates the types of the problem domain into types, which can be understood by the evolution Engine.
      Specified by:
      codec in interface Problem<Tree<Op<T>,?>,ProgramGene<T>,Double>
      Returns:
      the engine codec
    • error

      public double error(Tree<? extends Op<T>,?> program)
      Calculates the actual error for the given program.
      Parameters:
      program - the program to calculate the error value for
      Returns:
      the overall error value of the program
    • of

      public static <T> Regression<T> of(Codec<Tree<Op<T>,?>,ProgramGene<T>> codec, Error<T> error, Sampling<T> sampling)
      Create a new regression problem instance with the given parameters.
      Type Parameters:
      T - the operation type
      Parameters:
      codec - the problem codec to use
      error - the error function
      sampling - the sampling function
      Returns:
      a new regression problem instance
      Throws:
      NullPointerException - if on of the arguments is null
      See Also:
    • of

      public static <T> Regression<T> of(Codec<Tree<Op<T>,?>,ProgramGene<T>> codec, Error<T> error, Iterable<? extends Sample<T>> samples)
      Create a new regression problem instance with the given parameters.
      Type Parameters:
      T - the operation type
      Parameters:
      codec - the problem codec to use
      error - the error function
      samples - the sample points used for regression analysis
      Returns:
      a new regression problem instance
      Throws:
      IllegalArgumentException - if the given samples is empty
      NullPointerException - if one of the arguments is null
      See Also:
    • of

      @SafeVarargs public static <T> Regression<T> of(Codec<Tree<Op<T>,?>,ProgramGene<T>> codec, Error<T> error, Sample<T>... samples)
      Create a new regression problem instance with the given parameters.
      Type Parameters:
      T - the operation type
      Parameters:
      codec - the problem codec to use
      error - the error function
      samples - the sample points used for regression analysis
      Returns:
      a new regression problem instance
      Throws:
      IllegalArgumentException - if the given samples is empty
      NullPointerException - if one of the arguments is null
      See Also:
    • codecOf

      public static <T> Codec<Tree<Op<T>,?>,ProgramGene<T>> codecOf(ISeq<Op<T>> operations, ISeq<Op<T>> terminals, int depth, Predicate<? super ProgramChromosome<T>> validator)
      Create a new codec, usable for symbolic regression problems, with the given parameters.
      Type Parameters:
      T - the operation type
      Parameters:
      operations - the operations used for the symbolic regression
      terminals - the terminal operations of the program tree
      depth - the maximal tree depth (height) of newly created program trees
      validator - the chromosome validator. A typical validator would check the size of the tree and if the tree is too large, mark it at invalid. The validator may be null.
      Returns:
      a new codec, usable for symbolic regression
      Throws:
      IllegalArgumentException - if the tree depth is not in the valid range of [0, 30)
      NullPointerException - if the operations or terminals are null
    • codecOf

      public static <T> Codec<Tree<Op<T>,?>,ProgramGene<T>> codecOf(ISeq<Op<T>> operations, ISeq<Op<T>> terminals, int depth)
      Create a new codec, usable for symbolic regression problems, with the given parameters.
      Type Parameters:
      T - the operation type
      Parameters:
      operations - the operations used for the symbolic regression
      terminals - the terminal operations of the program tree
      depth - the maximal tree depth (height) of newly created program trees
      Returns:
      a new codec, usable for symbolic regression
      Throws:
      IllegalArgumentException - if the tree depth is not in the valid range of [0, 30)
      NullPointerException - if the operations or terminals are null