KnapsackSelectorComparison.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 static java.lang.Math.log10;
23 import static java.lang.Math.max;
24 import static java.lang.Math.pow;
25 import static org.jenetics.tool.evaluation.engines.KNAPSACK;
26 
27 import java.util.function.Supplier;
28 import java.util.stream.IntStream;
29 
30 import org.jenetics.BitGene;
31 import org.jenetics.MonteCarloSelector;
32 import org.jenetics.engine.limit;
33 import org.jenetics.tool.trial.Params;
34 import org.jenetics.tool.trial.TrialMeter;
35 import org.jenetics.util.ISeq;
36 
37 /**
38  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
39  @version 3.5
40  @since 3.5
41  */
42 public class KnapsackSelectorComparison {
43 
44     private static final double GEN_BASE = pow(10, log10(100)/20.0);
45     private static final Params<Long> PARAMS = Params.of(
46         "Fixed generation",
47         IntStream.rangeClosed(150)
48             .mapToLong(i -> max((long)pow(GEN_BASE, i), i))
49             .mapToObj(Long::valueOf)
50             .collect(ISeq.toISeq())
51     );
52 
53     private static final Supplier<TrialMeter<Long>>
54     TRIAL_METER = () -> TrialMeter.of(
55         "Fixed generation""Create fixed generation performance measures",
56         PARAMS,
57         "Generation1",
58         "Fitness1",
59         "Runtime1",
60         "Generation2",
61         "Fitness2",
62         "Runtime2"
63     );
64 
65     public static void main(final String[] argsthrows InterruptedException {
66         final Runner2<Long, BitGene, Double> runner = Runner2.of(
67             KNAPSACK.builder()
68                 .selector(new MonteCarloSelector<>())
69                 .build(),
70             limit::byFixedGeneration,
71             KNAPSACK,
72             limit::byFixedGeneration,
73             TRIAL_METER,
74             args
75         );
76 
77         runner.start();
78         runner.join();
79     }
80 
81 }