Skip to content

Commit

Permalink
#566: Intermediate commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
jenetics committed Jan 26, 2020
1 parent 0bf7986 commit b289e68
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 65 deletions.
131 changes: 78 additions & 53 deletions jenetics/src/main/java/io/jenetics/engine/Engine.java
Original file line number Diff line number Diff line change
Expand Up @@ -718,19 +718,23 @@ public static final class Builder<
// No default values for this properties.
private final Evaluator<G, C> _evaluator;
private final Factory<Genotype<G>> _genotypeFactory;
private Constraint<G, C> _constraint;
private Optimize _optimize = Optimize.MAXIMUM;

// This are the properties which default values.
// Evolution parameters.
/*
private Selector<G, C> _survivorsSelector = new TournamentSelector<>(3);
private Selector<G, C> _offspringSelector = new TournamentSelector<>(3);
private Alterer<G, C> _alterer = Alterer.of(
new SinglePointCrossover<G, C>(0.2),
new Mutator<>(0.15)
);
private Constraint<G, C> _constraint;
private Optimize _optimize = Optimize.MAXIMUM;
private double _offspringFraction = 0.6;
private int _populationSize = 50;
private long _maximalPhenotypeAge = 70;
*/
private final EvolutionParams.Builder<G, C> _evolutionParams = EvolutionParams.builder();


