Runner.java
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.4
041  */
042 public class Runner<
043     P,
044     extends Gene<?, G>,
045     extends Number &  Comparable<? super N>
046 >
047     extends AbstractRunner<P>
048 {
049 
050     private final Function<? super P, Engine<G, N>> _engine;
051     private final Function<? super P, Predicate<? super EvolutionResult<G, N>>> _terminator;
052 
053     public Runner(
054         final Function<? super P, Engine<G, N>> engine,
055         final Function<? super P, Predicate<? super EvolutionResult<G, N>>> terminator,
056         final Supplier<TrialMeter<P>> trialMeter,
057         final int sampleCount,
058         final Path resultPath
059     ) {
060         super(trialMeter, sampleCount, resultPath);
061         _engine = requireNonNull(engine);
062         _terminator = requireNonNull(terminator);
063     }
064 
065     protected double[] fitness(final P param) {
066         final Predicate<? super EvolutionResult<G, N>> terminator =
067             _terminator.apply(param);
068 
069         final long start = System.currentTimeMillis();
070         final EvolutionResult<G, N> result = _engine.apply(param).stream()
071             .limit(terminator)
072             .collect(EvolutionResult.toBestEvolutionResult());
073         final long end = System.currentTimeMillis();
074 
075         return new double[] {
076             result.getTotalGenerations(),
077             result.getBestFitness() != null
078                 ? result.getBestFitness().doubleValue()
079                 : Double.NEGATIVE_INFINITY,
080             end - start
081         };
082     }
083 
084     public static <P, G extends Gene<?, G>, N extends Number &  Comparable<? super N>>
085     Runner<P, G, N> of(
086         final Function<? super P, Engine<G, N>> engine,
087         final Function<? super P, Predicate<? super EvolutionResult<G, N>>> terminator,
088         final Supplier<TrialMeter<P>> trialMeter,
089         final String[] arguments
090     ) {
091         final Args args = Args.of(arguments);
092 
093         return new Runner<>(
094             engine,
095             terminator,
096             trialMeter,
097             args.intArg("sample-count")
098                 .orElse(50),
099             args.arg("result-file")
100                 .map(f -> Paths.get(f))
101                 .orElse(Paths.get("trial_meter.xml"))
102         );
103     }
104 
105 }