$darkmode
DENOPTIM
GARunner.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2019 Vishwesh Venkatraman <vishwesh.venkatraman@ntnu.no>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Affero General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Affero General Public License for more details.
14 *
15 * You should have received a copy of the GNU Affero General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19package denoptim.programs.denovo;
20
21import java.io.File;
22import java.io.IOException;
23import java.nio.file.Paths;
24import java.util.concurrent.ExecutorService;
25import java.util.concurrent.Executors;
26import java.util.concurrent.Future;
27import java.util.concurrent.TimeUnit;
28
29import denoptim.ga.EvolutionaryAlgorithm;
30import denoptim.ga.ExternalCmdsListener;
31import denoptim.task.ProgramTask;
32
39public class GARunner extends ProgramTask
40{
44 private EvolutionaryAlgorithm ea = null;
45
49 private ExternalCmdsListener ecl = null;
50
54 private ExecutorService executor = null;
55
59 private Future<?> futureWatchers = null;
60
61
62//------------------------------------------------------------------------------
63
69 public GARunner(File configFile, File workDir)
70 {
71 super(configFile,workDir);
72 }
73
74//------------------------------------------------------------------------------
75
76 @Override
77 public void runProgram() throws Throwable
78 {
79 GAParameters settings = new GAParameters();
80 if (workDir != null)
81 {
82 settings.setWorkingDirectory(workDir.getAbsolutePath());
83 }
84
85 settings.readParameterFile(configFilePathName.getAbsolutePath());
86 settings.checkParameters();
87 settings.processParameters();
89 settings.printParameters();
90
91 ecl = new ExternalCmdsListener(Paths.get(settings.getInterfaceDir()),
92 settings.getLogger());
93 executor = Executors.newSingleThreadExecutor();
94 futureWatchers = executor.submit(ecl);
95 executor.shutdown();
96
97 ea = new EvolutionaryAlgorithm(settings, ecl);
98 ea.run();
99
101 stopLogger();
102 }
103
104//------------------------------------------------------------------------------
105
106 protected void handleThrowable()
107 {
108 if (ea != null)
109 {
110 ea.stopRun();
111 }
113 super.handleThrowable();
114 }
115
116//------------------------------------------------------------------------------
117
125 {
126 if (executor != null)
127 {
128 try {
129 executor.awaitTermination(2, TimeUnit.SECONDS);
131 futureWatchers.cancel(true);
132 executor.awaitTermination(1, TimeUnit.SECONDS);
133 } catch (InterruptedException e) {
134 // we'll kill it anyway
135 } catch (IOException e) {
136 // we'll kill it anyway
137 }
138 executor.shutdownNow();
139 executor = null;
140 }
141 }
142
143//------------------------------------------------------------------------------
144
145}
DENOPTIM's evolutionary algorithm.
Service that watches the interface folder (i.e., see GAParameters#interfaceDir) for instructions comi...
Logger startProgramSpecificLogger(String loggerIdentifier)
Starts a logger with the given name.
void readParameterFile(String infile)
Read the parameter TXT file line by line and interpret its content.
Logger getLogger()
Get the name of the program specific logger.
void printParameters()
Print all parameters.
Parameters for genetic algorithm.
void checkParameters()
Evaluate consistency of input parameters.
void processParameters()
Processes currently loaded fields.
void setWorkingDirectory(String pathName)
Programs that runs de novo design by a genetic algorithm.
Definition: GARunner.java:40
Future<?> futureWatchers
Pending tasks of the service listening for commands.
Definition: GARunner.java:59
EvolutionaryAlgorithm ea
The implementation of the evolutionary algorithm we run here.
Definition: GARunner.java:44
ExecutorService executor
Executor of the service that listens for commands.
Definition: GARunner.java:54
void handleThrowable()
Method to handle any Throwable originated from the runProgram() method.
Definition: GARunner.java:106
ExternalCmdsListener ecl
The service that listens for commands from outside the JVM.
Definition: GARunner.java:49
GARunner(File configFile, File workDir)
Creates and configures the program task.
Definition: GARunner.java:69
void stopExternalCmdListener()
Stop the service that waits for instructions from the outside world.
Definition: GARunner.java:124
Task structure for any of the main programs in the denoptim project, such as genetic algorithm and co...
String loggerIdentifier
Identifier of this program's logger.
void stopLogger()
Stops the program-specific logger and releases the lock file on the logfile.
File configFilePathName
File containing configuration parameters for the program task.
File workDir
The file system location where we want to be placed when doing the work.
Definition: Task.java:78