T
- the operation typepublic final class Regression<T> extends Object implements Problem<Tree<Op<T>,?>,ProgramGene<T>,Double>
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.getRandom().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.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));
}
}
Modifier and Type | Method and 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<Op<T>,?> program)
Calculates the actual error for the given
program . |
Function<Tree<Op<T>,?>,Double> |
fitness() |
static <T> Regression<T> |
of(Codec<Tree<Op<T>,?>,ProgramGene<T>> codec,
Error<T> error,
Iterable<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,
Sample<T>... samples)
Create a new regression problem instance with the given parameters.
|
List<Sample<T>> |
samples()
Return an immutable list of the symbolic regression's points.
|
public List<Sample<T>> samples()
public double error(Tree<Op<T>,?> program)
program
.program
- the program to calculate the error value forpublic static <T> Regression<T> of(Codec<Tree<Op<T>,?>,ProgramGene<T>> codec, Error<T> error, Iterable<Sample<T>> samples)
T
- the operation typecodec
- the problem codec to useerror
- the error functionsamples
- the sample points used for regression analysisIllegalArgumentException
- if the given samples
is emptyNullPointerException
- if on of the arguments is null
codecOf(ISeq, ISeq, int)
,
codecOf(ISeq, ISeq, int, Predicate)
@SafeVarargs public static <T> Regression<T> of(Codec<Tree<Op<T>,?>,ProgramGene<T>> codec, Error<T> error, Sample<T>... samples)
T
- the operation typecodec
- the problem codec to useerror
- the error functionsamples
- the sample points used for regression analysisIllegalArgumentException
- if the given samples
is emptyNullPointerException
- if on of the arguments is null
codecOf(ISeq, ISeq, int)
,
codecOf(ISeq, ISeq, int, Predicate)
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)
T
- the operation typeoperations
- 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 be null
.IllegalArgumentException
- if the tree depth
is not in the
valid range of [0, 30)
NullPointerException
- if the operations
or terminals
are null
public static <T> Codec<Tree<Op<T>,?>,ProgramGene<T>> codecOf(ISeq<Op<T>> operations, ISeq<Op<T>> terminals, int depth)
T
- the operation typeoperations
- the operations used for the symbolic regressionterminals
- the terminal operations of the program treedepth
- the maximal tree depth (height) of newly created program
treesIllegalArgumentException
- if the tree depth
is not in the
valid range of [0, 30)
NullPointerException
- if the operations
or terminals
are null
© 2007-2019 Franz Wilhelmstötter (2019-11-18 20:30)