Skip to content

Commit 2d36eb8

Browse files
author
Lukas Molzberger
committed
Merge branch 'remove-ranges'
2 parents f732944 + 6ac8758 commit 2d36eb8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1302
-1790
lines changed

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

+8-13
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import network.aika.lattice.Node;
2121
import network.aika.lattice.OrNode;
2222
import network.aika.neuron.INeuron;
23-
import network.aika.neuron.relation.Relation;
2423
import network.aika.neuron.Synapse;
24+
import network.aika.neuron.relation.Relation;
2525

2626
import java.util.*;
2727

@@ -70,7 +70,7 @@ private boolean convert() {
7070
outputNode = neuron.node.get();
7171

7272
initInputNodesAndComputeWeightSums();
73-
initCreateBeginEndPositionFlags();
73+
initSlotFlags();
7474

7575
if(neuron.biasSum + neuron.posDirSum + neuron.posRecSum <= 0.0) {
7676
neuron.requiredSum = neuron.posDirSum + neuron.posRecSum;
@@ -153,14 +153,13 @@ private boolean convert() {
153153
}
154154

155155

156-
private void initCreateBeginEndPositionFlags() {
156+
private void initSlotFlags() {
157157
modifiedSynapses.forEach(s -> {
158-
boolean[] linksOutput = s.linksOutput();
159-
if(linksOutput[0]) {
160-
neuron.createBeginPosition = false;
158+
for(Integer slot: s.linksOutput()) {
159+
neuron.slotHasInputs.add(slot);
161160
}
162-
if(linksOutput[1]) {
163-
neuron.createEndPosition = false;
161+
for(Relation rel: s.relations.values()) {
162+
rel.registerRequiredSlots(s.input);
164163
}
165164
});
166165
}
@@ -290,11 +289,7 @@ private NodeContext expandNode(NodeContext nc, Synapse s) {
290289
Relation[] relations = new Relation[nc.offsets.length];
291290
for(int i = 0; i < nc.offsets.length; i++) {
292291
Synapse linkedSynapse = nc.offsets[i];
293-
Set<Relation> relSet = s.getRelationById(linkedSynapse.id);
294-
if (relSet != null) {
295-
assert relSet.size() == 1;
296-
relations[i] = relSet.iterator().next();
297-
}
292+
relations[i] = s.getRelationById(linkedSynapse.id);
298293
}
299294

300295
NodeContext nln = new NodeContext();

src/main/java/network/aika/DistanceFunction.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
public enum DistanceFunction {
77

88
NONE((iAct, oAct) -> 1.0),
9-
DEGRADING((iAct, oAct) -> 1.0 / ((double) (1 + Math.abs(iAct.range.begin.getDistance(oAct.range.begin)))));
9+
DEGRADING((iAct, oAct) -> 1.0 / ((double) (1 + Math.abs(iAct.getSlot(Activation.BEGIN).getDistance(oAct.getSlot(Activation.BEGIN))))));
1010

1111
Function f;
1212

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

+51-41
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import network.aika.neuron.activation.Activation.Link;
2626
import network.aika.neuron.activation.Candidate;
2727
import network.aika.neuron.range.Position;
28-
import network.aika.neuron.range.Range;
2928
import network.aika.neuron.activation.SearchNode;
3029
import network.aika.lattice.Node.ThreadState;
3130
import network.aika.neuron.activation.*;
@@ -88,22 +87,25 @@ public class Document implements Comparable<Document> {
8887
public TreeMap<Integer, Double> searchNodeWeights = new TreeMap<>();
8988

9089

91-
private TreeMap<ActKey, Activation> activationsByRangeBegin = new TreeMap<>((ak1, ak2) -> {
92-
int r = Position.compare(ak1.range.begin, ak2.range.begin);
90+
private TreeMap<ActKey, Activation> activationsBySlotAndPosition = new TreeMap<>((ak1, ak2) -> {
91+
int r = Integer.compare(ak1.slot, ak2.slot);
92+
if (r != 0) return r;
93+
r = Position.compare(ak1.pos, ak2.pos);
9394
if (r != 0) return r;
9495
r = ak1.node.compareTo(ak2.node);
9596
if (r != 0) return r;
9697
return Integer.compare(ak1.actId, ak2.actId);
9798
});
9899

99-
private TreeMap<ActKey, Activation> activationsByRangeEnd = new TreeMap<>((ak1, ak2) -> {
100-
int r = Position.compare(ak1.range.end, ak2.range.end);
100+
private TreeMap<ActKey, Activation> activationsByPosition = new TreeMap<>((ak1, ak2) -> {
101+
int r = Position.compare(ak1.pos, ak2.pos);
101102
if (r != 0) return r;
102103
r = ak1.node.compareTo(ak2.node);
103104
if (r != 0) return r;
104105
return Integer.compare(ak1.actId, ak2.actId);
105106
});
106107

108+
107109
public TreeMap<Integer, Activation> activationsById = new TreeMap<>();
108110

109111

@@ -112,12 +114,14 @@ public class Document implements Comparable<Document> {
112114

113115

114116
public static class ActKey {
115-
Range range;
117+
int slot;
118+
Position pos;
116119
Node node;
117120
int actId;
118121

119-
public ActKey(Range range, Node node, int actId) {
120-
this.range = range;
122+
public ActKey(int slot, Position pos, Node node, int actId) {
123+
this.slot = slot;
124+
this.pos = pos;
121125
this.node = node;
122126
this.actId = actId;
123127
}
@@ -134,7 +138,7 @@ public ActKey(Range range, Node node, int actId) {
134138

135139

136140
public static Comparator<Activation> ACTIVATIONS_OUTPUT_COMPARATOR = (act1, act2) -> {
137-
int r = Range.compare(act1.range, act2.range, false);
141+
int r = Position.compare(act1.getSlot(Activation.BEGIN), act2.getSlot(Activation.BEGIN));
138142
if (r != 0) return r;
139143
r = act1.node.compareTo(act2.node);
140144
if (r != 0) return r;
@@ -188,11 +192,16 @@ public Position lookupFinalPosition(int pos) {
188192
}
189193

190194

191-
public String getText(Range r) {
192-
if(r.begin.getFinalPosition() != null && r.end.getFinalPosition() != null) {
195+
public String getText(Position begin, Position end) {
196+
return getText(begin.getFinalPosition(), end.getFinalPosition());
197+
}
198+
199+
200+
public String getText(Integer begin, Integer end) {
201+
if(begin != null && end != null) {
193202
return content.substring(
194-
Math.max(0, Math.min(r.begin.getFinalPosition(), length())),
195-
Math.max(0, Math.min(r.end.getFinalPosition(), length()))
203+
Math.max(0, Math.min(begin, length())),
204+
Math.max(0, Math.min(end, length()))
196205
);
197206
} else {
198207
return "";
@@ -201,12 +210,13 @@ public String getText(Range r) {
201210

202211

203212
public void addActivation(Activation act) {
204-
ActKey dak = new ActKey(act.range, act.node, act.id);
205-
if (act.range.begin != null && act.range.begin.getFinalPosition() != null) {
206-
activationsByRangeBegin.put(dak, act);
207-
}
208-
if (act.range.end != null && act.range.end.getFinalPosition() != null) {
209-
activationsByRangeEnd.put(dak, act);
213+
for(Map.Entry<Integer, Position> me : act.slots.entrySet()) {
214+
Position pos = me.getValue();
215+
if (pos != null && pos.getFinalPosition() != null) {
216+
ActKey dak = new ActKey(me.getKey(), pos, act.node, act.id);
217+
activationsBySlotAndPosition.put(dak, act);
218+
activationsByPosition.put(dak, act);
219+
}
210220
}
211221
activationsById.put(act.id, act);
212222
}
@@ -225,21 +235,21 @@ public Collection<Activation> getActivations(boolean onlyFinal) {
225235
}
226236

227237

228-
public Collection<Activation> getActivationsByRangeBegin(Range fromKey, boolean fromInclusive, Range toKey, boolean toInclusive) {
229-
return activationsByRangeBegin.subMap(
230-
new Document.ActKey(fromKey, Node.MIN_NODE, Integer.MIN_VALUE),
238+
public Collection<Activation> getActivationsByPosition(int fromSlot, Position fromPos, boolean fromInclusive, int toSlot, Position toPos, boolean toInclusive) {
239+
return activationsBySlotAndPosition.subMap(
240+
new Document.ActKey(fromSlot, fromPos, Node.MIN_NODE, Integer.MIN_VALUE),
231241
fromInclusive,
232-
new Document.ActKey(toKey, Node.MAX_NODE, Integer.MAX_VALUE),
242+
new Document.ActKey(toSlot, toPos, Node.MAX_NODE, Integer.MAX_VALUE),
233243
toInclusive
234244
).values();
235245
}
236246

237247

238-
public Collection<Activation> getActivationByRangeEnd(Range fromKey, boolean fromInclusive, Range toKey, boolean toInclusive) {
239-
return activationsByRangeEnd.subMap(
240-
new Document.ActKey(fromKey, Node.MIN_NODE, Integer.MIN_VALUE),
248+
public Collection<Activation> getActivationsByPosition(Position fromPos, boolean fromInclusive, Position toPos, boolean toInclusive) {
249+
return activationsByPosition.subMap(
250+
new Document.ActKey(-1, fromPos, Node.MIN_NODE, Integer.MIN_VALUE),
241251
fromInclusive,
242-
new Document.ActKey(toKey, Node.MAX_NODE, Integer.MAX_VALUE),
252+
new Document.ActKey(-1, toPos, Node.MAX_NODE, Integer.MAX_VALUE),
243253
toInclusive
244254
).values();
245255
}
@@ -452,26 +462,26 @@ public String generateOutputText() {
452462

453463
TreeSet<Position> queue = new TreeSet<>(Comparator.comparingInt(p -> p.id));
454464

455-
for(Activation act: activationsByRangeBegin.values()) {
456-
if(act.range.begin.getFinalPosition() != null && act.range.end.getFinalPosition() == null) {
457-
queue.add(act.range.begin);
465+
for(Activation act: activationsById.values()) {
466+
if(act.getINeuron().getOutputText() != null && act.getSlot(Activation.BEGIN).getFinalPosition() != null && act.getSlot(Activation.END).getFinalPosition() == null) {
467+
queue.add(act.getSlot(Activation.BEGIN));
458468
}
459469
}
460470

461471
while(!queue.isEmpty()) {
462472
Position pos = queue.pollFirst();
463473

464-
for(Activation act: pos.beginActivations) {
465-
if (act.getINeuron().outputText != null && act.isFinalActivation()) {
466-
String outText = act.getINeuron().outputText;
467-
Position nextPos = act.range.end;
468-
nextPos.setFinalPosition(pos.getFinalPosition() + outText.length());
474+
pos.getActivations(Activation.BEGIN)
475+
.filter(act -> act.getINeuron().getOutputText() != null && act.isFinalActivation())
476+
.forEach(act -> {
477+
String outText = act.getINeuron().getOutputText();
478+
Position nextPos = act.getSlot(Activation.END);
479+
nextPos.setFinalPosition(pos.getFinalPosition() + outText.length());
469480

470-
content.replace(act.range.begin.getFinalPosition(), act.range.end.getFinalPosition(), outText);
481+
content.replace(act.getSlot(Activation.BEGIN).getFinalPosition(), act.getSlot(Activation.END).getFinalPosition(), outText);
471482

472-
queue.add(nextPos);
473-
}
474-
}
483+
queue.add(nextPos);
484+
});
475485
}
476486
return content.substring(oldLength, length());
477487
}
@@ -670,10 +680,10 @@ public double process(SearchNode sn) {
670680

671681
public void dumpOscillatingActivations() {
672682
activatedNeurons.stream()
673-
.flatMap(n -> n.getActivations(this, false).stream())
683+
.flatMap(n -> n.getActivations(this, false))
674684
.filter(act -> act.rounds.getLastRound() != null && act.rounds.getLastRound() > MAX_ROUND - 5)
675685
.forEach(act -> {
676-
log.error(act.id + " " + act.range + " " + act.decision + " " + act.rounds);
686+
log.error(act.id + " " + act.slotsToString() + " " + act.decision + " " + act.rounds);
677687
log.error(act.linksToString());
678688
log.error("");
679689
});

src/main/java/network/aika/Utils.java

+1
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,5 @@ public static String collapseText(String txt, int length) {
100100
public static double sigmoid(double x) {
101101
return 1.0 / (1.0 + Math.pow(Math.E, (-x)));
102102
}
103+
103104
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ public String toString() {
686686
if(!first) {
687687
sb.append(",");
688688
}
689-
sb.append(i + ":" + iAct.getLabel() + " " + iAct.range + " (" + iAct.id + ")");
689+
sb.append(i + ":" + iAct.getLabel() + " " + iAct.slotsToString() + " (" + iAct.id + ")");
690690

691691
first = false;
692692
}

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

+10-13
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
import network.aika.Provider;
2323
import network.aika.neuron.INeuron;
2424
import network.aika.neuron.Neuron;
25-
import network.aika.neuron.range.Range;
26-
import network.aika.neuron.relation.AncestorRelation;
27-
import network.aika.neuron.relation.RangeRelation;
25+
import network.aika.neuron.range.Position;
2826
import network.aika.neuron.relation.Relation;
2927
import network.aika.neuron.activation.Activation;
3028
import network.aika.PatternDiscovery;
@@ -39,8 +37,6 @@
3937
import java.io.IOException;
4038
import java.util.*;
4139

42-
import static network.aika.neuron.range.Range.Relation.*;
43-
4440

4541
/**
4642
* The {@code InputNode} class is the input layer for the boolean logic. The input-node has two sources of
@@ -59,7 +55,7 @@ public class InputNode extends Node<InputNode, InputActivation> {
5955

6056

6157
public static final Relation[] CANDIDATE_RELATIONS = new Relation[] {
62-
new RangeRelation(EQUALS),
58+
/* new RangeRelation(EQUALS),
6359
new RangeRelation(BEGIN_TO_END_EQUALS),
6460
new RangeRelation(END_TO_BEGIN_EQUALS),
6561
new RangeRelation(BEGIN_EQUALS),
@@ -68,7 +64,7 @@ public class InputNode extends Node<InputNode, InputActivation> {
6864
new RangeRelation(CONTAINED_IN),
6965
new AncestorRelation(AncestorRelation.Type.IS_DESCENDANT_OF),
7066
new AncestorRelation(AncestorRelation.Type.IS_ANCESTOR_OF),
71-
new AncestorRelation(AncestorRelation.Type.COMMON_ANCESTOR)
67+
new AncestorRelation(AncestorRelation.Type.COMMON_ANCESTOR)*/
7268
};
7369

7470

@@ -210,13 +206,13 @@ void apply(InputActivation act) {
210206
private void applyExactRelations(InputActivation act) {
211207
Activation iAct = act.input.input;
212208

213-
for(Range.Relation rel: new Range.Relation[] {BEGIN_EQUALS, END_EQUALS, BEGIN_TO_END_EQUALS, END_TO_BEGIN_EQUALS}) {
214-
for(Activation linkedAct: RangeRelation.getActivationsByRangeEquals(act.doc, iAct.range, rel)) {
209+
for (Map.Entry<Integer, Position> me : iAct.slots.entrySet()) {
210+
for (Activation linkedAct : act.doc.getActivationsByPosition(me.getValue(), true, me.getValue(), true)) {
215211
Provider<InputNode> in = linkedAct.getINeuron().outputNode;
216-
for (Map.Entry<AndNode.Refinement, AndNode.RefValue> me : andChildren.subMap(
212+
for (Map.Entry<AndNode.Refinement, AndNode.RefValue> mea : andChildren.subMap(
217213
new Refinement(RelationsMap.MIN, in),
218214
new Refinement(RelationsMap.MAX, in)).entrySet()) {
219-
addNextLevelActivations(in.get(act.doc), me.getKey(), me.getValue().child.get(act.doc), act);
215+
addNextLevelActivations(in.get(act.doc), mea.getKey(), mea.getValue().child.get(act.doc), act);
220216
}
221217
}
222218
}
@@ -232,7 +228,8 @@ private static void addNextLevelActivations(InputNode secondNode, Refinement ref
232228

233229
if(act.repropagateV != null && act.repropagateV != nln.markedCreated) return;
234230

235-
ref.relations.get(0).getActivations(secondNode.inputNeuron.get(doc), iAct).forEach(secondIAct -> {
231+
ref.relations.get(0).getActivations(secondNode.inputNeuron.get(doc), iAct)
232+
.forEach(secondIAct -> {
236233
if (secondIAct.outputToInputNode != null) {
237234
InputActivation secondAct = secondIAct.outputToInputNode.output;
238235
if (secondAct != null) {
@@ -361,7 +358,7 @@ public Activation getInputActivation(int i) {
361358

362359

363360
public String toString() {
364-
return "I-ACT(" + input.input.getLabel() + " " + input.input.range + ")";
361+
return "I-ACT(" + input.input.getLabel() + " " + input.input.slotsToString() + ")";
365362
}
366363
}
367364

0 commit comments

Comments
 (0)