Skip to content

Commit 894f22b

Browse files
author
Lukas Molzberger
committed
relation sets
1 parent bf93388 commit 894f22b

File tree

7 files changed

+73
-51
lines changed

7 files changed

+73
-51
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,11 @@ private NodeContext expandNode(NodeContext nc, Synapse s) {
286286
Relation[] relations = new Relation[nc.offsets.length];
287287
for(int i = 0; i < nc.offsets.length; i++) {
288288
Synapse linkedSynapse = nc.offsets[i];
289-
relations[i] = s.getRelationById(linkedSynapse.id);
289+
Set<Relation> relSet = s.getRelationById(linkedSynapse.id);
290+
if (relSet != null) {
291+
assert relSet.size() == 1;
292+
relations[i] = relSet.iterator().next();
293+
}
290294
}
291295

292296
NodeContext nln = new NodeContext();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ public void remove() {
345345

346346
public String logicToString() {
347347
StringBuilder sb = new StringBuilder();
348-
sb.append("AND[");
348+
sb.append("AND(" + level + ")[");
349349
boolean first = true;
350350
for(Entry e: parents) {
351351
if(!first) {

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public enum Type {
9595

9696

9797
// synapseId -> relation
98-
public Map<Integer, Relation> outputRelations;
98+
public Map<Integer, Set<Relation>> outputRelations;
9999

100100

101101
// A synapse is stored only in one direction, depending on the synapse weight.
@@ -515,9 +515,13 @@ public void write(DataOutput out) throws IOException {
515515

516516
if(outputRelations != null) {
517517
out.writeInt(outputRelations.size());
518-
for (Map.Entry<Integer, Relation> me : outputRelations.entrySet()) {
518+
for (Map.Entry<Integer, Set<Relation>> me : outputRelations.entrySet()) {
519519
out.writeInt(me.getKey());
520-
me.getValue().write(out);
520+
521+
out.writeInt(me.getValue().size());
522+
for(Relation rel: me.getValue()) {
523+
rel.write(out);
524+
}
521525
}
522526
} else {
523527
out.writeInt(0);
@@ -588,8 +592,14 @@ public void readFields(DataInput in, Model m) throws IOException {
588592
outputRelations = new TreeMap<>();
589593
for(int i = 0; i < l; i++) {
590594
Integer relId = in.readInt();
591-
Relation r = Relation.read(in, m);
592-
outputRelations.put(relId, r);
595+
596+
Set<Relation> relSet = new TreeSet(Relation.COMPARATOR);
597+
int s = in.readInt();
598+
for(int j = 0; j < s; j++) {
599+
Relation r = Relation.read(in, m);
600+
relSet.add(r);
601+
}
602+
outputRelations.put(relId, relSet);
593603
}
594604
}
595605

src/main/java/network/aika/neuron/Neuron.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ public String getLabel() {
5757
}
5858

5959

60+
public void setLabel(String label) {
61+
get().label = label;
62+
}
63+
64+
6065
/**
6166
* Propagate an input activation into the network.
6267
*

src/main/java/network/aika/neuron/Synapse.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public class Synapse implements Writable {
8787
public Output rangeOutput;
8888
public boolean identity;
8989

90-
public Map<Integer, Relation> relations = new TreeMap<>();
90+
public Map<Integer, Set<Relation>> relations = new TreeMap<>();
9191

9292
public DistanceFunction distanceFunction = null;
9393

@@ -305,13 +305,8 @@ public boolean isNegative() {
305305
}
306306

307307

308-
public Relation getRelationById(Integer id) {
309-
for(Map.Entry<Integer, Relation> me: relations.entrySet()) {
310-
if(me.getKey() == id) {
311-
return me.getValue();
312-
}
313-
}
314-
return null;
308+
public Set<Relation> getRelationById(Integer id) {
309+
return relations.get(id);
315310
}
316311

317312

@@ -333,9 +328,13 @@ public void write(DataOutput out) throws IOException {
333328
out.writeInt(output.id);
334329

335330
out.writeInt(relations.size());
336-
for(Map.Entry<Integer, Relation> me: relations.entrySet()) {
331+
for(Map.Entry<Integer, Set<Relation>> me: relations.entrySet()) {
337332
out.writeInt(me.getKey());
338-
me.getValue().write(out);
333+
334+
out.writeInt(me.getValue().size());
335+
for(Relation rel: me.getValue()) {
336+
rel.write(out);
337+
}
339338
}
340339

341340
out.writeBoolean(distanceFunction != null);
@@ -371,8 +370,13 @@ public void readFields(DataInput in, Model m) throws IOException {
371370
int l = in.readInt();
372371
for(int i = 0; i < l; i++) {
373372
Integer relId = in.readInt();
374-
Relation r = Relation.read(in, m);
375-
relations.put(relId, r);
373+
Set<Relation> relSet = new TreeSet(Relation.COMPARATOR);
374+
int s = in.readInt();
375+
for(int j = 0; j < s; j++) {
376+
Relation r = Relation.read(in, m);
377+
relSet.add(r);
378+
}
379+
relations.put(relId, relSet);
376380
}
377381

378382
if(in.readBoolean()) {

src/main/java/network/aika/neuron/activation/Linker.java

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ public void linkInput(Activation act) {
108108

109109
Activation iAct = s.input.get(act.doc).getActivation(act.doc, r, false);
110110

111-
link(s, iAct, act);
111+
if(iAct != null) {
112+
link(s, iAct, act);
113+
}
112114
}
113115
}
114116
doc.linker.process();
@@ -138,32 +140,21 @@ public void process() {
138140
while(!queue.isEmpty()) {
139141
Link l = queue.pollFirst();
140142
linkRelated(l.input, l.output, l.synapse.relations);
141-
/*
142-
for(Map.Entry<Relation.Key, Relation> me: l.synapse.relations.entrySet()) {
143-
int relId = me.getKey();
144-
if(relId >= 0) {
145-
Synapse s = l.output.getNeuron().getSynapseById(relId);
146-
if(s != null) {
147-
Relation r = me.getValue();
148-
linkRelated(l.input, l.output, s, r);
149-
}
150-
}
151-
}
152-
*/
153143
doc.propagate();
154144
}
155145
}
156146

157147

158-
private void linkRelated(Activation rAct, Activation oAct, Map<Integer, Relation> relations) {
159-
for(Map.Entry<Integer, Relation> me: relations.entrySet()) {
148+
private void linkRelated(Activation rAct, Activation oAct, Map<Integer, Set<Relation>> relations) {
149+
for(Map.Entry<Integer, Set<Relation>> me: relations.entrySet()) {
160150
Integer relId = me.getKey();
161151
if(relId >= 0) {
162152
Synapse s = oAct.getNeuron().getSynapseById(relId);
163153
if(s != null) {
164-
Relation r = me.getValue();
165-
if(r.follow(rAct, oAct, relations)) {
166-
linkRelated(rAct, oAct, s, r);
154+
for(Relation r: me.getValue()) {
155+
if (r.follow(rAct, oAct, relations)) {
156+
linkRelated(rAct, oAct, s, r);
157+
}
167158
}
168159
}
169160
}
@@ -229,11 +220,12 @@ protected void link(Synapse s, Activation iAct, Activation oAct) {
229220

230221

231222
private boolean checkRelations(Synapse s, Activation iAct, Activation oAct) {
232-
for(Map.Entry<Integer, Relation> me: s.relations.entrySet()) {
223+
for(Map.Entry<Integer, Set<Relation>> me: s.relations.entrySet()) {
233224
if(me.getKey() == Synapse.OUTPUT) {
234-
Relation r = me.getValue();
235-
if(!r.test(iAct, oAct)) {
236-
return false;
225+
for (Relation r : me.getValue()) {
226+
if (!r.test(iAct, oAct)) {
227+
return false;
228+
}
237229
}
238230
}
239231
// TODO: other relations

src/main/java/network/aika/neuron/relation/Relation.java

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@
1212
import java.io.DataInput;
1313
import java.io.DataOutput;
1414
import java.io.IOException;
15-
import java.util.Collection;
16-
import java.util.Comparator;
17-
import java.util.Map;
18-
import java.util.TreeMap;
15+
import java.util.*;
1916

2017

2118
public abstract class Relation implements Comparable<Relation>, Writable {
@@ -49,11 +46,21 @@ public static Relation read(DataInput in, Model m) throws IOException {
4946
public abstract Collection<Activation> getActivations(INeuron n, Activation linkedAct);
5047

5148

52-
public boolean follow(Activation rAct, Activation oAct, Map<Integer, Relation> relations) {
49+
public boolean follow(Activation rAct, Activation oAct, Map<Integer, Set<Relation>> relations) {
5350
return true;
5451
}
5552

5653

54+
public static void addRelation(Map<Integer, Set<Relation>> relMap, Integer synId, Relation r) {
55+
Set<Relation> relSet = relMap.get(synId);
56+
if(relSet == null) {
57+
relSet = new TreeSet<>(COMPARATOR);
58+
relMap.put(synId, relSet);
59+
}
60+
relSet.add(r);
61+
}
62+
63+
5764
public static class Builder implements Neuron.Builder {
5865
private int from;
5966
private int to;
@@ -93,15 +100,15 @@ public Relation getRelation() {
93100
}
94101

95102
public void connect(Neuron n) {
96-
Map<Integer, Relation> fromRel = getRelationsMap(from, n);
97-
Map<Integer, Relation> toRel = getRelationsMap(to, n);
103+
Map<Integer, Set<Relation>> fromRel = getRelationsMap(from, n);
104+
Map<Integer, Set<Relation>> toRel = getRelationsMap(to, n);
98105

99106
Relation r = getRelation();
100-
fromRel.put(to, r);
101-
toRel.put(from, r.invert());
107+
addRelation(fromRel, to, r);
108+
addRelation(toRel, from, r.invert());
102109
}
103110

104-
private static Map<Integer, Relation> getRelationsMap(int synapseId, Neuron n) {
111+
private static Map<Integer, Set<Relation>> getRelationsMap(int synapseId, Neuron n) {
105112
if(synapseId == Synapse.OUTPUT) {
106113
INeuron in = n.get();
107114
if (in.outputRelations == null) {

0 commit comments

Comments
 (0)