Skip to content

Commit

Permalink
#169: Implement (μ + λ) ES example.
Browse files Browse the repository at this point in the history
  • Loading branch information
jenetics committed Apr 16, 2017
1 parent d7adb33 commit d631f1f
Showing 1 changed file with 69 additions and 0 deletions.
@@ -0,0 +1,69 @@
/*
* Java Genetic Algorithm Library (@__identifier__@).
* Copyright (c) @__year__@ Franz Wilhelmstötter
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Author:
* Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
*/
package org.jenetics.example;

import org.jenetics.DoubleGene;
import org.jenetics.Mutator;
import org.jenetics.TruncationSelector;
import org.jenetics.engine.Codec;
import org.jenetics.engine.Engine;
import org.jenetics.engine.EvolutionResult;
import org.jenetics.engine.codecs;
import org.jenetics.util.DoubleRange;

/**
* The (μ + λ) evolution strategy is among the simplest ES algorithms.
*
* @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
* @version !__version__!
* @since !__version__!
*/
public class MpLStrategy {

static double fitness(final double x) {
return x;
}

public static void main(final String[] args) {
final int μ = 5;
final int λ = 20;
final double p = 0.2;

final Codec<Double, DoubleGene> codec = codecs
.ofScalar(DoubleRange.of(0, 1));

final Engine<DoubleGene, Double> engine = Engine
.builder(MpLStrategy::fitness, codec)
.populationSize(λ)
.survivorsSize(μ)
.selector(new TruncationSelector<>(μ))
.alterers(new Mutator<>(p))
.build();

System.out.println(
codec.decode(
engine.stream()
.limit(100)
.collect(EvolutionResult.toBestGenotype())
)
);
}

}

0 comments on commit d631f1f

Please sign in to comment.