Skip to content

Commit

Permalink
#566: Fixes after engine refactorings.
Browse files Browse the repository at this point in the history
  • Loading branch information
jenetics committed Aug 22, 2019
1 parent 726b292 commit e9960db
Showing 1 changed file with 22 additions and 43 deletions.
65 changes: 22 additions & 43 deletions jenetics/src/main/java/io/jenetics/engine/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@
*/
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.failedFuture;
import static java.util.concurrent.CompletableFuture.supplyAsync;
import static java.util.concurrent.ForkJoinPool.commonPool;
import static io.jenetics.internal.util.require.probability;

import java.time.Clock;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Function;
Expand All @@ -41,13 +37,9 @@
import io.jenetics.Chromosome;
import io.jenetics.Gene;
import io.jenetics.Genotype;
import io.jenetics.Mutator;
import io.jenetics.Optimize;
import io.jenetics.Phenotype;
import io.jenetics.Selector;
import io.jenetics.SinglePointCrossover;
import io.jenetics.TournamentSelector;
import io.jenetics.internal.util.require;
import io.jenetics.util.Copyable;
import io.jenetics.util.Factory;
import io.jenetics.util.ISeq;
Expand Down Expand Up @@ -544,6 +536,7 @@ public Optimize getOptimize() {
return _evolutionParams.getOptimize();
}


/**
* Return the {@link Clock} the engine is using for measuring the execution
* time.
Expand Down Expand Up @@ -586,16 +579,16 @@ public UnaryOperator<EvolutionResult<G, C>> getMapper() {
*/
public Builder<G, C> toBuilder() {
return new Builder<>(_evaluator, _genotypeFactory)
.alterers(_alterer)
.alterers(_evolutionParams.getAlterer())
.clock(_clock)
.executor(_executor)
.maximalPhenotypeAge(_maximalPhenotypeAge)
.offspringFraction((double)_offspringCount/(double)getPopulationSize())
.offspringSelector(_offspringSelector)
.optimize(_optimize)
.maximalPhenotypeAge(_evolutionParams.getMaximalPhenotypeAge())
.offspringFraction(_evolutionParams.getOffspringFraction())
.offspringSelector(_evolutionParams.getOffspringSelector())
.optimize(_evolutionParams.getOptimize())
.constraint(_constraint)
.populationSize(getPopulationSize())
.survivorsSelector(_survivorsSelector)
.survivorsSelector(_evolutionParams.getSurvivorsSelector())
.mapping(_mapper);
}

Expand Down Expand Up @@ -1042,34 +1035,20 @@ public Engine<G, C> build() {
_constraint == null
? RetryConstraint.of(_genotypeFactory)
: _constraint,
_survivorsSelector,
_offspringSelector,
_alterer,
_optimize,
getOffspringCount(),
getSurvivorsCount(),
_maximalPhenotypeAge,
_evolutionParams.build(),
_executor,
_clock,
_mapper
);
}

private int getSurvivorsCount() {
return _populationSize - getOffspringCount();
}

private int getOffspringCount() {
return (int)round(_offspringFraction*_populationSize);
}

/**
* Return the used {@link Alterer} of the GA.
*
* @return the used {@link Alterer} of the GA.
*/
public Alterer<G, C> getAlterers() {
return _alterer;
return _evolutionParams.alterers();
}

/**
Expand Down Expand Up @@ -1128,7 +1107,7 @@ public Constraint<G, C> getConstraint() {
* @return the maximal allowed phenotype age
*/
public long getMaximalPhenotypeAge() {
return _maximalPhenotypeAge;
return _evolutionParams.maximalPhenotypeAge();
}

/**
Expand All @@ -1137,7 +1116,7 @@ public long getMaximalPhenotypeAge() {
* @return the offspring fraction.
*/
public double getOffspringFraction() {
return _offspringFraction;
return _evolutionParams.offspringFraction();
}

/**
Expand All @@ -1148,7 +1127,7 @@ public double getOffspringFraction() {
* @return the used offspring {@link Selector} of the GA.
*/
public Selector<G, C> getOffspringSelector() {
return _offspringSelector;
return _evolutionParams.offspringSelector();
}

/**
Expand All @@ -1159,7 +1138,7 @@ public Selector<G, C> getOffspringSelector() {
* @return the used survivor {@link Selector} of the GA.
*/
public Selector<G, C> getSurvivorsSelector() {
return _survivorsSelector;
return _evolutionParams.survivorsSelector();
}

/**
Expand All @@ -1170,7 +1149,7 @@ public Selector<G, C> getSurvivorsSelector() {
* @return the optimization strategy
*/
public Optimize getOptimize() {
return _optimize;
return _evolutionParams.optimize();
}

/**
Expand All @@ -1181,7 +1160,7 @@ public Optimize getOptimize() {
* @return the number of individuals of a population
*/
public int getPopulationSize() {
return _populationSize;
return _evolutionParams.populationSize();
}

/**
Expand All @@ -1205,16 +1184,16 @@ public UnaryOperator<EvolutionResult<G, C>> getMapper() {
@Override
public Builder<G, C> copy() {
return new Builder<G, C>(_evaluator, _genotypeFactory)
.alterers(_alterer)
.alterers(_evolutionParams.alterers())
.clock(_clock)
.executor(_executor)
.maximalPhenotypeAge(_maximalPhenotypeAge)
.offspringFraction(_offspringFraction)
.offspringSelector(_offspringSelector)
.maximalPhenotypeAge(_evolutionParams.maximalPhenotypeAge())
.offspringFraction(_evolutionParams.offspringFraction())
.offspringSelector(_evolutionParams.offspringSelector())
.constraint(_constraint)
.optimize(_optimize)
.populationSize(_populationSize)
.survivorsSelector(_survivorsSelector)
.optimize(_evolutionParams.optimize())
.populationSize(_evolutionParams.populationSize())
.survivorsSelector(_evolutionParams.offspringSelector())
.mapping(_mapper);
}

Expand Down

0 comments on commit e9960db

Please sign in to comment.