engines.java
01 /*
02  * Java Genetic Algorithm Library (jenetics-3.7.0).
03  * Copyright (c) 2007-2016 Franz Wilhelmstötter
04  *
05  * Licensed under the Apache License, Version 2.0 (the "License");
06  * you may not use this file except in compliance with the License.
07  * You may obtain a copy of the License at
08  *
09  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * Author:
18  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
19  */
20 package org.jenetics.tool.evaluation;
21 
22 import java.util.Random;
23 
24 import org.jenetics.internal.util.require;
25 
26 import org.jenetics.BitGene;
27 import org.jenetics.Mutator;
28 import org.jenetics.RouletteWheelSelector;
29 import org.jenetics.SinglePointCrossover;
30 import org.jenetics.TournamentSelector;
31 import org.jenetics.engine.Engine;
32 import org.jenetics.tool.problem.Knapsack;
33 import org.jenetics.util.LCG64ShiftRandom;
34 
35 /**
36  * Definition of commonly used testing {@link Engine} objects.
37  *
38  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
39  @version 3.4
40  @since 3.4
41  */
42 public final class engines {
43     private engines() {require.noInstance();}
44 
45     /**
46      * The test {@link Knapsack} {@link Engine} used for the evolution
47      * performance tests.
48      */
49     public static Engine<BitGene, Double>
50         KNAPSACK = knapsack(new LCG64ShiftRandom(10101));
51 
52     public static Engine<BitGene, Double> KNAPSACK(final int populationSize) {
53         return knapsack(populationSize, new LCG64ShiftRandom(10101));
54     }
55 
56     public static Engine<BitGene, Double> knapsack(final Random random) {
57         return knapsack(150, random);
58     }
59 
60     /**
61      * Create a new {@link Engine} for solving the {@link Knapsack} problem. The
62      * engine is used for testing purpose.
63      *
64      @see Knapsack#of(int, Random)
65      *
66      @param populationSize the population size of the created engine
67      @param random the random engine used for creating the {@link Knapsack}
68      *        problem instance
69      @return a new {@link Knapsack} solving evolution {@link Engine}
70      */
71     public static Engine<BitGene, Double> knapsack(
72         final int populationSize,
73         final Random random
74     ) {
75         // Search space fo 2^250 ~ 10^75.
76         final Knapsack knapsack = Knapsack.of(250, random);
77 
78         // Configure and build the evolution engine.
79         return Engine.builder(knapsack)
80             .populationSize(populationSize)
81             .survivorsSelector(new TournamentSelector<>(5))
82             .offspringSelector(new RouletteWheelSelector<>())
83             .alterers(
84                 new Mutator<>(0.03),
85                 new SinglePointCrossover<>(0.125))
86             .build();
87     }
88 
89 }