Abstract BFS from diffableOrConstantUpToNextRV
@@ -21,7 +21,7 @@ public class DifferentiableChecker {
if (!allProbabilisticAreDoubleOrObserved(vertices)) {
return false;
}
- Set<Vertex> allParents = getSetOfAllParents(vertices);
+ Set<Vertex> allParents = allParentsOf(vertices);
Set<Vertex> constantValueVerticesCache = new HashSet<>();
return diffableOrConstantUptoNextRV(allParents, constantValueVerticesCache);
}
@@ -35,7 +35,7 @@ public class DifferentiableChecker {
return (v instanceof DoubleVertex || v.isObserved());
}
- private Set<Vertex> getSetOfAllParents(Collection<Vertex> vertices) {
+ private Set<Vertex> allParentsOf(Collection<Vertex> vertices) {
Set<Vertex> allParents = new HashSet<>();
for (Vertex vertex : vertices) {
allParents.addAll(vertex.getParents());
@@ -44,35 +44,15 @@ public class DifferentiableChecker {
}
private boolean diffableOrConstantUptoNextRV(Collection<Vertex> vertices, Set<Vertex> constantValueVerticesCache) {
- Queue<Vertex> queue = new LinkedList<>(vertices);
- Set<Vertex> queued = new HashSet<>(vertices);
-
- while (!queue.isEmpty()) {
- Vertex visiting = queue.poll();
-
- if (isNonDiffableAndNotConstant(visiting, constantValueVerticesCache)) {
- return false;
- }
-
- if (!visiting.isProbabilistic()) {
- Collection<Vertex> nextVertices = visiting.getParents();
- for (Vertex next : nextVertices) {
- if (!queued.contains(next)) {
- queue.offer(next);
- queued.add(next);
- }
- }
- }
- }
- return true;
- }
-
- private boolean isNonDiffableAndNotConstant(Vertex vertex, Set<Vertex> constantValueVerticesCache) {
- return !vertex.isDifferentiable() && !isVertexValueConstant(Collections.singletonList(vertex), constantValueVerticesCache);
+ return bfsExplorer(vertices,
+ vertex -> isNonDiffableAndNotConstant(vertex, constantValueVerticesCache),
+ vertex -> !vertex.isProbabilistic(),
+ doNothing -> {});
}
private boolean bfsExplorer(Collection<Vertex> vertices, Predicate<Vertex> failureCondition,
- Predicate<Vertex> shouldAddParents, Consumer<Collection<Vertex>> successfullyVisitedConsumer) {
+ Predicate<Vertex> shouldAddParents,
+ Consumer<Collection<Vertex>> successfullyVisitedConsumer) {
Queue<Vertex> queue = new LinkedList<>(vertices);
Set<Vertex> queued = new HashSet<>(vertices);
@@ -97,6 +77,10 @@ public class DifferentiableChecker {
return true;
}
+ private boolean isNonDiffableAndNotConstant(Vertex vertex, Set<Vertex> constantValueVerticesCache) {
+ return !vertex.isDifferentiable() && !isVertexValueConstant(Collections.singletonList(vertex), constantValueVerticesCache);
+ }
+
private boolean isVertexValueConstant(Collection<Vertex> vertices, Set<Vertex> constantValueVerticesCache) {
return bfsExplorer(vertices,
DifferentiableChecker::isUnobservedProbabilistic,
@@ -104,13 +88,13 @@ public class DifferentiableChecker {
constantValueVerticesCache::addAll);
}
+ private boolean isUnobservedProbabilistic(Vertex vertex) {
+ return vertex.isProbabilistic() && !vertex.isObserved();
+ }
+
// We know whether these are constant. For cases such as a MultiplicationVertex we would need to
// explore its parents to ensure its constant.
private boolean isValueKnownToBeConstant(Vertex vertex, Set<Vertex> constantValueVerticesCache) {
return vertex instanceof ConstantVertex || constantValueVerticesCache.contains(vertex) || vertex.isObserved();
}
-
- private boolean isUnobservedProbabilistic(Vertex vertex) {
- return vertex.isProbabilistic() && !vertex.isObserved();
- }
}
