001 /*
002 * Java Genetic Algorithm Library (jenetics-3.7.0).
003 * Copyright (c) 2007-2016 Franz Wilhelmstötter
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 *
017 * Author:
018 * Franz Wilhelmstötter (franz.wilhelmstoetter@gmx.at)
019 */
020 package org.jenetics.tool.evaluation;
021
022 import static java.util.Objects.requireNonNull;
023
024 import java.nio.file.Path;
025 import java.nio.file.Paths;
026 import java.util.function.Function;
027 import java.util.function.Predicate;
028 import java.util.function.Supplier;
029
030 import org.jenetics.internal.util.Args;
031
032 import org.jenetics.Gene;
033 import org.jenetics.engine.Engine;
034 import org.jenetics.engine.EvolutionResult;
035 import org.jenetics.tool.trial.TrialMeter;
036
037 /**
038 * @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
039 * @version 3.5
040 * @since 3.5
041 */
042 public class Runner2<
043 P,
044 G extends Gene<?, G>,
045 N extends Number & Comparable<? super N>
046 >
047 extends AbstractRunner<P>
048 {
049
050 private final Engine<G, N> _engine1;
051 private final Function<? super P, Predicate<? super EvolutionResult<G, N>>> _terminator1;
052 private final Engine<G, N> _engine2;
053 private final Function<? super P, Predicate<? super EvolutionResult<G, N>>> _terminator2;
054
055 public Runner2(
056 final Engine<G, N> engine1,
057 final Function<? super P, Predicate<? super EvolutionResult<G, N>>> terminator1,
058 final Engine<G, N> engine2,
059 final Function<? super P, Predicate<? super EvolutionResult<G, N>>> terminator2,
060 final Supplier<TrialMeter<P>> trialMeter,
061 final int sampleCount,
062 final Path resultPath
063 ) {
064 super(trialMeter, sampleCount, resultPath);
065 _engine1 = requireNonNull(engine1);
066 _terminator1 = requireNonNull(terminator1);
067 _engine2 = requireNonNull(engine2);
068 _terminator2 = requireNonNull(terminator2);
069 }
070
071 protected double[] fitness(final P param) {
072 final Predicate<? super EvolutionResult<G, N>> terminator1 =
073 _terminator1.apply(param);
074
075 final long start1 = System.currentTimeMillis();
076 final EvolutionResult<G, N> result1 = _engine1.stream()
077 .limit(terminator1)
078 .collect(EvolutionResult.toBestEvolutionResult());
079 final long end1 = System.currentTimeMillis();
080
081 final Predicate<? super EvolutionResult<G, N>> terminator2 =
082 _terminator2.apply(param);
083
084 final long start2 = System.currentTimeMillis();
085 final EvolutionResult<G, N> result2 = _engine2.stream()
086 .limit(terminator2)
087 .collect(EvolutionResult.toBestEvolutionResult());
088 final long end2 = System.currentTimeMillis();
089
090 return new double[] {
091 result1.getTotalGenerations(),
092 result1.getBestFitness() != null
093 ? result1.getBestFitness().doubleValue()
094 : Double.NEGATIVE_INFINITY,
095 end1 - start1,
096
097 result2.getTotalGenerations(),
098 result2.getBestFitness() != null
099 ? result2.getBestFitness().doubleValue()
100 : Double.NEGATIVE_INFINITY,
101 end2 - start2
102 };
103 }
104
105 public static <P, G extends Gene<?, G>, N extends Number & Comparable<? super N>>
106 Runner2<P, G, N> of(
107 final Engine<G, N> engine1,
108 final Function<? super P, Predicate<? super EvolutionResult<G, N>>> terminator1,
109 final Engine<G, N> engine2,
110 final Function<? super P, Predicate<? super EvolutionResult<G, N>>> terminator2,
111 final Supplier<TrialMeter<P>> trialMeter,
112 final String[] arguments
113 ) {
114 final Args args = Args.of(arguments);
115
116 return new Runner2<>(
117 engine1,
118 terminator1,
119 engine2,
120 terminator2,
121 trialMeter,
122 args.intArg("sample-count")
123 .orElse(50),
124 args.arg("result-file")
125 .map(f -> Paths.get(f))
126 .orElse(Paths.get("trial_meter.xml"))
127 );
128 }
129 }
|