Package io.jenetics.prog.regression
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 theRegression
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:
SampleBuffer
,Sampling
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description Codec<Tree<Op<T>,?>,ProgramGene<T>>
codec()
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.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.double
error(Tree<? extends Op<T>,?> program)
Calculates the actual error for the givenprogram
.Function<Tree<Op<T>,?>,Double>
fitness()
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.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.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.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.jenetics.engine.Problem
constraint, decode, fitness, fitness
-
-
-
-
Method Detail
-
error
public double error(Tree<? extends Op<T>,?> program)
Calculates the actual error for the givenprogram
.- 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 useerror
- the error functionsampling
- the sampling function- Returns:
- a new regression problem instance
- Throws:
NullPointerException
- if on of the arguments isnull
- See Also:
codecOf(ISeq, ISeq, int)
,codecOf(ISeq, ISeq, int, Predicate)
-
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 useerror
- the error functionsamples
- the sample points used for regression analysis- Returns:
- a new regression problem instance
- Throws:
IllegalArgumentException
- if the givensamples
is emptyNullPointerException
- if on of the arguments isnull
- See Also:
codecOf(ISeq, ISeq, int)
,codecOf(ISeq, ISeq, int, Predicate)
-
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 useerror
- the error functionsamples
- the sample points used for regression analysis- Returns:
- a new regression problem instance
- Throws:
IllegalArgumentException
- if the givensamples
is emptyNullPointerException
- if on of the arguments isnull
- See Also:
codecOf(ISeq, ISeq, int)
,codecOf(ISeq, ISeq, int, Predicate)
-
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 regressionterminals
- the terminal operations of the program treedepth
- the maximal tree depth (height) of newly created program treesvalidator
- 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 benull
.- Returns:
- a new codec, usable for symbolic regression
- Throws:
IllegalArgumentException
- if the treedepth
is not in the valid range of[0, 30)
NullPointerException
- if theoperations
orterminals
arenull
-
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 regressionterminals
- the terminal operations of the program treedepth
- the maximal tree depth (height) of newly created program trees- Returns:
- a new codec, usable for symbolic regression
- Throws:
IllegalArgumentException
- if the treedepth
is not in the valid range of[0, 30)
NullPointerException
- if theoperations
orterminals
arenull
-
-