19package denoptim.programs.fitnessevaluator;
22import java.util.ArrayList;
23import java.util.HashMap;
26import java.util.concurrent.ArrayBlockingQueue;
27import java.util.concurrent.Future;
28import java.util.concurrent.RejectedExecutionHandler;
29import java.util.concurrent.ThreadPoolExecutor;
30import java.util.concurrent.TimeUnit;
31import java.util.logging.Level;
33import org.apache.commons.io.FileUtils;
34import org.apache.commons.lang3.time.StopWatch;
35import org.openscience.cdk.interfaces.IAtomContainer;
37import denoptim.constants.DENOPTIMConstants;
38import denoptim.exception.DENOPTIMException;
39import denoptim.graph.DGraph;
40import denoptim.io.DenoptimIO;
41import denoptim.logging.StaticLogger;
42import denoptim.programs.combinatorial.FragSpaceExplorer;
43import denoptim.programs.denovo.GARunner;
44import denoptim.task.FitnessTask;
61 final Map<FitnessTask,Future<Object>>
futures;
71 final ThreadPoolExecutor
tpe;
94 futures =
new HashMap<FitnessTask,Future<Object>>();
98 TimeUnit.NANOSECONDS,
new ArrayBlockingQueue<Runnable>(1));
100 Runtime.getRuntime().addShutdownHook(
new Thread()
109 if (!
tpe.awaitTermination(5, TimeUnit.SECONDS))
114 if (!
tpe.awaitTermination(10, TimeUnit.SECONDS))
119 catch (InterruptedException ie)
125 Thread.currentThread().interrupt();
131 tpe.setRejectedExecutionHandler(
new RejectedExecutionHandler()
134 public void rejectedExecution(Runnable r,
135 ThreadPoolExecutor executor)
140 executor.getQueue().put(r);
142 catch (InterruptedException ex)
144 ex.printStackTrace();
145 String msg =
"EXCEPTION in rejectedExecution.";
171 public void run() throws Exception
174 StopWatch watch =
new StopWatch();
181 if (graphs.size() != iacs.size())
187 tpe.prestartAllCoreThreads();
189 int evaluationCount = 0;
190 for (
int i=0; i<graphs.size(); i++)
192 DGraph graph = graphs.get(i);
193 IAtomContainer iac = iacs.get(i);
212 for (
int i=0; i<graphs.size(); i++)
214 String tmpFileFromProvider = collectiveOutput +
"_" + i;
215 File tmpFile =
new File(tmpFileFromProvider);
216 if (!tmpFile.exists() || !tmpFile.canRead())
218 throw new Error(
"File '" + tmpFileFromProvider +
"' should "
219 +
"have been procuded by fitness provider, but is not "
225 content = content.substring(0, content.length()-1);
227 FileUtils.deleteQuietly(
new File(tmpFileFromProvider));
232 if (evaluationCount>1)
234 msg =
"Overall time: " + watch.toString() +
". " + DENOPTIMConstants.EOL
235 +
"Run " + evaluationCount +
" evaluation" + plural
236 +
" of fitness." + DENOPTIMConstants.EOL
248 List<FitnessTask> completed =
new ArrayList<FitnessTask>();
282 tpe.getQueue().clear();
General set of constants used in DENOPTIM.
static final String EOL
new line character
Container for the list of vertices and the edges that connect them.
Utility methods for input/output.
static ArrayList< IAtomContainer > readSDFFile(String fileName)
Reads a file containing multiple molecules (multiple SD format))
static ArrayList< DGraph > readDENOPTIMGraphsFromFile(File inFile)
Reads a list of <DGraphs from file.
static String readText(String fileName)
Read text from file.
static void writeData(String fileName, String data, boolean append)
Write text-like data file.
Logger class for DENOPTIM.
static final Logger appLogger
String getWorkDirectory()
Gets the pathname to the working directory.
Runs a fitness provider task as defined in the static parameters.
void cleanup()
clean all reference to submitted tasks
void cleanupCompleted()
Removes only tasks that are marked as completed.
final Map< FitnessTask, Future< Object > > futures
Storage of references to the submitted subtasks as Future.
void run()
Create and run the fitness task.
final ThreadPoolExecutor tpe
Asynchronous tasks manager.
FPRunner(FRParameters settings)
Constructor.
int numThreads
Number of parallel fitness evaluations we run.
void stopRun()
Stops all subtasks and shutdown executor.
FRParameters settings
The parameters controlling the.
final List< FitnessTask > submitted
Storage of references to the submitted subtasks.
Parameters controlling execution of FitnessRunner.
long getWallTime()
Returns the maximum number of seconds to wait for the fitness provider to deliver a result.
Task that calls the fitness provider.
Task that assesses the fitness of a given graph.