$darkmode
DENOPTIM
OffspringEvaluationTask.java
Go to the documentation of this file.
1/*
2 * DENOPTIM
3 * Copyright (C) 2019 Vishwesh Venkatraman <vishwesh.venkatraman@ntnu.no> and
4 * Marco Foscato <marco.foscato@uib.no>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Affero General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Affero General Public License for more details.
15 *
16 * You should have received a copy of the GNU Affero General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20package denoptim.ga;
21
22import java.io.File;
23import java.util.logging.Level;
24
25import org.openscience.cdk.CDKConstants;
26
27import denoptim.constants.DENOPTIMConstants;
28import denoptim.exception.DENOPTIMException;
29import denoptim.fitness.FitnessParameters;
30import denoptim.fragspace.FragmentSpace;
31import denoptim.fragspace.FragmentSpaceParameters;
32import denoptim.graph.Candidate;
33import denoptim.graph.DGraph;
34import denoptim.logging.CounterID;
35import denoptim.logging.Monitor;
36import denoptim.molecularmodeling.ThreeDimTreeBuilder;
37import denoptim.programs.RunTimeParameters.ParametersType;
38import denoptim.programs.denovo.GAParameters;
39import denoptim.task.FitnessTask;
40
47{
48 private final String molName;
49 private volatile Population population;
50 private volatile Monitor mnt;
51
52 private final boolean replaceWorstPopMember;
53
58
63
68
69//------------------------------------------------------------------------------
70
72 Candidate offspring, Candidate sibling, String workDir,
73 Population popln, Monitor mnt, String fileUID,
75 {
77 ParametersType.FIT_PARAMS)), offspring);
78 this.gaSettings = gaSettings;
81 {
84 }
85 this.fragSpace = fsParams.getFragmentSpace();
86 this.molName = offspring.getName();
87 this.workDir = new File(workDir);
88 this.fitProvMol = offspring.getChemicalRepresentation();
89 this.population = popln;
90 this.mnt = mnt;
91 this.sibling = sibling;
92 this.replaceWorstPopMember = replaceWorstPopMember;
93
94 result.setName(this.molName);
95 result.setUID(offspring.getUID());
96 result.setSmiles(offspring.getSmiles());
97
98 // Define pathnames to files used/produced by fitness provider
99 fitProvOutFile = this.workDir + SEP + this.molName +
101 fitProvInputFile = this.workDir + SEP + this.molName +
103 fitProvPNGFile = this.workDir + SEP + this.molName +
105 fitProvUIDFile = fileUID;
106 }
107
108//------------------------------------------------------------------------------
109
110 @Override
111 public Object call() throws DENOPTIMException, Exception
112 {
114
115 // Optionally improve the molecular representation, which
116 // is otherwise only given by the collection of building
117 // blocks (not aligned, nor roto-translated)
119 {
123
124 try {
125 DGraph gWithNoRCVs = dGraph.clone();
127
128 // To get a proper molecular representation we need
129 // 1) build a 3d tree
130 // 2) remove RCAs
131 // 3) remove dummy in multi-hapto
132 // 4) remove dummy in linearities
133 // 5) set atom properties that are expected by CDK classes (for
134 // example, the number of implicit atoms).
135 // All this should be done within the TreeBuilder3D and
136 // controlled by flags. Obviously, if we remove all these
137 // functional dummy atoms, then we cannot use them anymore,
138 // So: are there cases where we need to keep them?
139 // We can always rebuild the 3d-tree (with Dummy atoms) if
140 // we need to get it back. Thus, for the moment I do not see
141 // a reason for keeping them in the molecular representation,
142 // but potential down-stream effects have to be evaluated.
144 gWithNoRCVs,true);
145 } catch (Throwable t) {
146 //we have it already from before
147 }
148 }
149 fitProvMol.setProperty(CDKConstants.TITLE, molName);
156 if (dGraph.getLocalMsg() != null)
157 {
160 }
161
162 // Run the fitness provider, whatever that is (internal or external)
163 try
164 {
165 // Note that in here the molecular representation of the offspring
166 // is altered and the original references to fitProvMol will be lost.
168 }
169 catch (Throwable ex)
170 {
172 hasException = true;
173 errMsg = "Exception while running fitness provider";
174 thrownExc = ex;
175 ex.printStackTrace();
176 throw new DENOPTIMException(ex);
177 }
178
179 if (result.getError() != null)
180 {
182 }
183
184 if (result.hasFitness())
185 {
186 boolean addthisToPop = false;
187 boolean isWithinBestPrcentile = false;
188 if (population != null)
189 {
190 synchronized (population)
191 {
192 // Optionally keep the best of sibling
193 if (population.contains(sibling)
195 {
197 {
198 addthisToPop = true;
200 gaSettings.getLogger().log(Level.INFO, "Replacing "
201 + sibling.getName() + " with its sibling "
202 + molName + " in population");
203 }
204 } else {
207 {
209 {
210 addthisToPop = true;
211 } else {
212 gaSettings.getLogger().log(Level.WARNING,
213 "Candidate {0} is worse than the worst "
214 + "population member and does not "
215 + "enter the population.", molName);
216 }
217 } else {
218 addthisToPop = true;
219 gaSettings.getLogger().log(Level.INFO,
220 "Adding {0} to population", molName);
221 }
222 }
223
224 if (addthisToPop)
225 {
228 {
230 }
232 isWithinBestPrcentile = population.isWithinPercentile(
235 }
236 }
237 }
238
241 && isWithinBestPrcentile && addthisToPop)
242 {
246 fitProvMol);
247 }
248 }
249 completed = true;
250 return result;
251 }
252
253//------------------------------------------------------------------------------
254}
General set of constants used in DENOPTIM.
static final String GRAPHTAG
SDF tag containing graph encoding.
static final String INCHIKEYTAG
SDF tag containing the unique identifier of a candidate.
static final String PROVENANCE
SDF tag containing provenance data for a graph.
static final String CANDIDATE2DEXTENSION
Extension of output file with 2D picture of candidate.
static final String GCODETAG
SDF tag containing graph ID.
static final String UNIQUEIDTAG
SDF tag containing the unique identifier of a candidate.
static final String GRAPHJSONTAG
SDF tag containing graph encoding in JSON format.
static final String SMILESTAG
SDF tag containing the SMILES of a candidate.
static final String FITFILENAMEEXTOUT
Ending and extension of output file of external fitness provider.
static final String FITFILENAMEEXTIN
Ending and extension of input file of external fitness provider.
Settings defining the calculation of fitness.
Class defining a space of building blocks.
void addFusedRingsToFragmentLibrary(DGraph graph)
Extracts a system of one or more fused rings and adds them to the fragment space if not already prese...
Parameters defining the fragment space.
Task that calls the fitness provider for an offspring that can become a member of the current populat...
FragmentSpace fragSpace
The fragment space.
Candidate sibling
The sibling of this offspring.
GAParameters gaSettings
Parameters controlling GA experiments.
OffspringEvaluationTask(GAParameters gaSettings, Candidate offspring, Candidate sibling, String workDir, Population popln, Monitor mnt, String fileUID, boolean replaceWorstPopMember)
A collection of candidates.
Definition: Population.java:48
double getMinFitness()
Gets the minimum value of the fitness in this population.
boolean isWithinPercentile(double value, double percentile)
Checks if a given fitness value if within the given percentile of best candidates.
boolean add(Candidate c)
Definition: Population.java:87
Candidate getMinFitnessMember()
Gets the Candidate with minimum value of the fitness in this population.
Candidate remove(int index)
A candidate is the combination of a denoptim graph with molecular representation and may include also...
Definition: Candidate.java:40
boolean hasFitness
Flag signaling the presence of a fitness value associated.
Definition: Candidate.java:89
void setSmiles(String smiles)
Definition: Candidate.java:473
void setUID(String uid)
Definition: Candidate.java:466
void setName(String name)
Definition: Candidate.java:495
IAtomContainer getChemicalRepresentation()
Returns the atom container representing this candidate.
Definition: Candidate.java:389
Container for the list of vertices and the edges that connect them.
Definition: DGraph.java:102
String toJson()
Produces a string that represents this graph and that adheres to the JSON format.
Definition: DGraph.java:6660
String getLocalMsg()
Definition: DGraph.java:279
DGraph clone()
Returns almost "deep-copy" of this graph.
Definition: DGraph.java:3186
void replaceUnusedRCVsWithCapps(FragmentSpace fragSpace)
Removes unused ring-closing vertices.
Definition: DGraph.java:1526
A collection of counters user to count actions taken by the evolutionary algorithm.
Definition: Monitor.java:37
void increase(CounterID cid)
Definition: Monitor.java:149
Tool to build build three-dimensional (3D) tree-like molecular structures from DGraph.
IAtomContainer convertGraphTo3DAtomContainer(DGraph graph)
Created a three-dimensional molecular representation from a given DGraph.
boolean containsParameters(ParametersType type)
RunTimeParameters getParameters(ParametersType type)
Logger getLogger()
Get the name of the program specific logger.
Randomizer getRandomizer()
Returns the current program-specific randomizer.
Parameters for genetic algorithm.
boolean keepBestSibling
Flag controlling if we choose the best sibling out of crossover.
Task that assesses the fitness of a given graph.
DGraph dGraph
The graph representation of the entity to evaluate.
String fitProvInputFile
The file where we store the input to the fitness provider.
void runFitnessProvider()
This method runs the actual evaluation of the fitness, whether that is run internally (i....
String fitProvUIDFile
The file where we store the list of unique identifiers or previously evaluated candidates.
FitnessParameters fitnessSettings
Settings for the calculation of the fitness.
Candidate result
The data structure holding the results of this task.
String fitProvOutFile
The file where we store the final output from the fitness provider.
IAtomContainer fitProvMol
The chemical representation of the entity to evaluate.
String fitProvPNGFile
The file where we store the graphical representation of the candidate (i.e., a picture).
File workDir
The file system location where we want to be placed when doing the work.
Definition: Task.java:78
boolean completed
Flag about completion.
Definition: Task.java:41
String errMsg
Error message produced by any subtask.
Definition: Task.java:56
boolean hasException
Flag about exception.
Definition: Task.java:46
final String SEP
System-dependent file separator.
Definition: Task.java:88
Throwable thrownExc
Exception thrown.
Definition: Task.java:61
Identifier of a counter.
Definition: CounterID.java:29
FS_PARAMS
Parameters pertaining the definition of the fragment space.
FIT_PARAMS
Parameters pertaining the calculation of fitness (i.e., the fitness provider).