Skip to content

Commit bd15795

Browse files
author
Lukas Molzberger
committed
cleanup
1 parent 0147b40 commit bd15795

File tree

14 files changed

+285
-271
lines changed

14 files changed

+285
-271
lines changed

src/main/java/network/aika/Document.java

Lines changed: 14 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import network.aika.lattice.Converter;
2121
import network.aika.lattice.Node;
2222
import network.aika.lattice.NodeActivation;
23+
import network.aika.lattice.NodeQueue;
2324
import network.aika.neuron.INeuron;
2425
import network.aika.neuron.Synapse;
2526
import network.aika.neuron.activation.Activation;
@@ -73,7 +74,7 @@ public class Document implements Comparable<Document> {
7374
private Model model;
7475
private int threadId;
7576

76-
private NodeQueue nodeQueue = new NodeQueue();
77+
private NodeQueue nodeQueue = new NodeQueue(this);
7778
private ValueQueue valueQueue = new ValueQueue();
7879
private UpperBoundQueue ubQueue = new UpperBoundQueue();
7980
private Linker linker;
@@ -234,7 +235,17 @@ public String toString() {
234235
}
235236

236237

237-
public Position lookupFinalPosition(int pos) {
238+
public UpperBoundQueue getUpperBoundQueue() {
239+
return ubQueue;
240+
}
241+
242+
243+
public NodeQueue getNodeQueue() {
244+
return nodeQueue;
245+
}
246+
247+
248+
public Position lookupFinalPosition(int pos) {
238249
Position p = positions.get(pos);
239250

240251
if(p == null) {
@@ -333,7 +344,7 @@ public int compareTo(Document doc) {
333344
public void propagate() {
334345
boolean flag = true;
335346
while(flag) {
336-
nodeQueue.processChanges();
347+
nodeQueue.process();
337348
flag = ubQueue.process();
338349
}
339350
}
@@ -471,22 +482,6 @@ public void commit() {
471482
}
472483

473484

474-
475-
476-
public void addToUpperBoundQueue(Link l) {
477-
ubQueue.add(l);
478-
}
479-
480-
public void addToUpperBoundQueue(Activation act) {
481-
ubQueue.add(act);
482-
}
483-
484-
485-
public void addToNodeQueue(Node n) {
486-
nodeQueue.add(n);
487-
}
488-
489-
490485
/**
491486
* Removes the activations of this document from the model again.
492487
*/
@@ -589,60 +584,6 @@ public String activationsToString() {
589584
}
590585

591586

592-
private class NodeQueue {
593-
594-
private final TreeSet<Node> queue = new TreeSet<>(
595-
(n1, n2) -> Node.compareRank(threadId, n1, n2)
596-
);
597-
598-
private long queueIdCounter = 0;
599-
600-
private void add(Node n) {
601-
if(!n.isQueued(threadId, queueIdCounter++)) {
602-
queue.add(n);
603-
}
604-
}
605-
606-
private void processChanges() {
607-
while(!queue.isEmpty()) {
608-
Node n = queue.pollFirst();
609-
610-
n.setNotQueued(threadId);
611-
n.processChanges(Document.this);
612-
}
613-
}
614-
}
615-
616-
617-
private class UpperBoundQueue {
618-
private final ArrayDeque<Activation> queue = new ArrayDeque<>();
619-
620-
private void add(Link l) {
621-
if(!l.isRecurrent()) {
622-
add(l.getOutput());
623-
}
624-
}
625-
626-
private void add(Activation act) {
627-
if(!act.ubQueued && act.inputValue == null) {
628-
act.ubQueued = true;
629-
queue.addLast(act);
630-
}
631-
}
632-
633-
private boolean process() {
634-
boolean flag = false;
635-
while(!queue.isEmpty()) {
636-
flag = true;
637-
Activation act = queue.pollFirst();
638-
act.ubQueued = false;
639-
640-
act.processBounds();
641-
}
642-
return flag;
643-
}
644-
}
645-
646587
public void dumpOscillatingActivations() {
647588
activatedNeurons.stream()
648589
.flatMap(n -> n.getActivations(this, false))

src/main/java/network/aika/lattice/AndNode.java

Lines changed: 63 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,24 @@ private void init() {
7070
}
7171

7272

73-
public void propagate(AndActivation act) {
74-
apply(act);
73+
@Override
74+
protected void propagate(AndActivation act) {
75+
if (andChildren != null) {
76+
for (Link fl : act.inputs) {
77+
if(fl == null) continue;
78+
79+
NodeActivation<?> pAct = fl.input;
80+
81+
for (Link sl : pAct.outputsToAndNode.values()) {
82+
NodeActivation secondAct = sl.output;
83+
if (act != secondAct) {
84+
applyIntern(act, fl.refAct, fl.ref, fl.rv, secondAct, sl.refAct, sl.ref, sl.rv);
85+
}
86+
}
87+
}
88+
}
89+
90+
propagateToOrNode(act);
7591
}
7692

7793

@@ -95,26 +111,6 @@ void processActivation(AndActivation act) {
95111
}
96112

97113

98-
@Override
99-
void apply(AndActivation act) {
100-
if (andChildren != null) {
101-
for (Link fl : act.inputs) {
102-
if(fl == null) continue;
103-
104-
NodeActivation<?> pAct = fl.input;
105-
106-
for (Link sl : pAct.outputsToAndNode.values()) {
107-
NodeActivation secondAct = sl.output;
108-
if (act != secondAct) {
109-
applyIntern(act, fl.refAct, fl.ref, fl.rv, secondAct, sl.refAct, sl.ref, sl.rv);
110-
}
111-
}
112-
}
113-
}
114-
115-
OrNode.processCandidate(this, act, false);
116-
}
117-
118114

119115
private void applyIntern(AndActivation act, InputActivation refAct, Refinement ref, RefValue rv, NodeActivation secondAct, InputActivation secondRefAct, Refinement secondRef, RefValue secondRv) {
120116
Document doc = act.getDocument();
@@ -159,7 +155,7 @@ private AndActivation lookupAndActivation(NodeActivation<?> input, Refinement re
159155
}
160156

161157

162-
public RefValue extend(int threadId, Document doc, Refinement firstRef) {
158+
RefValue expand(int threadId, Document doc, Refinement firstRef) {
163159
if(!firstRef.isConvertible()) return null;
164160

165161
RefValue firstRV = getAndChild(firstRef);
@@ -178,45 +174,17 @@ public RefValue extend(int threadId, Document doc, Refinement firstRef) {
178174
for(Entry firstParent: parents) {
179175
Node parentNode = firstParent.rv.parent.get(doc);
180176

181-
Relation[] secondParentRelations = new Relation[firstRef.relations.length() - 1];
182-
for(int i = 0; i < firstRef.relations.length(); i++) {
183-
Integer j = firstParent.rv.reverseOffsets[i];
184-
if(j != null) {
185-
secondParentRelations[j] = firstRef.relations.get(i);
186-
}
187-
}
188-
189-
Refinement secondParentRef = new Refinement(new RelationsMap(secondParentRelations), firstRef.input);
190-
191-
RefValue secondParentRV = parentNode.extend(threadId, doc, secondParentRef);
177+
Refinement secondParentRef = new Refinement(getParentRelations(firstRef, firstParent), firstRef.input);
178+
RefValue secondParentRV = parentNode.expand(threadId, doc, secondParentRef);
192179

193180
if(secondParentRV == null) {
194181
continue;
195182
}
196183

197-
Relation[] secondRelations = new Relation[firstParent.ref.relations.length() + 1];
198-
for(int i = 0; i < firstParent.ref.relations.length(); i++) {
199-
int j = secondParentRV.offsets[i];
200-
secondRelations[j] = firstParent.ref.relations.get(i);
201-
}
202-
203-
Relation rel = firstRef.relations.get(firstParent.rv.refOffset);
204-
if(rel != null) {
205-
secondRelations[secondParentRV.refOffset] = rel.invert();
206-
}
207-
208-
Refinement secondRef = new Refinement(new RelationsMap(secondRelations), firstParent.ref.input);
209-
210-
Integer[] secondOffsets = new Integer[secondParentRV.offsets.length + 1];
211-
for(int i = 0; i < firstParent.rv.reverseOffsets.length; i++) {
212-
Integer j = firstParent.rv.reverseOffsets[i];
213-
if(j != null) {
214-
secondOffsets[secondParentRV.offsets[j]] = i;
215-
}
216-
}
217-
secondOffsets[secondParentRV.refOffset] = firstRefOffset;
184+
Refinement secondRef = new Refinement(getRelations(firstRef, firstParent, secondParentRV), firstParent.ref.input);
185+
RefValue secondRV = new RefValue(getOffsets(firstRefOffset, firstParent, secondParentRV), firstOffsets[firstParent.rv.refOffset], secondParentRV.child);
218186

219-
nextLevelParents.add(new Entry(secondRef, new RefValue(secondOffsets, firstOffsets[firstParent.rv.refOffset], secondParentRV.child)));
187+
nextLevelParents.add(new Entry(secondRef, secondRV));
220188
}
221189

222190
firstRV = new RefValue(firstOffsets, firstRefOffset, provider);
@@ -226,6 +194,45 @@ public RefValue extend(int threadId, Document doc, Refinement firstRef) {
226194
}
227195

228196

197+
private RelationsMap getParentRelations(Refinement firstRef, Entry firstParent) {
198+
Relation[] secondParentRelations = new Relation[firstRef.relations.length() - 1];
199+
for(int i = 0; i < firstRef.relations.length(); i++) {
200+
Integer j = firstParent.rv.reverseOffsets[i];
201+
if(j != null) {
202+
secondParentRelations[j] = firstRef.relations.get(i);
203+
}
204+
}
205+
return new RelationsMap(secondParentRelations);
206+
}
207+
208+
209+
private static RelationsMap getRelations(Refinement firstRef, Entry firstParent, RefValue secondParentRV) {
210+
Relation[] secondRelations = new Relation[firstParent.ref.relations.length() + 1];
211+
for(int i = 0; i < firstParent.ref.relations.length(); i++) {
212+
int j = secondParentRV.offsets[i];
213+
secondRelations[j] = firstParent.ref.relations.get(i);
214+
}
215+
216+
Relation rel = firstRef.relations.get(firstParent.rv.refOffset);
217+
if(rel != null) {
218+
secondRelations[secondParentRV.refOffset] = rel.invert();
219+
}
220+
return new RelationsMap(secondRelations);
221+
}
222+
223+
224+
private static Integer[] getOffsets(int firstRefOffset, Entry firstParent, RefValue secondParentRV) {
225+
Integer[] secondOffsets = new Integer[secondParentRV.offsets.length + 1];
226+
for(int i = 0; i < firstParent.rv.reverseOffsets.length; i++) {
227+
Integer j = firstParent.rv.reverseOffsets[i];
228+
if(j != null) {
229+
secondOffsets[secondParentRV.offsets[j]] = i;
230+
}
231+
}
232+
secondOffsets[secondParentRV.refOffset] = firstRefOffset;
233+
return secondOffsets;
234+
}
235+
229236

230237
static boolean createAndNode(Model m, Document doc, List<Entry> parents, int level) {
231238
if (parents != null) {

src/main/java/network/aika/lattice/Converter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ private NodeContext expandNode(NodeContext nc, Synapse s) {
217217
NodeContext nln = new NodeContext();
218218
nln.offsets = new Synapse[nc.offsets.length + 1];
219219
AndNode.Refinement ref = new AndNode.Refinement(new AndNode.RelationsMap(relations), s.getInput().get().getOutputNode());
220-
AndNode.RefValue rv = nc.node.extend(threadId, doc, ref);
220+
AndNode.RefValue rv = nc.node.expand(threadId, doc, ref);
221221
if(rv == null) {
222222
return null;
223223
}

src/main/java/network/aika/lattice/InputNode.java

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import network.aika.Document;
2121
import network.aika.Model;
2222
import network.aika.Provider;
23-
import network.aika.neuron.INeuron;
2423
import network.aika.neuron.Neuron;
2524
import network.aika.neuron.activation.Position;
2625
import network.aika.neuron.relation.Relation;
@@ -77,11 +76,6 @@ public void addActivation(Activation inputAct) {
7776
}
7877

7978

80-
public void propagate(InputActivation act) {
81-
apply(act);
82-
}
83-
84-
8579
public void reprocessInputs(Document doc) {
8680
inputNeuron.get(doc).getActivations(doc, false).forEach(act -> {
8781
// act.repropagateV = markedCreated;
@@ -121,8 +115,7 @@ void removeAndChild(AndNode.Refinement ref) {
121115
}
122116

123117

124-
125-
public RefValue extend(int threadId, Document doc, Refinement ref) {
118+
public RefValue expand(int threadId, Document doc, Refinement ref) {
126119
if(!ref.isConvertible()) return null;
127120

128121
Relation rel = ref.relations.get(0);
@@ -151,14 +144,14 @@ public RefValue extend(int threadId, Document doc, Refinement ref) {
151144
* @param act
152145
*/
153146
@Override
154-
void apply(InputActivation act) {
147+
protected void propagate(InputActivation act) {
155148
try {
156149
lock.acquireReadLock();
157150
if (andChildren != null) {
158151
TreeMap<AndNode.Refinement, AndNode.RefValue> children;
159152
if(andChildren.size() > CHILD_NODE_THRESHOLD) {
160153
children = nonExactAndChildren;
161-
applyExactRelations(act);
154+
propagateWithExactRelations(act);
162155
} else {
163156
children = andChildren;
164157
}
@@ -167,7 +160,7 @@ void apply(InputActivation act) {
167160
children.forEach((ref, rv) -> {
168161
InputNode in = ref.input.getIfNotSuspended();
169162
if (in != null) {
170-
addNextLevelActivations(in, ref, rv.child.get(act.getDocument()), act);
163+
in.addNextLevelActivations(ref, rv.child.get(act.getDocument()), act);
171164
}
172165
});
173166
}
@@ -176,11 +169,11 @@ void apply(InputActivation act) {
176169
lock.releaseReadLock();
177170
}
178171

179-
OrNode.processCandidate(this, act, false);
172+
propagateToOrNode(act);
180173
}
181174

182175

183-
private void applyExactRelations(InputActivation act) {
176+
private void propagateWithExactRelations(InputActivation act) {
184177
Activation iAct = act.input;
185178
Document doc = act.getDocument();
186179

@@ -190,23 +183,23 @@ private void applyExactRelations(InputActivation act) {
190183
for (Map.Entry<AndNode.Refinement, AndNode.RefValue> mea : andChildren.subMap(
191184
new Refinement(RelationsMap.MIN, in),
192185
new Refinement(RelationsMap.MAX, in)).entrySet()) {
193-
addNextLevelActivations(in.get(doc), mea.getKey(), mea.getValue().child.get(doc), act);
186+
in.get(doc).addNextLevelActivations(mea.getKey(), mea.getValue().child.get(doc), act);
194187
}
195188
}
196189
}
197190
}
198191

199192

200-
private static void addNextLevelActivations(InputNode secondNode, Refinement ref, AndNode nln, InputActivation act) {
193+
private void addNextLevelActivations(Refinement ref, AndNode nln, InputActivation act) {
201194
Document doc = act.getDocument();
202195

203-
if(secondNode.inputNeuron.get().isEmpty(doc)) return;
196+
if(inputNeuron.get().isEmpty(doc)) return;
204197

205198
Activation iAct = act.input;
206199

207200
if(act.repropagateV != null && act.repropagateV != nln.markedCreated) return;
208201

209-
ref.relations.get(0).getActivations(secondNode.inputNeuron.get(doc), iAct)
202+
ref.relations.get(0).getActivations(inputNeuron.get(doc), iAct)
210203
.filter(secondIAct -> secondIAct.getOutputNodeActivation() != null)
211204
.map(secondIAct -> secondIAct.getOutputNodeActivation())
212205
.filter(secondAct -> secondAct != null && secondAct.registered)

0 commit comments

Comments
 (0)