Trial.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.trial;
21 
22 import static java.lang.String.format;
23 import static java.nio.file.Files.exists;
24 import static java.util.Objects.requireNonNull;
25 
26 import java.nio.file.Path;
27 import java.time.LocalDateTime;
28 import java.time.format.DateTimeFormatter;
29 import java.util.function.Function;
30 import java.util.function.Predicate;
31 import java.util.function.Supplier;
32 
33 /**
34  @author <a href="mailto:franz.wilhelmstoetter@gmx.at">Franz Wilhelmstötter</a>
35  @version 3.4
36  @since 3.4
37  */
38 public class Trial<T> implements Runnable {
39 
40     private final Function<T, double[]> _function;
41     private final Supplier<TrialMeter<T>> _trialMeter;
42     private final Predicate<Integer> _stop;
43     private final Path _resultPath;
44 
45     public Trial(
46         final Function<T, double[]> function,
47         final Supplier<TrialMeter<T>> trialMeter,
48         final Predicate<Integer> stop,
49         final Path resultPath
50     ) {
51         _function = requireNonNull(function);
52         _trialMeter = requireNonNull(trialMeter);
53         _stop = requireNonNull(stop);
54         _resultPath = requireNonNull(resultPath);
55     }
56 
57     @Override
58     public void run() {
59         final TrialMeter<T> trialMeter;
60         if (exists(_resultPath)) {
61             trialMeter = TrialMeter.read(_resultPath);
62 
63             info("Continue existing trial: '%s'.", _resultPath.toAbsolutePath());
64             info("    " + trialMeter);
65         else {
66             trialMeter = _trialMeter.get();
67 
68             info("Writing results to '%s'.", _resultPath.toAbsolutePath());
69         }
70 
71         while (!_stop.test(trialMeter.dataSize()) &&
72             !Thread.currentThread().isInterrupted())
73         {
74             trialMeter.sample(param -> {
75                 //trialMeter.write(_resultPath);
76                 info(trialMeter.toString());
77 
78                 return _function.apply(param);
79             });
80 
81             trialMeter.write(_resultPath);
82         }
83     }
84 
85     private static final DateTimeFormatter FORMATTER =
86         DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
87 
88     public static void info(final String pattern, final Object... params) {
89         final LocalDateTime time = LocalDateTime.now();
90         System.out.println(
91             "" + FORMATTER.format(time" - " + format(pattern, params)
92         );
93         System.out.flush();
94     }
95 
96 }