// Engine execution environment.
private Executor _executor = commonPool();
Expand Down Expand Up @@ -772,7 +776,8 @@ public Builder(
public Builder<G, C> offspringSelector(
final Selector<G, C> selector
) {
_offspringSelector = requireNonNull(selector);
//_offspringSelector = requireNonNull(selector);
_evolutionParams.offspringSelector(selector);
return this;
}

Expand All @@ -786,7 +791,8 @@ public Builder<G, C> offspringSelector(
public Builder<G, C> survivorsSelector(
final Selector<G, C> selector
) {
_survivorsSelector = requireNonNull(selector);
//_survivorsSelector = requireNonNull(selector);
_evolutionParams.survivorsSelector(selector);
return this;
}

Expand All @@ -799,8 +805,9 @@ public Builder<G, C> survivorsSelector(
* @return {@code this} builder, for command chaining
*/
public Builder<G, C> selector(final Selector<G, C> selector) {
_offspringSelector = requireNonNull(selector);
_survivorsSelector = requireNonNull(selector);
//_offspringSelector = requireNonNull(selector);
//_survivorsSelector = requireNonNull(selector);
_evolutionParams.selector(selector);
return this;
}

Expand All @@ -822,13 +829,13 @@ public final Builder<G, C> alterers(
final Alterer<G, C> first,
final Alterer<G, C>... rest
) {
requireNonNull(first);
Stream.of(rest).forEach(Objects::requireNonNull);

_alterer = rest.length == 0
? first
: Alterer.of(rest).compose(first);

// requireNonNull(first);
// Stream.of(rest).forEach(Objects::requireNonNull);
//
// _alterer = rest.length == 0
// ? first
// : Alterer.of(rest).compose(first);
_evolutionParams.alterers(first, rest);
return this;
}

Expand Down Expand Up @@ -899,7 +906,8 @@ public Builder<G, C> minimizing() {
* within the range [0, 1].
*/
public Builder<G, C> offspringFraction(final double fraction) {
_offspringFraction = probability(fraction);
//_offspringFraction = probability(fraction);
_evolutionParams.offspringFraction(fraction);
return this;
}

Expand All @@ -919,7 +927,8 @@ public Builder<G, C> offspringFraction(final double fraction) {
* within the range [0, 1].
*/
public Builder<G, C> survivorsFraction(final double fraction) {
_offspringFraction = 1.0 - probability(fraction);
//_offspringFraction = 1.0 - probability(fraction);
_evolutionParams.survivorsFraction(fraction);
return this;
}

Expand All @@ -934,14 +943,16 @@ public Builder<G, C> survivorsFraction(final double fraction) {
* within the range [0, population-size].
*/
public Builder<G, C> offspringSize(final int size) {
if (size < 0) {
throw new IllegalArgumentException(format(
"Offspring size must be greater or equal zero, but was %s.",
size
));
}

return offspringFraction((double)size/(double)_populationSize);
// if (size < 0) {
// throw new IllegalArgumentException(format(
// "Offspring size must be greater or equal zero, but was %s.",
// size
// ));
// }
//
// return offspringFraction((double)size/(double)_populationSize);
_evolutionParams.offspringCount(size);
return this;
}

/**
Expand All @@ -955,14 +966,16 @@ public Builder<G, C> offspringSize(final int size) {
* within the range [0, population-size].
*/
public Builder<G, C> survivorsSize(final int size) {
if (size < 0) {
throw new IllegalArgumentException(format(
"Survivors must be greater or equal zero, but was %s.",
size
));
}

return survivorsFraction((double)size/(double)_populationSize);
// if (size < 0) {
// throw new IllegalArgumentException(format(
// "Survivors must be greater or equal zero, but was %s.",
// size
// ));
// }
//
// return survivorsFraction((double)size/(double)_populationSize);
_evolutionParams.survivorsCount(size);
return this;
}

/**
Expand All @@ -974,13 +987,15 @@ public Builder<G, C> survivorsSize(final int size) {
* @throws java.lang.IllegalArgumentException if {@code size < 1}
*/
public Builder<G, C> populationSize(final int size) {
if (size < 1) {
throw new IllegalArgumentException(format(
"Population size must be greater than zero, but was %s.",
size
));
}
_populationSize = size;
// if (size < 1) {
// throw new IllegalArgumentException(format(
// "Population size must be greater than zero, but was %s.",
// size
// ));
// }
// _populationSize = size;
// return this;
_evolutionParams.populationSize(size);
return this;
}

Expand All @@ -993,12 +1008,14 @@ public Builder<G, C> populationSize(final int size) {
* @throws java.lang.IllegalArgumentException if {@code age < 1}
*/
public Builder<G, C> maximalPhenotypeAge(final long age) {
if (age < 1) {
throw new IllegalArgumentException(format(
"Phenotype age must be greater than one, but was %s.", age
));
}
_maximalPhenotypeAge = age;
// if (age < 1) {
// throw new IllegalArgumentException(format(
// "Phenotype age must be greater than one, but was %s.", age
// ));
// }
// _maximalPhenotypeAge = age;
// return this;
_evolutionParams.maximalPhenotypeAge(age);
return this;
}

Expand Down Expand Up @@ -1074,11 +1091,13 @@ public Engine<G, C> build() {
}

private int getSurvivorsCount() {
return _populationSize - getOffspringCount();
//return _populationSize - getOffspringCount();
return _evolutionParams.survivorsCount();
}

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

/**
Expand All @@ -1087,7 +1106,8 @@ private int getOffspringCount() {
* @return the used {@link Alterer} of the GA.
*/
public Alterer<G, C> getAlterers() {
return _alterer;
//return _alterer;
return _evolutionParams.alterers();
}

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

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

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

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

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

/**
Expand Down
20 changes: 8 additions & 12 deletions jenetics/src/main/java/io/jenetics/engine/EvolutionParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,6 @@ Builder<G, C> builder() {
return new Builder<>();
}

/**
* Return the default evolution parameters.
*
* @param <G> the gene type
* @param <C> the fitness function result type
* @return the default evolution parameters
*/
public static <G extends Gene<?, G>, C extends Comparable<? super C>>
EvolutionParams<G, C> defaultParams() {
return EvolutionParams.<G, C>builder().build();
}


/* *************************************************************************
* Params builder
Expand Down Expand Up @@ -445,6 +433,10 @@ public int survivorsCount() {
return _survivorsCount;
}

public double survivorsFraction() {
return _survivorsCount/(double)populationSize();
}

/**
* Return the offspring count.
*
Expand All @@ -454,6 +446,10 @@ public int offspringCount() {
return _offspringCount;
}

public double offspringFraction() {
return _offspringCount/(double)populationSize();
}

/**
* Return the number of individuals of a population.
*
Expand Down

0 comments on commit b289e68

Please sign in to comment.