23import java.util.ArrayList;
24import java.util.LinkedHashMap;
27import java.util.Map.Entry;
28import java.util.TreeMap;
29import java.util.logging.Level;
30import java.util.stream.Collectors;
32import org.apache.http.TruncatedChunkException;
33import org.openscience.cdk.interfaces.IAtomContainer;
34import org.paukov.combinatorics3.Generator;
36import denoptim.constants.DENOPTIMConstants;
37import denoptim.exception.DENOPTIMException;
38import denoptim.fragspace.APMapFinder;
39import denoptim.fragspace.FragmentSpace;
40import denoptim.fragspace.FragmentSpaceParameters;
41import denoptim.fragspace.GraphLinkFinder;
42import denoptim.fragspace.IdFragmentAndAP;
43import denoptim.graph.APClass;
44import denoptim.graph.APMapping;
45import denoptim.graph.AttachmentPoint;
46import denoptim.graph.DGraph;
47import denoptim.graph.Edge;
48import denoptim.graph.Ring;
49import denoptim.graph.SymmetricAPs;
50import denoptim.graph.SymmetricVertexes;
51import denoptim.graph.Template;
52import denoptim.graph.Template.ContractLevel;
53import denoptim.graph.Vertex;
54import denoptim.graph.Vertex.BBType;
55import denoptim.graph.rings.ChainLink;
56import denoptim.graph.rings.ClosableChain;
57import denoptim.graph.rings.PathSubGraph;
58import denoptim.graph.rings.RandomCombOfRingsIterator;
59import denoptim.graph.rings.RingClosingAttractor;
60import denoptim.graph.rings.RingClosureParameters;
61import denoptim.io.DenoptimIO;
62import denoptim.logging.CounterID;
63import denoptim.logging.Monitor;
64import denoptim.molecularmodeling.ThreeDimTreeBuilder;
65import denoptim.programs.RunTimeParameters.ParametersType;
66import denoptim.programs.denovo.GAParameters;
67import denoptim.utils.CrossoverType;
68import denoptim.utils.GraphUtils;
69import denoptim.utils.MutationType;
70import denoptim.utils.Randomizer;
96 int maxSizeXoverSubGraph)
102 List<Vertex[]> compatibleVrtxPairs =
new ArrayList<Vertex[]>();
103 for (
Edge eA : graphA.getEdgeList())
105 Vertex vA = eA.getTrgAP().getOwner();
110 for (
Edge eB : graphB.getEdgeList())
112 Vertex vB = eB.getTrgAP().getOwner();
121 compatibleVrtxPairs.add(pair);
129 ArrayList<XoverSite> sites =
new ArrayList<XoverSite>();
130 for (
Vertex[] pair : compatibleVrtxPairs)
138 List<Vertex> descendantsA =
new ArrayList<Vertex>();
140 List<Vertex> descendantsB =
new ArrayList<Vertex>();
155 List<Vertex> branchOnVA =
new ArrayList<Vertex>();
157 branchOnVA.addAll(descendantsA);
158 List<Vertex> branchOnVB =
new ArrayList<Vertex>();
160 branchOnVB.addAll(descendantsB);
174 List<Vertex[]> usablePairs =
new ArrayList<Vertex[]>();
189 for (
Vertex[] otherPair : compatibleVrtxPairs)
193 Vertex nextToEndOnA = otherPair[0];
194 Vertex nextToEndOnB = otherPair[1];
197 if (endOnA==
null || endOnB==
null)
201 if (!descendantsA.contains(endOnA) && endOnA!=vA
202 || !descendantsB.contains(endOnB) && endOnB!=vB)
223 if (!usablePairs.contains(pairOfEnds))
224 usablePairs.add(pairOfEnds);
228 TreeMap<String,List<Vertex[]>> sitesByBranchIdA =
229 new TreeMap<String,List<Vertex[]>>();
230 TreeMap<String,List<Vertex[]>> sitesByBranchIdB =
231 new TreeMap<String,List<Vertex[]>>();
232 for (
Vertex[] pp : usablePairs)
236 if (sitesByBranchIdA.containsKey(branchIdA))
238 sitesByBranchIdA.get(branchIdA).add(pp);
240 ArrayList<Vertex[]> lst =
new ArrayList<Vertex[]>();
242 sitesByBranchIdA.put(branchIdA, lst);
244 if (sitesByBranchIdB.containsKey(branchIdB))
246 sitesByBranchIdB.get(branchIdB).add(pp);
248 ArrayList<Vertex[]> lst =
new ArrayList<Vertex[]>();
250 sitesByBranchIdB.put(branchIdB, lst);
256 TreeMap<String,List<Vertex[]>> fewestBranchesSide =
null;
257 if (sitesByBranchIdA.size() <= sitesByBranchIdB.size())
258 fewestBranchesSide = sitesByBranchIdA;
260 fewestBranchesSide = sitesByBranchIdB;
263 for (List<
Vertex[]> val : fewestBranchesSide.values())
264 val.add(
new Vertex[]{
null,
null});
269 List<List<Vertex[]>> preCombsOfEnds = Generator.cartesianProduct(
270 fewestBranchesSide.values())
273 .collect(Collectors.<List<
Vertex[]>>toList());
277 List<List<Vertex[]>> combsOfEnds =
new ArrayList<List<Vertex[]>>();
278 for (List<
Vertex[]> comb : preCombsOfEnds)
280 List<Vertex[]> nullPurgedComb =
new ArrayList<Vertex[]>();
281 for (
Vertex[] inPair : comb)
283 if (inPair[0]!=
null && inPair[1]!=
null)
284 nullPurgedComb.add(inPair);
288 if (nullPurgedComb.size()>0)
289 combsOfEnds.add(nullPurgedComb);
312 for (
Vertex vA : graphA.getVertexList())
321 for (
Vertex vB : graphB.getVertexList())
332 maxSizeXoverSubGraph))
334 if (!sites.contains(xos))
357 List<
Vertex[]> cominationOfEnds,
362 if (cominationOfEnds.size()==0)
383 Generator.permutation(cominationOfEnds)
406 List<
Vertex[]> chosenSequenceOfEndpoints,
415 boolean exclude =
false;
416 for (
Vertex[] pairA : chosenSequenceOfEndpoints)
418 for (
Vertex[] pairB : chosenSequenceOfEndpoints)
423 if (pairA[0]==pairB[0] || pairA[1]==pairB[1])
435 List<Vertex> subGraphEndInA =
new ArrayList<Vertex>();
436 List<Vertex> subGraphEndInB =
new ArrayList<Vertex>();
437 List<Vertex> alreadyIncludedFromA =
new ArrayList<Vertex>();
438 List<Vertex> alreadyIncludedFromB =
new ArrayList<Vertex>();
439 for (
Vertex[] otherPair : chosenSequenceOfEndpoints)
441 Vertex endOnA = otherPair[0];
442 Vertex endOnB = otherPair[1];
445 if (alreadyIncludedFromA.contains(endOnA)
446 || alreadyIncludedFromB.contains(endOnB))
451 subGraphEndInA.add(endOnA);
452 subGraphEndInB.add(endOnB);
456 ArrayList<Vertex> subGraphA =
new ArrayList<Vertex>();
458 if (!subGraphEndInA.contains(vA))
461 ArrayList<Vertex> subGraphB =
new ArrayList<Vertex>();
463 if (!subGraphEndInB.contains(vB))
467 if (subGraphA.size()>1 && subGraphB.size()>1)
474 if (subGraphA.get(0).sameAs(subGraphB.get(0),
new StringBuilder()))
494 List<Vertex> subGraphA,
496 List<XoverSite> collector)
498 DGraph gOwnerA = subGraphA.get(0).getGraphOwner();
499 DGraph gOwnerB = subGraphB.get(0).getGraphOwner();
511 if (allAPsA.size() < needyAPsB.size()
512 || allAPsB.size() < needyAPsA.size())
529 && jacketTmplB==
null)
532 needyAPsB, xoverType);
533 if (!collector.contains(xos))
544 for (
Vertex v : subGraphA)
547 for (
Vertex v : subGraphB)
568 allAPsA, needyAPsA, allAPsB, needyAPsB,
576 subGraphB, needyAPsB, xoverType);
577 if (!collector.contains(xos))
630 DGraph graph = vertex.getGraphOwner();
678 DGraph graph = vertex.getGraphOwner();
713 return extendLink(vertex, chosenAPId, -1 , mnt, fragSpace);
750 +vertex+
" has no edge user.");
755 +
"vertex (AP "+chosenAPId+
" of vertex "+vertex+
").");
757 return extendLink(e, chosenNewVrtxId, mnt, fragSpace);
796 LinkedHashMap<AttachmentPoint,Integer> apMap =
797 new LinkedHashMap<AttachmentPoint,Integer>();
798 for (Entry<AttachmentPoint, AttachmentPoint> e :
801 apMap.put(e.getKey(), e.getValue().getIndexInOwner());
804 DGraph graph = edge.getSrcAP().getOwner().getGraphOwner();
841 boolean force,
int chosenVrtxIdx,
int chosenApId,
844 DGraph g = vertex.getGraphOwner();
847 Edge e = vertex.getEdgeToParent();
850 String msg =
"Program Bug in substituteFragment: Unable to locate "
851 +
"parent edge for vertex "+vertex+
" in graph "+g;
852 settings.getLogger().log(Level.SEVERE, msg);
867 return extendGraph(parentVrt,
false,symmetry,force,chosenVrtxIdx,
868 chosenApId, settings);
884 long vid = vertex.getVertexId();
885 DGraph molGraph = vertex.getGraphOwner();
889 List<Vertex> toRemove =
new ArrayList<Vertex>();
924 long vid = vertex.getVertexId();
925 DGraph molGraph = vertex.getGraphOwner();
929 List<Vertex> toRemove =
new ArrayList<Vertex>();
933 if (!v.getMutationTypes(
new ArrayList<MutationType>())
980 vertex.getGraphOwner().removeCappingGroups();
982 List<AttachmentPoint> freeeAPs = vertex.getFreeAPThroughout();
983 if (freeeAPs.size()==0)
988 DGraph originalGraph = vertex.getGraphOwner();
995 List<Ring> setOfRingsOnTmpGraph =
null;
998 APClass apc = srcAP.getAPClass();
1001 if (!fragSpace.getRCCompatibilityMatrix().containsKey(apc))
1005 List<APClass> rcTrgAPCs = fragSpace.getRCCompatibilityMatrix().get(
1009 Vertex rcvOnSrcAP =
null;
1010 List<Vertex> candidateRCVs = fragSpace.getRCVsForAPClass(apc);
1011 boolean rcvIsChosenArbitrarily =
false;
1012 if (candidateRCVs.size()>0)
1016 rcvIsChosenArbitrarily =
true;
1017 rcvOnSrcAP = fragSpace.getPolarizedRCV(
true);
1021 List<Vertex> rcvAddedToGraph =
new ArrayList<Vertex>();
1023 rcvAddedToGraph.add(rcvOnSrcAP);
1028 for (
int i=0; i<20; i++)
1031 List<AttachmentPoint> apsToTry =
1033 int numberOfAttempts = apsToTry.size();
1035 for (
int iap=0; iap<numberOfAttempts; iap++)
1038 if (rcTrgAPCs.contains(candidate.
getAPClass()))
1043 apsToTry.remove(trgAP);
1052 Vertex rcvOnTrgAP =
null;
1053 if (rcvIsChosenArbitrarily)
1055 rcvOnTrgAP = fragSpace.getPolarizedRCV(
false);
1057 List<Vertex> candRCVs = fragSpace.getRCVsForAPClass(
1059 if (candRCVs.size()>0)
1063 List<Vertex> keptRCVs =
new ArrayList<Vertex>();
1064 for (
Vertex rcv : candRCVs)
1066 if (requiredAPC.
equals(rcv.getAP(0).getAPClass()))
1070 if (rcvOnTrgAP==
null)
1085 rcvAddedToGraph.add(rcvOnTrgAP);
1087 if (rcvAddedToGraph.size() < 2)
1096 settings.getLogger(), rng);
1102 settings.getMaxRingsAddedByMutation(),
1103 fragSpace, rcParams);
1109 setOfRingsOnTmpGraph = rCombIter.
next();
1112 if (setOfRingsOnTmpGraph.size()>0)
1116 for (
Vertex toRemove : rcvAddedToGraph)
1119 if (setOfRingsOnTmpGraph==
null || setOfRingsOnTmpGraph.size()==0)
1126 boolean done =
false;
1127 for (
Ring rOnTmp : setOfRingsOnTmpGraph)
1131 tmpGraph.
indexOf(rOnTmp.getHeadVertex().getParent()));
1133 rOnTmp.getHeadVertex().getEdgeToParent().getSrcAP()
1134 .getIndexInOwner());
1143 tmpGraph.
indexOf(rOnTmp.getTailVertex().getParent()));
1145 rOnTmp.getTailVertex().getEdgeToParent().getSrcAP()
1146 .getIndexInOwner());
1154 originalGraph.
addRing(headRCV, tailRCV);
1159 vertex.getGraphOwner().addCappingGroups(fragSpace);
1182 boolean symmetryOnAps,
1186 return extendGraph(curVertex, extend, symmetryOnAps,
false, -1, -1,
1217 boolean symmetryOnAps,
1240 boolean status =
false;
1243 if (!curVrtx.hasFreeAP())
1248 DGraph molGraph = curVrtx.getGraphOwner();
1249 int lvl = molGraph.
getLevel(curVrtx);
1251 ArrayList<Long> addedVertices =
new ArrayList<>();
1253 List<AttachmentPoint> lstDaps = curVrtx.getAttachmentPoints();
1254 List<AttachmentPoint> toDoAPs =
new ArrayList<AttachmentPoint>();
1255 toDoAPs.addAll(lstDaps);
1256 for (
int i=0; i<lstDaps.size(); i++)
1277 boolean allowOnlyRingClosure =
false;
1289 double extendGraphProb = molSizeProb * byLevelProb * crowdingProb;
1290 boolean fgrow = settings.getRandomizer().nextBoolean(
1295 && settings.getRandomizer().nextBoolean(byLevelProb
1298 allowOnlyRingClosure =
true;
1306 if (!allowOnlyRingClosure
1311 apId, molGraph, addedVertices, settings);
1320 if (allowOnlyRingClosure)
1345 if ((curVrtx.getGraphOwner().getHeavyAtomsCount() +
1355 settings.getSymmetryProbability(),
1358 if (curVrtx.getSymmetricAPs(ap).size()!=0
1359 && (cpOnSymAPs || symmetryOnAps)
1360 && !allowOnlyRingClosure)
1362 symAPs.addAll(curVrtx.getSymmetricAPs(ap));
1365 boolean allOnSameSrc =
true;
1368 if (!symAP.hasSameSrcAtom(ap))
1370 allOnSameSrc =
false;
1387 crowdedness = crowdedness + 1;
1391 double shot = settings.getRandomizer().nextDouble();
1400 crowdedness, settings);
1402 if (shot > crowdProb)
1406 crowdedness = crowdedness + 1;
1426 symVerts.
add(curVrtx);
1430 if ((curVrtx.getGraphOwner().getHeavyAtomsCount() +
1432 * symAPs.size()) > maxHeavyAtoms)
1438 List<AttachmentPoint> allAPsFromSymVerts =
new ArrayList<>();
1439 for (
Vertex symVrt : symVerts)
1444 apOnVrt.getIndexInOwner());
1448 if (apOnVrt.sameAs(apOnSymVrt)
1450 && !symAPs.contains(apOnSymVrt))
1452 allAPsFromSymVerts.add(apOnSymVrt);
1456 symAPs.addAll(allAPsFromSymVerts);
1464 if (!symAP.isAvailable())
1480 addedVertices.add(newVrtId);
1481 newSymSetOfVertices.
add(fragVertex);
1485 if (newSymSetOfVertices.size() > 1)
1494 for (
int i=0; i<addedVertices.size(); i++)
1496 long vid = addedVertices.get(i);
1502 if (addedVertices.size() > 0)
1547 int dapidx,
int chosenVrtxIdx,
int chosenApId,
1550 List<AttachmentPoint> lstDaps = curVertex.getAttachmentPoints();
1556 if (!fragSpace.useAPclassBasedApproach())
1558 int fid = fragSpace.getRandomizer().nextInt(
1559 fragSpace.getFragmentLibrary().size());
1564 List<IdFragmentAndAP> candidates =
1565 fragSpace.getFragAPsCompatibleWithClass(
1567 if (candidates.size() > 0)
1569 if (chosenVrtxIdx>-1 && chosenApId>-1)
1575 res = fragSpace.getRandomizer().randomlyChooseOne(candidates);
1601 List<Vertex> rcvs = fragSpace.getRCVs();
1603 if (!fragSpace.useAPclassBasedApproach())
1607 List<Vertex> candidates = fragSpace.getRCVsForAPClass(
1609 if (candidates.size() > 0)
1629 boolean res =
false;
1639 curVertex, dapidx, molGraph);
1650 int numCands = lscFfCc.size();
1653 int chosenId = settings.getRandomizer().nextInt(numCands);
1655 ArrayList<Integer> newFragIds = chosenFfCc.
getFragIDs();
1656 int molIdNewFrag = newFragIds.get(0);
1658 int dapNewFrag = newFragIds.get(2);
1659 if (molIdNewFrag != -1)
1663 newvid, molIdNewFrag, typeNewFrag,
1666 molGraph.appendVertexOnAP(curVertex.getAP(dapidx),
1667 newVrtx.
getAP(dapNewFrag));
1671 addedVertices.add(newvid);
1673 molGraph.getClosableChains().removeAll(
1675 APClass apc = curVertex.getAttachmentPoints().get(
1676 dapidx).getAPClass();
1679 settings.getSymmetryProbability(),
1688 String msg =
"BUG: Incorrect vertex num. Contact author.";
1689 settings.getLogger().log(Level.SEVERE, msg);
1765 ArrayList<FragForClosabChains> lstChosenFfCc =
1766 new ArrayList<FragForClosabChains>();
1768 if (molGraph.getClosableChains().size() == 0)
1770 return lstChosenFfCc;
1777 int posInCc = cc.involvesVertex(curVertex);
1792 if (cc.getSize() > (posInCc+1))
1795 ChainLink nextChainLink = cc.getLink(posInCc+1);
1796 nfid = nextChainLink.
getIdx();
1808 if ((posInCc-1) >= 0)
1811 ChainLink nextChainLink = cc.getLink(posInCc-1);
1812 nfid = nextChainLink.
getIdx();
1823 ArrayList<Integer> eligibleFrgId =
new ArrayList<Integer>();
1824 eligibleFrgId.add(nfid);
1825 eligibleFrgId.add(nfty.
toOldInt());
1826 eligibleFrgId.add(nfap);
1827 boolean found =
false;
1830 int fidA = ffcc.getFragIDs().get(0);
1832 int fapA = ffcc.getFragIDs().get(2);
1833 if (nfid==fidA && nfty==ftyA && nfap==fapA)
1836 ffcc.getCompatibleCC().add(cc);
1840 ffcc.getIncompatibleCC().add(cc);
1845 ArrayList<ClosableChain> compatChains =
1846 new ArrayList<ClosableChain>();
1847 ArrayList<ClosableChain> incompatChains =
1848 new ArrayList<ClosableChain>();
1851 incompatChains.addAll(otherFfCc.getCompatibleCC());
1859 lstChosenFfCc.add(newChosenCc);
1866 Edge edge = molGraph.getEdgeWithParent(
1867 curVertex.getVertexId());
1874 int posInCc = cc.involvesVertexAndAP(curVertex, dapidx, chidAp);
1888 List<Integer> altertnativeDirections =
new ArrayList<>();
1889 altertnativeDirections.add(-1);
1890 altertnativeDirections.add(+1);
1891 for (
int altDir : altertnativeDirections)
1893 ChainLink parentLink = cc.getLink(posInCc + altDir);
1894 int pLnkId = parentLink.
getIdx();
1907 if (pLnkId==prntId && pLnkTyp==prntTyp && pLnkAp == prntAp &&
1910 if (cc.getSize() > (posInCc+altDir)
1911 && (posInCc+altDir) >= 0)
1913 ChainLink nextChainLink = cc.getLink(posInCc + altDir);
1914 nfid = nextChainLink.
getIdx();
1931 ArrayList<Integer> eligibleFrgId =
new ArrayList<Integer>();
1932 eligibleFrgId.add(nfid);
1933 eligibleFrgId.add(nfty.
toOldInt());
1934 eligibleFrgId.add(nfap);
1935 boolean found =
false;
1938 int fidA = ffcc.getFragIDs().get(0);
1940 int fapA = ffcc.getFragIDs().get(2);
1941 if (nfid==fidA && nfty==ftyA && nfap==fapA)
1944 ffcc.getCompatibleCC().add(cc);
1948 ffcc.getIncompatibleCC().add(cc);
1953 ArrayList<ClosableChain> compatChains =
1954 new ArrayList<ClosableChain>();
1955 ArrayList<ClosableChain> incompatChains =
1956 new ArrayList<ClosableChain>();
1959 incompatChains.addAll(otherFfCc.getCompatibleCC());
1966 lstChosenFfCc.add(newChosenCc);
1970 return lstChosenFfCc;
1988 DGraph gA = site.getA().get(0).getGraphOwner();
1989 DGraph gB = site.getB().get(0).getGraphOwner();
1992 List<AttachmentPoint> allAPsOnA = gA.
getSubgraphAPs(site.getA());
1993 List<AttachmentPoint> allAPsOnB = gB.
getSubgraphAPs(site.getB());
1997 List<AttachmentPoint> needyAPsOnA = site.getAPsNeedingMappingA();
1998 List<AttachmentPoint> needyAPsOnB = site.getAPsNeedingMappingB();
2005 if (!ap.isSrcInUserThroughout())
2014 if (!ap.isSrcInUserThroughout())
2021 if (apToParentA==
null || apToParentB==
null)
2024 +
"point connecting a subgraph to the rest of the graph. "
2025 +
"This violates assumption that crossover does not "
2026 +
"involve scaffold or vertexes without parent.");
2039 fixedRootAPs.put(apToParentA, apToParentB);
2045 allAPsOnA, needyAPsOnA,
2046 allAPsOnB, needyAPsOnB, fixedRootAPs,
2065 LinkedHashMap<AttachmentPoint,AttachmentPoint>
2066 apMapA =
new LinkedHashMap<AttachmentPoint,AttachmentPoint>();
2067 LinkedHashMap<AttachmentPoint,AttachmentPoint>
2068 apMapB =
new LinkedHashMap<AttachmentPoint,AttachmentPoint>();
2083 apMapA.put(apOnA, apOnSubGraphB);
2084 apMapB.put(apOnB, apOnSubGraphA);
2111 double symmetryProbability,
Randomizer randomizer)
2144 List<Vertex> mutable = graph.getMutableSites(
2145 settings.getExcludedMutationTypes());
2146 if (mutable.size() == 0)
2149 String msg =
"Graph has no mutable site. Mutation aborted.";
2150 settings.getLogger().info(msg);
2153 boolean doneMutation =
true;
2155 settings.getMultiSiteMutationWeights(),
2156 settings.getRandomizer().nextDouble());
2157 for (
int i=0; i<numberOfMutations; i++)
2161 mutable = graph.getMutableSites(
2162 settings.getExcludedMutationTypes());
2165 Vertex v = settings.getRandomizer().randomlyChooseOne(mutable);
2170 return doneMutation;
2191 List<MutationType> mTypes = vertex.getMutationTypes(
2192 settings.getExcludedMutationTypes());
2193 if (mTypes.size() == 0)
2197 MutationType mType = settings.getRandomizer().randomlyChooseOne(mTypes);
2219 int pos = vertex.getGraphOwner().
indexOf(vertex);
2223 }
catch (IllegalArgumentException|NullPointerException e)
2225 String debugFile =
"failedMutation_" + mType
2226 +
"_" + vertex.getVertexId() +
"(" + pos +
")_"
2227 + settings.timeStamp +
".sdf";
2229 settings.getLogger(), settings.getRandomizer());
2230 settings.getLogger().warning(
"Fatal exception while performing "
2231 +
"mutation. See file '" + debugFile +
"' to reproduce the "
2276 DGraph graph = vertex.getGraphOwner();
2280 settings.getLogger().info(
"Vertex has no owner - "
2281 +
"Mutation aborted");
2284 if (!vertex.getMutationTypes(settings.getExcludedMutationTypes())
2288 settings.getLogger().info(
"Vertex does not allow mutation type "
2289 +
"'" + mType +
"' - Mutation aborted");
2294 int positionOfVertex = graph.
indexOf(vertex);
2300 +
" (" + positionOfVertex +
")";
2303 boolean done =
false;
2307 done =
rebuildBranch(vertex, force, chosenVrtxIdx, chosenApId,
2340 List<Integer> candidates =
new ArrayList<Integer>();
2343 candidates.add(c.getEdgeToParent().getSrcAP()
2344 .getIndexInOwner());
2346 if (candidates.size() == 0)
2352 chosenApId = settings.getRandomizer().randomlyChooseOne(
2355 done =
extendLink(vertex, chosenApId, chosenVrtxIdx, mnt,
2370 vertex.getGraphOwner().removeCappingGroupsOn(vertex);
2371 done =
extendGraph(vertex,
false,
false, force, chosenVrtxIdx,
2372 chosenApId, settings);
2384 String msg =
"Mutation '" + mType.toString() +
"' on vertex "
2385 + vertex.toString() +
" (position " + positionOfVertex
2386 +
" in graph " + graphId+
"): ";
2398 msg = msg +
"unsuccessful";
2400 settings.getLogger().info(msg);
General set of constants used in DENOPTIM.
static final Object STOREDVID
Key of the property remembering vertex IDs.
An utility class to encapsulate the search for an AttachmentPoint-AttachmentPoint mapping.
APMapping getChosenAPMapping()
Returns the AttachmentPoint-AttachmentPoint mapping chosen among the possible mappings.
boolean foundMapping()
Returns true if any mapping has been found.
Class defining a space of building blocks.
boolean imposeSymmetryOnAPsOfClass(APClass apClass)
Checks if the symmetry settings impose use of symmetry on attachment points of the given AP class.
Parameters defining the fragment space.
FragmentSpace getFragmentSpace()
An utility class to encapsulate the search for vertexes that satisfy constraints.
Vertex getChosenAlternativeLink()
Returns the vertex chosen as alternative.
APMapping getChosenAPMapping()
Returns the AP mapping that allows the usage of the vertex chosen either as alternative to be install...
boolean foundAlternativeLink()
Data structure containing information that identifies a single AP of a vertex/fragment.
Helper methods for the genetic algorithm.
static double getGrowthByLevelProbability(int level, GAParameters settings)
Calculates the probability of adding a fragment to the given level.
static int chooseNumberOfSitesToMutate(double[] multiSiteMutationProb, double hit)
Takes a decision on how many sites to mutate on a candidate.
static double getCrowdingProbability(AttachmentPoint ap, GAParameters settings)
Calculated the probability of using and attachment point rooted on an atom that is holding other atta...
static int getCrowdedness(AttachmentPoint ap)
Calculate the current crowdedness of the given attachment point.
static double getMolSizeProbability(DGraph graph, GAParameters settings)
Calculated the probability of extending a graph based on the current size of the molecular representa...
Private class representing a selected closable chain of fragments.
ArrayList< ClosableChain > incompatChains
FragForClosabChains(ArrayList< ClosableChain > compatChains, ArrayList< ClosableChain > incompatChains, ArrayList< Integer > fragIds)
ArrayList< Integer > fragIds
ArrayList< ClosableChain > compatChains
ArrayList< ClosableChain > getCompatibleCC()
ArrayList< ClosableChain > getIncompatibleCC()
void addCompatibleCC(ClosableChain icc)
ArrayList< Integer > getFragIDs()
Collection of operators meant to alter graphs and associated utilities.
static IdFragmentAndAP getFrgApForSrcAp(Vertex curVertex, int dapidx, FragmentSpace fragSpace)
Select a compatible fragment for the given attachment point.
static boolean performMutation(Vertex vertex, MutationType mType, boolean force, int chosenVrtxIdx, int chosenApId, Monitor mnt, GAParameters settings)
Mutates the given vertex according to the given mutation type, if possible.
static boolean extendGraph(Vertex curVrtx, boolean extend, boolean symmetryOnAps, boolean force, int chosenVrtxIdx, int chosenApId, GAParameters settings)
function that will keep extending the graph.
static boolean extendLink(Vertex vertex, int chosenAPId, Monitor mnt, FragmentSpace fragSpace)
Inserts a vertex in between two connected vertexes that are identified by the one vertex holding the ...
static List< XoverSite > locateCompatibleXOverPoints(DGraph graphA, DGraph graphB, FragmentSpace fragSpace, int maxSizeXoverSubGraph)
Identify crossover sites, i.e., subgraphs that can be swapped between two graphs (i....
static boolean applySymmetry(boolean apclassImposed, double symmetryProbability, Randomizer randomizer)
Decides whether to apply constitutional symmetry or not.
static void checkAndAddXoverSites(FragmentSpace fragSpace, List< Vertex > subGraphA, List< Vertex > subGraphB, CrossoverType xoverType, List< XoverSite > collector)
Here we check that the given subgraphs have a mapping that allows to swap them, and full fill any oth...
static boolean performMutation(Vertex vertex, MutationType mType, Monitor mnt, GAParameters settings)
Mutates the given vertex according to the given mutation type, if possible.
static boolean substituteLink(Vertex vertex, int chosenVrtxIdx, Monitor mnt, FragmentSpace fragSpace)
Substitutes a vertex while keeping its surrounding.
static boolean isCrossoverPossible(Edge eA, Edge eB, FragmentSpace fragSpace)
Evaluate AP class-compatibility of a pair of edges with respect to crossover.
static void processCombinationOfEndPoints(Vertex[] pair, List< Vertex[]> cominationOfEnds, List< XoverSite > collector, FragmentSpace fragSpace)
Given a pair of seed vertexes that define where a pair of subgraphs stars in the corresponding origin...
static boolean attachFragmentInClosableChain(Vertex curVertex, int dapidx, DGraph molGraph, ArrayList< Long > addedVertices, GAParameters settings)
static void processPermutationOfEndPoints(Vertex[] pair, List< Vertex[]> chosenSequenceOfEndpoints, List< XoverSite > collector, FragmentSpace fragSpace)
Given a pair of seed vertexes that define where a pair of subgraphs stars in the corresponding origin...
static boolean extendGraph(Vertex curVertex, boolean extend, boolean symmetryOnAps, GAParameters settings)
function that will keep extending the graph according to the growth/substitution probability.
static ArrayList< FragForClosabChains > getFragmentForClosableChain(Vertex curVertex, int dapidx, DGraph molGraph)
Method to select fragments that increase the likeliness of generating closable chains.
static boolean performMutation(Vertex vertex, Monitor mnt, GAParameters settings)
Tries to do mutate the given vertex.
static IdFragmentAndAP getFrgApForSrcAp(Vertex curVertex, int dapidx, int chosenVrtxIdx, int chosenApId, FragmentSpace fragSpace)
Select a compatible fragment for the given attachment point.
static IdFragmentAndAP getRCVForSrcAp(Vertex curVertex, int dapidx, FragmentSpace fragSpace)
Select a compatible ring-closing vertex for the given attachment point.
static boolean deleteFragment(Vertex vertex)
Deletion mutation removes the vertex and also the symmetric partners on its parent.
static boolean rebuildBranch(Vertex vertex, boolean force, int chosenVrtxIdx, int chosenApId, GAParameters settings)
Substitutes a vertex and any child branch.
static boolean deleteLink(Vertex vertex, int chosenVrtxIdx, Monitor mnt, FragmentSpace fragSpace)
Removes a vertex while merging as many of the child branches into the parent vertex.
static boolean extendLink(Vertex vertex, int chosenAPId, int chosenNewVrtxId, Monitor mnt, FragmentSpace fragSpace)
Inserts a vertex in between two connected vertexes that are identified by the one vertex holding the ...
static boolean performMutation(DGraph graph, Monitor mnt, GAParameters settings)
Tries to do mutate the given graph.
static boolean addRing(Vertex vertex, Monitor mnt, boolean force, FragmentSpace fragSpace, GAParameters settings)
Tries to use any free AP of the given vertex to close ring in the graph by adding a chord.
static boolean extendLink(Edge edge, int chosenBBIdx, Monitor mnt, FragmentSpace fragSpace)
Replace an edge with two edges with a new vertex in between, thus inserting a vertex in between two d...
static boolean deleteChain(Vertex vertex, Monitor mnt, FragmentSpace fragSpace)
Deletes the given vertex and all other vertexes that are not connected to more than 2 non-capping gro...
static boolean performCrossover(XoverSite site, FragmentSpace fragSpace)
Performs the crossover that swaps the two subgraphs defining the given XoverSite.
This class collects the data identifying the subgraphs that would be swapped by a crossover event.
boolean isCPMapCompatibleWith(APClass other, FragmentSpace fragSpace)
Check compatibility as defined in the compatibility matrix considering this AP as source and the othe...
Class representing a mapping between attachment points (APs).
LinkedHashMap< Integer, Integer > toIntMappig()
Produces an index-based version of this mapping where each index represents the attachment point as i...
An attachment point (AP) is a possibility to attach a Vertex onto the vertex holding the AP (i....
APClass getAPClass()
Returns the Attachment Point class.
boolean isAvailable()
Check availability of this attachment point.
Edge getEdgeUserThroughout()
Gets the edge that is using this AP, or null if no edge is using this AP.
Container for the list of vertices and the edges that connect them.
boolean removeVertexAndWeld(Vertex vertex, FragmentSpace fragSpace)
Remove a given vertex belonging to this graph and re-connects the resulting graph branches as much as...
boolean replaceVertex(Vertex vertex, int bbId, BBType bbt, LinkedHashMap< Integer, Integer > apIdMap, FragmentSpace fragSpace)
Replaced a given vertex belonging to this graph with a new vertex generated specifically for this pur...
Vertex getVertexWithId(long vid)
Searches for a vertex with the given identifier.
DGraph extractSubgraph(int index)
Creates a new graph that corresponds to the subgraph of this graph when exploring the spanning tree f...
boolean isIsomorphicTo(DGraph other)
Checks if this graph is "DENOPTIM-isomorphic" to the other one given.
ArrayList< Ring > getRingsInvolvingVertex(Vertex v)
Returns the list of rings that include the given vertex in their fundamental cycle.
void getChildrenTree(Vertex vertex, List< Vertex > children)
Gets all the children of the current vertex recursively.
void removeVertex(Vertex vertex)
Remove a vertex from this graph.
int indexOf(Vertex v)
Returns the index of a vertex in the list of vertices of this graph.
Vertex getVertexAtPosition(int pos)
Returns the vertex that is in the given position of the list of vertices belonging to this graph.
Vertex getDeepestAmongThese(List< Vertex > list)
Identify the oldest ancestor (i.e., most great grandparent) in the given collection.
String getBranchIdOfVertexAsStr(Vertex v)
Returns the branch identifier as a literal string.
List< AttachmentPoint > getAvailableAPsThroughout()
Returns the list of attachment points contained in this graph that are available throughout the templ...
void appendVertexOnAP(AttachmentPoint srcAP, AttachmentPoint trgAP)
Append a vertex to this graph: adds the new vertex to the list of vertices belonging to the graph,...
List< AttachmentPoint > getInterfaceAPs(List< Vertex > subGraphB)
Searches for all AttachmentPoints that represent the interface between a subgraph,...
DGraph clone()
Returns almost "deep-copy" of this graph.
boolean replaceSubGraph(List< Vertex > subGrpVrtxs, DGraph incomingGraph, LinkedHashMap< AttachmentPoint, AttachmentPoint > apMap, FragmentSpace fragSpace)
Replaced the subgraph represented by a given collection of vertices that belong to this graph.
int getLevel(Vertex v)
Calculates the level of a vertex in this graph.
void addSymmetricSetOfVertices(SymmetricVertexes symSet)
Adds a symmetric set of vertices to this graph.
SymmetricVertexes getSymSetForVertex(Vertex v)
Returns the set of vertexes symmetric to the given one.
List< AttachmentPoint > getSubgraphAPs(List< Vertex > subGraphB)
Searches for all AttachmentPoints that are owned by vertices in a subgraph but either available or us...
boolean hasSymmetryInvolvingVertex(Vertex v)
boolean insertVertex(Edge edge, int bbId, BBType bbt, LinkedHashMap< AttachmentPoint, Integer > apMap, FragmentSpace fragSpace)
Inserts a given vertex in between two vertices connected by the given edge.
void getChildTreeLimited(Vertex vertex, List< Vertex > children, boolean stopBeforeRCVs)
Gets all the children of the current vertex recursively.
boolean removeBranchStartingAt(Vertex v, boolean symmetry)
Deletes the branch, i.e., the specified vertex and its children.
Template getTemplateJacket()
boolean removeChainUpToBranching(Vertex v, FragmentSpace fragSpace)
Mutates the graph by removing the chain where a given vertex is located up to the first branching (i....
void setLocalMsg(String msg)
boolean isIsostructuralTo(DGraph other)
Checks if this graph is "DENOPTIM-isostructural" to the other one given.
This class represents the edge between two vertices.
AttachmentPoint getTrgAP()
AttachmentPoint getSrcAP()
This class represents the closure of a ring in a spanning tree.
A collection of AttachmentPoints that are related by a relation that we call "symmetry",...
boolean add(T item)
Adds an item to this list, if not already present.
A collection of Vertexs that are related by a relation that we call "symmetry", even though this clas...
ContractLevel getContractLevel()
Returns the contract level of this template, i.e., to what extent the content of this template can be...
void clearIAtomContainer()
Removes the molecular representation.
A vertex is a data structure that has an identity and holds a list of AttachmentPoints.
abstract Vertex clone()
Returns a deep-copy of this vertex.
int getBuildingBlockId()
Returns the index of the building block that should correspond to the position of the building block ...
Edge getEdgeToParent()
Looks into the edges that use any of the APs that belong to this vertex and returns the edge that has...
void setVertexId(long vertexId2)
Vertex getParent()
Looks into the edges that use any of the APs that belong to this vertex and returns the vertex which ...
Vertex.BBType getBuildingBlockType()
DGraph getGraphOwner()
Returns the graph this vertex belongs to or null.
ArrayList< Vertex > getChilddren()
Looks into the edges that use any of the APs that belong to this vertex and returns the list of verti...
abstract int getHeavyAtomsCount()
ArrayList< AttachmentPoint > getFreeAPThroughout()
Gets attachment points that are availability throughout the graph level, i.e., checks also across the...
AttachmentPoint getAP(int i)
Get attachment point i on this vertex.
static Vertex newVertexFromLibrary(int bbId, Vertex.BBType bbt, FragmentSpace fragSpace)
Builds a new molecular fragment kind of vertex.
ChainLink represents a vertex in a closable chain.
Vertex.BBType getFragType()
ClosableChain represents a chain of fragments (chain links) that is closable (or candidate closable).
This object represents a path in a DGraph.
int getPathLength()
Returns the length of the list of edges involved in this path.
List< Vertex > getVertecesPath()
Returns the list of verteces involved.
A class for iterating over sets of ring combinations generated by considering any constrain and setti...
The RingClosingAttractor represent the available valence/connection that allows to close a ring.
static final Map< APClass, APClass > RCAAPCMAP
Recognized APClasses on RingClosingAttractor and compatible types.
Parameters and setting related to handling ring closures.
void allowRingClosures(boolean value)
boolean selectFragmentsFromClosableChains()
Utility methods for input/output.
static void writeGraphToSDF(File file, DGraph graph, boolean append, boolean make3D, Logger logger, Randomizer randomizer)
Writes the graph to SDF file.
A collection of counters user to count actions taken by the evolutionary algorithm.
Tool to build build three-dimensional (3D) tree-like molecular structures from DGraph.
void setAlignBBsIn3D(boolean align)
Sets the flag that controls whether building blocks have to be aligned according to the AP vectors or...
IAtomContainer convertGraphTo3DAtomContainer(DGraph graph)
Created a three-dimensional molecular representation from a given DGraph.
RunTimeParameters getParameters(ParametersType type)
Randomizer getRandomizer()
Returns the current program-specific randomizer.
Parameters for genetic algorithm.
static synchronized void ensureVertexIDConsistency(long l)
Method used to ensure consistency between internal atomic integer and vertex id from imported graphs.
static synchronized long getUniqueVertexIndex()
Unique counter for the number of graph vertices generated.
Tool to generate random numbers and random decisions.
boolean nextBoolean()
Returns the next pseudo-random, uniformly distributed boolean value from this random number generator...
public< T > T randomlyChooseOne(Collection< T > c)
Chooses one member among the given collection.
Enum specifying to what extent the template's inner graph can be changed.
FIXED
Inner graphs are effectively equivalent to the Fragment class, as no change in the inner structure is...
FIXED_STRUCT
Inner graph keep the same structure, but the identify of vertices can change.
The type of building block.
static BBType parseInt(int i)
Translates the integer into the enum.
FAILEDMUTATTEMTS_PERFORM_NOADDLINK_EDIT
FAILEDMUTATTEMTS_PERFORM_NODELETE
FAILEDMUTATTEMTS_PERFORM_NOADDRING
FAILEDMUTATTEMTS_PERFORM_NOADDLINK_FIND
FAILEDMUTATTEMTS_PERFORM_NODELLINK_EDIT
FAILEDMUTATTEMTS_PERFORM_NOADDRING_NORINGCOMB
FAILEDMUTATTEMTS_PERFORM_NOOWNER
FAILEDMUTATTEMTS_PERFORM_NOADDLINK
FAILEDMUTATTEMTS_PERFORM_NOEXTEND
FAILEDMUTATTEMTS_PERFORM_NOMUTSITE
FAILEDMUTATTEMTS_PERFORM_NODELETECHAIN
FAILEDMUTATTEMTS_PERFORM_NOCHANGELINK_EDIT
FAILEDMUTATTEMTS_PERFORM_NOCHANGELINK
FAILEDMUTATTEMTS_PERFORM_NOCHANGEBRANCH
FAILEDMUTATTEMTS_PERFORM_NOADDRING_NOFREEAP
FAILEDMUTATTEMTS_PERFORM_BADMUTTYPE
FAILEDMUTATTEMTS_PERFORM_NOCHANGELINK_FIND
Identifier of the type of parameters.
FS_PARAMS
Parameters pertaining the definition of the fragment space.
RC_PARAMS
Parameters pertaining to ring closures in graphs.
Types of crossover defined.
SUBGRAPH
Swaps a portion of a branch trying to retain cyclicity.
BRANCH
Swaps the entire branch starting from a given vertex.
Types of mutation defined in relation to what happens to the target vertex (i.e., the actual mutation...
DELETECHAIN
Removes a vertex and all its neighbors recursively until a branching point, i.e., until a vertex that...