java.lang.Object
io.jenetics.prog.op.Val<T>
io.jenetics.prog.op.EphemeralConst<T>
- All Implemented Interfaces:
Op<T>
,Serializable
,Function<T[],
,T> Supplier<Op<T>>
Implementation of an ephemeral constant. It causes the insertion of
a mutable constant into the operation tree. Every time this terminal
is chosen, a different value is generated which is then used for that
particular terminal, and which will remain fixed for the given tree. The main
usage would be to introduce random terminal values.
Serialization
Although the
The serialization of the constant will fail, if the lambda has to
capture variables form a non-serializable context (class). In such a
case, it is advisable to create a dedicated supplier class.
final Random random = ...;
final Op<Double> val = EphemeralConst.of(random::nextDouble);
EphemeralConst
class implements the Serializable
interface, the serialization will fail if the const supplier is not
serializable as well. This can be achieved by casting the
supplier to a Serializable
.
final Random random = new Random();
final EphemeralConst<Integer> object = EphemeralConst.of(
"R",
(Supplier<Integer> & Serializable)random::nextInt
);
final class RandomInt implements Supplier<Integer>, Serializable {
private final Random rnd = new Random();
private final int min;
private final int max;
private RandomInt(final int min, final int max) {
this.min = min;
this.max = max;
}
@Override
public Integer get() {
return rnd.nextInt(max - min) + min;
}
}
- Since:
- 3.9
- Version:
- 7.0
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionget()
Return a newly created, uninitialized constant of typeT
.static <T> EphemeralConst
<T> Create a new ephemeral constant with the givenname
and valuesupplier
.static <T> EphemeralConst
<T> Create a new ephemeral constant with the given valuesupplier
.toString()
value()
Fixes and returns the constant value.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface io.jenetics.prog.op.Op
arity, isTerminal, name
-
Method Details
-
get
Return a newly created, uninitialized constant of typeT
. -
value
Fixes and returns the constant value. -
toString
-
of
Create a new ephemeral constant with the givenname
and valuesupplier
. For every newly created operation tree, a new constant value is chosen for this terminal operation. The value is then kept constant for this tree.- Type Parameters:
T
- the constant type- Parameters:
name
- the name of the ephemeral constantsupplier
- the value supplier- Returns:
- a new ephemeral constant
- Throws:
NullPointerException
- if one of the arguments isnull
-
of
Create a new ephemeral constant with the given valuesupplier
. For every newly created operation tree, a new constant value is chosen for this terminal operation. The value is then kept constant for this tree.- Type Parameters:
T
- the constant type- Parameters:
supplier
- the value supplier- Returns:
- a new ephemeral constant
- Throws:
NullPointerException
- if thesupplier
isnull
-