Skip to content

Commit

Permalink
#566: Add serialization.
Browse files Browse the repository at this point in the history
  • Loading branch information
jenetics committed Jan 29, 2020
1 parent 6f5aafb commit e7a2498
Show file tree
Hide file tree
Showing 3 changed files with 202 additions and 30 deletions.
95 changes: 72 additions & 23 deletions jenetics/src/main/java/io/jenetics/engine/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/
package io.jenetics.engine;

import static java.lang.Math.round;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
import static java.util.concurrent.CompletableFuture.supplyAsync;
Expand Down Expand Up @@ -118,28 +117,6 @@ public final class Engine<
EvolutionStreamable<G, C>
{


public static final class ProblemParams<
G extends Gene<?, G>,
C extends Comparable<? super C>
> {

}

public static final class EvolParams<
G extends Gene<?, G>,
C extends Comparable<? super C>
> {

}

public static final class ExecutionParams<
G extends Gene<?, G>,
C extends Comparable<? super C>
> {

}

// Problem definition.
private final Evaluator<G, C> _evaluator;
private final Factory<Genotype<G>> _genotypeFactory;
Expand Down Expand Up @@ -1202,3 +1179,75 @@ public Builder<G, C> copy() {
}

}


final class ProblemParams<
G extends Gene<?, G>,
C extends Comparable<? super C>
> {
private final Evaluator<G, C> _evaluator;
private final Factory<Genotype<G>> _genotypeFactory;
private final Constraint<G, C> _constraint;
private final Optimize _optimize;

ProblemParams(
final Evaluator<G, C> evaluator,
final Factory<Genotype<G>> genotypeFactory,
final Constraint<G, C> constraint,
final Optimize optimize
) {
_evaluator = requireNonNull(evaluator);
_genotypeFactory = requireNonNull(genotypeFactory);
_constraint = requireNonNull(constraint);
_optimize = requireNonNull(optimize);
}

public Evaluator<G, C> evaluator() {
return _evaluator;
}

public Factory<Genotype<G>> genotypeFactory() {
return _genotypeFactory;
}

public Constraint<G, C> constraint() {
return _constraint;
}

public Optimize optimize() {
return _optimize;
}

}

final class ExecutionParams<
G extends Gene<?, G>,
C extends Comparable<? super C>
> {
private final Executor _executor;
private final Clock _clock;
private final UnaryOperator<EvolutionResult<G, C>> _mapper;

ExecutionParams(
final Executor executor,
final Clock clock,
final UnaryOperator<EvolutionResult<G, C>> mapper
) {
_executor = requireNonNull(executor);
_clock = requireNonNull(clock);
_mapper = requireNonNull(mapper);
}

public Executor executor() {
return _executor;
}

public Clock clock() {
return _clock;
}

public UnaryOperator<EvolutionResult<G, C>> mapper() {
return _mapper;
}

}
64 changes: 57 additions & 7 deletions jenetics/src/main/java/io/jenetics/engine/EvolutionParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,20 @@
*/
package io.jenetics.engine;

import static io.jenetics.internal.util.SerialIO.readInt;
import static io.jenetics.internal.util.SerialIO.readLong;
import static io.jenetics.internal.util.SerialIO.writeInt;
import static io.jenetics.internal.util.SerialIO.writeLong;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
import static io.jenetics.internal.util.Requires.probability;

import java.io.IOException;
import java.io.InvalidObjectException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.Serializable;
import java.util.Objects;
import java.util.stream.Stream;

Expand All @@ -32,6 +42,7 @@
import io.jenetics.Selector;
import io.jenetics.SinglePointCrossover;
import io.jenetics.TournamentSelector;
import io.jenetics.internal.util.Requires;

/**
* This class collects the parameters which control the behaviour of the
Expand All @@ -51,7 +62,9 @@
public final class EvolutionParams<
G extends Gene<?, G>,
C extends Comparable<? super C>
> {
>
implements Serializable
{

private final Selector<G, C> _survivorsSelector;
private final Selector<G, C> _offspringSelector;
Expand All @@ -68,12 +81,12 @@ private EvolutionParams(
final double offspringFraction,
final long maximalPhenotypeAge
) {
_survivorsSelector = survivorsSelector;
_offspringSelector = offspringSelector;
_alterer = alterer;
_populationSize = populationSize;
_offspringFraction = offspringFraction;
_maximalPhenotypeAge = maximalPhenotypeAge;
_survivorsSelector = requireNonNull(survivorsSelector);
_offspringSelector = requireNonNull(offspringSelector);
_alterer = requireNonNull(alterer);
_populationSize = Requires.positive(populationSize);
_offspringFraction = Requires.probability(offspringFraction);
_maximalPhenotypeAge = Requires.positive(maximalPhenotypeAge);
}


Expand Down Expand Up @@ -444,4 +457,41 @@ public int survivorsSize() {

}

/* *************************************************************************
* Java object serialization
* ************************************************************************/

private Object writeReplace() {
return new Serial(Serial.EVOLUTION_PARAMS, this);
}

private void readObject(final ObjectInputStream stream)
throws InvalidObjectException
{
throw new InvalidObjectException("Serialization proxy required.");
}

void write(final ObjectOutput out) throws IOException {
out.writeObject(survivorsSelector());
out.writeObject(offspringSelector());
out.writeObject(alterer());
writeInt(populationSize(), out);
out.writeDouble(offspringFraction());
writeLong(maximalPhenotypeAge(), out);
}

@SuppressWarnings({"unchecked", "rawtypes"})
static EvolutionParams read(final ObjectInput in)
throws IOException, ClassNotFoundException
{
return new EvolutionParams(
(Selector)in.readObject(),
(Selector)in.readObject(),
(Alterer)in.readObject(),
readInt(in),
in.readDouble(),
readLong(in)
);
}

}
73 changes: 73 additions & 0 deletions jenetics/src/main/java/io/jenetics/engine/Serial.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package io.jenetics.engine;

import java.io.Externalizable;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.io.StreamCorruptedException;

/**
* @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
* @version !__version__!
* @since !__version__!
*/
final class Serial implements Externalizable {

private static final long serialVersionUID = 1;

static final byte EVOLUTION_PARAMS = 1;

/**
* The type being serialized.
*/
private byte _type;

/**
* The object being serialized.
*/
private Object _object;

/**
* Constructor for deserialization.
*/
public Serial() {
}

/**
* Creates an instance for serialization.
*
* @param type the type
* @param object the object
*/
Serial(final byte type, final Object object) {
_type = type;
_object = object;
}

@Override
public void writeExternal(final ObjectOutput out) throws IOException {
out.writeByte(_type);
switch (_type) {
case EVOLUTION_PARAMS: ((EvolutionParams)_object).write(out); break;
default:
throw new StreamCorruptedException("Unknown serialized type.");
}
}

@Override
public void readExternal(final ObjectInput in)
throws IOException, ClassNotFoundException
{
_type = in.readByte();
switch (_type) {
case EVOLUTION_PARAMS: _object = EvolutionParams.read(in); break;
default:
throw new StreamCorruptedException("Unknown serialized type.");
}
}

private Object readResolve() {
return _object;
}

}

0 comments on commit e7a2498

Please sign in to comment.