KnapsackGenerationPopulationSize.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 java.lang.String.format;
26 import static org.jenetics.tool.evaluation.engines.KNAPSACK;
27 
28 import java.util.function.Supplier;
29 import java.util.regex.Pattern;
30 import java.util.stream.IntStream;
31 
32 import org.jenetics.BitGene;
33 import org.jenetics.engine.limit;
34 import org.jenetics.tool.trial.Params;
35 import org.jenetics.tool.trial.TrialMeter;
36 import org.jenetics.util.ISeq;
37 
38 /**
39  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
40  @version 3.6
41  @since 3.6
42  */
43 public class KnapsackGenerationPopulationSize {
44 
45     private static final double GEN_BASE = pow(10, log10(100)/20.0);
46     private static final ISeq<String> GENERATIONS = IntStream.rangeClosed(140)
47         .mapToLong(i -> max((long)pow(GEN_BASE, i), i))
48         .mapToObj(String::valueOf)
49         .collect(ISeq.toISeq());
50 
51     private static final ISeq<String> POPULATION_SIZES = IntStream.rangeClosed(130)
52         .mapToLong(i -> max((long)pow(GEN_BASE, i), i))
53         .mapToObj(String::valueOf)
54         .collect(ISeq.toISeq());
55 
56     private static final ISeq<String> GEN_POP = GENERATIONS.stream()
57         .flatMap(g -> POPULATION_SIZES.stream()
58             .map(ps -> format("%s:%s", g, ps)))
59         .collect(ISeq.toISeq());
60 
61     private static final Params<String> PARAMS = Params.of(
62         "Generation/Population size",
63         GEN_POP
64     );
65 
66     private static long toGeneration(final String param) {
67         return Long.parseLong(param.split(Pattern.quote(":"))[0]);
68     }
69 
70     private static int toPopulationSize(final String param) {
71         return Integer.parseInt(param.split(Pattern.quote(":"))[1]);
72     }
73 
74     private static final Supplier<TrialMeter<String>>
75         TRIAL_METER = () -> TrialMeter.of(
76         "Execution time",
77         "Create execution time performance measures",
78         PARAMS,
79         "Generation",
80         "Fitness",
81         "Runtime"
82     );
83 
84     public static void main(final String[] argsthrows InterruptedException {
85         final Runner<String, BitGene, Double> runner = Runner.of(
86             param -> KNAPSACK(toPopulationSize(param)),
87             param -> limit.byFixedGeneration(toGeneration(param)),
88             TRIAL_METER,
89             args
90         );
91 
92         runner.start();
93         runner.join();
94     }
95 
96 }