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 Summary
Modifier and TypeMethodDescriptioncodec()
static <T> Codec
<Tree<Op<T>, ?>, ProgramGene<T>> 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
Calculates the actual error for the givenprogram
.fitness()
static <T> Regression
<T> Create a new regression problem instance with the given parameters.static <T> Regression
<T> 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 Details
-
fitness
-
codec
-
error
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:
-
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 one of the arguments isnull
- 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 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 one of the arguments isnull
- See Also:
-
codecOf
public static <T> Codec<Tree<Op<T>,?>, codecOfProgramGene<T>> (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>,?>, codecOfProgramGene<T>> (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
-