Skip to content

Commit 76456dc

Browse files
author
Lukas Molzberger
committed
cleanup, comparators -> lambda
1 parent dee5038 commit 76456dc

File tree

11 files changed

+64
-108
lines changed

11 files changed

+64
-108
lines changed

src/main/java/org/aika/Converter.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,10 @@ public class Converter {
3737
public static boolean REMOVE_SYNAPSES_WITH_INVERTED_SIGNS = false;
3838

3939

40-
public static Comparator<Synapse> SYNAPSE_COMP = new Comparator<Synapse>() {
41-
@Override
42-
public int compare(Synapse s1, Synapse s2) {
43-
int r = Double.compare(s2.weight, s1.weight);
44-
if(r != 0) return r;
45-
return Synapse.INPUT_SYNAPSE_COMP.compare(s1, s2);
46-
}
40+
public static Comparator<Synapse> SYNAPSE_COMP = (s1, s2) -> {
41+
int r = Double.compare(s2.weight, s1.weight);
42+
if (r != 0) return r;
43+
return Synapse.INPUT_SYNAPSE_COMP.compare(s1, s2);
4744
};
4845

4946
private Model model;

src/main/java/org/aika/Input.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ public Input setEndRangeMapping(Mapping endMapping) {
242242

243243
Synapse getSynapse(Neuron outputNeuron) {
244244
Synapse s = new Synapse(
245-
neuron.model,
246245
neuron,
247246
outputNeuron,
248247
new Synapse.Key(

src/main/java/org/aika/Model.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public void discardAll() {
247247
* @return
248248
*/
249249
public Neuron initNeuron(Neuron n, double bias, Collection<Input> inputs) {
250-
Set<Synapse> is = new TreeSet<>(Synapse.INPUT_SYNAPSE_BY_WEIGHTS_COMP);
250+
List<Synapse> is = new ArrayList<>();
251251

252252
for (Input input : inputs) {
253253
Synapse s = input.getSynapse(n);

src/main/java/org/aika/Provider.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,5 +155,4 @@ public int compareTo(Provider<?> n) {
155155
else if (id > n.id) return 1;
156156
else return 0;
157157
}
158-
159158
}

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

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -79,28 +79,22 @@ public class Document implements Comparable<Document> {
7979
public TreeSet<Activation> errorSignalActivations = new TreeSet<>();
8080
public TreeMap<INeuron, Set<Synapse>> modifiedWeights = new TreeMap<>();
8181

82-
public TreeMap<NodeActivation.Key, NodeActivation> activationsByRid = new TreeMap<>(new Comparator<NodeActivation.Key>() {
83-
@Override
84-
public int compare(NodeActivation.Key act1, NodeActivation.Key act2) {
85-
int r = Integer.compare(act1.rid, act2.rid);
86-
if(r != 0) return r;
87-
return act1.compareTo(act2);
88-
}
82+
public TreeMap<NodeActivation.Key, NodeActivation> activationsByRid = new TreeMap<>((act1, act2) -> {
83+
int r = Integer.compare(act1.rid, act2.rid);
84+
if (r != 0) return r;
85+
return act1.compareTo(act2);
8986
});
9087
public TreeSet<Node> addedNodes = new TreeSet<>();
9188

9289

93-
public static Comparator<NodeActivation> ACTIVATIONS_OUTPUT_COMPARATOR = new Comparator<NodeActivation>() {
94-
@Override
95-
public int compare(NodeActivation act1, NodeActivation act2) {
96-
int r = Range.compare(act1.key.range, act2.key.range, false);
97-
if(r != 0) return r;
98-
r = Utils.compareInteger(act1.key.rid, act2.key.rid);
99-
if(r != 0) return r;
100-
r = act1.key.interpretation.compareTo(act2.key.interpretation);
101-
if(r != 0) return r;
102-
return act1.key.node.compareTo(act2.key.node);
103-
}
90+
public static Comparator<NodeActivation> ACTIVATIONS_OUTPUT_COMPARATOR = (act1, act2) -> {
91+
int r = Range.compare(act1.key.range, act2.key.range, false);
92+
if (r != 0) return r;
93+
r = Utils.compareInteger(act1.key.rid, act2.key.rid);
94+
if (r != 0) return r;
95+
r = act1.key.interpretation.compareTo(act2.key.interpretation);
96+
if (r != 0) return r;
97+
return act1.key.node.compareTo(act2.key.node);
10498
};
10599

106100

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

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -208,42 +208,33 @@ public Node(Model m, int level) {
208208
}
209209

210210

211-
public static final Comparator<NodeActivation.Key> BEGIN_COMP = new Comparator<Key>() {
212-
@Override
213-
public int compare(Key k1, Key k2) {
214-
int r;
215-
r = Range.compare(k1.range, k2.range, false);
216-
if (r != 0) return r;
217-
r = Utils.compareInteger(k1.rid, k2.rid);
218-
if (r != 0) return r;
219-
return InterprNode.compare(k1.interpretation, k2.interpretation);
220-
}
211+
public static final Comparator<NodeActivation.Key> BEGIN_COMP = (k1, k2) -> {
212+
int r;
213+
r = Range.compare(k1.range, k2.range, false);
214+
if (r != 0) return r;
215+
r = Utils.compareInteger(k1.rid, k2.rid);
216+
if (r != 0) return r;
217+
return InterprNode.compare(k1.interpretation, k2.interpretation);
221218
};
222219

223220

224-
public static final Comparator<NodeActivation.Key> END_COMP = new Comparator<Key>() {
225-
@Override
226-
public int compare(Key k1, Key k2) {
227-
int r;
228-
r = Range.compare(k1.range, k2.range, true);
229-
if (r != 0) return r;
230-
r = Utils.compareInteger(k1.rid, k2.rid);
231-
if (r != 0) return r;
232-
return InterprNode.compare(k1.interpretation, k2.interpretation);
233-
}
221+
public static final Comparator<NodeActivation.Key> END_COMP = (k1, k2) -> {
222+
int r;
223+
r = Range.compare(k1.range, k2.range, true);
224+
if (r != 0) return r;
225+
r = Utils.compareInteger(k1.rid, k2.rid);
226+
if (r != 0) return r;
227+
return InterprNode.compare(k1.interpretation, k2.interpretation);
234228
};
235229

236230

237-
public static final Comparator<NodeActivation.Key> RID_COMP = new Comparator<Key>() {
238-
@Override
239-
public int compare(Key k1, Key k2) {
240-
int r;
241-
r = Utils.compareInteger(k1.rid, k2.rid);
242-
if (r != 0) return r;
243-
r = Range.compare(k1.range, k2.range, false);
244-
if (r != 0) return r;
245-
return InterprNode.compare(k1.interpretation, k2.interpretation);
246-
}
231+
public static final Comparator<NodeActivation.Key> RID_COMP = (k1, k2) -> {
232+
int r;
233+
r = Utils.compareInteger(k1.rid, k2.rid);
234+
if (r != 0) return r;
235+
r = Range.compare(k1.range, k2.range, false);
236+
if (r != 0) return r;
237+
return InterprNode.compare(k1.interpretation, k2.interpretation);
247238
};
248239

249240

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,5 +460,4 @@ public int compareTo(OrEntry on) {
460460
return node.compareTo(on.node);
461461
}
462462
}
463-
464463
}

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

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -119,22 +119,16 @@ public static class SynapseActivation {
119119
public final Activation input;
120120
public final Activation output;
121121

122-
public static Comparator<SynapseActivation> INPUT_COMP = new Comparator<SynapseActivation>() {
123-
@Override
124-
public int compare(SynapseActivation sa1, SynapseActivation sa2) {
125-
int r = Synapse.INPUT_SYNAPSE_COMP.compare(sa1.synapse, sa2.synapse);
126-
if(r != 0) return r;
127-
return sa1.input.compareTo(sa2.input);
128-
}
122+
public static Comparator<SynapseActivation> INPUT_COMP = (sa1, sa2) -> {
123+
int r = Synapse.INPUT_SYNAPSE_COMP.compare(sa1.synapse, sa2.synapse);
124+
if (r != 0) return r;
125+
return sa1.input.compareTo(sa2.input);
129126
};
130127

131-
public static Comparator<SynapseActivation> OUTPUT_COMP = new Comparator<SynapseActivation>() {
132-
@Override
133-
public int compare(SynapseActivation sa1, SynapseActivation sa2) {
134-
int r = Synapse.OUTPUT_SYNAPSE_COMP.compare(sa1.synapse, sa2.synapse);
135-
if(r != 0) return r;
136-
return sa1.output.compareTo(sa2.output);
137-
}
128+
public static Comparator<SynapseActivation> OUTPUT_COMP = (sa1, sa2) -> {
129+
int r = Synapse.OUTPUT_SYNAPSE_COMP.compare(sa1.synapse, sa2.synapse);
130+
if (r != 0) return r;
131+
return sa1.output.compareTo(sa2.output);
138132
};
139133

140134

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ private void trainSynapse(Document doc, Activation iAct, Document.SynEvalResult
372372
}
373373

374374
if(synapse == null) {
375-
synapse = new Synapse(provider.model, inputNeuron.provider, provider, ser.synapseKey);
375+
synapse = new Synapse(inputNeuron.provider, provider, ser.synapseKey);
376376

377377
if(in == null) {
378378
in = InputNode.add(provider.model, ser.synapseKey.createInputNodeKey(), synapse.input.get(doc));
@@ -575,8 +575,8 @@ private static Collection<Synapse> getActiveSynapses(Model m, Document doc, int
575575

576576
Collection<Synapse> synsTmp;
577577
ArrayList<Synapse> newSyns = new ArrayList<>();
578-
Synapse lk = new Synapse(m, null, null, Synapse.Key.MIN_KEY);
579-
Synapse uk = new Synapse(m, null, null, Synapse.Key.MAX_KEY);
578+
Synapse lk = new Synapse(null, null, Synapse.Key.MIN_KEY);
579+
Synapse uk = new Synapse(null, null, Synapse.Key.MAX_KEY);
580580

581581
for (INeuron n : doc.activatedNeurons) {
582582
if (dir == 0) {
@@ -760,19 +760,19 @@ public void reactivate() {
760760
}
761761

762762

763-
public static Neuron init(Model m, int threadId, Neuron pn, double biasDelta, Set<Synapse> inputs) {
763+
public static Neuron init(Model m, int threadId, Neuron pn, double biasDelta, Collection<Synapse> inputs) {
764764
INeuron n = pn.get();
765765
n.biasDelta += biasDelta;
766766

767767
ArrayList<Synapse> modifiedSynapses = new ArrayList<>();
768-
for (Synapse s : inputs) {
768+
inputs.forEach(s -> {
769769
assert !s.key.startRangeOutput || s.key.startRangeMatch == Range.Operator.EQUALS;
770770
assert !s.key.endRangeOutput || s.key.endRangeMatch == Range.Operator.EQUALS;
771771

772772
s.link();
773773

774774
modifiedSynapses.add(s);
775-
}
775+
});
776776

777777
if (!Converter.convert(m, threadId, n, modifiedSynapses)) return null;
778778

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

Lines changed: 12 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -58,37 +58,21 @@
5858
*/
5959
public class Synapse implements Writable {
6060

61-
public static final Comparator<Synapse> INPUT_SYNAPSE_BY_WEIGHTS_COMP = new Comparator<Synapse>() {
62-
@Override
63-
public int compare(Synapse s1, Synapse s2) {
64-
int r = compareWeights(s1.weight, s2.weight, 0.00001);
65-
if (r != 0) return r;
66-
r = s1.input.compareTo(s2.input);
67-
if (r != 0) return r;
68-
return s1.key.compareTo(s2.key);
69-
}
70-
};
7161

72-
73-
public static final Comparator<Synapse> INPUT_SYNAPSE_COMP = new Comparator<Synapse>() {
74-
@Override
75-
public int compare(Synapse s1, Synapse s2) {
76-
int r = s1.input.compareTo(s2.input);
77-
if (r != 0) return r;
78-
return s1.key.compareTo(s2.key);
79-
}
62+
public static final Comparator<Synapse> INPUT_SYNAPSE_COMP = (s1, s2) -> {
63+
int r = s1.input.compareTo(s2.input);
64+
if (r != 0) return r;
65+
return s1.key.compareTo(s2.key);
8066
};
8167

8268

83-
public static final Comparator<Synapse> OUTPUT_SYNAPSE_COMP = new Comparator<Synapse>() {
84-
@Override
85-
public int compare(Synapse s1, Synapse s2) {
86-
int r = s1.output.compareTo(s2.output);
87-
if (r != 0) return r;
88-
return s1.key.compareTo(s2.key);
89-
}
69+
public static final Comparator<Synapse> OUTPUT_SYNAPSE_COMP = (s1, s2) -> {
70+
int r = s1.output.compareTo(s2.output);
71+
if (r != 0) return r;
72+
return s1.key.compareTo(s2.key);
9073
};
9174

75+
9276
public Neuron input;
9377
public Neuron output;
9478

@@ -116,14 +100,14 @@ public int compare(Synapse s1, Synapse s2) {
116100
public Synapse() {}
117101

118102

119-
public Synapse(Model m, Neuron input, Neuron output) {
103+
public Synapse(Neuron input, Neuron output) {
120104
this.input = input;
121105
this.output = output;
122106
}
123107

124108

125-
public Synapse(Model m, Neuron input, Neuron output, Key key) {
126-
this(m, input, output);
109+
public Synapse(Neuron input, Neuron output, Key key) {
110+
this(input, output);
127111
this.key = lookupKey(key);
128112
}
129113

0 commit comments

Comments
 (0)