KnapsackPopulationSize.java
01 /*
02  * Java Genetic Algorithm Library (jenetics-3.8.0).
03  * Copyright (c) 2007-2017 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.Math.round;
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.tool.trial.Tuple2;
37 import org.jenetics.util.ISeq;
38 
39 /**
40  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
41  @version 3.6
42  @since 3.6
43  */
44 public class KnapsackPopulationSize {
45 
46     private static final int SIZE = 30;
47 
48     private static final double BASE = pow(10, log10(100)/20.0);
49     private static final double MAX = (long)pow(BASE, SIZE);
50 
51     private static final Params<Tuple2<Long, Integer>> PARAMS = Params.of(
52         "Generation/Population size",
53         IntStream.rangeClosed(1, SIZE)
54             .mapToObj(i -> Tuple2.of(round(max(MAX/i, 1)), i))
55             .collect(ISeq.toISeq())
56     );
57 
58     private static final Supplier<TrialMeter<Tuple2<Long, Integer>>>
59         TRIAL_METER = () -> TrialMeter.of(
60         "Execution time",
61         "Create execution time performance measures",
62         PARAMS,
63         "Generation",
64         "Fitness",
65         "Runtime"
66     );
67 
68     public static void main(final String[] argsthrows InterruptedException {
69         final Runner<Tuple2<Long, Integer>, BitGene, Double> runner = Runner.of(
70             param -> KNAPSACK(param._2),
71             param -> limit.byFixedGeneration(param._1),
72             TRIAL_METER,
73             args
74         );
75 
76         runner.start();
77         runner.join();
78     }
79 
80     private static long toGeneration(final String param) {
81         return Long.parseLong(param.split(Pattern.quote(":"))[0]);
82     }
83 
84     private static int toPopulationSize(final String param) {
85         return Integer.parseInt(param.split(Pattern.quote(":"))[1]);
86     }
87 
88 }