Skip to content

Commit 44fc051

Browse files
author
Lukas Molzberger
committed
optimized checkSelfReferencing
1 parent 14d9d5e commit 44fc051

File tree

5 files changed

+62
-29
lines changed

5 files changed

+62
-29
lines changed

src/main/java/org/aika/corpus/InterprNode.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ public class InterprNode implements Comparable<InterprNode> {
5151
public final int id;
5252
public int length;
5353

54-
public Map<NodeActivation, InterprNode> orInterprNodes;
54+
public Set<InterprNode> orInterprNodes;
55+
public Set<InterprNode> selectedOrInterprNodes;
5556
public Set<InterprNode> refByOrInterprNode;
5657
public InterprNode largestCommonSubset;
5758
public Set<InterprNode> linkedByLCS;
@@ -64,7 +65,6 @@ public class InterprNode implements Comparable<InterprNode> {
6465
private long visitedLinkRelations;
6566
private long visitedContains;
6667
private long visitedCollect;
67-
private long visitedExpandActivations;
6868
private long visitedIsConflicting;
6969
private long visitedStoreFinalWeight;
7070
private long visitedComputeLargestCommonSubset;
@@ -186,12 +186,12 @@ private void computeLargestCommonSubsetRecursiveStep(List<InterprNode> results,
186186
}
187187

188188

189-
public void addOrOption(NodeActivation inputAct, InterprNode n) {
189+
public void addOrInterpretationNode(InterprNode n) {
190190
if (orInterprNodes == null) {
191-
orInterprNodes = new TreeMap<>();
191+
orInterprNodes = new TreeSet<>();
192192
}
193193
computeLargestCommonSubsetIncremental(n);
194-
orInterprNodes.put(inputAct, n);
194+
orInterprNodes.add(n);
195195
if (n.refByOrInterprNode == null) {
196196
n.refByOrInterprNode = new TreeSet<>();
197197
}
@@ -546,7 +546,7 @@ private String toString(boolean level) {
546546
if(!level && n.orInterprNodes != null) {
547547
sb.append("[");
548548
boolean f2 = true;
549-
for(InterprNode on: n.orInterprNodes.values()) {
549+
for(InterprNode on: n.orInterprNodes) {
550550
if(!f2) sb.append(",");
551551
f2 = false;
552552
sb.append(on.toString(true));

src/main/java/org/aika/corpus/SearchNode.java

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,8 @@ public void computeBestInterpretation(Document doc) {
139139
SearchNode child = new SearchNode(doc, this, null, c, level + 1, changed);
140140
child.search(doc, searchSteps, candidates);
141141

142+
markUnselected(refinement);
143+
142144
if (doc.selectedSearchNode != null) {
143145
doc.selectedSearchNode.reconstructSelectedResult(doc);
144146
doc.selectedSearchNode.collectResults(results);
@@ -235,6 +237,8 @@ private NormWeight search(Document doc, int[] searchSteps, Candidate[] candidate
235237
selectedWeight = child.search(doc, searchSteps, candidates);
236238
child.changeState(StateChange.Mode.OLD);
237239
}
240+
241+
markUnselected(refinement);
238242
}
239243
if(doc.interrupted) {
240244
return NormWeight.ZERO_WEIGHT;
@@ -452,18 +456,40 @@ private void markSelected(List<InterprNode> changed, List<InterprNode> n) {
452456
}
453457

454458

455-
private boolean markSelected(List<InterprNode> changed, InterprNode n) {
456-
if(isCovered(n.markedSelected)) return false;
459+
private void markSelected(List<InterprNode> changed, InterprNode n) {
460+
if(isCovered(n.markedSelected)) return;
457461

458462
n.markedSelected = visited;
459463

464+
if(n.refByOrInterprNode != null) {
465+
for (InterprNode ref : n.refByOrInterprNode) {
466+
if(ref.selectedOrInterprNodes == null) {
467+
ref.selectedOrInterprNodes = new TreeSet<>();
468+
}
469+
ref.selectedOrInterprNodes.add(n);
470+
}
471+
}
472+
460473
if(n.isBottom()) {
461-
return false;
474+
return;
462475
}
463476

464477
if(changed != null) changed.add(n);
465478

466-
return false;
479+
return;
480+
}
481+
482+
483+
private void markUnselected(List<InterprNode> n) {
484+
for(InterprNode x: n) {
485+
if(x.markedSelected == visited) {
486+
if (x.refByOrInterprNode != null) {
487+
for (InterprNode ref : x.refByOrInterprNode) {
488+
ref.selectedOrInterprNodes.remove(x);
489+
}
490+
}
491+
}
492+
}
467493
}
468494

469495

@@ -507,7 +533,7 @@ private void markExcludedRecursiveStep(List<InterprNode> changed, InterprNode n)
507533

508534

509535
private boolean checkOrNodeExcluded(InterprNode n) {
510-
for(InterprNode on: n.orInterprNodes.values()) {
536+
for(InterprNode on: n.orInterprNodes) {
511537
if(!isCovered(on.markedExcluded)) {
512538
return false;
513539
}
@@ -516,14 +542,6 @@ private boolean checkOrNodeExcluded(InterprNode n) {
516542
}
517543

518544

519-
public boolean containedInSelectedBranch(InterprNode n) {
520-
for(InterprNode p: n.parents) {
521-
if(!isCovered(p.markedSelected)) return false;
522-
}
523-
return true;
524-
}
525-
526-
527545
public String pathToString(Document doc) {
528546
return (selectedParent != null ? selectedParent.pathToString(doc) : "") + " - " + toString(doc);
529547
}

src/main/java/org/aika/lattice/OrNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public void addActivation(Document doc, Integer ridOffset, NodeActivation inputA
133133
InterprNode no = lookupOrOption(doc, r, true);
134134

135135
for(NodeActivation iAct: inputs) {
136-
no.addOrOption(iAct, iAct.key.o);
136+
no.addOrInterpretationNode(iAct.key.o);
137137
}
138138

139139
if(neuron.get().outputText != null) {

src/main/java/org/aika/neuron/INeuron.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public void computeBounds(Activation act) {
185185
if (iAct == act) continue;
186186

187187
if (s.isNegative()) {
188-
if (!checkSelfReferencing(act.key.o, iAct.key.o, null, 0) && act.key.o.contains(iAct.key.o, true)) {
188+
if (!checkSelfReferencing(act.key.o, iAct.key.o, 0) && act.key.o.contains(iAct.key.o, true)) {
189189
ub += iAct.lowerBound * s.w;
190190
}
191191

@@ -254,7 +254,7 @@ private State getInputState(int round, SearchNode sn, InterprNode o, Synapse s,
254254

255255
State is = State.ZERO;
256256
if (s.key.isRecurrent) {
257-
if (!s.isNegative() || !checkSelfReferencing(o, io, sn, 0)) {
257+
if (!s.isNegative() || !checkSelfReferencingForSelected(o, io, 0)) {
258258
is = round == 0 ? getInitialState(sn.getCoverage(io)) : iAct.rounds.get(round - 1);
259259
}
260260
} else {
@@ -382,14 +382,29 @@ private void trainSynapse(Activation iAct, Synapse.Key sk, double x, long v) {
382382
}
383383

384384

385-
private static boolean checkSelfReferencing(InterprNode nx, InterprNode ny, SearchNode en, int depth) {
386-
if (nx == ny && (en == null || en.isCovered(ny.markedSelected))) return true;
385+
private static boolean checkSelfReferencing(InterprNode nx, InterprNode ny, int depth) {
386+
if (nx == ny) return true;
387387

388388
if (depth > MAX_SELF_REFERENCING_DEPTH) return false;
389389

390390
if (ny.orInterprNodes != null) {
391-
for (InterprNode n : ny.orInterprNodes.values()) {
392-
if (checkSelfReferencing(nx, n, en, depth + 1)) return true;
391+
for (InterprNode n : ny.orInterprNodes) {
392+
if (checkSelfReferencing(nx, n, depth + 1)) return true;
393+
}
394+
}
395+
396+
return false;
397+
}
398+
399+
400+
private static boolean checkSelfReferencingForSelected(InterprNode nx, InterprNode ny, int depth) {
401+
if (nx == ny) return true;
402+
403+
if (depth > MAX_SELF_REFERENCING_DEPTH) return false;
404+
405+
if (ny.selectedOrInterprNodes != null) {
406+
for (InterprNode n : ny.selectedOrInterprNodes) {
407+
if (checkSelfReferencingForSelected(nx, n, depth + 1)) return true;
393408
}
394409
}
395410

@@ -553,7 +568,7 @@ private static void addConflict(Document doc, InterprNode io, InterprNode o, Nod
553568
Conflicts.add(doc, act, io, o);
554569
}
555570
} else {
556-
for (InterprNode no : o.orInterprNodes.values()) {
571+
for (InterprNode no : o.orInterprNodes) {
557572
addConflict(doc, io, no, act, inputActs, v);
558573
}
559574
}

src/test/java/org/aika/network/EntityResolutionTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ public void testSimpleERExample() {
102102
Assert.assertEquals(0, eJaguar.get().node.get().getFirstActivation(doc).key.o.primId);
103103
Assert.assertEquals(1, ePuma.get().node.get().getFirstActivation(doc).key.o.primId);
104104

105-
Assert.assertEquals(doc.bottom, eJaguar.get().node.get().getFirstActivation(doc).key.o.orInterprNodes.values().iterator().next());
106-
Assert.assertEquals(doc.bottom, ePuma.get().node.get().getFirstActivation(doc).key.o.orInterprNodes.values().iterator().next());
105+
Assert.assertEquals(doc.bottom, eJaguar.get().node.get().getFirstActivation(doc).key.o.orInterprNodes.iterator().next());
106+
Assert.assertEquals(doc.bottom, ePuma.get().node.get().getFirstActivation(doc).key.o.orInterprNodes.iterator().next());
107107

108108
Assert.assertEquals(1, eJaguar.get().node.get().getFirstActivation(doc).key.o.orInterprNodes.size());
109109
Assert.assertEquals(1, ePuma.get().node.get().getFirstActivation(doc).key.o.orInterprNodes.size());

0 commit comments

Comments
 (0)